Search in sources :

Example 1 with ColorSpaceHelper

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

the class ShadingPatternValidationProcess method checkColorSpace.

/**
 * Checks if the ColorSapce entry is consistent which rules of the PDF Reference and the ISO 190005-1:2005
 * Specification.
 *
 * This method is called by the validate method.
 *
 * @param context the preflight context.
 * @param page the page to check.
 * @param shadingRes the Shading pattern to check.
 * @throws ValidationException
 */
protected void checkColorSpace(PreflightContext context, PDPage page, PDShading shadingRes) throws ValidationException {
    try {
        PDColorSpace pColorSpace = shadingRes.getColorSpace();
        PreflightConfiguration config = context.getConfig();
        ColorSpaceHelperFactory csFact = config.getColorSpaceHelperFact();
        ColorSpaceHelper csh = csFact.getColorSpaceHelper(context, pColorSpace, ColorSpaceRestriction.NO_PATTERN);
        csh.validate();
    } catch (IOException e) {
        LOGGER.debug("Unable to get the color space", e);
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE, e.getMessage()));
    }
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) IOException(java.io.IOException) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 2 with ColorSpaceHelper

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

the class SinglePageValidationProcess method validateColorSpaces.

/**
 * Check that all ColorSpace present in the Resource dictionary are conforming to the ISO 19005:2005-1
 * specification.
 *
 * @param context the preflight context.
 * @param page the page to check.
 */
protected void validateColorSpaces(PreflightContext context, PDPage page) {
    PDResources resources = page.getResources();
    if (resources != null) {
        PreflightConfiguration config = context.getConfig();
        ColorSpaceHelperFactory colorSpaceFactory = config.getColorSpaceHelperFact();
        for (COSName name : resources.getColorSpaceNames()) {
            try {
                PDColorSpace pdCS = resources.getColorSpace(name);
                ColorSpaceHelper csHelper = colorSpaceFactory.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_RESTRICTION);
                csHelper.validate();
            } catch (IOException e) {
                // fixme: this code was previously in PDResources
                LOGGER.debug("Unable to create colorspace", e);
            }
        }
    }
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) COSName(org.apache.pdfbox.cos.COSName) PDResources(org.apache.pdfbox.pdmodel.PDResources) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 3 with ColorSpaceHelper

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

the class PreflightStreamEngine method checkSetColorSpaceOperators.

/**
 * This method validates if the ColorSpace used as operand is consistent with
 * the color space defined in OutputIntent dictionaries.
 *
 * @param operator
 * @param arguments
 * @throws IOException
 */
protected void checkSetColorSpaceOperators(Operator operator, List<COSBase> arguments) throws IOException {
    if (!("CS".equals(operator.getName()) || "cs".equals(operator.getName()))) {
        return;
    }
    String colorSpaceName;
    if (arguments.get(0) instanceof COSString) {
        colorSpaceName = (arguments.get(0)).toString();
    } else if (arguments.get(0) instanceof COSName) {
        colorSpaceName = ((COSName) arguments.get(0)).getName();
    } else {
        registerError("The operand " + arguments.get(0) + " for colorSpace operator " + operator.getName() + " doesn't have the expected type", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
        return;
    }
    ColorSpaceHelper csHelper = null;
    ColorSpaces cs = null;
    try {
        cs = ColorSpaces.valueOf(colorSpaceName);
    } catch (IllegalArgumentException e) {
        /*
             * The color space is unknown. Try to access the resources dictionary, the color space can be a reference.
             */
        PDColorSpace pdCS = this.getResources().getColorSpace(COSName.getPDFName(colorSpaceName));
        if (pdCS != null) {
            cs = ColorSpaces.valueOf(pdCS.getName());
            PreflightConfiguration cfg = context.getConfig();
            ColorSpaceHelperFactory csFact = cfg.getColorSpaceHelperFact();
            csHelper = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_RESTRICTION);
        }
    }
    if (cs == null) {
        registerError("The ColorSpace " + colorSpaceName + " is unknown", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
        return;
    }
    if (csHelper == null) {
        PDColorSpace pdCS = PDColorSpace.create(COSName.getPDFName(colorSpaceName));
        PreflightConfiguration cfg = context.getConfig();
        ColorSpaceHelperFactory csFact = cfg.getColorSpaceHelperFact();
        csHelper = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_RESTRICTION);
    }
    csHelper.validate();
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) COSName(org.apache.pdfbox.cos.COSName) ColorSpaces(org.apache.pdfbox.preflight.graphic.ColorSpaces) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) COSString(org.apache.pdfbox.cos.COSString) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) COSString(org.apache.pdfbox.cos.COSString) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 4 with ColorSpaceHelper

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

the class PreflightStreamEngine method validateInlineImageColorSpace.

/**
 * This method validates if the ColorSpace used by the InlinedImage is consistent with
 * the color space defined in OutputIntent dictionaries.
 *
 * @param operator the InlinedImage object (BI to EI)
 * @throws IOException
 */
protected void validateInlineImageColorSpace(Operator operator) throws IOException {
    COSDictionary dict = operator.getImageParameters();
    COSBase csInlinedBase = dict.getDictionaryObject(COSName.CS, COSName.COLORSPACE);
    ColorSpaceHelper csHelper = null;
    if (csInlinedBase != null) {
        if (COSUtils.isString(csInlinedBase, cosDocument)) {
            // In InlinedImage only DeviceGray/RGB/CMYK and restricted Indexed
            // color spaces are allowed.
            String colorSpace = COSUtils.getAsString(csInlinedBase, cosDocument);
            ColorSpaces cs = null;
            try {
                cs = ColorSpaces.valueOf(colorSpace);
            } catch (IllegalArgumentException e) {
                // The color space is unknown. Try to access the resources dictionary,
                // the color space can be a reference.
                PDColorSpace pdCS = this.getResources().getColorSpace(COSName.getPDFName(colorSpace));
                if (pdCS != null) {
                    cs = ColorSpaces.valueOf(pdCS.getName());
                    csHelper = getColorSpaceHelper(pdCS);
                }
            }
            if (cs == null) {
                registerError("The ColorSpace " + colorSpace + " is unknown", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
                return;
            }
        }
        if (csHelper == null) {
            // convert to long names first
            csInlinedBase = toLongName(csInlinedBase);
            if (csInlinedBase instanceof COSArray && ((COSArray) csInlinedBase).size() > 1) {
                COSArray srcArray = (COSArray) csInlinedBase;
                COSBase csType = srcArray.get(0);
                if (COSName.I.equals(csType) || COSName.INDEXED.equals(csType)) {
                    COSArray dstArray = new COSArray();
                    dstArray.addAll(srcArray);
                    dstArray.set(0, COSName.INDEXED);
                    dstArray.set(1, toLongName(srcArray.get(1)));
                    csInlinedBase = dstArray;
                }
            }
            PDColorSpace pdCS = PDColorSpace.create(csInlinedBase);
            csHelper = getColorSpaceHelper(pdCS);
        }
        csHelper.validate();
    }
}
Also used : COSDictionary(org.apache.pdfbox.cos.COSDictionary) ColorSpaces(org.apache.pdfbox.preflight.graphic.ColorSpaces) COSArray(org.apache.pdfbox.cos.COSArray) COSBase(org.apache.pdfbox.cos.COSBase) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) COSString(org.apache.pdfbox.cos.COSString) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 5 with ColorSpaceHelper

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

the class XObjImageValidator method checkColorSpaceAndImageMask.

/*
     * According to the PDF Reference file, there are some specific rules on following fields ColorSpace, Mask,
     * ImageMask and BitsPerComponent. If ImageMask is set to true, ColorSpace and Mask entries are forbidden.
     */
protected void checkColorSpaceAndImageMask() throws ValidationException {
    COSBase csImg = this.xobject.getItem(COSName.COLORSPACE);
    COSBase bitsPerComp = this.xobject.getItem("BitsPerComponent");
    COSBase mask = this.xobject.getItem(COSName.MASK);
    if (isImageMaskTrue()) {
        if (csImg != null || mask != null) {
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_KEY, "ImageMask entry is true, ColorSpace and Mask are forbidden."));
        }
        Integer bitsPerCompValue = COSUtils.getAsInteger(bitsPerComp, cosDocument);
        if (bitsPerCompValue != null && bitsPerCompValue != 1) {
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY, "ImageMask entry is true, BitsPerComponent must be absent or 1."));
        }
    } else {
        try {
            PreflightConfiguration config = context.getConfig();
            ColorSpaceHelperFactory csFact = config.getColorSpaceHelperFact();
            PDColorSpace pdCS = PDColorSpace.create(csImg);
            ColorSpaceHelper csh = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_PATTERN);
            csh.validate();
        } catch (IOException e) {
            LOGGER.debug("Couldn't create PDColorSpace " + csImg, e);
            context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE));
        }
    }
}
Also used : PreflightConfiguration(org.apache.pdfbox.preflight.PreflightConfiguration) ColorSpaceHelperFactory(org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) ColorSpaceHelper(org.apache.pdfbox.preflight.graphic.ColorSpaceHelper) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Aggregations

PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)5 ColorSpaceHelper (org.apache.pdfbox.preflight.graphic.ColorSpaceHelper)5 PreflightConfiguration (org.apache.pdfbox.preflight.PreflightConfiguration)4 ColorSpaceHelperFactory (org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory)4 IOException (java.io.IOException)3 COSBase (org.apache.pdfbox.cos.COSBase)2 COSName (org.apache.pdfbox.cos.COSName)2 COSString (org.apache.pdfbox.cos.COSString)2 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)2 ColorSpaces (org.apache.pdfbox.preflight.graphic.ColorSpaces)2 COSArray (org.apache.pdfbox.cos.COSArray)1 COSDictionary (org.apache.pdfbox.cos.COSDictionary)1 PDResources (org.apache.pdfbox.pdmodel.PDResources)1