Package org.apache.commons.imaging

Examples of org.apache.commons.imaging.ImageReadException


    private BufferedImage readXbmImage(final XbmHeader xbmHeader, final BasicCParser cParser)
            throws ImageReadException, IOException {
        String token;
        token = cParser.nextToken();
        if (!"static".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no 'static' token");
        }
        token = cParser.nextToken();
        if (token == null) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no 'unsigned' "
                            + "or 'char' token");
        }
        if ("unsigned".equals(token)) {
            token = cParser.nextToken();
        }
        if (!"char".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no 'char' token");
        }
        final String name = cParser.nextToken();
        if (name == null) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no variable name");
        }
        if (name.charAt(0) != '_' && !Character.isLetter(name.charAt(0))) {
            throw new ImageReadException(
                    "Parsing XBM file failed, variable name "
                            + "doesn't start with letter or underscore");
        }
        for (int i = 0; i < name.length(); i++) {
            final char c = name.charAt(i);
            if (!Character.isLetterOrDigit(c) && c != '_') {
                throw new ImageReadException(
                        "Parsing XBM file failed, variable name "
                                + "contains non-letter non-digit non-underscore");
            }
        }
        token = cParser.nextToken();
        if (!"[".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no '[' token");
        }
        token = cParser.nextToken();
        if (!"]".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no ']' token");
        }
        token = cParser.nextToken();
        if (!"=".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no '=' token");
        }
        token = cParser.nextToken();
        if (!"{".equals(token)) {
            throw new ImageReadException(
                    "Parsing XBM file failed, no '{' token");
        }

        final int rowLength = (xbmHeader.width + 7) / 8;
        final byte[] imageData = new byte[rowLength * xbmHeader.height];
        for (int i = 0; i < imageData.length; i++) {
            token = cParser.nextToken();
            if (token == null || !token.startsWith("0x")) {
                throw new ImageReadException("Parsing XBM file failed, "
                        + "hex value missing");
            }
            if (token.length() > 4) {
                throw new ImageReadException("Parsing XBM file failed, "
                        + "hex value too long");
            }
            final int value = Integer.parseInt(token.substring(2), 16);
            int flipped = 0;
            for (int j = 0; j < 8; j++) {
                if ((value & (1 << j)) != 0) {
                    flipped |= (0x80 >>> j);
                }
            }
            imageData[i] = (byte) flipped;

            token = cParser.nextToken();
            if (token == null) {
                throw new ImageReadException("Parsing XBM file failed, "
                        + "premature end of file");
            }
            if (!",".equals(token)
                    && (i < (imageData.length - 1) || !"}".equals(token))) {
                throw new ImageReadException("Parsing XBM file failed, "
                        + "punctuation error");
            }
        }

        int[] palette = { 0xffffff, 0x000000 };
View Full Code Here


            IOException {
        final byte identifier1 = readByte("Identifier1", is, "Not a Valid PNM File");
        final byte identifier2 = readByte("Identifier2", is, "Not a Valid PNM File");

        if (identifier1 != PnmConstants.PNM_PREFIX_BYTE) {
            throw new ImageReadException("PNM file has invalid prefix byte 1");
        }
       
        final WhiteSpaceReader wsr = new WhiteSpaceReader(is);
       
        if (identifier2 == PnmConstants.PBM_TEXT_CODE
                || identifier2 == PnmConstants.PBM_RAW_CODE
                || identifier2 == PnmConstants.PGM_TEXT_CODE
                || identifier2 == PnmConstants.PGM_RAW_CODE
                || identifier2 == PnmConstants.PPM_TEXT_CODE
                || identifier2 == PnmConstants.PPM_RAW_CODE) {
           
            final int width = Integer.parseInt(wsr.readtoWhiteSpace());
            final int height = Integer.parseInt(wsr.readtoWhiteSpace());
   
            if (identifier2 == PnmConstants.PBM_TEXT_CODE) {
                return new PbmFileInfo(width, height, false);
            } else if (identifier2 == PnmConstants.PBM_RAW_CODE) {
                return new PbmFileInfo(width, height, true);
            } else if (identifier2 == PnmConstants.PGM_TEXT_CODE) {
                final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
                return new PgmFileInfo(width, height, false, maxgray);
            } else if (identifier2 == PnmConstants.PGM_RAW_CODE) {
                final int maxgray = Integer.parseInt(wsr.readtoWhiteSpace());
                return new PgmFileInfo(width, height, true, maxgray);
            } else if (identifier2 == PnmConstants.PPM_TEXT_CODE) {
                final int max = Integer.parseInt(wsr.readtoWhiteSpace());
                return new PpmFileInfo(width, height, false, max);
            } else if (identifier2 == PnmConstants.PPM_RAW_CODE) {
                final int max = Integer.parseInt(wsr.readtoWhiteSpace());
                return new PpmFileInfo(width, height, true, max);
            } else {
                throw new ImageReadException("PNM file has invalid header.");
            }
        } else if (identifier2 == PnmConstants.PAM_RAW_CODE) {
            int width = -1;
            boolean seenWidth = false;
            int height = -1;
            boolean seenHeight = false;
            int depth = -1;
            boolean seenDepth = false;
            int maxVal = -1;
            boolean seenMaxVal = false;
            final StringBuilder tupleType = new StringBuilder();
            boolean seenTupleType = false;
           
            // Advance to next line
            wsr.readLine();
            String line;
            while ((line = wsr.readLine()) != null) {
                line = line.trim();
                if (line.charAt(0) == '#') {
                    continue;
                }
                final StringTokenizer tokenizer = new StringTokenizer(line, " ", false);
                final String type = tokenizer.nextToken();
                if ("WIDTH".equals(type)) {
                    seenWidth = true;
                    width = Integer.parseInt(tokenizer.nextToken());
                } else if ("HEIGHT".equals(type)) {
                    seenHeight = true;
                    height = Integer.parseInt(tokenizer.nextToken());
                } else if ("DEPTH".equals(type)) {
                    seenDepth = true;
                    depth = Integer.parseInt(tokenizer.nextToken());
                } else if ("MAXVAL".equals(type)) {
                    seenMaxVal = true;
                    maxVal = Integer.parseInt(tokenizer.nextToken());
                } else if ("TUPLTYPE".equals(type)) {
                    seenTupleType = true;
                    tupleType.append(tokenizer.nextToken());
                } else if ("ENDHDR".equals(type)) {
                    break;
                } else {
                    throw new ImageReadException("Invalid PAM file header type " + type);
                }
            }
           
            if (!seenWidth) {
                throw new ImageReadException("PAM header has no WIDTH");
            } else if (!seenHeight) {
                throw new ImageReadException("PAM header has no HEIGHT");
            } else if (!seenDepth) {
                throw new ImageReadException("PAM header has no DEPTH");
            } else if (!seenMaxVal) {
                throw new ImageReadException("PAM header has no MAXVAL");
            } else if (!seenTupleType) {
                throw new ImageReadException("PAM header has no TUPLTYPE");
            }
           
            return new PamFileInfo(width, height, depth, maxVal, tupleType.toString());
        } else {
            throw new ImageReadException("PNM file has invalid prefix byte 2");
        }
    }
View Full Code Here

    public Dimension getImageSize(final ByteSource byteSource, final Map<String, Object> params)
            throws ImageReadException, IOException {
        final FileInfo info = readHeader(byteSource);

        if (info == null) {
            throw new ImageReadException("PNM: Couldn't read Header");
        }

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

    public ImageInfo getImageInfo(final ByteSource byteSource, final Map<String, Object> params)
            throws ImageReadException, IOException {
        final FileInfo info = readHeader(byteSource);

        if (info == null) {
            throw new ImageReadException("PNM: Couldn't read Header");
        }

        final List<String> comments = new ArrayList<String>();

        final int bitsPerPixel = info.getBitDepth() * info.getNumComponents();
 
View Full Code Here

            if (longitudeRef.trim().equalsIgnoreCase("e")) {
                return result;
            } else if (longitudeRef.trim().equalsIgnoreCase("w")) {
                return -result;
            } else {
                throw new ImageReadException("Unknown longitude ref: \""
                        + longitudeRef + "\"");
            }
        }
View Full Code Here

            if (latitudeRef.trim().equalsIgnoreCase("n")) {
                return result;
            } else if (latitudeRef.trim().equalsIgnoreCase("s")) {
                return -result;
            } else {
                throw new ImageReadException("Unknown latitude ref: \""
                        + latitudeRef + "\"");
            }
        }
View Full Code Here

   
            try {
                final InputStream rgbTxtStream =
                        XpmImageParser.class.getResourceAsStream("rgb.txt");
                if (rgbTxtStream == null) {
                    throw new ImageReadException("Couldn't find rgb.txt in our resources");
                }
                final Map<String, Integer> colors = new HashMap<String, Integer>();
                BufferedReader reader = null;
                boolean canThrow = false;
                try {
                    reader = new BufferedReader(new InputStreamReader(rgbTxtStream,
                            "US-ASCII"));
                    String line;
                    while ((line = reader.readLine()) != null) {
                        if (line.charAt(0) == '!') {
                            continue;
                        }
                        try {
                            final int red = Integer.parseInt(line.substring(0, 3).trim());
                            final int green = Integer.parseInt(line.substring(4, 7).trim());
                            final int blue = Integer.parseInt(line.substring(8, 11).trim());
                            final String colorName = line.substring(11).trim();
                            colors.put(colorName, 0xff000000 | (red << 16)
                                    | (green << 8) | blue);
                        } catch (final NumberFormatException nfe) {
                            throw new ImageReadException("Couldn't parse color in rgb.txt", nfe);
                        }
                    }
                    canThrow = true;
                } finally {
                    IoUtils.closeQuietly(canThrow, reader);
                }
                colorNames = colors;
            } catch (final IOException ioException) {
                throw new ImageReadException("Could not parse rgb.txt", ioException);
            }
        }
    }
View Full Code Here

            is = byteSource.getInputStream();
            final StringBuilder firstComment = new StringBuilder();
            final ByteArrayOutputStream preprocessedFile = BasicCParser.preprocess(
                    is, firstComment, null);
            if (!"XPM".equals(firstComment.toString().trim())) {
                throw new ImageReadException("Parsing XPM file failed, "
                        + "signature isn't '/* XPM */'");
            }

            final XpmParseResult xpmParseResult = new XpmParseResult();
            xpmParseResult.cParser = new BasicCParser(new ByteArrayInputStream(
View Full Code Here

    private boolean parseNextString(final BasicCParser cParser,
            final StringBuilder stringBuilder) throws IOException, ImageReadException {
        stringBuilder.setLength(0);
        String token = cParser.nextToken();
        if (token.charAt(0) != '"') {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "no string found where expected");
        }
        BasicCParser.unescapeString(stringBuilder, token);
        for (token = cParser.nextToken(); token.charAt(0) == '"'; token = cParser
                .nextToken()) {
            BasicCParser.unescapeString(stringBuilder, token);
        }
        if (",".equals(token)) {
            return true;
        } else if ("}".equals(token)) {
            return false;
        } else {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "no ',' or '}' found where expected");
        }
    }
View Full Code Here

    private XpmHeader parseXpmValuesSection(final String row)
            throws ImageReadException {
        final String[] tokens = BasicCParser.tokenizeRow(row);
        if (tokens.length < 4 && tokens.length > 7) {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "<Values> section has incorrect tokens");
        }
        try {
            final int width = Integer.parseInt(tokens[0]);
            final int height = Integer.parseInt(tokens[1]);
            final int numColors = Integer.parseInt(tokens[2]);
            final int numCharsPerPixel = Integer.parseInt(tokens[3]);
            int xHotSpot = -1;
            int yHotSpot = -1;
            boolean xpmExt = false;
            if (tokens.length >= 6) {
                xHotSpot = Integer.parseInt(tokens[4]);
                yHotSpot = Integer.parseInt(tokens[5]);
            }
            if (tokens.length == 5 || tokens.length == 7) {
                if ("XPMEXT".equals(tokens[tokens.length - 1])) {
                    xpmExt = true;
                } else {
                    throw new ImageReadException("Parsing XPM file failed, "
                            + "can't parse <Values> section XPMEXT");
                }
            }
            return new XpmHeader(width, height, numColors, numCharsPerPixel,
                    xHotSpot, yHotSpot, xpmExt);
        } catch (final NumberFormatException nfe) {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "error parsing <Values> section", nfe);
        }
    }
View Full Code Here

TOP

Related Classes of org.apache.commons.imaging.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.