Search in sources :

Example 1 with ICCProfileWrapper

use of org.apache.pdfbox.preflight.graphic.ICCProfileWrapper in project pdfbox by apache.

the class CatalogValidationProcess method validateICCProfile.

/**
 * This method checks the destOutputProfile which must be a valid ICCProfile.
 *
 * If an other ICCProfile exists in the mapDestOutputProfile, a ValdiationError
 * (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE) is returned because of only one profile is authorized. If the
 * ICCProfile already exist in the mapDestOutputProfile, the method returns null. If the destOutputProfile contains
 * an invalid ICCProfile, a ValidationError (ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID) is returned If the
 * destOutputProfile is an empty stream, a ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY) is returned.
 *
 * If the destOutputFile is valid, mapDestOutputProfile is updated, the ICCProfile is added to the document ctx and
 * null is returned.
 *
 * @param destOutputProfile
 * @param mapDestOutputProfile
 * @param ctx the preflight context.
 * @throws ValidationException
 */
protected void validateICCProfile(COSBase destOutputProfile, Map<COSObjectKey, Boolean> mapDestOutputProfile, PreflightContext ctx) throws ValidationException {
    try {
        if (destOutputProfile == null) {
            return;
        }
        // destOutputProfile should be an instance of COSObject because of this is a object reference
        if (destOutputProfile instanceof COSObject) {
            if (mapDestOutputProfile.containsKey(new COSObjectKey((COSObject) destOutputProfile))) {
                // the profile is already checked. continue
                return;
            } else if (!mapDestOutputProfile.isEmpty()) {
                // A DestOutputProfile exits but it isn't the same, error
                addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_MULTIPLE, "More than one ICCProfile is defined"));
                return;
            }
        // else the profile will be kept in the tmpDestOutputProfile if it is valid
        }
        // keep reference to avoid multiple profile definition
        mapDestOutputProfile.put(new COSObjectKey((COSObject) destOutputProfile), true);
        COSDocument cosDocument = ctx.getDocument().getDocument();
        COSStream stream = COSUtils.getAsStream(destOutputProfile, cosDocument);
        if (stream == null) {
            addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_INVALID_ENTRY, "OutputIntent object uses a NULL Object"));
            return;
        }
        InputStream is = stream.createInputStream();
        ICC_Profile iccp = null;
        try {
            iccp = ICC_Profile.getInstance(is);
        } finally {
            is.close();
        }
        if (!validateICCProfileNEntry(stream, ctx, iccp)) {
            return;
        }
        if (!validateICCProfileVersion(iccp, ctx)) {
            return;
        }
        if (ctx.getIccProfileWrapper() == null) {
            ctx.setIccProfileWrapper(new ICCProfileWrapper(iccp));
        }
    } catch (IllegalArgumentException e) {
        // this is not a ICC_Profile
        addValidationError(ctx, new ValidationError(ERROR_GRAPHIC_OUTPUT_INTENT_ICC_PROFILE_INVALID, "DestOutputProfile isn't a valid ICCProfile: " + e.getMessage(), e));
    } catch (IOException e) {
        throw new ValidationException("Unable to parse the ICC Profile.", e);
    }
}
Also used : COSObjectKey(org.apache.pdfbox.cos.COSObjectKey) COSStream(org.apache.pdfbox.cos.COSStream) ValidationException(org.apache.pdfbox.preflight.exception.ValidationException) COSObject(org.apache.pdfbox.cos.COSObject) InputStream(java.io.InputStream) COSDocument(org.apache.pdfbox.cos.COSDocument) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) ICC_Profile(java.awt.color.ICC_Profile) ICCProfileWrapper(org.apache.pdfbox.preflight.graphic.ICCProfileWrapper)

Aggregations

ICC_Profile (java.awt.color.ICC_Profile)1 IOException (java.io.IOException)1 InputStream (java.io.InputStream)1 COSDocument (org.apache.pdfbox.cos.COSDocument)1 COSObject (org.apache.pdfbox.cos.COSObject)1 COSObjectKey (org.apache.pdfbox.cos.COSObjectKey)1 COSStream (org.apache.pdfbox.cos.COSStream)1 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)1 ValidationException (org.apache.pdfbox.preflight.exception.ValidationException)1 ICCProfileWrapper (org.apache.pdfbox.preflight.graphic.ICCProfileWrapper)1