Package org.apache.sanselan

Examples of org.apache.sanselan.ImageReadException


            // throw new ImageReadException("Png: No chunks");
            return null;
        }

        if (chunks.size() > 1)
            throw new ImageReadException(
                    "PNG contains more than one ICC Profile ");

        PNGChunkiCCP pngChunkiCCP = (PNGChunkiCCP) chunks.get(0);
        byte bytes[] = pngChunkiCCP.UncompressedProfile;
View Full Code Here


            throws ImageReadException, IOException
    {
        ArrayList chunks = readChunks(byteSource, new int[] { IHDR, }, true);

        if ((chunks == null) || (chunks.size() < 1))
            throw new ImageReadException("Png: No chunks");

        if (chunks.size() > 1)
            throw new ImageReadException("PNG contains more than one Header");

        PNGChunkIHDR pngChunkIHDR = (PNGChunkIHDR) chunks.get(0);

        return new Dimension(pngChunkIHDR.width, pngChunkIHDR.height);
    }
View Full Code Here

        case COLOR_TYPE_TRUE_COLOR_WITH_ALPHA:
            return false;
        }

        // return -1;
        throw new ImageReadException("PNG: unknown color type: " + colorType);
    }
View Full Code Here

        case COLOR_TYPE_TRUE_COLOR_WITH_ALPHA:
            return 4;
        }

        // return -1;
        throw new ImageReadException("PNG: unknown color type: " + colorType);
    }
View Full Code Here

        case COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B
            // triple,
            // followed by an alpha sample.
            return true;
        default:
            throw new ImageReadException("PNG: unknown color type: "
                    + ColorType);
        }
    }
View Full Code Here

        case COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a grayscale
            // sample,
        case COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an R,G,B
            // triple,
        default:
            throw new ImageReadException(
                    "Simple Transparency not compatible with ColorType: "
                            + ColorType);
        }
    }
View Full Code Here

        // if(chunks!=null)
        // System.out.println("chunks: " + chunks.size());

        if ((chunks == null) || (chunks.size() < 1))
            throw new ImageReadException("PNG: no chunks");

        ArrayList IHDRs = filterChunks(chunks, IHDR);
        if (IHDRs.size() != 1)
            throw new ImageReadException("PNG contains more than one Header");

        PNGChunkIHDR pngChunkIHDR = (PNGChunkIHDR) IHDRs.get(0);
        PNGChunk pngChunktRNS = null;

        boolean isTransparent = false;

        ArrayList tRNSs = filterChunks(chunks, tRNS);
        if (tRNSs.size() > 0)
        {
            isTransparent = true;
            pngChunktRNS = (PNGChunk) IHDRs.get(0);
        } else {
            // CE - Fix Alpha.
            isTransparent = hasAlphaChannel(pngChunkIHDR.colorType);
            // END FIX
        }

        PNGChunkpHYs pngChunkpHYs = null;

        ArrayList pHYss = filterChunks(chunks, pHYs);
        if (pHYss.size() > 1)
            throw new ImageReadException("PNG contains more than one pHYs: "
                    + pHYss.size());
        else if (pHYss.size() == 1)
            pngChunkpHYs = (PNGChunkpHYs) pHYss.get(0);

        ArrayList tEXts = filterChunks(chunks, tEXt);
        ArrayList zTXts = filterChunks(chunks, zTXt);
        ArrayList iTXts = filterChunks(chunks, iTXt);

        {
            ArrayList comments = new ArrayList();
            List textChunks = new ArrayList();

            for (int i = 0; i < tEXts.size(); i++)
            {
                PNGChunktEXt pngChunktEXt = (PNGChunktEXt) tEXts.get(i);
                comments.add(pngChunktEXt.keyword + ": " + pngChunktEXt.text);
                textChunks.add(pngChunktEXt.getContents());
            }
            for (int i = 0; i < zTXts.size(); i++)
            {
                PNGChunkzTXt pngChunkzTXt = (PNGChunkzTXt) zTXts.get(i);
                comments.add(pngChunkzTXt.keyword + ": " + pngChunkzTXt.text);
                textChunks.add(pngChunkzTXt.getContents());
            }
            for (int i = 0; i < iTXts.size(); i++)
            {
                PNGChunkiTXt pngChunkiTXt = (PNGChunkiTXt) iTXts.get(i);
                comments.add(pngChunkiTXt.keyword + ": " + pngChunkiTXt.text);
                textChunks.add(pngChunkiTXt.getContents());
            }

            int BitsPerPixel = pngChunkIHDR.bitDepth
                    * samplesPerPixel(pngChunkIHDR.colorType);
            ImageFormat Format = ImageFormat.IMAGE_FORMAT_PNG;
            String FormatName = "PNG Portable Network Graphics";
            int Height = pngChunkIHDR.height;
            String MimeType = "image/png";
            int NumberOfImages = 1;
            int Width = pngChunkIHDR.width;
            boolean isProgressive = (pngChunkIHDR.interlaceMethod != 0);

            int PhysicalHeightDpi = -1;
            float PhysicalHeightInch = -1;
            int PhysicalWidthDpi = -1;
            float PhysicalWidthInch = -1;

            // if (pngChunkpHYs != null)
            // {
            // System.out.println("\t" + "pngChunkpHYs.UnitSpecifier: " +
            // pngChunkpHYs.UnitSpecifier );
            // System.out.println("\t" + "pngChunkpHYs.PixelsPerUnitYAxis: " +
            // pngChunkpHYs.PixelsPerUnitYAxis );
            // System.out.println("\t" + "pngChunkpHYs.PixelsPerUnitXAxis: " +
            // pngChunkpHYs.PixelsPerUnitXAxis );
            // }
            if ((pngChunkpHYs != null) && (pngChunkpHYs.UnitSpecifier == 1)) // meters
            {
                double meters_per_inch = 0.0254;

                PhysicalWidthDpi = (int) Math
                        .round(pngChunkpHYs.PixelsPerUnitXAxis
                                * meters_per_inch);
                PhysicalWidthInch = (float) ((double) Width
                        * (double) pngChunkpHYs.PixelsPerUnitXAxis * meters_per_inch);
                PhysicalHeightDpi = (int) Math
                        .round(pngChunkpHYs.PixelsPerUnitYAxis
                                * meters_per_inch);
                PhysicalHeightInch = (float) ((double) Height
                        * (double) pngChunkpHYs.PixelsPerUnitYAxis * meters_per_inch);
            }

            String FormatDetails = "Png";

            boolean usesPalette = false;

            ArrayList PLTEs = filterChunks(chunks, PLTE);
            if (PLTEs.size() > 1)
                usesPalette = true;

            int ColorType;
            switch (pngChunkIHDR.colorType)
            {
            case COLOR_TYPE_GREYSCALE: // 1,2,4,8,16 Each pixel is a grayscale
                // sample.
            case COLOR_TYPE_GREYSCALE_WITH_ALPHA: // 8,16 Each pixel is a
                // grayscale sample,
                // followed by an alpha sample.
                ColorType = ImageInfo.COLOR_TYPE_GRAYSCALE;
                break;
            case COLOR_TYPE_TRUE_COLOR: // 8,16 Each pixel is an R,G,B triple.
            case COLOR_TYPE_INDEXED_COLOR: // 1,2,4,8 Each pixel is a palette
                // index;
            case COLOR_TYPE_TRUE_COLOR_WITH_ALPHA: // 8,16 Each pixel is an
                // R,G,B triple,
                // followed by an alpha sample.
                ColorType = ImageInfo.COLOR_TYPE_RGB;
                break;
            default:
                throw new ImageReadException("Png: Unknown ColorType: "
                        + pngChunkIHDR.colorType);
            }

            String compressionAlgorithm = ImageInfo.COMPRESSION_ALGORITHM_PNG_FILTER;

View Full Code Here

        ArrayList chunks = readChunks(byteSource, new int[] { IHDR, PLTE, IDAT,
                tRNS, iCCP, gAMA, sRGB, }, false);

        if ((chunks == null) || (chunks.size() < 1))
            throw new ImageReadException("PNG: no chunks");

        ArrayList IHDRs = filterChunks(chunks, IHDR);
        if (IHDRs.size() != 1)
            throw new ImageReadException("PNG contains more than one Header");

        PNGChunkIHDR pngChunkIHDR = (PNGChunkIHDR) IHDRs.get(0);

        ArrayList PLTEs = filterChunks(chunks, PLTE);
        if (PLTEs.size() > 1)
            throw new ImageReadException("PNG contains more than one Palette");

        PNGChunkPLTE pngChunkPLTE = null;
        if (PLTEs.size() == 1)
            pngChunkPLTE = (PNGChunkPLTE) PLTEs.get(0);

        // -----

        ArrayList IDATs = filterChunks(chunks, IDAT);
        if (IDATs.size() < 1)
            throw new ImageReadException("PNG missing image data");

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        for (int i = 0; i < IDATs.size(); i++)
        {
            PNGChunkIDAT pngChunkIDAT = (PNGChunkIDAT) IDATs.get(i);
            byte bytes[] = pngChunkIDAT.bytes;
            // System.out.println(i + ": bytes: " + bytes.length);
            baos.write(bytes);
        }

        byte compressed[] = baos.toByteArray();

        baos = null;

        TransparencyFilter transparencyFilter = null;

        ArrayList tRNSs = filterChunks(chunks, tRNS);
        if (tRNSs.size() > 0)
        {
            PNGChunk pngChunktRNS = (PNGChunk) tRNSs.get(0);
            transparencyFilter = getTransparencyFilter(pngChunkIHDR.colorType,
                    pngChunktRNS);
        }

        ICC_Profile icc_profile = null;
        GammaCorrection gammaCorrection = null;
        {
            ArrayList sRGBs = filterChunks(chunks, sRGB);
            ArrayList gAMAs = filterChunks(chunks, gAMA);
            ArrayList iCCPs = filterChunks(chunks, iCCP);
            if (sRGBs.size() > 1)
                throw new ImageReadException("PNG: unexpected sRGB chunk");
            if (gAMAs.size() > 1)
                throw new ImageReadException("PNG: unexpected gAMA chunk");
            if (iCCPs.size() > 1)
                throw new ImageReadException("PNG: unexpected iCCP chunk");

            if (sRGBs.size() == 1)
            {
                // no color management neccesary.
                if (debug)
                    System.out.println("sRGB, no color management neccesary.");
            } else if (iCCPs.size() == 1)
            {
                if (debug)
                    System.out.println("iCCP.");

                PNGChunkiCCP pngChunkiCCP = (PNGChunkiCCP) iCCPs.get(0);
                byte bytes[] = pngChunkiCCP.UncompressedProfile;

                icc_profile = ICC_Profile.getInstance(bytes);
            } else if (gAMAs.size() == 1)
            {
                PNGChunkgAMA pngChunkgAMA = (PNGChunkgAMA) gAMAs.get(0);
                double gamma = pngChunkgAMA.getGamma();

                // charles: what is the correct target value here?
                // double targetGamma = 2.2;
                double targetGamma = 1.0;
                double diff = Math.abs(targetGamma - gamma);
                if (diff >= 0.5)
                    gammaCorrection = new GammaCorrection(gamma, targetGamma);

                if (gammaCorrection != null)
                    if (pngChunkPLTE != null)
                        pngChunkPLTE.correct(gammaCorrection);

            }
        }

        {
            int width = pngChunkIHDR.width;
            int height = pngChunkIHDR.height;
            int colorType = pngChunkIHDR.colorType;
            int bitDepth = pngChunkIHDR.bitDepth;

            int bitsPerSample = bitDepth;

            if (pngChunkIHDR.filterMethod != 0)
                throw new ImageReadException("PNG: unknown FilterMethod: "
                        + pngChunkIHDR.filterMethod);

            int samplesPerPixel = samplesPerPixel(pngChunkIHDR.colorType);
            boolean isGrayscale = isGrayscale(pngChunkIHDR.colorType);

            int bitsPerPixel = bitsPerSample * samplesPerPixel;

            boolean hasAlpha = colorType == COLOR_TYPE_GREYSCALE_WITH_ALPHA
                    || colorType == COLOR_TYPE_TRUE_COLOR_WITH_ALPHA;

            BufferedImage result;
            if (isGrayscale)
                result = getBufferedImageFactory(params)
                        .getGrayscaleBufferedImage(width, height, hasAlpha);
            else
                result = getBufferedImageFactory(params).getColorBufferedImage(
                        width, height, hasAlpha);

            ByteArrayInputStream bais = new ByteArrayInputStream(compressed);
            InflaterInputStream iis = new InflaterInputStream(bais);

            ScanExpediter scanExpediter;

            if (pngChunkIHDR.interlaceMethod == 0)
                scanExpediter = new ScanExpediterSimple(width, height, iis,
                        result, colorType, bitDepth, bitsPerPixel,
                        pngChunkPLTE, gammaCorrection, transparencyFilter);
            else if (pngChunkIHDR.interlaceMethod == 1)
                scanExpediter = new ScanExpediterInterlaced(width, height, iis,
                        result, colorType, bitDepth, bitsPerPixel,
                        pngChunkPLTE, gammaCorrection, transparencyFilter);
            else
                throw new ImageReadException("Unknown InterlaceMethod: "
                        + pngChunkIHDR.interlaceMethod);

            scanExpediter.drive();

            if (icc_profile != null)
View Full Code Here

        }

        if (xmpChunks.size() < 1)
            return null;
        if (xmpChunks.size() > 1)
            throw new ImageReadException(
                    "PNG contains more than one XMP chunk.");

        PNGChunkiTXt chunk = (PNGChunkiTXt) xmpChunks.get(0);
        return chunk.getText();
    }
View Full Code Here

            nextByte = readByte("Header", is, "Error reading WBMP header");
            value <<= 7;
            value |= nextByte & 0x7f;
            totalBits += 7;
            if (totalBits > 31)
                throw new ImageReadException("Overflow reading WBMP multi-byte field");
        } while ((nextByte & 0x80) != 0);
        return value;
    }
View Full Code Here

TOP

Related Classes of org.apache.sanselan.ImageReadException

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.