WriteJpeg() public method

Writes compressed JPEG image to stream.
public WriteJpeg ( Stream output ) : void
output Stream Output stream.
return void
コード例 #1
0
        /// <inheritdoc/>
        public void Encode(ImageBase image, Stream stream)
        {
            Guard.NotNull(image, nameof(image));
            Guard.NotNull(stream, nameof(stream));

            int imageWidth = image.Width;
            int imageHeight = image.Height;

            SampleRow[] rows = new SampleRow[imageHeight];

            Parallel.For(
                0,
                imageHeight,
                y =>
                    {
                        byte[] samples = new byte[imageWidth * 3];

                        for (int x = 0; x < imageWidth; x++)
                        {
                            Bgra32 color = Color.ToNonPremultiplied(image[x, y]);

                            int start = x * 3;
                            samples[start] = color.R;
                            samples[start + 1] = color.G;
                            samples[start + 2] = color.B;
                        }

                        rows[y] = new SampleRow(samples, imageWidth, 8, 3);
                    });

            using (JpegImage jpg = new JpegImage(rows, Colorspace.RGB))
            {
                jpg.WriteJpeg(stream, new CompressionParameters { Quality = this.Quality });
            }
        }
コード例 #2
0
        public void Encode(Image image, Stream stream)
        {
            int pixelWidth  = image.Width;
            int pixelHeight = image.Height;

            byte[] sourcePixels = image.Pixels;

            SampleRow[] rows = new SampleRow[pixelHeight];

            for (int y = 0; y < pixelHeight; y++)
            {
                byte[] samples = new byte[pixelWidth * 3];

                for (int x = 0; x < pixelWidth; x++)
                {
                    int start = x * 3;
                    int source = (y * pixelWidth + x) * 4;

                    samples[start] = sourcePixels[source + 2];
                    samples[start + 1] = sourcePixels[source + 1];
                    samples[start + 2] = sourcePixels[source];
                }

                rows[y] = new SampleRow(samples, pixelWidth, 8, 3);
            }

            JpegImage jpg = new JpegImage(rows, Colorspace.RGB);
            jpg.WriteJpeg(stream, new CompressionParameters { Quality = Quality });
        }
コード例 #3
0
ファイル: JpegSupport.cs プロジェクト: zer09/Cosmos
        public override void Save(Image i, Stream dest)
        {
            BitMiracle.LibJpeg.JpegImage j = BitMiracle.LibJpeg.JpegImage.FromBitmap((System.Drawing.Bitmap)i);
            CompressionParameters        c = new CompressionParameters();

            c.Quality           = 100;
            c.SimpleProgressive = false;
            j.WriteJpeg(dest, c);
            j.Dispose();
            System.GC.Collect();
        }
コード例 #4
0
ファイル: JpegImage.cs プロジェクト: lygroup/libjpeg.net
        private static void testJpegOutput(JpegImage jpeg, CompressionParameters parameters, string jpegFileName, string folderWithExpectedResults)
        {
            using (FileStream output = new FileStream(jpegFileName, FileMode.Create))
                jpeg.WriteJpeg(output, parameters);

            FileAssert.AreEqual(jpegFileName, Path.Combine(folderWithExpectedResults, jpegFileName));
        }
コード例 #5
0
ファイル: JpegImage.cs プロジェクト: lygroup/libjpeg.net
        private static void testJpegFromBitmap(Bitmap bmp, string jpegFileName)
        {
            using (JpegImage jpeg = new JpegImage(bmp))
            {
                Assert.AreEqual(jpeg.Width, bmp.Width);
                Assert.AreEqual(jpeg.Height, bmp.Height);
                Assert.AreEqual(jpeg.ComponentsPerSample, 3);//Number of components in Bitmap

                using (FileStream output = new FileStream(jpegFileName, FileMode.Create))
                    jpeg.WriteJpeg(output);
            }

            FileAssert.AreEqual(jpegFileName, Path.Combine(m_expectedResults, jpegFileName));
        }
コード例 #6
0
ファイル: DicomJpegCodecImpl.cs プロジェクト: aerik/fo-dicom
        internal static void Encode(
            DicomPixelData oldPixelData,
            DicomPixelData newPixelData,
            DicomJpegParams parameters)
        {
            if ((oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull422)
                || (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial422)
                || (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial420))
            {
                throw new InvalidOperationException(
                    "Photometric Interpretation '" + oldPixelData.PhotometricInterpretation
                    + "' not supported by JPEG encoder");
            }

            var w = oldPixelData.Width;
            var h = oldPixelData.Height;

            for (var frame = 0; frame < oldPixelData.NumberOfFrames; frame++)
            {
                var frameData = oldPixelData.GetFrame(frame);

                var nc = oldPixelData.SamplesPerPixel;
                var sgnd = oldPixelData.PixelRepresentation == PixelRepresentation.Signed;

                var rows = Enumerable.Range(0, h).Select(i => new short[w, nc]).ToArray();

                for (var c = 0; c < nc; c++)
                {
                    var pos = oldPixelData.PlanarConfiguration == PlanarConfiguration.Planar ? (c * w * h) : c;
                    var offset = oldPixelData.PlanarConfiguration == PlanarConfiguration.Planar ? 1 : nc;

                    if (oldPixelData.BytesAllocated == 1)
                    {
                        var data = frameData.Data;
                        if (sgnd)
                        {
                            if (oldPixelData.BitsStored < 8)
                            {
                                var sign = (byte)(1 << oldPixelData.HighBit);
                                var mask = (byte)(0xff >> (oldPixelData.BitsAllocated - oldPixelData.BitsStored));
                                for (var y = 0; y < h; ++y)
                                {
                                    for (var x = 0; x < w; ++x)
                                    {
                                        var pixel = (sbyte)data[pos];
                                        rows[y][x, c] = (short)((pixel & sign) > 0 ? -(((-pixel) & mask) + 1) : pixel);
                                        pos += offset;
                                    }
                                }
                            }
                            else
                            {
                                for (var y = 0; y < h; ++y)
                                {
                                    for (var x = 0; x < w; ++x)
                                    {
                                        rows[y][x, c] = (sbyte)data[pos];
                                        pos += offset;
                                    }
                                }
                            }
                        }
                        else
                        {
                            for (var y = 0; y < h; ++y)
                            {
                                for (var x = 0; x < w; ++x)
                                {
                                    rows[y][x, c] = data[pos];
                                    pos += offset;
                                }
                            }
                        }
                    }
#if SUPPORT16BIT
                    else if (oldPixelData.BytesAllocated == 2)
                    {
                        if (sgnd)
                        {
                            if (oldPixelData.BitsStored < 16)
                            {
                                var frameData16 = new ushort[w * h];
                                Buffer.BlockCopy(frameData.Data, 0, frameData16, 0, (int)frameData.Size);

                                var sign = (ushort)(1 << oldPixelData.HighBit);
                                var mask = (ushort)(0xffff >> (oldPixelData.BitsAllocated - oldPixelData.BitsStored));
                                for (var y = 0; y < h; ++y)
                                {
                                    for (var x = 0; x < w; ++x)
                                    {
                                        var pixel = frameData16[pos];
                                        rows[y][x, c] = (short)((pixel & sign) > 0 ? -(((-pixel) & mask) + 1) : pixel);
                                        pos += offset;
                                    }
                                }
                            }
                            else
                            {
                                var frameData16 = new short[w * h];
                                Buffer.BlockCopy(frameData.Data, 0, frameData16, 0, (int)frameData.Size);

                                for (var y = 0; y < h; ++y)
                                {
                                    for (var x = 0; x < w; ++x)
                                    {
                                        rows[y][x, c] = frameData16[pos];
                                        pos += offset;
                                    }
                                }
                            }
                        }
                        else
                        {
                            var frameData16 = new ushort[w * h];
                            Buffer.BlockCopy(frameData.Data, 0, frameData16, 0, (int)frameData.Size);

                            for (var y = 0; y < h; ++y)
                            {
                                for (var x = 0; x < w; ++x)
                                {
                                    rows[y][x, c] = (short)frameData16[pos];
                                    pos += offset;
                                }
                            }
                        }
                    }
#endif
                    else
                    {
                        throw new InvalidOperationException(
                            $"JPEG codec does not support Bits Allocated == {oldPixelData.BitsAllocated}");
                    }
                }

                try
                {
                    using (var stream = new MemoryStream())
                    {
                        var sampleRows =
                            rows.Select(
                                row =>
                                new SampleRow(
                                    ToRawRow(row, oldPixelData.BitsAllocated),
                                    w,
                                    (byte)oldPixelData.BitsStored,
                                    (byte)nc)).ToArray();
                        var colorSpace = GetColorSpace(oldPixelData.PhotometricInterpretation);
                        using (var encoder = new JpegImage(sampleRows, colorSpace))
                        {
                            encoder.WriteJpeg(stream, ToCompressionParameters(parameters));
                        }
                        newPixelData.AddFrame(new MemoryByteBuffer(stream.ToArray()));
                    }
                }
                catch (Exception e)
                {
                    throw new InvalidOperationException("Unable to JPEG encode image", e);
                }
            }

            if (oldPixelData.PhotometricInterpretation == PhotometricInterpretation.Rgb)
            {
                newPixelData.PlanarConfiguration = PlanarConfiguration.Interleaved;
            }
        }