Search in sources :

Example 11 with PDColorSpace

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

the class SampledImageReader method from1Bit.

private static Bitmap from1Bit(PDImage pdImage, Rect clipped, final int subsampling, final int width, final int height) throws IOException {
    int currentSubsampling = subsampling;
    final PDColorSpace colorSpace = pdImage.getColorSpace();
    final float[] decode = getDecodeArray(pdImage);
    Bitmap raster = Bitmap.createBitmap(width, height, Bitmap.Config.ALPHA_8);
    ByteBuffer buffer = ByteBuffer.allocate(raster.getRowBytes() * height);
    raster.copyPixelsToBuffer(buffer);
    byte[] output;
    DecodeOptions options = new DecodeOptions(currentSubsampling);
    options.setSourceRegion(clipped);
    // read bit stream
    InputStream iis = null;
    try {
        // create stream
        iis = pdImage.createInputStream(options);
        final int inputWidth;
        final int startx;
        final int starty;
        final int scanWidth;
        final int scanHeight;
        if (options.isFilterSubsampled()) {
            // Decode options were honored, and so there is no need for additional clipping or subsampling
            inputWidth = width;
            startx = 0;
            starty = 0;
            scanWidth = width;
            scanHeight = height;
            currentSubsampling = 1;
        } else {
            // Decode options not honored, so we need to clip and subsample ourselves.
            inputWidth = pdImage.getWidth();
            startx = clipped.left;
            starty = clipped.top;
            scanWidth = clipped.width();
            scanHeight = clipped.height();
        }
        output = buffer.array();
        // colorSpace instanceof PDIndexed; TODO: PdfBox-Android
        final boolean isIndexed = false;
        int rowLen = inputWidth / 8;
        if (inputWidth % 8 > 0) {
            rowLen++;
        }
        // read stream
        byte value0;
        byte value1;
        if (isIndexed || decode[0] < decode[1]) {
            value0 = 0;
            value1 = (byte) 255;
        } else {
            value0 = (byte) 255;
            value1 = 0;
        }
        byte[] buff = new byte[rowLen];
        int idx = 0;
        for (int y = 0; y < starty + scanHeight; y++) {
            int x = 0;
            int readLen = iis.read(buff);
            if (y < starty || y % currentSubsampling > 0) {
                continue;
            }
            for (int r = 0; r < rowLen && r < readLen; r++) {
                int value = buff[r];
                int mask = 128;
                for (int i = 0; i < 8; i++) {
                    if (x >= startx + scanWidth) {
                        break;
                    }
                    int bit = value & mask;
                    mask >>= 1;
                    if (x >= startx && x % currentSubsampling == 0) {
                        output[idx++] = bit == 0 ? value0 : value1;
                    }
                    x++;
                }
            }
            if (readLen != rowLen) {
                Log.w("PdfBox-Android", "premature EOF, image will be incomplete");
                break;
            }
        }
        buffer.rewind();
        raster.copyPixelsFromBuffer(buffer);
        // use the color space to convert the image to RGB
        return colorSpace.toRGBImage(raster);
    } finally {
        if (iis != null) {
            iis.close();
        }
    }
}
Also used : Bitmap(android.graphics.Bitmap) MemoryCacheImageInputStream(com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(com.tom_roush.harmony.javax.imageio.stream.ImageInputStream) InputStream(java.io.InputStream) ByteBuffer(java.nio.ByteBuffer) Paint(android.graphics.Paint) DecodeOptions(com.tom_roush.pdfbox.filter.DecodeOptions) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 12 with PDColorSpace

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

the class JPEGFactory method createFromByteArray.

/**
 * Creates a new JPEG Image XObject from a byte array containing JPEG data.
 *
 * @param document the document where the image will be created
 * @param byteArray bytes of JPEG image
 * @return a new Image XObject
 *
 * @throws IOException if the input stream cannot be read
 */
public static PDImageXObject createFromByteArray(PDDocument document, byte[] byteArray) throws IOException {
    // copy stream
    ByteArrayInputStream byteStream = new ByteArrayInputStream(byteArray);
    Dimensions meta = retrieveDimensions(byteStream);
    // TODO: PdfBox-Android
    PDColorSpace colorSpace = PDDeviceRGB.INSTANCE;
    // create PDImageXObject from stream
    PDImageXObject pdImage = new PDImageXObject(document, byteStream, COSName.DCT_DECODE, meta.width, meta.height, 8, colorSpace);
    return pdImage;
}
Also used : ByteArrayInputStream(java.io.ByteArrayInputStream) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 13 with PDColorSpace

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

the class SetStrokingColorSpace method process.

@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());
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) COSBase(com.tom_roush.pdfbox.cos.COSBase) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace)

Example 14 with PDColorSpace

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

the class SetColor method process.

@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
    PDColorSpace colorSpace = getColorSpace();
    // if (!(colorSpace instanceof PDPattern)) TODO: PdfBox-Android
    {
        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));
}
Also used : COSArray(com.tom_roush.pdfbox.cos.COSArray) MissingOperandException(com.tom_roush.pdfbox.contentstream.operator.MissingOperandException) PDColorSpace(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColorSpace) PDColor(com.tom_roush.pdfbox.pdmodel.graphics.color.PDColor)

Example 15 with PDColorSpace

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

the class SetNonStrokingColorSpace method process.

@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());
}
Also used : COSName(com.tom_roush.pdfbox.cos.COSName) 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