Example usage for org.apache.pdfbox.pdmodel.graphics.color PDColorSpace getName

List of usage examples for org.apache.pdfbox.pdmodel.graphics.color PDColorSpace getName

Introduction

In this page you can find the example usage for org.apache.pdfbox.pdmodel.graphics.color PDColorSpace getName.

Prototype

public abstract String getName();

Source Link

Document

Returns the name of the color space.

Usage

From source file:fi.nls.oskari.printout.printing.PDPageContentStream.java

License:Apache License

private void writeColorSpace(PDColorSpace colorSpace) throws IOException {
    COSName key = null;//  www  .j  a  v a2  s  .  co m
    if (colorSpace instanceof PDDeviceGray || colorSpace instanceof PDDeviceRGB
            || colorSpace instanceof PDDeviceCMYK) {
        key = COSName.getPDFName(colorSpace.getName());
    } else {
        COSDictionary colorSpaces = (COSDictionary) resources.getCOSDictionary()
                .getDictionaryObject(COSName.COLORSPACE);
        if (colorSpaces == null) {
            colorSpaces = new COSDictionary();
            resources.getCOSDictionary().setItem(COSName.COLORSPACE, colorSpaces);
        }
        key = colorSpaces.getKeyForValue(colorSpace.getCOSObject());

        if (key == null) {
            int counter = 0;
            String csName = "CS";
            while (colorSpaces.containsValue(csName + counter)) {
                counter++;
            }
            key = COSName.getPDFName(csName + counter);
            colorSpaces.setItem(key, colorSpace);
        }
    }
    key.writePDF(output);
    appendRawCommands(SPACE);
}

From source file:net.padaf.preflight.graphics.color.DeviceColorSpaceHelper.java

License:Apache License

/**
 * Indexed color space is authorized only if the BaseColorSpace is a DeviceXXX
 * color space. In all other cases the given list is updated with a
 * ValidationError (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and
 * returns false./*from w w w . j a v a 2 s .  co  m*/
 */
protected boolean processIndexedColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    PDIndexed indexed = (PDIndexed) pdcs;
    try {
        if (iccpw == null) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING,
                    "DestOutputProfile is missing"));
            return false;
        }

        PDColorSpace based = indexed.getBaseColorSpace();
        ColorSpaces cs = ColorSpaces.valueOf(based.getName());
        switch (cs) {
        case DeviceCMYK:
        case DeviceCMYK_SHORT:
        case DeviceRGB:
        case DeviceRGB_SHORT:
        case DeviceGray:
        case DeviceGray_SHORT:
            return processAllColorSpace(based, result);
        default:
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN,
                    cs.getLabel() + " ColorSpace is forbidden"));
            return false;
        }

    } catch (IOException e) {
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE,
                "Unable to read Indexed Color Space : " + e.getMessage()));
        return false;
    }
}

From source file:net.padaf.preflight.graphics.color.StandardColorSpaceHelper.java

License:Apache License

/**
 * Method called by the validate method. According to the ColorSpace, a
 * specific ColorSpace method is called.
 * //  w  w w .  j a  v a 2s. c o  m
 * @param pdcs
 *          the color space object to check.
 * @param result
 *          the list of error to update if the validation fails.
 * @return true if the validation succeed, false otherwise.
 */
protected final boolean processAllColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    ColorSpaces cs = ColorSpaces.valueOf(pdcs.getName());
    switch (cs) {
    case DeviceRGB:
    case DeviceRGB_SHORT:
        return processRGBColorSpace(result);

    case DeviceCMYK:
    case DeviceCMYK_SHORT:
        return processCYMKColorSpace(result);

    case CalRGB:
    case CalGray:
    case Lab:
        return processCalibratedColorSpace(result);

    case DeviceGray:
    case DeviceGray_SHORT:
        return processGrayColorSpace(result);

    case ICCBased:
        return processICCBasedColorSpace(pdcs, result);

    case DeviceN:
        return processDeviceNColorSpace(pdcs, result);

    case Indexed:
    case Indexed_SHORT:
        return processIndexedColorSpace(pdcs, result);

    case Separation:
        return processSeparationColorSpace(pdcs, result);

    case Pattern:
        return processPatternColorSpace(result);

    default:
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE,
                cs.getLabel() + " is unknown as ColorSpace"));
        return false;
    }
}

From source file:net.padaf.preflight.graphics.color.StandardColorSpaceHelper.java

License:Apache License

/**
 * 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)
 * /*from  ww  w . ja  va 2 s. co  m*/
 * @param pdcs
 *          the color space object to check.
 * @param result
 *          the list of error to update if the validation fails.
 * @return true if the color space is valid, false otherwise.
 */
protected boolean processICCBasedColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    PDICCBased iccBased = (PDICCBased) pdcs;
    try {
        if (iccpw == null) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING,
                    "DestOutputProfile is missing"));
            return false;
        }

        List<PDColorSpace> altCs = iccBased.getAlternateColorSpaces();
        for (PDColorSpace altpdcs : altCs) {
            if (altpdcs != null) {

                ColorSpaces altCsId = ColorSpaces.valueOf(altpdcs.getName());
                if (altCsId == ColorSpaces.Pattern) {
                    result.add(new ValidationError(ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN,
                            "Pattern is forbidden as AlternateColorSpace of a ICCBased"));
                    return false;
                }

                if (!processAllColorSpace(altpdcs, result)) {
                    return false;
                }
            }
        }
    } catch (IOException e) {
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE,
                "Unable to read ICCBase color space : " + e.getMessage()));
        return false;
    }

    return true;
}

From source file:net.padaf.preflight.graphics.color.StandardColorSpaceHelper.java

License:Apache License

/**
 * 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)/*  w  ww . j  a v  a2 s .  c o m*/
 * 
 * @param pdcs
 *          the color space object to check.
 * @param result
 *          the list of error to update if the validation fails.
 * @return true if the color space is valid, false otherwise.
 */
protected boolean processIndexedColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    PDIndexed indexed = (PDIndexed) pdcs;
    try {
        if (iccpw == null) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING,
                    "DestOutputProfile is missing"));
            return false;
        }

        PDColorSpace based = indexed.getBaseColorSpace();
        ColorSpaces cs = ColorSpaces.valueOf(based.getName());
        if (cs == ColorSpaces.Indexed || cs == ColorSpaces.Indexed_SHORT) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED,
                    "Indexed color space can't be used as Base color space"));
            return false;
        }
        if (cs == ColorSpaces.Pattern) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_INDEXED,
                    "Pattern color space can't be used as Base color space"));
            return false;
        }
        return processAllColorSpace(based, result);
    } catch (IOException e) {
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE,
                "Unable to read Indexed color space : " + e.getMessage()));
        return false;
    }
}

From source file:net.padaf.preflight.graphics.color.StandardColorSpaceHelper.java

License:Apache License

/**
 * 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)//w w w  .  jav a  2s  .  c o m
 * 
 * @param pdcs
 *          the color space object to check.
 * @param result
 *          the list of error to update if the validation fails.
 * @return true if the color space is valid, false otherwise.
 */
protected boolean processSeparationColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    PDSeparation separation = (PDSeparation) pdcs;
    try {
        if (iccpw == null) {
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_MISSING,
                    "DestOutputProfile is missing"));
            return false;
        }

        PDColorSpace altCol = separation.getAlternateColorSpace();
        if (altCol != null) {
            ColorSpaces acs = ColorSpaces.valueOf(altCol.getName());
            switch (acs) {
            case Separation:
            case DeviceN:
            case Pattern:
            case Indexed:
            case Indexed_SHORT:
                result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_ALTERNATE,
                        acs.getLabel() + " color space can't be used as alternate color space"));
                return false;
            default:
                return processAllColorSpace(altCol, result);
            }
        }

        return true;
    } catch (IOException e) {
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE,
                "Unable to read Separation color space : " + e.getMessage()));
        return false;
    }
}

From source file:net.padaf.preflight.utils.ContentStreamEngine.java

License:Apache License

/**
 * This method validates if the ColorSpace used by the InlinedImage is
 * consistent with the color space defined in OutputIntent dictionaries.
 * /*  w  ww  .  j ava2 s.  c  o  m*/
 * @param operator
 *          the InlinedImage object (BI to EI)
 * @throws ContentStreamException
 */
protected void validImageColorSpace(PDFOperator operator) throws ContentStreamException, IOException {
    COSDictionary dict = operator.getImageParameters().getDictionary();

    COSDocument doc = this.documentHandler.getDocument().getDocument();
    COSBase csInlinedBase = dict.getItem(COSName.getPDFName(STREAM_DICTIONARY_KEY_COLOR_SPACE));

    ColorSpaceHelper csHelper = null;
    if (csInlinedBase != null) {

        if (COSUtils.isString(csInlinedBase, doc)) {
            // ---- In InlinedImage only DeviceGray/RGB/CMYK and restricted Indexed
            // color spaces
            // are allowed.
            String colorSpace = COSUtils.getAsString(csInlinedBase, doc);
            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 = (PDColorSpace) this.getColorSpaces().get(colorSpace);
                if (pdCS != null) {
                    cs = ColorSpaces.valueOf(pdCS.getName());
                    csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(pdCS, documentHandler,
                            ColorSpaceRestriction.ONLY_DEVICE);
                }
            }

            if (cs == null) {
                throwContentStreamException("The ColorSpace is unknown",
                        ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
            }
        }

        if (csHelper == null) {
            csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(csInlinedBase, documentHandler,
                    ColorSpaceRestriction.ONLY_DEVICE);
        }
        List<ValidationError> errors = new ArrayList<ValidationError>();
        try {
            if (!csHelper.validate(errors)) {
                ValidationError ve = errors.get(0);
                throwContentStreamException(ve.getDetails(), ve.getErrorCode());
            }
        } catch (ValidationException e) {
            throw new IOException(e.getMessage());
        }
    }
}

From source file:net.padaf.preflight.utils.ContentStreamEngine.java

License:Apache License

/**
 * This method validates if the ColorSpace used as operand is consistent with
 * the color space defined in OutputIntent dictionaries.
 * //  w w  w. j a v a 2 s .  co m
 * @param operator
 * @param arguments
 * @throws IOException
 */
protected void checkSetColorSpaceOperators(PDFOperator operator, List<?> arguments) throws IOException {
    if (!("CS".equals(operator.getOperation()) || "cs".equals(operator.getOperation()))) {
        return;
    }

    String colorSpaceName = null;
    if (arguments.get(0) instanceof String) {
        colorSpaceName = (String) arguments.get(0);
    } else if (arguments.get(0) instanceof COSString) {
        colorSpaceName = ((COSString) arguments.get(0)).toString();
    } else if (arguments.get(0) instanceof COSName) {
        colorSpaceName = ((COSName) arguments.get(0)).getName();
    } else {
        throwContentStreamException("The operand doesn't have the expected type",
                ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
    }

    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 = (PDColorSpace) this.getColorSpaces().get(colorSpaceName);
        if (pdCS != null) {
            cs = ColorSpaces.valueOf(pdCS.getName());
            csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(pdCS, documentHandler,
                    ColorSpaceRestriction.NO_RESTRICTION);
        }
    }

    if (cs == null) {
        throwContentStreamException("The ColorSpace is unknown", ERROR_GRAPHIC_UNEXPECTED_VALUE_FOR_KEY);
    }

    if (csHelper == null) {
        csHelper = ColorSpaceHelperFactory.getColorSpaceHelper(COSName.getPDFName(colorSpaceName),
                documentHandler, ColorSpaceRestriction.NO_RESTRICTION);
    }

    List<ValidationError> errors = new ArrayList<ValidationError>();
    try {
        if (!csHelper.validate(errors)) {
            ValidationError ve = errors.get(0);
            throwContentStreamException(ve.getDetails(), ve.getErrorCode());
        }
    } catch (ValidationException e) {
        //      throw new IOException(e.getMessage(), e); java 6
        throw new IOException(e.getMessage());
    }
}

From source file:org.apache.padaf.preflight.graphics.color.DeviceColorSpaceHelper.java

License:Apache License

/**
 * Indexed color space is authorized only if the BaseColorSpace is a DeviceXXX
 * color space. In all other cases the given list is updated with a
 * ValidationError (ERROR_GRAPHIC_INVALID_PATTERN_COLOR_SPACE_FORBIDDEN) and
 * returns false./*w w  w.j  a va2 s. com*/
 */
protected boolean processIndexedColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    PDIndexed indexed = (PDIndexed) pdcs;
    try {
        PDColorSpace based = indexed.getBaseColorSpace();
        ColorSpaces cs = ColorSpaces.valueOf(based.getName());
        switch (cs) {
        case Indexed:
        case Indexed_SHORT:
        case Pattern:
            result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE_FORBIDDEN,
                    cs.getLabel() + " ColorSpace is forbidden"));
            return false;

        default:
            return processAllColorSpace(based, result);
        }

    } catch (IOException e) {
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_COLOR_SPACE,
                "Unable to read Indexed Color Space : " + e.getMessage()));
        return false;
    }
}

From source file:org.apache.padaf.preflight.graphics.color.StandardColorSpaceHelper.java

License:Apache License

/**
 * Method called by the validate method. According to the ColorSpace, a
 * specific ColorSpace method is called.
 * //  ww w.ja  va  2  s .  co m
 * @param pdcs
 *          the color space object to check.
 * @param result
 *          the list of error to update if the validation fails.
 * @return true if the validation succeed, false otherwise.
 */
protected final boolean processAllColorSpace(PDColorSpace pdcs, List<ValidationError> result) {
    ColorSpaces cs = ColorSpaces.valueOf(pdcs.getName());

    switch (cs) {
    case DeviceRGB:
    case DeviceRGB_SHORT:
        return processRGBColorSpace(result);

    case DeviceCMYK:
    case DeviceCMYK_SHORT:
        return processCYMKColorSpace(result);

    case CalRGB:
    case CalGray:
    case Lab:
        return processCalibratedColorSpace(result);

    case DeviceGray:
    case DeviceGray_SHORT:
        return processGrayColorSpace(result);

    case ICCBased:
        return processICCBasedColorSpace(pdcs, result);

    case DeviceN:
        return processDeviceNColorSpace(pdcs, result);

    case Indexed:
    case Indexed_SHORT:
        return processIndexedColorSpace(pdcs, result);

    case Separation:
        return processSeparationColorSpace(pdcs, result);

    case Pattern:
        return processPatternColorSpace(result);

    default:
        result.add(new ValidationError(ERROR_GRAPHIC_INVALID_UNKNOWN_COLOR_SPACE,
                cs.getLabel() + " is unknown as ColorSpace"));
        return false;
    }
}