private static void SaveQntHeader(Stream stream, QntHeader qnt) { long startPosition = stream.Position; var bw = new BinaryWriter(stream); bw.WriteStringNullTerminated("QNT"); bw.Write(qnt.fileVersion); if (qnt.fileVersion == 0) { qnt.headerSize = 48; bw.Write(qnt.xLocation); bw.Write(qnt.yLocation); bw.Write(qnt.width); bw.Write(qnt.height); bw.Write(qnt.bpp); bw.Write(qnt.reserved); bw.Write(qnt.pixelSize); bw.Write(qnt.alphaSize); } else { bw.Write(qnt.headerSize); bw.Write(qnt.xLocation); bw.Write(qnt.yLocation); bw.Write(qnt.width); bw.Write(qnt.height); bw.Write(qnt.bpp); bw.Write(qnt.reserved); bw.Write(qnt.pixelSize); bw.Write(qnt.alphaSize); } int position = (int)(stream.Position - startPosition); if (position < qnt.headerSize) { int remainingBytes = qnt.headerSize - position; if (qnt.extraData != null && qnt.extraData.Length >= remainingBytes) { stream.Write(qnt.extraData, 0, remainingBytes); } else if (qnt.extraData != null && qnt.extraData.Length < remainingBytes) { int sizeFromArray = qnt.extraData.Length; stream.Write(qnt.extraData, 0, sizeFromArray); stream.WriteZeroes(remainingBytes - sizeFromArray); } else { //pad with zeroes stream.WriteZeroes(remainingBytes); } } }
public static void SaveImage(Stream stream, FreeImageBitmap bitmap) { string comment = bitmap.Comment; var qntHeader = new QntHeader(); if (string.IsNullOrEmpty(comment) || !qntHeader.ParseComment(comment)) { qntHeader.headerSize = 0x44; qntHeader.fileVersion = 2; } if (qntHeader.bpp == 0 || qntHeader.bpp == 32) { qntHeader.bpp = 24; } qntHeader.height = bitmap.Height; qntHeader.width = bitmap.Width; qntHeader.pixelSize = 0; qntHeader.alphaSize = 0; long headerPosition = stream.Position; SaveQntHeader(stream, qntHeader); long imageStartPosition = stream.Position; long imageEndPosition; long alphaStartPosition = headerPosition; long alphaEndPosition = headerPosition; if (bitmap.ColorDepth == 32) { SaveQntPixels(stream, bitmap); imageEndPosition = stream.Position; alphaStartPosition = stream.Position; SaveQntAlpha(stream, bitmap); alphaEndPosition = stream.Position; } else { SaveQntPixels(stream, bitmap); imageEndPosition = stream.Position; } qntHeader.pixelSize = (int)(imageEndPosition - imageStartPosition); qntHeader.alphaSize = (int)(alphaEndPosition - alphaStartPosition); long endPosition = stream.Position; stream.Position = headerPosition; //update QNT header SaveQntHeader(stream, qntHeader); stream.Position = endPosition; }
public static QntHeader GetQntHeader(Stream ms) { long startPosition = ms.Position; var br = new BinaryReader(ms); QntHeader qnt = new QntHeader(); qnt.signature = br.ReadInt32(); qnt.fileVersion = br.ReadInt32(); if (qnt.fileVersion == 0) { qnt.headerSize = 48; } else { qnt.headerSize = br.ReadInt32(); } if (qnt.headerSize > 1024 * 1024 || qnt.headerSize < 0) { return(null); } qnt.xLocation = br.ReadInt32(); qnt.yLocation = br.ReadInt32(); qnt.width = br.ReadInt32(); qnt.height = br.ReadInt32(); qnt.bpp = br.ReadInt32(); qnt.reserved = br.ReadInt32(); qnt.pixelSize = br.ReadInt32(); qnt.alphaSize = br.ReadInt32(); long endPosition = ms.Position - startPosition; int extraDataLength = qnt.headerSize - (int)endPosition; if (extraDataLength < 0 || extraDataLength > 1024 * 1024) { return(null); } if (extraDataLength > 0) { qnt.extraData = br.ReadBytes(extraDataLength); } return(qnt); }
static byte[] GetQntAlpha(byte[] inputBytes, QntHeader qntHeader) { byte[] raw = null; using (var ms = new MemoryStream(inputBytes)) { ms.Position = qntHeader.headerSize + qntHeader.pixelSize; using (var rawMs = ZLibCompressor.DeCompress(ms)) { raw = rawMs.ToArray(); } } int w = qntHeader.width; int h = qntHeader.height; byte[] pic = DecodeQntAlpha(raw, w, h); VerifyQntAlpha(raw, w, h, pic); return(pic); }
public static byte[] GetQntPixels(byte[] inputBytes, QntHeader qntHeader) { byte[] pic; if (qntHeader.pixelSize > 0) { byte[] raw = null; using (var ms = new MemoryStream(inputBytes, qntHeader.headerSize, qntHeader.pixelSize)) { using (var rawMs = ZLibCompressor.DeCompress(ms)) { raw = rawMs.ToArray(); } int endPosition = (int)ms.Position; //int length = (int)ms.Position - qntHeader.headerSize; //if (length != qntHeader.pixelSize + qntHeader.alphaSize) //{ //} } int w = qntHeader.width; int h = qntHeader.height; if (raw != null && raw.Length < w * h * 3) { throw new InvalidDataException("Size of decompressed data is wrong"); } pic = DecodeQntPixels(raw, w, h); //VerifyQntPixels(raw, w, h, pic); } else { int w = qntHeader.width; int h = qntHeader.height; pic = new byte[w * h * 3]; } return(pic); }