Exemplo n.º 1
0
        /// <summary>
        /// Encodes the image to the specified stream from the <see cref="Image{TPixel}"/>.
        /// </summary>
        /// <typeparam name="TPixel">The pixel format.</typeparam>
        /// <param name="image">The <see cref="ImageFrame{TPixel}"/> to encode from.</param>
        /// <param name="stream">The <see cref="Stream"/> to encode the image data to.</param>
        public void Encode <TPixel>(Image <TPixel> image, Stream stream)
            where TPixel : struct, IPixel <TPixel>
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            this.width  = image.Width;
            this.height = image.Height;

            ImageMetadata metadata    = image.Metadata;
            PngMetadata   pngMetadata = metadata.GetPngMetadata();

            PngEncoderOptionsHelpers.AdjustOptions <TPixel>(this.options, pngMetadata, out this.use16Bit, out this.bytesPerPixel);
            IQuantizedFrame <TPixel> quantized = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image);

            this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, image, quantized);

            stream.Write(PngConstants.HeaderBytes, 0, PngConstants.HeaderBytes.Length);

            this.WriteHeaderChunk(stream);
            this.WritePaletteChunk(stream, quantized);
            this.WriteTransparencyChunk(stream, pngMetadata);
            this.WritePhysicalChunk(stream, metadata);
            this.WriteGammaChunk(stream);
            this.WriteExifChunk(stream, metadata);
            this.WriteTextChunks(stream, pngMetadata);
            this.WriteDataChunks(image.Frames.RootFrame, quantized, stream);
            this.WriteEndChunk(stream);
            stream.Flush();

            quantized?.Dispose();
        }
Exemplo n.º 2
0
        /// <summary>
        /// Creates the quantized image and sets calculates and sets the bit depth.
        /// </summary>
        /// <typeparam name="TPixel">The type of the pixel.</typeparam>
        /// <param name="image">The image to quantize.</param>
        /// <param name="clonedImage">Cloned image with transparent pixels are changed to black.</param>
        /// <returns>The quantized image.</returns>
        private IndexedImageFrame <TPixel> CreateQuantizedImage <TPixel>(Image <TPixel> image, Image <TPixel> clonedImage)
            where TPixel : unmanaged, IPixel <TPixel>
        {
            IndexedImageFrame <TPixel> quantized;

            if (this.options.TransparentColorMode == PngTransparentColorMode.Clear)
            {
                quantized     = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, clonedImage);
                this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, quantized);
            }
            else
            {
                quantized     = PngEncoderOptionsHelpers.CreateQuantizedFrame(this.options, image);
                this.bitDepth = PngEncoderOptionsHelpers.CalculateBitDepth(this.options, quantized);
            }

            return(quantized);
        }