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();
}
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();
}
}
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);
}
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));
}
}
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));
}
}
Aggregations