org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace Java Examples

The following examples show how to use org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: CCITTFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private static PDImageXObject prepareImageXObject(PDDocument document,
        byte[] byteArray, int width, int height,
        PDColorSpace initColorSpace) throws IOException
{
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    Filter filter = FilterFactory.INSTANCE.getFilter(COSName.CCITTFAX_DECODE);
    COSDictionary dict = new COSDictionary();
    dict.setInt(COSName.COLUMNS, width);
    dict.setInt(COSName.ROWS, height);
    filter.encode(new ByteArrayInputStream(byteArray), baos, dict, 0);

    ByteArrayInputStream encodedByteStream = new ByteArrayInputStream(baos.toByteArray());
    PDImageXObject image = new PDImageXObject(document, encodedByteStream, COSName.CCITTFAX_DECODE,
            width, height, 1, initColorSpace);
    dict.setInt(COSName.K, -1);
    image.getCOSObject().setItem(COSName.DECODE_PARMS, dict);
    return image;
}
 
Example #2
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private void intersectShadingBBox(PDColor color, Area area) throws IOException
{
    if (color.getColorSpace() instanceof PDPattern)
    {
        PDColorSpace colorSpace = color.getColorSpace();
        PDAbstractPattern pat = ((PDPattern) colorSpace).getPattern(color);
        if (pat instanceof PDShadingPattern)
        {
            PDShading shading = ((PDShadingPattern) pat).getShading();
            PDRectangle bbox = shading.getBBox();
            if (bbox != null)
            {
                Matrix m = Matrix.concatenate(getInitialMatrix(), pat.getMatrix());
                Area bboxArea = new Area(bbox.transform(m));
                area.intersect(bboxArea);
            }
        }
    }
}
 
Example #3
Source File: PageDrawer.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private boolean isGray(PDColorSpace colorSpace)
{
    if (colorSpace instanceof PDDeviceGray)
    {
        return true;
    }
    if (colorSpace instanceof PDICCBased)
    {
        try
        {
            return ((PDICCBased) colorSpace).getAlternateColorSpace() instanceof PDDeviceGray;
        }
        catch (IOException ex)
        {
            return false;
        }
    }
    return false;
}
 
Example #4
Source File: TilingPaintFactory.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
Paint create(PDTilingPattern pattern, PDColorSpace colorSpace,
        PDColor color, AffineTransform xform) throws IOException
{
    Paint paint = null;
    TilingPaintParameter tilingPaintParameter
            = new TilingPaintParameter(drawer.getInitialMatrix(), pattern.getCOSObject(), colorSpace, color, xform);
    WeakReference<Paint> weakRef = weakCache.get(tilingPaintParameter);
    if (weakRef != null)
    {
        // PDFBOX-4058: additional WeakReference makes gc work better
        paint = weakRef.get();
    }
    if (paint == null)
    {
        paint = new TilingPaint(drawer, pattern, colorSpace, color, xform);
        weakCache.put(tilingPaintParameter, new WeakReference<Paint>(paint));
    }
    return paint;
}
 
Example #5
Source File: SetColor.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace colorSpace = getColorSpace();
    if (!(colorSpace instanceof PDPattern))
    {
        if (arguments.size() < colorSpace.getNumberOfComponents())
        {
            throw new MissingOperandException(operator, arguments);
        }
        if (!checkArrayTypesClass(arguments, COSNumber.class))
        {
            return;
        }
    }
    COSArray array = new COSArray();
    array.addAll(arguments);
    setColor(new PDColor(array, colorSpace));
}
 
Example #6
Source File: PDInlineImage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private PDColorSpace createColorSpace(COSBase cs) throws IOException
{
    if (cs instanceof COSName)
    {
        return PDColorSpace.create(toLongName(cs), resources);
    }

    if (cs instanceof COSArray && ((COSArray) cs).size() > 1)
    {
        COSArray srcArray = (COSArray) cs;
        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)));
            return PDColorSpace.create(dstArray, resources);
        }

        throw new IOException("Illegal type of inline image color space: " + csType);
    }

    throw new IOException("Illegal type of object for inline image color space: " + cs);
}
 
Example #7
Source File: PDInlineImage.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDColorSpace getColorSpace() throws IOException
{
    COSBase cs = parameters.getDictionaryObject(COSName.CS, COSName.COLORSPACE);
    if (cs != null)
    {
        return createColorSpace(cs);
    }
    else if (isStencil())
    {
        // stencil mask color space must be gray, it is often missing
        return PDDeviceGray.INSTANCE;
    }
    else
    {
        // an image without a color space is always broken
        throw new IOException("could not determine inline image color space");
    }
}
 
Example #8
Source File: PDDefaultAppearanceString.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
/**
 * Process the font color operator.
 * 
 * This is assumed to be an RGB color.
 * 
 * @param operands the color components
 * @throws IOException in case of the color components not matching
 */
private void processSetFontColor(List<COSBase> operands) throws IOException
{
    PDColorSpace colorSpace;

    switch (operands.size())
    {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            throw new IOException("Missing operands for set non stroking color operator " + Arrays.toString(operands.toArray()));
    }
    COSArray array = new COSArray();
    array.addAll(operands);
    setFontColor(new PDColor(array, colorSpace));
}
 
Example #9
Source File: PDAnnotation.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
protected PDColor getColor(COSName itemName)
{
    COSBase c = this.getCOSObject().getItem(itemName);
    if (c instanceof COSArray)
    {
        PDColorSpace colorSpace = null;
        switch (((COSArray) c).size())
        {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            break;
        }
        return new PDColor((COSArray) c, colorSpace);
    }
    return null;
}
 
Example #10
Source File: PDAppearanceCharacteristicsDictionary.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
private PDColor getColor(COSName itemName)
{
    COSBase c = this.getCOSObject().getItem(itemName);
    if (c instanceof COSArray)
    {
        PDColorSpace colorSpace;
        switch (((COSArray) c).size())
        {
        case 1:
            colorSpace = PDDeviceGray.INSTANCE;
            break;
        case 3:
            colorSpace = PDDeviceRGB.INSTANCE;
            break;
        case 4:
            colorSpace = PDDeviceCMYK.INSTANCE;
            break;
        default:
            return null;
        }
        return new PDColor((COSArray) c, colorSpace);
    }
    return null;
}
 
Example #11
Source File: DefaultResourceCache.java    From gcs with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public PDColorSpace getColorSpace(COSObject indirect) throws IOException
{
    SoftReference<PDColorSpace> colorSpace = colorSpaces.get(indirect);
    if (colorSpace != null)
    {
        return colorSpace.get();
    }
    return null;
}
 
Example #12
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected void setNonStrokingColorSpaceStack(PDColorSpace colorSpace)
{
    if (nonStrokingColorSpaceStack.isEmpty())
    {
        nonStrokingColorSpaceStack.add(colorSpace);
    }
    else
    {
        nonStrokingColorSpaceStack.pop();
        nonStrokingColorSpaceStack.push(colorSpace);
    }
}
 
Example #13
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the color components of current stroking color space.
 *
 * @param components The components to set for the current color.
 * @throws IOException If there is an error while writing to the stream.
 * @deprecated Use {@link #setStrokingColor(PDColor)} instead.
 */
@Deprecated
public void setStrokingColor(float[] components) throws IOException
{
    if (strokingColorSpaceStack.isEmpty())
    {
        throw new IllegalStateException("The color space must be set before setting a color");
    }

    for (float component : components)
    {
        writeOperand(component);
    }

    PDColorSpace currentStrokingColorSpace = strokingColorSpaceStack.peek();

    if (currentStrokingColorSpace instanceof PDSeparation ||
        currentStrokingColorSpace instanceof PDPattern ||
        currentStrokingColorSpace instanceof PDICCBased)
    {
        writeOperator(OperatorName.STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.STROKING_COLOR);
    }
}
 
Example #14
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private COSName getName(PDColorSpace colorSpace) throws IOException
{
    if (colorSpace instanceof PDDeviceGray ||
        colorSpace instanceof PDDeviceRGB ||
        colorSpace instanceof PDDeviceCMYK)
    {
        return COSName.getPDFName(colorSpace.getName());
    }
    else
    {
        return resources.add(colorSpace);
    }
}
 
Example #15
Source File: PDTransparencyGroupAttributes.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the group color space or null if it isn't defined.
 *
 * @param resources useful for its cache. Can be null.
 * @return the group color space.
 * @throws IOException
 */
public PDColorSpace getColorSpace(PDResources resources) throws IOException
{
    if (colorSpace == null && getCOSObject().containsKey(COSName.CS))
    {
        colorSpace = PDColorSpace.create(getCOSObject().getDictionaryObject(COSName.CS), resources);
    }
    return colorSpace;
}
 
Example #16
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected void setStrokingColorSpaceStack(PDColorSpace colorSpace)
{
    if (strokingColorSpaceStack.isEmpty())
    {
        strokingColorSpaceStack.add(colorSpace);
    }
    else
    {
        strokingColorSpaceStack.pop();
        strokingColorSpaceStack.push(colorSpace);
    }
}
 
Example #17
Source File: PDResources.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Returns the color space resource with the given name, or null if none exists. This method is
 * for PDFBox internal use only, others should use {@link #getColorSpace(COSName)}.
 *
 * @param name Name of the color space resource.
 * @param wasDefault if current color space was used by a default color space. This parameter is
 * to
 * @return a new color space.
 * @throws IOException if something went wrong.
 */
public PDColorSpace getColorSpace(COSName name, boolean wasDefault) throws IOException
{
    COSObject indirect = getIndirect(COSName.COLORSPACE, name);
    if (cache != null && indirect != null)
    {
        PDColorSpace cached = cache.getColorSpace(indirect);
        if (cached != null)
        {
            return cached;
        }
    }

    // get the instance
    PDColorSpace colorSpace;
    COSBase object = get(COSName.COLORSPACE, name);
    if (object != null)
    {
        colorSpace = PDColorSpace.create(object, this, wasDefault);
    }
    else
    {
        colorSpace = PDColorSpace.create(name, this, wasDefault);
    }

    // we can't cache PDPattern, because it holds page resources, see PDFBOX-2370
    if (cache != null && !(colorSpace instanceof PDPattern))
    {
        cache.put(indirect, colorSpace);
    }
    return colorSpace;
}
 
Example #18
Source File: JPEGFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private static PDColorSpace getColorSpaceFromAWT(BufferedImage awtImage)
{
    if (awtImage.getColorModel().getNumComponents() == 1)
    {
        // 256 color (gray) JPEG
        return PDDeviceGray.INSTANCE;
    }
    
    ColorSpace awtColorSpace = awtImage.getColorModel().getColorSpace();
    if (awtColorSpace instanceof ICC_ColorSpace && !awtColorSpace.isCS_sRGB())
    {
        throw new UnsupportedOperationException("ICC color spaces not implemented");
    }
    
    switch (awtColorSpace.getType())
    {
        case ColorSpace.TYPE_RGB:
            return PDDeviceRGB.INSTANCE;
        case ColorSpace.TYPE_GRAY:
            return PDDeviceGray.INSTANCE;
        case ColorSpace.TYPE_CMYK:
            return PDDeviceCMYK.INSTANCE;
        default:
            throw new UnsupportedOperationException("color space not implemented: "
                    + awtColorSpace.getType());
    }
}
 
Example #19
Source File: TilingPaintFactory.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private TilingPaintParameter(Matrix matrix, COSDictionary patternDict, PDColorSpace colorSpace,
        PDColor color, AffineTransform xform)
{
    this.matrix = matrix.clone();
    this.patternDict = patternDict;
    this.colorSpace = colorSpace;
    this.color = color;
    this.xform = xform;
}
 
Example #20
Source File: PDAbstractContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
protected COSName getName(PDColorSpace colorSpace)
{
    if (colorSpace instanceof PDDeviceGray ||
        colorSpace instanceof PDDeviceRGB ||
        colorSpace instanceof PDDeviceCMYK)
    {
        return COSName.getPDFName(colorSpace.getName());
    }
    else
    {
        return resources.add(colorSpace);
    }
}
 
Example #21
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
/**
 * Set the color components of current non-stroking color space.
 *
 * @param components The components to set for the current color.
 * @throws IOException If there is an error while writing to the stream.
 * @deprecated Use {@link #setNonStrokingColor(PDColor)} instead.
 */
@Deprecated
public void setNonStrokingColor(float[] components) throws IOException
{
    if (nonStrokingColorSpaceStack.isEmpty())
    {
        throw new IllegalStateException("The color space must be set before setting a color");
    }

    for (float component : components)
    {
        writeOperand(component);
    }

    PDColorSpace currentNonStrokingColorSpace = nonStrokingColorSpaceStack.peek();

    if (currentNonStrokingColorSpace instanceof PDSeparation ||
        currentNonStrokingColorSpace instanceof PDPattern ||
        currentNonStrokingColorSpace instanceof PDICCBased)
    {
        writeOperator(OperatorName.NON_STROKING_COLOR_N);
    }
    else
    {
        writeOperator(OperatorName.NON_STROKING_COLOR);
    }
}
 
Example #22
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void setStrokingColorSpaceStack(PDColorSpace colorSpace)
{
    if (strokingColorSpaceStack.isEmpty())
    {
        strokingColorSpaceStack.add(colorSpace);
    }
    else
    {
        strokingColorSpaceStack.setElementAt(colorSpace, strokingColorSpaceStack.size() - 1);
    }
}
 
Example #23
Source File: PDPageContentStream.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
private void setNonStrokingColorSpaceStack(PDColorSpace colorSpace)
{
    if (nonStrokingColorSpaceStack.isEmpty())
    {
        nonStrokingColorSpaceStack.add(colorSpace);
    }
    else
    {
        nonStrokingColorSpaceStack.setElementAt(colorSpace, nonStrokingColorSpaceStack.size() - 1);
    }
}
 
Example #24
Source File: SetStrokingDeviceCMYKColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICECMYK);
    context.getGraphicsState().setStrokingColorSpace(cs);
    super.process(operator, arguments);
}
 
Example #25
Source File: SetNonStrokingDeviceCMYKColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICECMYK);
    context.getGraphicsState().setNonStrokingColorSpace(cs);
    super.process(operator, arguments);
}
 
Example #26
Source File: SetStrokingColorSpace.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    COSBase base = arguments.get(0);
    if (!(base instanceof COSName))
    {
        return;
    }
    PDColorSpace cs = context.getResources().getColorSpace((COSName) base);
    context.getGraphicsState().setStrokingColorSpace(cs);
    context.getGraphicsState().setStrokingColor(cs.getInitialColor());
}
 
Example #27
Source File: SetNonStrokingDeviceRGBColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICERGB);
    context.getGraphicsState().setNonStrokingColorSpace(cs);
    super.process(operator, arguments);
}
 
Example #28
Source File: SetNonStrokingColorSpace.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    COSName name = (COSName)arguments.get(0);

    PDColorSpace cs = context.getResources().getColorSpace(name);
    context.getGraphicsState().setNonStrokingColorSpace(cs);
    context.getGraphicsState().setNonStrokingColor(cs.getInitialColor());
}
 
Example #29
Source File: SetNonStrokingDeviceGrayColor.java    From gcs with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException
{
    PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
    context.getGraphicsState().setNonStrokingColorSpace(cs);
    super.process(operator, arguments);
}
 
Example #30
Source File: NativePdfBoxVisibleSignatureDrawer.java    From dss with GNU Lesser General Public License v2.1 5 votes vote down vote up
@Override
protected String getColorSpaceName(DSSDocument image) throws IOException {
	try (InputStream is = image.openStream()) {
		byte[] bytes = IOUtils.toByteArray(is);
		PDImageXObject imageXObject = PDImageXObject.createFromByteArray(document, bytes, image.getName());
		PDColorSpace colorSpace = imageXObject.getColorSpace();
		return colorSpace.getName();
	}
}