use of org.apache.pdfbox.preflight.graphic.ColorSpaces 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.preflight.graphic.ColorSpaces 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();
}
}
Aggregations