public static WriteableBitmap DecodeJpeg(Stream srcStream) { // Decode JPEG var decoder = new FluxJpeg.Core.Decoder.JpegDecoder(srcStream); var jpegDecoded = decoder.Decode(); var img = jpegDecoded.Image; img.ChangeColorSpace(ColorSpace.RGB); // Init Buffer int w = img.Width; int h = img.Height; var result = new WriteableBitmap(w, h); int[] p = result.Pixels; byte[][,] pixelsFromJpeg = img.Raster; // Copy FluxJpeg buffer into WriteableBitmap int i = 0; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { p[i++] = (0xFF << 24) // A | (pixelsFromJpeg[0][x, y] << 16) // R | (pixelsFromJpeg[1][x, y] << 8) // G | pixelsFromJpeg[2][x, y]; // B } } return(result); }
public static WriteableBitmap DecodeJpeg(Stream srcStream) { // Decode JPEG var decoder = new FluxJpeg.Core.Decoder.JpegDecoder(srcStream); var jpegDecoded = decoder.Decode(); var img = jpegDecoded.Image; img.ChangeColorSpace(ColorSpace.RGB); // Init Buffer int w = img.Width; int h = img.Height; var result = new WriteableBitmap(w, h); int[] p = result.Pixels; byte[][,] pixelsFromJpeg = img.Raster; // Copy FluxJpeg buffer into WriteableBitmap int i = 0; for (int x = 0; x < w; x++) { for (int y = 0; y < h; y++) { p[i++] = (0xFF << 24) // A | (pixelsFromJpeg[0][x, y] << 16) // R | (pixelsFromJpeg[1][x, y] << 8) // G | pixelsFromJpeg[2][x, y]; // B } } return result; }
/// Raises TiltBrush.ImageDecodeError if the data is not a valid jpeg static public RawImage FromJpeg(byte[] jpegData, string filename) { using (var stream = new MemoryStream(jpegData)) { FluxJpeg.Core.Image image; try { FluxJpeg.Core.DecodedJpeg decoded = new FluxJpeg.Core.Decoder.JpegDecoder(stream).Decode(); image = decoded.Image; image.ChangeColorSpace(FluxJpeg.Core.ColorSpace.RGB); } catch (Exception e) { // Library throws bare Exception :-P throw new ImageLoadError(e, "JPEG decode error"); } var reds = image.Raster[0]; var greens = image.Raster[1]; var blues = image.Raster[2]; int _width = image.Width; int _height = image.Height; var buf = new Color32[_width * _height]; unsafe { unchecked { fixed(Color32 *pBuf = buf) { byte *cur = (byte *)pBuf; for (int y = _height - 1; y >= 0; --y) { for (int x = 0; x < _width; ++x) { cur[0] = reds[x, y]; cur[1] = greens[x, y]; cur[2] = blues[x, y]; cur[3] = 0xff; cur += 4; } } } } } return(new RawImage { ColorData = buf, ColorWidth = _width, ColorHeight = _height, ColorAspect = _height == 0 ? 1f : ((float)_width / _height) }); } }