/// <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"); 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.DensityX = jpg.Image.DensityX; image.DensityY = jpg.Image.DensityY; 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(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(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); } }