Search in sources :

Example 6 with PDColorSpace

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace in project PdfBox-Android by TomRoush.

the class SampledImageReader method getRGBImage.

/**
 * Returns the content of the given image as an AWT buffered image with an RGB color space.
 * If a color key mask is provided then an ARGB image is returned instead.
 * This method never returns null.
 * @param pdImage the image to read
 * @param region The region of the source image to get, or null if the entire image is needed.
 *               The actual region will be clipped to the dimensions of the source image.
 * @param subsampling The amount of rows and columns to advance for every output pixel, a value
 * of 1 meaning every pixel will be read. It must not be larger than the image width or height.
 * @param colorKey an optional color key mask
 * @return content of this image as an (A)RGB buffered image
 * @throws IOException if the image cannot be read
 */
public static Bitmap getRGBImage(PDImage pdImage, Rect region, int subsampling, COSArray colorKey) throws IOException {
    if (pdImage.isEmpty()) {
        throw new IOException("Image stream is empty");
    }
    Rect clipped = clipRegion(pdImage, region);
    // get parameters, they must be valid or have been repaired
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final int numComponents = colorSpace.getNumberOfComponents();
    final int width = (int) Math.ceil(clipped.width() / subsampling);
    final int height = (int) Math.ceil(clipped.height() / subsampling);
    final int bitsPerComponent = pdImage.getBitsPerComponent();
    final float[] decode = getDecodeArray(pdImage);
    if (width <= 0 || height <= 0 || pdImage.getWidth() <= 0 || pdImage.getHeight() <= 0) {
        throw new IOException("image width and height must be positive");
    }
    try {
        if (bitsPerComponent == 1 && colorKey == null && numComponents == 1) {
            return from1Bit(pdImage, clipped, subsampling, width, height);
        }
        // 
        // An AWT raster must use 8/16/32 bits per component. Images with < 8bpc
        // will be unpacked into a byte-backed raster. Images with 16bpc will be reduced
        // in depth to 8bpc as they will be drawn to TYPE_INT_RGB images anyway. All code
        // in PDColorSpace#toRGBImage expects an 8-bit range, i.e. 0-255.
        final float[] defaultDecode = pdImage.getColorSpace().getDefaultDecode(8);
        if (pdImage.getSuffix() != null && pdImage.getSuffix().equals("jpg") && subsampling == 1) {
            return BitmapFactory.decodeStream(pdImage.createInputStream());
        } else if (bitsPerComponent == 8 && Arrays.equals(decode, defaultDecode) && colorKey == null) {
            // convert image, faster path for non-decoded, non-colormasked 8-bit images
            return from8bit(pdImage, clipped, subsampling, width, height);
        }
        Log.e("PdfBox-Android", "Trying to create other-bit image not supported");
        // return fromAny(pdImage, colorKey, clipped, subsampling, width, height);
        return from8bit(pdImage, clipped, subsampling, width, height);
    } catch (NegativeArraySizeException ex) {
        throw new IOException(ex);
    }
}
Also used : Rect(android.graphics.Rect) IOException(java.io.IOException) Paint(android.graphics.Paint) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 7 with PDColorSpace

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace in project PdfBox-Android by TomRoush.

the class PDDefaultAppearanceString method processSetFontColor.

/**
 * 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; TODO: PdfBox-Android
            colorSpace = PDDeviceRGB.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));
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) IOException(java.io.IOException) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 8 with PDColorSpace

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace in project PdfBox-Android by TomRoush.

the class PDPageContentStream method setStrokingColor.

/**
 * 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);
// } TODO: PdfBox-Android
}
Also used : PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 9 with PDColorSpace

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace in project PdfBox-Android by TomRoush.

the class PDResources method getColorSpace.

/**
 * 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 (// TODO: PdfBox-Android
    cache != null) /*&& !(colorSpace instanceof PDPattern)*/
    {
        cache.put(indirect, colorSpace);
    }
    return colorSpace;
}
Also used : COSObject(com.tom_roush.pdfbox.cos.COSObject) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 10 with PDColorSpace

use of com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace in project PdfBox-Android by TomRoush.

the class PageDrawer method getColor.

// protected Paint getPaint(PDColor color) throws IOException TODO: PdfBox-Android
// returns an integer for color that Android understands from the PDColor
// TODO: alpha?
private int getColor(PDColor color) throws IOException {
    PDColorSpace colorSpace = color.getColorSpace();
    float[] floats = colorSpace.toRGB(color.getComponents());
    int r = Math.round(floats[0] * 255);
    int g = Math.round(floats[1] * 255);
    int b = Math.round(floats[2] * 255);
    return Color.rgb(r, g, b);
}
Also used : Paint(android.graphics.Paint) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Aggregations

PDColorSpace (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)17 Paint (android.graphics.Paint)3 COSArray (com.tom_roush.pdfbox.cos.COSArray)2 COSBase (com.tom_roush.pdfbox.cos.COSBase)2 COSName (com.tom_roush.pdfbox.cos.COSName)2 PDColor (com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)2 IOException (java.io.IOException)2 Bitmap (android.graphics.Bitmap)1 Rect (android.graphics.Rect)1 ImageInputStream (com.tom_roush.harmony.javax.imageio.stream.ImageInputStream)1 MemoryCacheImageInputStream (com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream)1 MissingOperandException (com.tom_roush.pdfbox.contentstream.operator.MissingOperandException)1 COSObject (com.tom_roush.pdfbox.cos.COSObject)1 DecodeOptions (com.tom_roush.pdfbox.filter.DecodeOptions)1 ByteArrayInputStream (java.io.ByteArrayInputStream)1 InputStream (java.io.InputStream)1 ByteBuffer (java.nio.ByteBuffer)1