Package org.apache.commons.imaging

Examples of org.apache.commons.imaging.ImageReadException


            }

            return getPixelARGB(alpha, red, green, blue);
        }
        default:
            throw new ImageReadException("PNG: unknown color type: " + colorType);
        }
    }
View Full Code Here


    protected byte[] getNextScanline(final InputStream is, final int length, final byte[] prev,
            final int bytesPerPixel) throws ImageReadException, IOException {
        final int filterType = is.read();
        if (filterType < 0) {
            throw new ImageReadException("PNG: missing filter type");
        }
        if (filterType >= FilterType.values().length) {
            throw new ImageReadException("PNG: unknown filterType: " + filterType);
        }

        byte[] scanline = readBytes("scanline", is, length, "PNG: missing image data");

        return unfilterScanline(FilterType.values()[filterType], scanline, prev, bytesPerPixel);
View Full Code Here

    }

    public String parseXmpJpegSegment(final byte[] segmentData)
            throws ImageReadException {
        if (!isXmpJpegSegment(segmentData)) {
            throw new ImageReadException("Invalid JPEG XMP Segment.");
        }
        final int index = JpegConstants.XMP_IDENTIFIER.size();

        try {
            // segment data is UTF-8 encoded xml.
            return new String(segmentData, index, segmentData.length - index, "utf-8");
        } catch (final UnsupportedEncodingException e) {
            throw new ImageReadException("Invalid JPEG XMP Segment.", e);
        }
    }
View Full Code Here

        if (index >= bytes.length) {
            return rgb;
        }

        if ((index < 0) || (index > bytes.length)) {
            throw new ImageReadException(
                    "TransparencyFilterIndexedColor index: " + index + ", bytes.length: " + bytes.length);
        }

        final int alpha = bytes[index];
        return ((0xff & alpha) << 24) | (0x00ffffff & rgb);
View Full Code Here

                    if (!hadBackSlash) {
                        return token.toString();
                    }
                    hadBackSlash = false;
                } else if (c == '\r' || c == '\n') {
                    throw new ImageReadException(
                            "Unterminated string in XPM file");
                } else {
                    token.append((char) c);
                    hadBackSlash = false;
                }
            } else if (inIdentifier) {
                if (Character.isLetterOrDigit(c) || c == '_') {
                    token.append((char) c);
                } else {
                    is.unread(c);
                    return token.toString();
                }
            } else {
                if (c == '"') {
                    token.append('"');
                    inString = true;
                } else if (Character.isLetterOrDigit(c) || c == '_') {
                    token.append((char) c);
                    inIdentifier = true;
                } else if (c == '{' || c == '}' || c == '[' || c == ']'
                        || c == '*' || c == ';' || c == '=' || c == ',') {
                    token.append((char) c);
                    return token.toString();
                } else if (c == ' ' || c == '\t' || c == '\r' || c == '\n') {
                    // ignore
                } else {
                    throw new ImageReadException(
                            "Unhandled/invalid character '" + ((char) c)
                                    + "' found in XPM file");
                }
            }
        }

        if (inIdentifier) {
            return token.toString();
        }
        if (inString) {
            throw new ImageReadException("Unterminated string ends XMP file");
        }
        return null;
    }
View Full Code Here

                    } else {
                        inSingleQuotes = false;
                    }
                    out.write('\'');
                } else if (c == '\r' || c == '\n') {
                    throw new ImageReadException("Unterminated single quote in file");
                } else {
                    if (hadBackSlash) {
                        out.write('\\');
                        hadBackSlash = false;
                    }
                    out.write(c);
                }
            } else if (inString) {
                if (c == '\\') {
                    if (hadBackSlash) {
                        out.write('\\');
                        out.write('\\');
                        hadBackSlash = false;
                    } else {
                        hadBackSlash = true;
                    }
                } else if (c == '"') {
                    if (hadBackSlash) {
                        out.write('\\');
                        hadBackSlash = false;
                    } else {
                        inString = false;
                    }
                    out.write('"');
                } else if (c == '\r' || c == '\n') {
                    throw new ImageReadException("Unterminated string in file");
                } else {
                    if (hadBackSlash) {
                        out.write('\\');
                        hadBackSlash = false;
                    }
                    out.write(c);
                }
            } else if (inDirective) {
                if (c == '\r' || c == '\n') {
                    inDirective = false;
                    final String[] tokens = tokenizeRow(directiveBuffer.toString());
                    if (tokens.length < 2 || tokens.length > 3) {
                        throw new ImageReadException("Bad preprocessor directive");
                    }
                    if (!tokens[0].equals("define")) {
                        throw new ImageReadException("Invalid/unsupported "
                                + "preprocessor directive '" + tokens[0] + "'");
                    }
                    defines.put(tokens[1], (tokens.length == 3) ? tokens[2]
                            : null);
                    directiveBuffer.setLength(0);
                } else {
                    directiveBuffer.append((char) c);
                }
            } else {
                if (c == '/') {
                    if (hadSlash) {
                        out.write('/');
                    }
                    hadSlash = true;
                } else if (c == '*') {
                    if (hadSlash) {
                        inComment = true;
                        hadSlash = false;
                    } else {
                        out.write(c);
                    }
                } else if (c == '\'') {
                    if (hadSlash) {
                        out.write('/');
                    }
                    hadSlash = false;
                    out.write(c);
                    inSingleQuotes = true;
                } else if (c == '"') {
                    if (hadSlash) {
                        out.write('/');
                    }
                    hadSlash = false;
                    out.write(c);
                    inString = true;
                } else if (c == '#') {
                    if (defines == null) {
                        throw new ImageReadException("Unexpected preprocessor directive");
                    }
                    inDirective = true;
                } else {
                    if (hadSlash) {
                        out.write('/');
                    }
                    hadSlash = false;
                    out.write(c);
                    // Only whitespace allowed before first comment:
                    if (c != ' ' && c != '\t' && c != '\r' && c != '\n') {
                        seenFirstComment = true;
                    }
                }
            }
        }
        if (hadSlash) {
            out.write('/');
        }
        if (hadStar) {
            out.write('*');
        }
        if (inString) {
            throw new ImageReadException("Unterminated string at the end of file");
        }
        if (inComment) {
            throw new ImageReadException("Unterminated comment at the end of file");
        }
        return out;
    }
View Full Code Here

    }

    public static void unescapeString(final StringBuilder stringBuilder, final String string)
            throws ImageReadException {
        if (string.length() < 2) {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "string is too short");
        }
        if (string.charAt(0) != '"'
                || string.charAt(string.length() - 1) != '"') {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "string not surrounded by '\"'");
        }
        boolean hadBackSlash = false;
        for (int i = 1; i < (string.length() - 1); i++) {
            final char c = string.charAt(i);
            if (hadBackSlash) {
                if (c == '\\') {
                    stringBuilder.append('\\');
                } else if (c == '"') {
                    stringBuilder.append('"');
                } else if (c == '\'') {
                    stringBuilder.append('\'');
                } else if (c == 'x') {
                    if (i + 2 >= string.length()) {
                        throw new ImageReadException(
                                "Parsing XPM file failed, "
                                        + "hex constant in string too short");
                    }
                    final char hex1 = string.charAt(i + 1);
                    final char hex2 = string.charAt(i + 2);
                    i += 2;
                    int constant;
                    try {
                        constant = Integer.parseInt(Character.toString(hex1) + Character.toString(hex2), 16);
                    } catch (final NumberFormatException nfe) {
                        throw new ImageReadException(
                                "Parsing XPM file failed, "
                                        + "hex constant invalid", nfe);
                    }
                    stringBuilder.append((char) constant);
                } else if (c == '0' || c == '1' || c == '2' || c == '3'
                        || c == '4' || c == '5' || c == '6' || c == '7') {
                    int length = 1;
                    if (i + 1 < string.length() && '0' <= string.charAt(i + 1)
                            && string.charAt(i + 1) <= '7') {
                        ++length;
                    }
                    if (i + 2 < string.length() && '0' <= string.charAt(i + 2)
                            && string.charAt(i + 2) <= '7') {
                        ++length;
                    }
                    int constant = 0;
                    for (int j = 0; j < length; j++) {
                        constant *= 8;
                        constant += (string.charAt(i + j) - '0');
                    }
                    i += length - 1;
                    stringBuilder.append((char) constant);
                } else if (c == 'a') {
                    stringBuilder.append((char) 0x07);
                } else if (c == 'b') {
                    stringBuilder.append((char) 0x08);
                } else if (c == 'f') {
                    stringBuilder.append((char) 0x0c);
                } else if (c == 'n') {
                    stringBuilder.append((char) 0x0a);
                } else if (c == 'r') {
                    stringBuilder.append((char) 0x0d);
                } else if (c == 't') {
                    stringBuilder.append((char) 0x09);
                } else if (c == 'v') {
                    stringBuilder.append((char) 0x0b);
                } else {
                    throw new ImageReadException("Parsing XPM file failed, "
                            + "invalid escape sequence");
                }
                hadBackSlash = false;
            } else {
                if (c == '\\') {
                    hadBackSlash = true;
                } else if (c == '"') {
                    throw new ImageReadException("Parsing XPM file failed, "
                            + "extra '\"' found in string");
                } else {
                    stringBuilder.append(c);
                }
            }
        }
        if (hadBackSlash) {
            throw new ImageReadException("Parsing XPM file failed, "
                    + "unterminated escape sequence found in string");
        }
    }
View Full Code Here

   
            final byte[] idString = readBytes("", bis,
                    JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING.size(),
                    "App13 Segment missing identification string");
            if (!JpegConstants.PHOTOSHOP_IDENTIFICATION_STRING.equals(idString)) {
                throw new ImageReadException("Not a Photoshop App13 Segment");
            }
   
            // int index = PHOTOSHOP_IDENTIFICATION_STRING.length;
   
            while (true) {
                final int imageResourceBlockSignature;
                try {
                    imageResourceBlockSignature = read4Bytes("", bis,
                            "Image Resource Block missing identification string", APP13_BYTE_ORDER);
                } catch (final IOException ioEx) {
                    break;
                }
                if (imageResourceBlockSignature != JpegConstants.CONST_8BIM) {
                    throw new ImageReadException(
                            "Invalid Image Resource Block Signature");
                }
   
                final int blockType = read2Bytes("", bis, "Image Resource Block missing type", APP13_BYTE_ORDER);
                if (verbose) {
                    Debug.debug("blockType: " + blockType + " (0x" + Integer.toHexString(blockType) + ")");
                }
   
                final int blockNameLength = readByte("Name length", bis, "Image Resource Block missing name length");
                if (verbose && blockNameLength > 0) {
                    Debug.debug("blockNameLength: " + blockNameLength + " (0x"
                            + Integer.toHexString(blockNameLength) + ")");
                }
                byte[] blockNameBytes;
                if (blockNameLength == 0) {
                    readByte("Block name bytes", bis, "Image Resource Block has invalid name");
                    blockNameBytes = new byte[0];
                } else {
                    try {
                        blockNameBytes = readBytes("", bis, blockNameLength,
                                "Invalid Image Resource Block name");
                    } catch (final IOException ioEx) {
                        if (strict) {
                            throw ioEx;
                        }
                        break;
                    }
   
                    if (blockNameLength % 2 == 0) {
                        readByte("Padding byte", bis, "Image Resource Block missing padding byte");
                    }
                }
   
                final int blockSize = read4Bytes("", bis, "Image Resource Block missing size", APP13_BYTE_ORDER);
                if (verbose) {
                    Debug.debug("blockSize: " + blockSize + " (0x" + Integer.toHexString(blockSize) + ")");
                }
   
                /*
                 * doesn't catch cases where blocksize is invalid but is still less
                 * than bytes.length but will at least prevent OutOfMemory errors
                 */
                if (blockSize > bytes.length) {
                    throw new ImageReadException("Invalid Block Size : " + blockSize + " > " + bytes.length);
                }
   
                final byte[] blockData;
                try {
                    blockData = readBytes("", bis, blockSize, "Invalid Image Resource Block data");
View Full Code Here

        super(marker, markerLength);

        final byte[] signature = readBytes(is, JpegConstants.JFIF0_SIGNATURE.size());
        if (!JpegConstants.JFIF0_SIGNATURE.equals(signature)
                && !JpegConstants.JFIF0_SIGNATURE_ALTERNATIVE.equals(signature)) {
            throw new ImageReadException(
                    "Not a Valid JPEG File: missing JFIF string");
        }

        jfifMajorVersion = readByte("JFIF_major_version", is,
                "Not a Valid JPEG File");
View Full Code Here

        final JFIFPieces jfifPieces = analyzeJFIF(byteSource);
        final List<JFIFPiece> oldPieces = jfifPieces.pieces;
        final List<JFIFPiece> photoshopApp13Segments = findPhotoshopApp13Segments(oldPieces);

        if (photoshopApp13Segments.size() > 1) {
            throw new ImageReadException(
                    "Image contains more than one Photoshop App13 segment.");
        }
        final List<JFIFPiece> newPieces = removePhotoshopApp13Segments(oldPieces);
        if (photoshopApp13Segments.size() == 1) {
            final JFIFPieceSegment oldSegment = (JFIFPieceSegment) photoshopApp13Segments
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.