public void WriteToStream(Stream stream) { Header.WriteToStream(stream); LogicalScreenDescriptor.WriteToStream(stream); if (LogicalScreenDescriptor.HasGlobalColorTable) { GlobalColorTable.WriteToStream(stream); } foreach (var frame in Frames) { frame.WriteToStream(stream); } stream.WriteByte(trailer); }
/// <summary> /// Populates the global color table byte[] from the source image, and performs lexical parsing on the array. /// </summary> /// <param name="bytes"></param> public static GlobalColorTable ParseGlobalColorTable(byte[] bytes) { GlobalColorTable gct = new GlobalColorTable() { TotalBlockLength = bytes.Length, Bytes = (byte[])bytes.Clone(), DistinctColorCount = bytes.Length / 3 }; for (int i = 0; i <= bytes.Length - 3; i += 3) { gct.Colors.Add(new byte[3] { bytes[i], bytes[i + 1], bytes[i + 2] }); } return(gct); }
private void WriteLogicalScreenDescriptor() { if (Debug) { Console.Error.WriteLine("Writing logical screen descriptor"); } WriteShort(ScreenWidth); WriteShort(ScreenHeight); int packedFields = 0; if (GlobalColorTable != null) { packedFields |= 128 | (GlobalColorTable.BitSize() - 1); if (GlobalColorTable.Ordered) { packedFields |= 8; } } packedFields |= (ColorResolution - 1) << 4; WriteByte(packedFields); if (GlobalColorTable != null) { WriteByte(GlobalColorTable.BackgroundColorIndex); } else { WriteByte(0); } if (PixelAspectRatio != 0) { WriteByte((int)(64 * PixelAspectRatio - 15)); } else { WriteByte(0); } }
private void Read(BinaryReader br) { ReadSignature(br); ReadVersion(br); ReadScreenConfig(br); ReadGlobalColorTable(br); ReadBackgroundColorIndex(br); ReadAspectRatio(br); GlobalColorTable?.ReadTable(br); GraphicsControlExtensionBlock gcb = null; do { var b = br.ReadByte(); if (b == 0x2C) { var frame = new GifFrame { ControlExtension = gcb }; frame.Read(br); gcb = null; Frames.Add(frame); } else if (b == 0x21) { var label = br.ReadByte(); if (label == 0xF9) { gcb = new GraphicsControlExtensionBlock(br); } else if (label == 0x01) { var frame = new GifFrame { PlainTextExtension = new PlainTextExtensionBlock(br), ControlExtension = gcb }; gcb = null; Frames.Add(frame); } else if (label == 0xFF) { ApplicationExtensionBlocks.Add(new ApplicationExtensionBlock(br)); } else if (label == 0xFE) { CommentExtensionBlocks.Add(new CommentExtensionBlock(br)); } else { throw new GifDecodingException($"Unknown extension label {label:X2}"); } } else if (b == 0x3B) { break; } else { throw new GifDecodingException($"Unknown separator {b:X2}"); } } while (true); }
public GlobalColorTableMangler(ref VcvjImage vcvjImage) { VcvjImage = vcvjImage; GlobalColorTable = VcvjImage.DataStream.LogicalScreen.GlobalColorTable; }