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.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; } } return new Image(pixelWidth, pixelHeight, pixels); }
/// <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); } }
/// <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; float[] pixels = new float[pixelWidth * pixelHeight * 4]; if (jpg.Colorspace == Colorspace.RGB && jpg.BitsPerComponent == 8) { Parallel.For( 0, 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] = sample[0] / 255f; pixels[offset + 1] = sample[1] / 255f; pixels[offset + 2] = sample[2] / 255f; pixels[offset + 3] = 1; } }); } else if (jpg.Colorspace == Colorspace.Grayscale && jpg.BitsPerComponent == 8) { Parallel.For( 0, 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] = sample[0] / 255f; pixels[offset + 1] = sample[0] / 255f; pixels[offset + 2] = sample[0] / 255f; pixels[offset + 3] = 1; } }); } else { throw new NotSupportedException("JpegDecoder only supports RGB and Grayscale color spaces."); } image.SetPixels(pixelWidth, pixelHeight, pixels); jpg.Dispose(); }
internal static void Decode( DicomPixelData oldPixelData, DicomPixelData newPixelData, DicomJpegParams parameters) { var pixelCount = oldPixelData.Height * oldPixelData.Width; if (newPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrIct || newPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrRct) { newPixelData.PhotometricInterpretation = PhotometricInterpretation.Rgb; } if (newPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull422 || newPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrPartial422) { newPixelData.PhotometricInterpretation = PhotometricInterpretation.YbrFull; } if (newPixelData.PhotometricInterpretation == PhotometricInterpretation.YbrFull) { newPixelData.PlanarConfiguration = PlanarConfiguration.Planar; } 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)); } } }