/// <summary> /// Writes the image descriptor to the stream. /// </summary> /// <param name="image">The <see cref="QuantizedImage"/> containing indexed pixels.</param> /// <param name="quality">The quality (number of colors) to encode the image to.</param> /// <param name="stream">The stream to write to.</param> private void WriteImageDescriptor(QuantizedImage image, int quality, Stream stream) { this.WriteByte(stream, GifConstants.ImageDescriptorLabel); // 2c // TODO: Can we capture this? this.WriteShort(stream, 0); // Left position this.WriteShort(stream, 0); // Top position this.WriteShort(stream, image.Width); this.WriteShort(stream, image.Height); // Calculate the quality. int bitDepth = this.GetBitsNeededForColorDepth(quality); // No LCT use GCT. this.WriteByte(stream, 0); // Write the image data. this.WriteImageData(image, stream, bitDepth); }
/// <summary> /// Writes the image pixel data to the stream. /// </summary> /// <param name="image">The <see cref="QuantizedImage"/> containing indexed pixels.</param> /// <param name="stream">The stream to write to.</param> /// <param name="bitDepth">The bit depth of the image.</param> private void WriteImageData(QuantizedImage image, Stream stream, int bitDepth) { byte[] indexedPixels = image.Pixels; LzwEncoder encoder = new LzwEncoder(indexedPixels, (byte)bitDepth); encoder.Encode(stream); this.WriteByte(stream, GifConstants.Terminator); }