private static void SetPlainTextExtension(byte[] gifBytes, ref int byteIndex, ref GifData gifData) { PlainTextExtension plainTxtEx = new PlainTextExtension(); // Extension Introducer(1 Byte) // 0x21 plainTxtEx.m_extensionIntroducer = gifBytes[byteIndex]; byteIndex++; // Plain Text Label(1 Byte) // 0x01 plainTxtEx.m_plainTextLabel = gifBytes[byteIndex]; byteIndex++; // Block Size(1 Byte) // 0x0c plainTxtEx.m_blockSize = gifBytes[byteIndex]; byteIndex++; // Text Grid Left Position(2 Bytes) // Not supported byteIndex += 2; // Text Grid Top Position(2 Bytes) // Not supported byteIndex += 2; // Text Grid Width(2 Bytes) // Not supported byteIndex += 2; // Text Grid Height(2 Bytes) // Not supported byteIndex += 2; // Character Cell Width(1 Bytes) // Not supported byteIndex++; // Character Cell Height(1 Bytes) // Not supported byteIndex++; // Text Foreground Color Index(1 Bytes) // Not supported byteIndex++; // Text Background Color Index(1 Bytes) // Not supported byteIndex++; // Block Size & Plain Text Data List while (true) { // Block Size(1 Byte) byte blockSize = gifBytes[byteIndex]; byteIndex++; if (blockSize == 0x00) { // Block Terminator(1 Byte) break; } var plainTextDataBlock = new PlainTextExtension.PlainTextDataBlock(); plainTextDataBlock.m_blockSize = blockSize; // Plain Text Data(n Byte) plainTextDataBlock.m_plainTextData = new byte[plainTextDataBlock.m_blockSize]; for (int i = 0; i < plainTextDataBlock.m_plainTextData.Length; i++) { plainTextDataBlock.m_plainTextData[i] = gifBytes[byteIndex]; byteIndex++; } if (plainTxtEx.m_plainTextDataList == null) { plainTxtEx.m_plainTextDataList = new List <PlainTextExtension.PlainTextDataBlock>(); } plainTxtEx.m_plainTextDataList.Add(plainTextDataBlock); } if (gifData.m_plainTextExList == null) { gifData.m_plainTextExList = new List <PlainTextExtension>(); } gifData.m_plainTextExList.Add(plainTxtEx); }
static void SetPlainTextExtension (byte[] gifBytes, ref int byteIndex, ref GifData gifData) { PlainTextExtension plainTxtEx = new PlainTextExtension (); // Extension Introducer(1 Byte) // 0x21 plainTxtEx.extensionIntroducer = gifBytes[byteIndex]; byteIndex++; // Plain Text Label(1 Byte) // 0x01 plainTxtEx.plainTextLabel = gifBytes[byteIndex]; byteIndex++; // Block Size(1 Byte) // 0x0c plainTxtEx.blockSize = gifBytes[byteIndex]; byteIndex++; // Text Grid Left Position(2 Bytes) // Not supported byteIndex += 2; // Text Grid Top Position(2 Bytes) // Not supported byteIndex += 2; // Text Grid Width(2 Bytes) // Not supported byteIndex += 2; // Text Grid Height(2 Bytes) // Not supported byteIndex += 2; // Character Cell Width(1 Bytes) // Not supported byteIndex++; // Character Cell Height(1 Bytes) // Not supported byteIndex++; // Text Foreground Color Index(1 Bytes) // Not supported byteIndex++; // Text Background Color Index(1 Bytes) // Not supported byteIndex++; // Block Size & Plain Text Data List while (true) { // Block Size(1 Byte) byte blockSize = gifBytes[byteIndex]; byteIndex++; if (blockSize == 0x00) { // Block Terminator(1 Byte) break; } var plainTextDataBlock = new PlainTextExtension.PlainTextDataBlock (); plainTextDataBlock.blockSize = blockSize; // Plain Text Data(n Byte) plainTextDataBlock.plainTextData = new byte[plainTextDataBlock.blockSize]; for (int i = 0; i < plainTextDataBlock.plainTextData.Length; i++) { plainTextDataBlock.plainTextData[i] = gifBytes[byteIndex]; byteIndex++; } if (plainTxtEx.plainTextDataList == null) { plainTxtEx.plainTextDataList = new List<PlainTextExtension.PlainTextDataBlock> (); } plainTxtEx.plainTextDataList.Add (plainTextDataBlock); } if (gifData.plainTextExList == null) { gifData.plainTextExList = new List<PlainTextExtension> (); } gifData.plainTextExList.Add (plainTxtEx); }
private static List <Block> ReadBlocks(byte[] bytes, ref int startIndex) { var blocks = new List <Block>(); var index = startIndex; while (true) { switch (bytes[index]) { case Block.ExtensionIntroducer: { Block extension; switch (bytes[index + 1]) { case Block.PlainTextExtensionLabel: extension = new PlainTextExtension(bytes, ref index); break; case Block.GraphicControlExtensionLabel: extension = new GraphicControlExtension(bytes, ref index); break; case Block.CommentExtensionLabel: extension = new CommentExtension(bytes, ref index); break; case Block.ApplicationExtensionLabel: extension = new ApplicationExtension(bytes, ref index); break; default: throw new NotSupportedException("Unknown extension!"); } blocks.Add(extension); break; } case Block.ImageDescriptorLabel: { var descriptor = new ImageDescriptor(bytes, ref index); blocks.Add(descriptor); if (descriptor.LocalColorTableFlag == 1) { var localColorTable = new ColorTable(descriptor.LocalColorTableSize, bytes, ref index); blocks.Add(localColorTable); } var data = new TableBasedImageData(bytes, ref index); blocks.Add(data); break; } case 0x3B: // End { return(blocks); } default: throw new NotSupportedException($"Unsupported GIF block: {bytes[index]:X}."); } } }
/// <summary> /// Returns the length of the parsed data stream byte[]. /// </summary> /// <returns></returns> public int GetLength() { int length = 0; length += Header.TotalBlockLength + LogicalScreenDescriptor.TotalBlockLength + Trailer.TotalBlockLength; if (LogicalScreen.GlobalColorTable != null) { length += LogicalScreen.GlobalColorTable.TotalBlockLength; } foreach (DataBlock db in DataBlocks) { if (db is GraphicBlock) { GraphicBlock gb = (GraphicBlock)db; if (gb.GraphicControlExtension != null) { length += GraphicControlExtension.TotalBlockLength; } if (gb.GraphicRenderingBlock is PlainTextExtension) { PlainTextExtension pte = (PlainTextExtension)gb.GraphicRenderingBlock; length += pte.TotalBlockLength; } else if (gb.GraphicRenderingBlock is TableBasedImage) { TableBasedImage tbi = (TableBasedImage)gb.GraphicRenderingBlock; length += tbi.ImageData.TotalBlockLength + ImageDescriptor.TotalBlockLength; if (tbi.LocalColorTable != null) { length += tbi.LocalColorTable.TotalBlockLength; } } } else if (db is SpecialPurposeBlock) { SpecialPurposeBlock spb = (SpecialPurposeBlock)db; if (spb is ApplicationExtension) { ApplicationExtension ae = (ApplicationExtension)spb; length += ae.TotalBlockLength; } else if (spb is CommentExtension) { CommentExtension ce = (CommentExtension)spb; length += ce.TotalBlockLength; } } } return(length); }
/// <summary> /// Compiles the parsed tokens/components back into a byte array. /// </summary> public void CompileByteArray() { List <byte> output = new List <byte>(); output.AddRange(DataStream.Header.Bytes); output.AddRange(DataStream.LogicalScreen.LogicalScreenDescriptor.Bytes); if (DataStream.LogicalScreen.GlobalColorTable != null) { output.AddRange(DataStream.LogicalScreen.GlobalColorTable.Bytes); } foreach (DataBlock db in DataStream.DataBlocks) { if (db is GraphicBlock) { GraphicBlock gb = (GraphicBlock)db; if (gb.GraphicControlExtension != null) { output.AddRange(gb.GraphicControlExtension.Bytes); } if (gb.GraphicRenderingBlock is PlainTextExtension) { PlainTextExtension pte = (PlainTextExtension)gb.GraphicRenderingBlock; output.AddRange(pte.Bytes); } else if (gb.GraphicRenderingBlock is TableBasedImage) { TableBasedImage tbi = (TableBasedImage)gb.GraphicRenderingBlock; output.AddRange(tbi.ImageDescriptor.Bytes); if (tbi.LocalColorTable != null) { output.AddRange(tbi.LocalColorTable.Bytes); } output.AddRange(tbi.ImageData.Bytes); } } else if (db is SpecialPurposeBlock) { SpecialPurposeBlock spb = (SpecialPurposeBlock)db; if (spb is ApplicationExtension) { ApplicationExtension ae = (ApplicationExtension)spb; output.AddRange(ae.Bytes); } else if (spb is CommentExtension) { CommentExtension ce = (CommentExtension)spb; output.AddRange(ce.Bytes); } } } output.AddRange(DataStream.Trailer.Bytes); DataStream.Bytes = output.ToArray(); }