コード例 #1
0
ファイル: JpegDecoder.cs プロジェクト: yufeih/Nine.Imaging
        public Image Decode(Stream stream)
        {
            JpegImage jpg = new JpegImage(stream);

            int pixelWidth  = jpg.Width;
            int pixelHeight = jpg.Height;

            byte[] pixels = new byte[pixelWidth * pixelHeight * 4];

            if (jpg.BitsPerComponent != 8)
            {
                throw new NotSupportedException("JpegDecoder only supports 8 bits per component.");
            }

            if (jpg.Colorspace == Colorspace.RGB)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    SampleRow row = jpg.GetRow(y);

                    for (int x = 0; x < pixelWidth; x++)
                    {
                        Sample sample = row.GetAt(x);

                        int offset = (y * pixelWidth + x) * 4;

                        pixels[offset + 0] = (byte)sample[2];
                        pixels[offset + 1] = (byte)sample[1];
                        pixels[offset + 2] = (byte)sample[0];
                        pixels[offset + 3] = (byte)255;
                    }
                }
            }
            else if (jpg.Colorspace == Colorspace.Grayscale)
            {
                for (int y = 0; y < pixelHeight; y++)
                {
                    SampleRow row = jpg.GetRow(y);

                    for (int x = 0; x < pixelWidth; x++)
                    {
                        Sample sample = row.GetAt(x);

                        int offset = (y * pixelWidth + x) * 4;

                        pixels[offset + 0] = (byte)sample[0];
                        pixels[offset + 1] = (byte)sample[0];
                        pixels[offset + 2] = (byte)sample[0];
                        pixels[offset + 3] = (byte)255;
                    }
                }
            }
            else
            {
                throw new NotSupportedException($"JpegDecoder doesn't support {jpg.Colorspace} color space.");
            }

            return(new Image(pixelWidth, pixelHeight, pixels));
        }
コード例 #2
0
ファイル: JpegDecoder.cs プロジェクト: goupviet/Nine.Imaging
        /// <summary>
        /// Decodes the image from the specified stream and sets
        /// the data to image.
        /// </summary>
        /// <param name="image">The image, where the data should be set to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image should be
        /// decoded from. 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 Decode(Image image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");
            JpegImage jpg = new JpegImage(stream);

            int pixelWidth  = jpg.Width;
            int pixelHeight = jpg.Height;

            byte[] pixels = new byte[pixelWidth * pixelHeight * 4];

            if (!(jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8))
            {
                throw new NotSupportedException("JpegDecoder only support RGB color space.");
            }

            for (int y = 0; y < pixelHeight; y++)
            {
                SampleRow row = jpg.GetRow(y);

                for (int x = 0; x < pixelWidth; x++)
                {
                    Sample sample = row.GetAt(x);

                    int offset = (y * pixelWidth + x) * 4;

                    pixels[offset + 0] = (byte)sample[2];
                    pixels[offset + 1] = (byte)sample[1];
                    pixels[offset + 2] = (byte)sample[0];
                    pixels[offset + 3] = (byte)255;
                }
            }

            image.SetPixels(pixelWidth, pixelHeight, pixels);
        }
コード例 #3
0
        public ChartSamples()
        {
            // Sample 1
            MainThread.BeginInvokeOnMainThread(async() =>
            {
                var streamImageSource = ImageSource.FromResource("UrhoCharts.Forms.Example.Resources.sample1.jpg") as StreamImageSource;
                using (var imageStream = await streamImageSource.Stream(CancellationToken.None))
                {
                    var jpegImage = new JpegImage(imageStream);
                    var imageData = new byte[jpegImage.Width * jpegImage.Height];
                    for (var i = 0; i < jpegImage.Height; i++)
                    {
                        var row = jpegImage.GetRow(i);
                        Buffer.BlockCopy(src: row.ToBytes(),
                                         srcOffset: 0,
                                         dst: imageData,
                                         dstOffset: (i * jpegImage.Width),
                                         count: jpegImage.Width);
                    }

                    Sample1 = new SurfaceChart
                    {
                        XSize = jpegImage.Width,
                        YSize = jpegImage.Height,
                        ZData = imageData,
                    };
                }
            });

            // Sample 2
            Sample2 = new SurfaceChart
            {
                XSize = 100,
                YSize = 100,
            };

            Sample2.ZData = new byte[Sample2.XSize * Sample2.YSize];
            for (var x = 0; x < Sample2.XSize; x++)
            {
                for (var y = 0; y < Sample2.YSize; y++)
                {
                    Sample2.ZData[x * Sample2.XSize + y]
                        = (byte)(240 * ((Math.Sin(x * Math.PI / Sample2.XSize) * Math.Cos(y * Math.PI / Sample2.YSize) + 1) / 2) + 8);
                }
            }
        }
コード例 #4
0
        internal static void Decode(
            DicomPixelData oldPixelData,
            DicomPixelData newPixelData,
            DicomJpegParams parameters)
        {
            var pixelCount = oldPixelData.Height * oldPixelData.Width;

            if (parameters.ConvertColorspaceToRGB)
            {
                if (oldPixelData.PixelRepresentation == PixelRepresentation.Signed)
                {
                    throw new DicomCodecException(
                              "JPEG codec unable to perform colorspace conversion on signed pixel data");
                }

                newPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb;
                newPixelData.PlanarConfiguration       = PlanarConfiguration.Interleaved;
            }

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

                // Destination frame should be of even length
                var frameSize = newPixelData.UncompressedFrameSize;
                if ((frameSize & 1) == 1)
                {
                    ++frameSize;
                }
                var destArray = new byte[frameSize];

                using (var stream = new MemoryStream(jpegData.Data))
                {
                    var decoder = new JpegImage(stream);
                    var w       = decoder.Width;
                    var h       = decoder.Height;

                    for (var c = 0; c < decoder.ComponentsPerSample; c++)
                    {
                        var pos    = newPixelData.PlanarConfiguration == PlanarConfiguration.Planar ? c * pixelCount : c;
                        var offset = newPixelData.PlanarConfiguration == PlanarConfiguration.Planar
                                         ? 1
                                         : decoder.ComponentsPerSample;

                        if (newPixelData.BytesAllocated == 1)
                        {
                            for (var y = 0; y < h; ++y)
                            {
                                var row = decoder.GetRow(y);
                                for (var x = 0; x < w; ++x)
                                {
                                    destArray[pos] = (byte)row[x][c];
                                    pos           += offset;
                                }
                            }
                        }
#if SUPPORT16BIT
                        else if (newPixelData.BytesAllocated == 2)
                        {
                            var destArray16 = new short[frameSize >> 1];
                            for (var y = 0; y < h; ++y)
                            {
                                var row = decoder.GetRow(y);
                                for (var x = 0; x < w; ++x)
                                {
                                    destArray16[pos] = row[x][c];
                                    pos += offset;
                                }
                            }

                            Buffer.BlockCopy(destArray16, 0, destArray, 0, frameSize);
                        }
#endif
                        else
                        {
                            throw new InvalidOperationException(
                                      $"JPEG module does not support Bits Allocated == {newPixelData.BitsAllocated}!");
                        }
                    }

                    newPixelData.AddFrame(new MemoryByteBuffer(destArray));
                }
            }
        }
コード例 #5
0
        /// <summary>
        /// Decodes the image from the specified stream and sets
        /// the data to image.
        /// </summary>
        /// <param name="image">The image, where the data should be set to.
        /// Cannot be null (Nothing in Visual Basic).</param>
        /// <param name="stream">The stream, where the image should be
        /// decoded from. 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 Decode(ExtendedImage image, Stream stream)
        {
            Guard.NotNull(image, "image");
            Guard.NotNull(stream, "stream");

            if (UseLegacyLibrary)
            {
                FluxCoreJpegDecoder fluxCoreJpegDecoder = new FluxCoreJpegDecoder(stream);

                DecodedJpeg jpg = fluxCoreJpegDecoder.Decode();

                jpg.Image.ChangeColorSpace(ColorSpace.RGB);

                int pixelWidth  = jpg.Image.Width;
                int pixelHeight = jpg.Image.Height;

                byte[] pixels = new byte[pixelWidth * pixelHeight * 4];

                byte[][,] sourcePixels = jpg.Image.Raster;

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

                        pixels[offset + 0] = sourcePixels[0][x, y];
                        pixels[offset + 1] = sourcePixels[1][x, y];
                        pixels[offset + 2] = sourcePixels[2][x, y];
                        pixels[offset + 3] = (byte)255;
                    }
                }

                //-------
                image.DensityXInt32 = jpg.Image.DensityX;
                image.DensityYInt32 = jpg.Image.DensityY;
                image.SetPixels(pixelWidth, pixelHeight, pixels);
            }
            else
            {
                JpegImage jpg = new JpegImage(stream);

                int pixelWidth  = jpg.Width;
                int pixelHeight = jpg.Height;

                byte[] pixels = new byte[pixelWidth * pixelHeight * 4];

                if (!(jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8))
                {
                    throw new UnsupportedImageFormatException();
                }

                for (int y = 0; y < pixelHeight; y++)
                {
                    SampleRow row = jpg.GetRow(y);
                    for (int x = 0; x < pixelWidth; x++)
                    {
                        //Sample sample = row.GetAt(x);
                        int offset = (y * pixelWidth + x) * 4;
                        row.GetComponentsAt(x, out pixels[offset + 0], out pixels[offset + 1], out pixels[offset + 2]);
                        //r = (byte)sample[0];
                        //g = (byte)sample[1];
                        //b = (byte)sample[2];
                        //pixels[offset + 0] = r;
                        //pixels[offset + 1] = g;
                        //pixels[offset + 2] = b;
                        pixels[offset + 3] = (byte)255;
                    }
                }

                image.SetPixels(pixelWidth, pixelHeight, pixels);
            }
        }