예제 #1
0
        /// <summary>
        /// Encodes the data of the specified image and writes the result to
        /// the specified stream.
        /// </summary>
        /// <param name="image">The image, where the data should be get from.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image data should be written to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="System.ArgumentNullException">
        ///     <para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        ///     <para>- or -</para>
        ///     <para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Encode(ExtendedImage image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");

            const int bands = 3;

            int pixelWidth  = image.PixelWidth;
            int pixelHeight = image.PixelHeight;

            byte[] sourcePixels = image.Pixels;

            byte[][,] pixels = new byte[bands][, ];

            for (int b = 0; b < bands; b++)
            {
                pixels[b] = new byte[pixelWidth, pixelHeight];
            }

            for (int y = 0; y < pixelHeight; y++)
            {
                for (int x = 0; x < pixelWidth; x++)
                {
                    int offset = (y * pixelWidth + x) * 4;

                    float a = (float)sourcePixels[offset + 3] / 255.0f;

                    pixels[0][x, y] = (byte)(sourcePixels[offset + 0] * a + (1 - a) * _transparentColor.R);
                    pixels[1][x, y] = (byte)(sourcePixels[offset + 1] * a + (1 - a) * _transparentColor.G);
                    pixels[2][x, y] = (byte)(sourcePixels[offset + 2] * a + (1 - a) * _transparentColor.B);
                }
            }

            Image newImage = new Image(new ColorModel {
                ColorSpace = ColorSpace.RGB, Opaque = false
            }, pixels);

            if (image.DensityX > 0 && image.DensityY > 0)
            {
                newImage.DensityX = image.DensityX;
                newImage.DensityY = image.DensityY;
            }

            // Create a jpg image from the image object.
            DecodedJpeg jpg = new DecodedJpeg(newImage);

            // Create a new encoder and start encoding.
            FluxCoreJpegEncoder fluxCoreJpegEncoder = new FluxCoreJpegEncoder(jpg, _quality, stream);

            fluxCoreJpegEncoder.Encode();
        }
예제 #2
0
        /// <summary>
        /// Encodes the data of the specified image and writes the result to
        /// the specified stream.
        /// </summary>
        /// <param name="image">The image, where the data should be get from.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image data should be written to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <exception cref="System.ArgumentNullException">
        /// 	<para><paramref name="image"/> is null (Nothing in Visual Basic).</para>
        /// 	<para>- or -</para>
        /// 	<para><paramref name="stream"/> is null (Nothing in Visual Basic).</para>
        /// </exception>
        public void Encode(ExtendedImage image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");
            
            const int bands = 3;

            int pixelWidth  = image.PixelWidth;
            int pixelHeight = image.PixelHeight;

            byte[] sourcePixels = image.Pixels;

            byte[][,] pixels = new byte[bands][,];

            for (int b = 0; b < bands; b++)
            {
                pixels[b] = new byte[pixelWidth, pixelHeight];
            }

            for (int y = 0; y < pixelHeight; y++)
            {
                for (int x = 0; x < pixelWidth; x++)
                {
                    int offset = (y * pixelWidth + x) * 4;

                    float a = (float)sourcePixels[offset + 3] / 255.0f;

                    pixels[0][x, y] = (byte)(sourcePixels[offset + 0] * a + (1 - a) * _transparentColor.R);
                    pixels[1][x, y] = (byte)(sourcePixels[offset + 1] * a + (1 - a) * _transparentColor.G);
                    pixels[2][x, y] = (byte)(sourcePixels[offset + 2] * a + (1 - a) * _transparentColor.B);
                }
            }

            Image newImage = new Image(new ColorModel { ColorSpace = ColorSpace.RGB, Opaque = false }, pixels);

            if (image.DensityXInt32 > 0 && image.DensityYInt32 > 0)
            {
                newImage.DensityX = image.DensityXInt32;
                newImage.DensityY = image.DensityYInt32;
            }

            // Create a jpg image from the image object.
            DecodedJpeg jpg = new DecodedJpeg(newImage);

            // Create a new encoder and start encoding.
            FluxCoreJpegEncoder fluxCoreJpegEncoder = new FluxCoreJpegEncoder(jpg, _quality, stream);
            fluxCoreJpegEncoder.Encode();
        }