Examples of PDColorSpace


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

   
    // slower, general-purpose image conversion from any image format
    private static BufferedImage fromAny(PDImage pdImage, WritableRaster raster, COSArray colorKey)
            throws IOException
    {
        final PDColorSpace colorSpace = pdImage.getColorSpace();
        final int numComponents = colorSpace.getNumberOfComponents();
        final int width = pdImage.getWidth();
        final int height = pdImage.getHeight();
        final int bitsPerComponent = pdImage.getBitsPerComponent();
        final float[] decode = getDecodeArray(pdImage);

        // read bit stream
        ImageInputStream iis = null;
        try
        {
            // create stream
            iis = new MemoryCacheImageInputStream(pdImage.getStream().createInputStream());
            final float sampleMax = (float)Math.pow(2, bitsPerComponent) - 1f;
            final boolean isIndexed = colorSpace instanceof PDIndexed;

            // init color key mask
            float[] colorKeyRanges = null;
            BufferedImage colorKeyMask = null;
            if (colorKey != null)
            {
                colorKeyRanges = colorKey.toFloatArray();
                colorKeyMask = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
            }

            // calculate row padding
            int padding = 0;
            if (width * numComponents * bitsPerComponent % 8 > 0)
            {
                padding = 8 - (width * numComponents * bitsPerComponent % 8);
            }

            // read stream
            byte[] srcColorValues = new byte[numComponents];
            byte[] alpha = new byte[1];
            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    boolean isMasked = true;
                    for (int c = 0; c < numComponents; c++)
                    {
                        int value = (int)iis.readBits(bitsPerComponent);

                        // color key mask requires values before they are decoded
                        if (colorKeyRanges != null)
                        {
                            isMasked &= value >= colorKeyRanges[c * 2] &&
                                        value <= colorKeyRanges[c * 2 + 1];
                        }

                        // decode array
                        final float dMin = decode[c * 2];
                        final float dMax = decode[(c * 2) + 1];

                        // interpolate to domain
                        float output = dMin + (value * ((dMax - dMin) / sampleMax));

                        if (isIndexed)
                        {
                            // indexed color spaces get the raw value, because the TYPE_BYTE
                            // below cannot be reversed by the color space without it having
                            // knowledge of the number of bits per component
                            srcColorValues[c] = (byte)Math.round(output);
                        }
                        else
                        {
                            // interpolate to TYPE_BYTE
                            int outputByte = Math.round(((output - Math.min(dMin, dMax)) /
                                    Math.abs(dMax - dMin)) * 255f);

                            srcColorValues[c] = (byte)outputByte;
                        }
                    }
                    raster.setDataElements(x, y, srcColorValues);

                    // set alpha channel in color key mask, if any
                    if (colorKeyMask != null)
                    {
                        alpha[0] = (byte)(isMasked ? 255 : 0);
                        colorKeyMask.getRaster().setDataElements(x, y, alpha);
                    }
                }

                // rows are padded to the nearest byte
                iis.readBits(padding);
            }

            // use the color space to convert the image to RGB
            BufferedImage rgbImage = colorSpace.toRGBImage(raster);

            // apply color mask, if any
            if (colorKeyMask != null)
            {
                return applyColorKeyMask(rgbImage, colorKeyMask);
View Full Code Here

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

        {
            try
            {
                PreflightConfiguration config = context.getConfig();
                ColorSpaceHelperFactory csFact = config.getColorSpaceHelperFact();
                PDColorSpace pdCS = PDColorSpace.create(csImg);
                ColorSpaceHelper csh = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.NO_PATTERN);
                csh.validate();
            }
            catch (IOException e)
            {
View Full Code Here

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

                }
                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());
                        PreflightConfiguration cfg = context.getConfig();
                        ColorSpaceHelperFactory csFact = cfg.getColorSpaceHelperFact();
                        csHelper = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.ONLY_DEVICE);
                    }
                }

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

            if (csHelper == null)
            {
                PDColorSpace pdCS = PDColorSpace.create(csInlinedBase);
                PreflightConfiguration cfg = context.getConfig();
                ColorSpaceHelperFactory csFact = cfg.getColorSpaceHelperFact();
                csHelper = csFact.getColorSpaceHelper(context, pdCS, ColorSpaceRestriction.ONLY_DEVICE);
            }
View Full Code Here

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

     * @param operation the color operator
     * @throws ContentStreamException
     */
    protected void checkColorOperators(String operation) throws ContentStreamException
    {
        PDColorSpace cs = getColorSpace(operation);

        if ("rg".equals(operation) || "RG".equals(operation))
        {
            if (!validColorSpace(cs, ColorSpaceType.RGB))
            {
View Full Code Here

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

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

View Full Code Here

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

public class SetNonStrokingDeviceCMYKColor extends SetNonStrokingColor
{
    @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);
    }
View Full Code Here

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

public class SetNonStrokingDeviceGrayColor extends SetNonStrokingColor
{
    @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);
    }
View Full Code Here

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

*/
public class SetStrokingDeviceGrayColor extends SetStrokingColor
{
    public void process(Operator operator, List<COSBase> arguments) throws IOException
    {
        PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
        context.getGraphicsState().setStrokingColorSpace(cs);
        super.process(operator, arguments);
    }
View Full Code Here

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

public class SetNonStrokingDeviceRGBColor extends SetNonStrokingColor
{
    @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);
    }
View Full Code Here

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

    {
        COSName name = (COSName)arguments.get(0);

        try
        {
            PDColorSpace cs = context.getResources().getColorSpace(name);
            context.getGraphicsState().setNonStrokingColorSpace(cs);
            context.getGraphicsState().setNonStrokingColor(cs.getInitialColor());
        }
        catch (PDColorSpace.MissingException e)
        {
            LOG.error("Missing color space: " + name.getName());
        }
View Full Code Here
TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.