Esempio n. 1
0
        // Indexed Image with Bit Depth == 8
        private byte[] getImageColorType3BitDepth8()
        {
            int j = 0;
            int n = 0;

            byte[] alpha = null;
            if (alphaForPalette != null)
            {
                alpha = new byte[this.w * this.h];
            }

            byte[] image          = new byte[3 * (inflated.Length - this.h)];
            int    scanLineLength = this.w + 1;

            for (int i = 0; i < inflated.Length; i++)
            {
                if (i % scanLineLength == 0)
                {
                    // Skip the filter byte
                    continue;
                }

                int k = ((int)inflated[i] & 0x000000ff);
                image[j++] = rgb[3 * k];
                image[j++] = rgb[3 * k + 1];
                image[j++] = rgb[3 * k + 2];

                if (alphaForPalette != null)
                {
                    alpha[n++] = alphaForPalette[k];
                }
            }

            if (alphaForPalette != null)
            {
                Compressor compressor = new Compressor(alpha);
                deflatedAlpha = compressor.GetCompressedData();
            }

            return(image);
        }
Esempio n. 2
0
        // Truecolor Image with Alpha Transparency
        private byte[] GetImageColorType6BitDepth8(byte[] buf)
        {
            byte[] idata = new byte[3 * this.w * this.h]; // Image data
            byte[] alpha = new byte[this.w * this.h];     // Alpha values

            byte[] image        = new byte[4 * this.w * this.h];
            byte[] filters      = new byte[this.h];
            int    bytesPerLine = 4 * this.w + 1;
            int    k            = 0;
            int    j            = 0;
            int    i            = 0;

            for (; i < buf.Length; i++)
            {
                if (i % bytesPerLine == 0)
                {
                    filters[j++] = buf[i];
                }
                else
                {
                    image[k++] = buf[i];
                }
            }
            ApplyFilters(filters, image, this.w, this.h, 4);

            k = 0;
            j = 0;
            i = 0;
            while (i < image.Length)
            {
                idata[j++] = image[i++];
                idata[j++] = image[i++];
                idata[j++] = image[i++];
                alpha[k++] = image[i++];
            }
            deflatedAlphaData = Compressor.deflate(alpha);

            return(idata);
        }
Esempio n. 3
0
        /**
         * Used to embed PNG images in the PDF document.
         *
         */
        public PNGImage(Stream inputStream)
        {
            ValidatePNG(inputStream);

            List <Chunk> chunks = ProcessPNG(inputStream);

            for (int i = 0; i < chunks.Count; i++)
            {
                Chunk  chunk     = chunks[i];
                String chunkType = System.Text.Encoding.UTF8.GetString(chunk.type);
                if (chunkType.Equals("IHDR"))
                {
                    this.w         = (int)ToUInt32(chunk.GetData(), 0); // Width
                    this.h         = (int)ToUInt32(chunk.GetData(), 4); // Height
                    this.bitDepth  = chunk.GetData()[8];                // Bit Depth
                    this.colorType = chunk.GetData()[9];                // Color Type

                    // Console.WriteLine(
                    //         "Bit Depth == " + chunk.GetData()[8]);
                    // Console.WriteLine(
                    //         "Color Type == " + chunk.GetData()[9]);
                    // Console.WriteLine(chunk.GetData()[10]);
                    // Console.WriteLine(chunk.GetData()[11]);
                    // Console.WriteLine(chunk.GetData()[12]);

                    if (chunk.GetData()[12] == 1)
                    {
                        Console.WriteLine("Interlaced PNG images are not supported.");
                        Console.WriteLine("Convert the image using OptiPNG:\noptipng -i0 -o7 myimage.png\n");
                    }
                }
                else if (chunkType.Equals("IDAT"))
                {
                    iDAT = AppendIdatChunk(iDAT, chunk.GetData());
                }
                else if (chunkType.Equals("PLTE"))
                {
                    pLTE = chunk.GetData();
                    if (pLTE.Length % 3 != 0)
                    {
                        throw new Exception("Incorrect palette length.");
                    }
                }
                else if (chunkType.Equals("gAMA"))
                {
                    // TODO:
                    // Console.WriteLine("gAMA chunk found!");
                }
                else if (chunkType.Equals("tRNS"))
                {
                    // Console.WriteLine("tRNS chunk found!");
                    if (colorType == 3)
                    {
                        tRNS = chunk.GetData();
                    }
                }
                else if (chunkType.Equals("cHRM"))
                {
                    // TODO:
                    // Console.WriteLine("cHRM chunk found!");
                }
                else if (chunkType.Equals("sBIT"))
                {
                    // TODO:
                    // Console.WriteLine("sBIT chunk found!");
                }
                else if (chunkType.Equals("bKGD"))
                {
                    // TODO:
                    // Console.WriteLine("bKGD chunk found!");
                }
            }

            byte[] inflatedImageData = Decompressor.inflate(iDAT);

            byte[] imageData;
            if (colorType == 0)
            {
                // Grayscale Image
                if (bitDepth == 16)
                {
                    imageData = GetImageColorType0BitDepth16(inflatedImageData);
                }
                else if (bitDepth == 8)
                {
                    imageData = GetImageColorType0BitDepth8(inflatedImageData);
                }
                else if (bitDepth == 4)
                {
                    imageData = GetImageColorType0BitDepth4(inflatedImageData);
                }
                else if (bitDepth == 2)
                {
                    imageData = GetImageColorType0BitDepth2(inflatedImageData);
                }
                else if (bitDepth == 1)
                {
                    imageData = GetImageColorType0BitDepth1(inflatedImageData);
                }
                else
                {
                    throw new Exception("Image with unsupported bit depth == " + bitDepth);
                }
            }
            else if (colorType == 6)
            {
                if (bitDepth == 8)
                {
                    imageData = GetImageColorType6BitDepth8(inflatedImageData);
                }
                else
                {
                    throw new Exception("Image with unsupported bit depth == " + bitDepth);
                }
            }
            else
            {
                // Color Image
                if (pLTE == null)
                {
                    // Trucolor Image
                    if (bitDepth == 16)
                    {
                        imageData = GetImageColorType2BitDepth16(inflatedImageData);
                    }
                    else
                    {
                        imageData = GetImageColorType2BitDepth8(inflatedImageData);
                    }
                }
                else
                {
                    // Indexed Image
                    if (bitDepth == 8)
                    {
                        imageData = GetImageColorType3BitDepth8(inflatedImageData);
                    }
                    else if (bitDepth == 4)
                    {
                        imageData = GetImageColorType3BitDepth4(inflatedImageData);
                    }
                    else if (bitDepth == 2)
                    {
                        imageData = GetImageColorType3BitDepth2(inflatedImageData);
                    }
                    else if (bitDepth == 1)
                    {
                        imageData = GetImageColorType3BitDepth1(inflatedImageData);
                    }
                    else
                    {
                        throw new Exception("Image with unsupported bit depth == " + bitDepth);
                    }
                }
            }

            deflatedImageData = Compressor.deflate(imageData);
        }
Esempio n. 4
0
        // Truecolor Image with Alpha Transparency
        private byte[] getImageColorType6BitDepth8()
        {
            int j = 0;
            byte[] image = new byte[4 * this.w * this.h];

            byte filter = 0x00;
            int scanLineLength = 4 * this.w;

            for (int i = 0; i < inflated.Length; i++) {
            if (i % ( scanLineLength + 1) == 0) {
                filter = inflated[i];
                continue;
            }

            image[j] = inflated[i];

            int a = 0;
            int b = 0;
            int c = 0;

            if (j % scanLineLength >= 4 ) {
                a = (image[j - 4] & 0x000000ff);
            }
            if (j >= scanLineLength ) {
                b = (image[j - scanLineLength] & 0x000000ff );
            }
            if (j % scanLineLength >= 4 && j >= scanLineLength) {
                c = (image[j - (scanLineLength + 4 )] & 0x000000ff);
            }

            applyFilters(filter, image, j, a, b, c);
            j++;
            }

            byte[] idata = new byte[ 3 * this.w * this.h ]; // Image data
            byte[] alpha = new byte[this.w * this.h];       // Alpha values

            int k = 0;
            int n = 0;
            for (int i = 0; i < image.Length; i += 4) {
            idata[k]     = image[i];
            idata[k + 1] = image[i + 1];
            idata[k + 2] = image[i + 2];
            alpha[n]     = image[i + 3];

            k += 3;
            n += 1;
            }

            Compressor compressor = new Compressor(alpha);
            deflatedAlpha = compressor.GetCompressedData();

            return idata;
        }
Esempio n. 5
0
        // Indexed Image with Bit Depth == 8
        private byte[] getImageColorType3BitDepth8()
        {
            int j = 0;
            int n = 0;

            byte[] alpha = null;
            if (alphaForPalette != null) {
            alpha = new byte[this.w * this.h];
            }

            byte[] image = new byte[3*(inflated.Length - this.h)];
            int scanLineLength = this.w + 1;
            for (int i = 0; i < inflated.Length; i++) {
            if (i % scanLineLength == 0) {
                // Skip the filter byte
                continue;
            }

            int k = ((int) inflated[i] & 0x000000ff);
            image[j++] = rgb[3*k];
            image[j++] = rgb[3*k + 1];
            image[j++] = rgb[3*k + 2];

            if (alphaForPalette != null) {
                alpha[n++] = alphaForPalette[k];
            }
            }

            if (alphaForPalette != null) {
            Compressor compressor = new Compressor(alpha);
            deflatedAlpha = compressor.GetCompressedData();
            }

            return image;
        }
Esempio n. 6
0
 private byte[] DeflateReconstructedData()
 {
     Compressor compressor = new Compressor(image);
     return compressor.GetCompressedData();
 }
Esempio n. 7
0
        private byte[] DeflateReconstructedData()
        {
            Compressor compressor = new Compressor(image);

            return(compressor.GetCompressedData());
        }
Esempio n. 8
0
        // Truecolor Image with Alpha Transparency
        private byte[] getImageColorType6BitDepth8()
        {
            int j = 0;

            byte[] image = new byte[4 * this.w * this.h];

            byte filter         = 0x00;
            int  scanLineLength = 4 * this.w;

            for (int i = 0; i < inflated.Length; i++)
            {
                if (i % (scanLineLength + 1) == 0)
                {
                    filter = inflated[i];
                    continue;
                }

                image[j] = inflated[i];

                int a = 0;
                int b = 0;
                int c = 0;

                if (j % scanLineLength >= 4)
                {
                    a = (image[j - 4] & 0x000000ff);
                }
                if (j >= scanLineLength)
                {
                    b = (image[j - scanLineLength] & 0x000000ff);
                }
                if (j % scanLineLength >= 4 && j >= scanLineLength)
                {
                    c = (image[j - (scanLineLength + 4)] & 0x000000ff);
                }

                applyFilters(filter, image, j, a, b, c);
                j++;
            }

            byte[] idata = new byte[3 * this.w * this.h]; // Image data
            byte[] alpha = new byte[this.w * this.h];     // Alpha values

            int k = 0;
            int n = 0;

            for (int i = 0; i < image.Length; i += 4)
            {
                idata[k]     = image[i];
                idata[k + 1] = image[i + 1];
                idata[k + 2] = image[i + 2];
                alpha[n]     = image[i + 3];

                k += 3;
                n += 1;
            }

            Compressor compressor = new Compressor(alpha);

            deflatedAlpha = compressor.GetCompressedData();

            return(idata);
        }