Пример #1
0
 public void Decode(ExtendedImage image, Stream stream)
 {
     using (MemoryStream ms = new MemoryStream())
     {
         PngReader reader = new PngReader(stream);
         for (int row = 0; row < reader.ImgInfo.Rows; row++)
         {
             ImageLine line = reader.ReadRowByte(row);
             //int[] scline1 = line.Scanline;
             byte[] buffers = line.ScanlineB;
             ms.Write(buffers, 0, buffers.Length);
         }
         ms.Flush();
         image.SetPixels(ms.ToArray());
         image.PixelHeight   = reader.ImgInfo.Rows;
         image.PixelWidth    = reader.ImgInfo.Cols;
         image.DensityXInt32 = image.DensityYInt32 = 96;
     }
 }
Пример #2
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");


            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);
        }
Пример #3
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);
            }
        }
Пример #4
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>
        public void Decode(ExtendedImage Image, Stream Stream)
        {
            // Initialize the input.
            byte[] Input = Stream is MemoryStream ? ((MemoryStream)Stream).ToArray() : null;
            // Initialize a new instance of the NanoJPEG class.
            NanoJPEG NanoJPEG = new NanoJPEG();

            // Check if the input is invalid.
            if (Input == null)
            {
                // Initialize a new instance of the MemoryStream class.
                using (MemoryStream MemoryStream = new MemoryStream()) {
                    // Initialize the buffer.
                    byte[] Buffer = new byte[16 * 1024];
                    // Initialize the number of bytes read.
                    int Read;
                    // Read bytes and check if reading was successful.
                    while ((Read = Stream.Read(Buffer, 0, Buffer.Length)) > 0)
                    {
                        // Write the read bytes.
                        MemoryStream.Write(Buffer, 0, Read);
                    }
                    // Set the input.
                    Input = MemoryStream.ToArray();
                }
            }
            // Decode the image.
            if (NanoJPEG.njDecode(Input) == nj_result_t.NJ_OK)
            {
                // Initialize the status indicating color.
                bool IsColor = NanoJPEG.njIsColor();
                // Initialize the RGB-formatted image..
                byte[] Output = NanoJPEG.njGetImage();
                // Initialzie the iterator.
                int i = Output.Length - 3, Height = NanoJPEG.njGetHeight(), Width = NanoJPEG.njGetWidth();
                // Initialize NanoJPEG to dispose of copies.
                NanoJPEG.njInit();
                // Check if the image has color.
                if (IsColor)
                {
                    // Resize the RGB-formatted image to accommendate RGBA-formatting.
                    Array.Resize(ref Output, Output.Length * 4 / 3);
                    // Iterate through each pixel in the RGB-formatting.
                    for (int j = Output.Length - 4; i >= 0; i -= 3, j -= 4)
                    {
                        // Change the pixel from RGB-formatting to RGBA-formatting.
                        Buffer.BlockCopy(Output, i, Output, j, 3);
                        // Set the alpha channel for the pixel.
                        Output[j + 3] = 0;
                    }
                }
                else
                {
                    // Resize the Grayscale image to accommendate RGBA-formatting.
                    Array.Resize(ref Output, Output.Length * 4);
                    // Iterate through each pixel in the RGB-formatting.
                    for (int j = Output.Length - 4; i >= 0; i -= 3, j -= 4)
                    {
                        // Change the pixel from Grayscale to RGBA-formatting.
                        Output[j] = Output[j + 1] = Output[j + 2] = Output[i];
                        // Set the alpha channel for the pixel.
                        Output[j + 3] = 0;
                    }
                }
                // Return the RGBA-formatted image.
                Image.SetPixels(Width, Height, Output);
            }
        }
Пример #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="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)
        {
            _image = image;

            _stream = stream;
            _stream.Seek(8, SeekOrigin.Current);

            bool isEndChunckReached = false;

            PngChunk currentChunk = null;

            byte[] palette      = null;
            byte[] paletteAlpha = null;

            using (MemoryStream dataStream = new MemoryStream())
            {
                while ((currentChunk = ReadChunk()) != null)
                {
                    if (isEndChunckReached)
                    {
                        throw new ImageFormatException("Image does not end with end chunk.");
                    }

                    if (currentChunk.Type == PngChunkTypes.Header)
                    {
                        ReadHeaderChunk(currentChunk.Data);

                        ValidateHeader();
                    }
                    else if (currentChunk.Type == PngChunkTypes.Physical)
                    {
                        ReadPhysicalChunk(currentChunk.Data);
                    }
                    else if (currentChunk.Type == PngChunkTypes.Data)
                    {
                        dataStream.Write(currentChunk.Data, 0, currentChunk.Data.Length);
                    }
                    else if (currentChunk.Type == PngChunkTypes.Palette)
                    {
                        palette = currentChunk.Data;
                    }
                    else if (currentChunk.Type == PngChunkTypes.PaletteAlpha)
                    {
                        paletteAlpha = currentChunk.Data;
                    }
                    else if (currentChunk.Type == PngChunkTypes.Text)
                    {
                        ReadTextChunk(currentChunk.Data);
                    }
                    else if (currentChunk.Type == PngChunkTypes.End)
                    {
                        isEndChunckReached = true;
                    }
                }

                byte[] pixels = new byte[_header.Width * _header.Height * 4];

                PngColorTypeInformation colorTypeInformation = _colorTypes[_header.ColorType];

                if (colorTypeInformation != null)
                {
                    IColorReader colorReader = colorTypeInformation.CreateColorReader(palette, paletteAlpha);

                    ReadScanlines(dataStream, pixels, colorReader, colorTypeInformation);
                }

                image.SetPixels(_header.Width, _header.Height, pixels);
            }
        }
Пример #6
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);
            }
        }
Пример #7
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="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)
        {
            _image = image;

            _stream = stream;
            _stream.Seek(8, SeekOrigin.Current);

            bool isEndChunckReached = false;
            
            PngChunk currentChunk = null;

            byte[] palette = null;
            byte[] paletteAlpha = null;

            using (MemoryStream dataStream = new MemoryStream())
            {
                while ((currentChunk = ReadChunk()) != null)
                {
                    if (isEndChunckReached)
                    {
                        throw new ImageFormatException("Image does not end with end chunk.");
                    }

                    if (currentChunk.Type == PngChunkTypes.Header)
                    {
                        ReadHeaderChunk(currentChunk.Data);

                        ValidateHeader();
                    }
                    else if (currentChunk.Type == PngChunkTypes.Physical)
                    {
                        ReadPhysicalChunk(currentChunk.Data);
                    }
                    else if (currentChunk.Type == PngChunkTypes.Data)
                    {
                        dataStream.Write(currentChunk.Data, 0, currentChunk.Data.Length);
                    }
                    else if (currentChunk.Type == PngChunkTypes.Palette)
                    {
                        palette = currentChunk.Data;
                    }
                    else if (currentChunk.Type == PngChunkTypes.PaletteAlpha)
                    {
                        paletteAlpha = currentChunk.Data;
                    }
                    else if (currentChunk.Type == PngChunkTypes.Text)
                    {
                        ReadTextChunk(currentChunk.Data);
                    }
                    else if (currentChunk.Type == PngChunkTypes.End)
                    {
                        isEndChunckReached = true;
                    }
                }
                
                byte[] pixels = new byte[_header.Width * _header.Height * 4];

                PngColorTypeInformation colorTypeInformation = _colorTypes[_header.ColorType];

                if (colorTypeInformation != null)
                {
                    IColorReader colorReader = colorTypeInformation.CreateColorReader(palette, paletteAlpha);

                    ReadScanlines(dataStream, pixels, colorReader, colorTypeInformation);
                }

                image.SetPixels(_header.Width, _header.Height, pixels);
            }
        }
Пример #8
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="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)
        {
            _stream = stream;

            try
            {
                ReadFileHeader();
                ReadInfoHeader();

                int colorMapSize = -1;

                if (_infoHeader.ClrUsed == 0)
                {
                    if (_infoHeader.BitsPerPixel == 1 ||
                        _infoHeader.BitsPerPixel == 4 ||
                        _infoHeader.BitsPerPixel == 8)
                    {
                        colorMapSize = (int)Math.Pow(2, _infoHeader.BitsPerPixel) * 4;
                    }
                }
                else
                {
                    colorMapSize = _infoHeader.ClrUsed * 4;
                }

                byte[] palette = null;

                if (colorMapSize > 0)
                {
                    palette = new byte[colorMapSize];

                    _stream.Read(palette, 0, colorMapSize);
                }

                byte[] imageData = new byte[_infoHeader.Width * _infoHeader.Height * 4];

                switch (_infoHeader.Compression)
                {
                case BmpCompression.RGB:
                    if (_infoHeader.HeaderSize != 40)
                    {
                        throw new ImageFormatException(
                                  string.Format(CultureInfo.CurrentCulture,
                                                "Header Size value '{0}' is not valid.", _infoHeader.HeaderSize));
                    }

                    if (_infoHeader.BitsPerPixel == 32)
                    {
                        ReadRgb32(imageData, _infoHeader.Width, _infoHeader.Height);
                    }
                    else if (_infoHeader.BitsPerPixel == 24)
                    {
                        ReadRgb24(imageData, _infoHeader.Width, _infoHeader.Height);
                    }
                    else if (_infoHeader.BitsPerPixel == 16)
                    {
                        ReadRgb16(imageData, _infoHeader.Width, _infoHeader.Height);
                    }
                    else if (_infoHeader.BitsPerPixel <= 8)
                    {
                        ReadRgbPalette(imageData, palette,
                                       _infoHeader.Width,
                                       _infoHeader.Height,
                                       _infoHeader.BitsPerPixel);
                    }
                    break;

                default:
                    throw new NotSupportedException("Does not support this kind of bitmap files.");
                }

                image.SetPixels(_infoHeader.Width, _infoHeader.Height, imageData);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new ImageFormatException("Bitmap does not have a valid format.", e);
            }
        }
Пример #9
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="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)
        {
            _stream = stream;

            try
            {
                ReadFileHeader();
                ReadInfoHeader();

                int colorMapSize = -1;

                if (_infoHeader.ClrUsed == 0)
                {
                    if (_infoHeader.BitsPerPixel == 1 ||
                        _infoHeader.BitsPerPixel == 4 ||
                        _infoHeader.BitsPerPixel == 8)
                    {
                        colorMapSize = (int)Math.Pow(2, _infoHeader.BitsPerPixel) * 4;
                    }
                }
                else
                {
                    colorMapSize = _infoHeader.ClrUsed * 4;
                }

                byte[] palette = null;

                if (colorMapSize > 0)
                {
                    palette = new byte[colorMapSize];

                    _stream.Read(palette, 0, colorMapSize);
                }

                byte[] imageData = new byte[_infoHeader.Width * _infoHeader.Height * 4];

                switch (_infoHeader.Compression)
                {
                    case BmpCompression.RGB:
                        if (_infoHeader.HeaderSize != 40)
                        {
                            throw new ImageFormatException(
                                string.Format(CultureInfo.CurrentCulture,
                                    "Header Size value '{0}' is not valid.", _infoHeader.HeaderSize));
                        }

                        if (_infoHeader.BitsPerPixel == 32)
                        {
                            ReadRgb32(imageData, _infoHeader.Width, _infoHeader.Height);
                        }
                        else if (_infoHeader.BitsPerPixel == 24)
                        {
                            ReadRgb24(imageData, _infoHeader.Width, _infoHeader.Height);
                        }
                        else if (_infoHeader.BitsPerPixel == 16)
                        {
                            ReadRgb16(imageData, _infoHeader.Width, _infoHeader.Height);
                        }
                        else if (_infoHeader.BitsPerPixel <= 8)
                        {
                            ReadRgbPalette(imageData, palette,
                                _infoHeader.Width,
                                _infoHeader.Height,
                                _infoHeader.BitsPerPixel);
                        }
                        break;
                    default:
                        throw new NotSupportedException("Does not support this kind of bitmap files.");
                }

                image.SetPixels(_infoHeader.Width, _infoHeader.Height, imageData);
            }
            catch (IndexOutOfRangeException e)
            {
                throw new ImageFormatException("Bitmap does not have a valid format.", e);
            }
        }