Search in sources :

Example 26 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace 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 27 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace 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 28 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class StandardColorSpaceHelper method processIndexedColorSpace.

/**
 * Method called by the processAllColorSpace if the ColorSpace to check is Indexed. Because this kind of ColorSpace
 * can have a Base color space, the processAllColorSpace is called to check this base color space. (Indexed and
 * Pattern can't be a Base color space)
 *
 * @param colorSpace
 *            the color space object to check.
 */
protected void processIndexedColorSpace(PDColorSpace colorSpace) {
    PDIndexed indexed = (PDIndexed) colorSpace;
    PDColorSpace based = indexed.getBaseColorSpace();
    ColorSpaces cs = ColorSpaces.valueOf(based.getName());
    if (cs == ColorSpaces.Indexed || cs == ColorSpaces.I) {
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED, "Indexed color space can't be used as Base color space"));
        return;
    }
    if (cs == ColorSpaces.Pattern) {
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED, "Pattern color space can't be used as Base color space"));
        return;
    }
    processAllColorSpace(based);
}
Also used : PDIndexed(org.apache.pdfbox.pdmodel.graphics.color.PDIndexed) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 29 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class StandardColorSpaceHelper method processICCBasedColorSpace.

/**
 * Method called by the processAllColorSpace if the ColorSpace to check is a ICCBased color space. Because this kind
 * of ColorSpace can have alternate color space, the processAllColorSpace is called to check this alternate color
 * space. (Pattern is forbidden as Alternate Color Space)
 *
 * @param colorSpace
 *            the color space object to check.
 */
protected void processICCBasedColorSpace(PDColorSpace colorSpace) {
    PDICCBased iccBased = (PDICCBased) colorSpace;
    try {
        ICC_Profile.getInstance(iccBased.getPDStream().createInputStream());
        PDColorSpace altpdcs = iccBased.getAlternateColorSpace();
        if (altpdcs != null) {
            ColorSpaces altCsId = ColorSpaces.valueOf(altpdcs.getName());
            if (altCsId == ColorSpaces.Pattern) {
                context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN, "Pattern is forbidden as AlternateColorSpace of a ICCBased"));
            }
        /*
                 * According to the ISO-19005-1:2005
                 * 
                 * A conforming reader shall render ICCBased colour spaces as specified by the ICC specification,
                 * and shall not use the Alternate colour space specified in an ICC profile stream dictionary
                 * 
                 * We don't check the alternate ColorSpaces
                 */
        }
    } catch (IllegalArgumentException e) {
        // this is not a ICC_Profile
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_ICCBASED, "ICCBased color space is invalid: " + e.getMessage(), e));
    } catch (IOException e) {
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read ICCBase color space: " + e.getMessage(), e));
    }
}
Also used : PDICCBased(org.apache.pdfbox.pdmodel.graphics.color.PDICCBased) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 30 with PDColorSpace

use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.

the class StandardColorSpaceHelper method processSeparationColorSpace.

/**
 * Method called by the processAllColorSpace if the ColorSpace to check is Separation. Because this kind of
 * ColorSpace can have an alternate color space, the processAllColorSpace is called to check this alternate color
 * space. (Indexed, Separation, DeviceN and Pattern can't be a Base color space)
 *
 * @param colorSpace
 *            the color space object to check.
 */
protected void processSeparationColorSpace(PDColorSpace colorSpace) {
    try {
        COSBase cosAlt = ((COSArray) colorSpace.getCOSObject()).getObject(2);
        PDColorSpace altCol = PDColorSpace.create(cosAlt);
        if (altCol != null) {
            ColorSpaces acs = ColorSpaces.valueOf(altCol.getName());
            switch(acs) {
                case Separation:
                case DeviceN:
                case Pattern:
                case Indexed:
                case I:
                    context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_ALTERNATE, acs.getLabel() + " color space can't be used as alternate color space"));
                    break;
                default:
                    processAllColorSpace(altCol);
            }
        }
    } catch (IOException e) {
        context.addValidationError(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE, "Unable to read Separation color space : " + e.getMessage(), e));
    }
}
Also used : COSArray(org.apache.pdfbox.cos.COSArray) COSBase(org.apache.pdfbox.cos.COSBase) ValidationError(org.apache.pdfbox.preflight.ValidationResult.ValidationError) IOException(java.io.IOException) PDColorSpace(org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)

Aggregations

PDColorSpace (org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace)35 IOException (java.io.IOException)13 PDPattern (org.apache.pdfbox.pdmodel.graphics.color.PDPattern)9 PDColor (org.apache.pdfbox.pdmodel.graphics.color.PDColor)8 ValidationError (org.apache.pdfbox.preflight.ValidationResult.ValidationError)8 COSArray (org.apache.pdfbox.cos.COSArray)7 COSBase (org.apache.pdfbox.cos.COSBase)6 COSName (org.apache.pdfbox.cos.COSName)6 PDRectangle (org.apache.pdfbox.pdmodel.common.PDRectangle)5 ColorSpaceHelper (org.apache.pdfbox.preflight.graphic.ColorSpaceHelper)5 BufferedImage (java.awt.image.BufferedImage)4 WritableRaster (java.awt.image.WritableRaster)4 PDPageContentStream (org.apache.pdfbox.pdmodel.PDPageContentStream)4 PDIndexed (org.apache.pdfbox.pdmodel.graphics.color.PDIndexed)4 PDTilingPattern (org.apache.pdfbox.pdmodel.graphics.pattern.PDTilingPattern)4 PreflightConfiguration (org.apache.pdfbox.preflight.PreflightConfiguration)4 ColorSpaceHelperFactory (org.apache.pdfbox.preflight.graphic.ColorSpaceHelperFactory)4 Paint (java.awt.Paint)3 Point (java.awt.Point)3 Rectangle2D (java.awt.geom.Rectangle2D)3