Search in sources :

Example 1 with MemoryCacheImageInputStream

use of com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream in project PdfBox-Android by TomRoush.

the class SampledImageReader method getStencilImage.

/**
 * Returns an ARGB image filled with the given paint and using the given image as a mask.
 * @param paint the paint to fill the visible portions of the image with
 * @return a masked image filled with the given paint
 * @throws IOException if the image cannot be read
 * @throws IllegalStateException if the image is not a stencil.
 */
public static Bitmap getStencilImage(PDImage pdImage, Paint paint) throws IOException {
    int width = pdImage.getWidth();
    int height = pdImage.getHeight();
    // compose to ARGB
    Bitmap masked = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    Canvas g = new Canvas(masked);
    // draw the mask
    // g.drawImage(mask, 0, 0, null);
    // fill with paint using src-in
    // g.setComposite(AlphaComposite.SrcIn);
    g.drawRect(0, 0, width, height, paint);
    // set the alpha
    // avoid getting a Bitmap for the mask to lessen memory footprint.
    // Such masks are always bpc=1 and have no colorspace, but have a decode.
    // (see 8.9.6.2 Stencil Masking)
    ImageInputStream iis = null;
    try {
        iis = new MemoryCacheImageInputStream(pdImage.createInputStream());
        final float[] decode = getDecodeArray(pdImage);
        int value = decode[0] < decode[1] ? 1 : 0;
        int rowLen = width / 8;
        if (width % 8 > 0) {
            rowLen++;
        }
        byte[] buff = new byte[rowLen];
        for (int y = 0; y < height; y++) {
            int x = 0;
            int readLen = iis.read(buff);
            for (int r = 0; r < rowLen && r < readLen; r++) {
                int byteValue = buff[r];
                int mask = 128;
                int shift = 7;
                for (int i = 0; i < 8; i++) {
                    int bit = (byteValue & mask) >> shift;
                    mask >>= 1;
                    --shift;
                    if (bit == value) {
                        masked.setPixel(x, y, Color.TRANSPARENT);
                    }
                    x++;
                    if (x == width) {
                        break;
                    }
                }
            }
            if (readLen != rowLen) {
                Log.w("PdfBox-Android", "premature EOF, image will be incomplete");
                break;
            }
        }
    } finally {
        if (iis != null) {
            iis.close();
        }
    }
    return masked;
}
Also used : Bitmap(android.graphics.Bitmap) Canvas(android.graphics.Canvas) MemoryCacheImageInputStream(com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream) ImageInputStream(com.tom_roush.harmony.javax.imageio.stream.ImageInputStream) MemoryCacheImageInputStream(com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream) Paint(android.graphics.Paint)

Example 2 with MemoryCacheImageInputStream

use of com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream in project PdfBox-Android by TomRoush.

the class LZWFilter method doLZWDecode.

private void doLZWDecode(InputStream encoded, OutputStream decoded, int earlyChange) throws IOException {
    List<byte[]> codeTable = new ArrayList<byte[]>();
    int chunk = 9;
    final MemoryCacheImageInputStream in = new MemoryCacheImageInputStream(encoded);
    long nextCommand;
    long prevCommand = -1;
    try {
        while ((nextCommand = in.readBits(chunk)) != EOD) {
            if (nextCommand == CLEAR_TABLE) {
                chunk = 9;
                codeTable = createCodeTable();
                prevCommand = -1;
            } else {
                if (nextCommand < codeTable.size()) {
                    byte[] data = codeTable.get((int) nextCommand);
                    byte firstByte = data[0];
                    decoded.write(data);
                    if (prevCommand != -1) {
                        checkIndexBounds(codeTable, prevCommand, in);
                        data = codeTable.get((int) prevCommand);
                        byte[] newData = Arrays.copyOf(data, data.length + 1);
                        newData[data.length] = firstByte;
                        codeTable.add(newData);
                    }
                } else {
                    checkIndexBounds(codeTable, prevCommand, in);
                    byte[] data = codeTable.get((int) prevCommand);
                    byte[] newData = Arrays.copyOf(data, data.length + 1);
                    newData[data.length] = data[0];
                    decoded.write(newData);
                    codeTable.add(newData);
                }
                chunk = calculateChunk(codeTable.size(), earlyChange);
                prevCommand = nextCommand;
            }
        }
    } catch (EOFException ex) {
        Log.w("PdfBox-Android", "Premature EOF in LZW stream, EOD code missing");
    }
    decoded.flush();
}
Also used : ArrayList(java.util.ArrayList) MemoryCacheImageInputStream(com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream) EOFException(java.io.EOFException)

Aggregations

MemoryCacheImageInputStream (com.tom_roush.harmony.javax.imageio.stream.MemoryCacheImageInputStream)2 Bitmap (android.graphics.Bitmap)1 Canvas (android.graphics.Canvas)1 Paint (android.graphics.Paint)1 ImageInputStream (com.tom_roush.harmony.javax.imageio.stream.ImageInputStream)1 EOFException (java.io.EOFException)1 ArrayList (java.util.ArrayList)1