use of com.tom_roush.pdfbox.filter.DecodeOptions 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();
}
}
}
use of com.tom_roush.pdfbox.filter.DecodeOptions in project PdfBox-Android by TomRoush.
the class SampledImageReader method from8bit.
// faster, 8-bit non-decoded, non-colormasked image conversion
private static Bitmap from8bit(PDImage pdImage, Rect clipped, final int subsampling, final int width, final int height) throws IOException {
int currentSubsampling = subsampling;
DecodeOptions options = new DecodeOptions(currentSubsampling);
options.setSourceRegion(clipped);
InputStream input = pdImage.createInputStream(options);
try {
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();
}
final int numComponents = pdImage.getColorSpace().getNumberOfComponents();
// get the raster's underlying byte buffer
int[] banks = new int[width * height];
// byte[][] banks = ((DataBufferByte) raster.getDataBuffer()).getBankData();
byte[] tempBytes = new byte[numComponents * inputWidth];
// compromise between memory and time usage:
// reading the whole image consumes too much memory
// reading one pixel at a time makes it slow in our buffering infrastructure
int i = 0;
for (int y = 0; y < starty + scanHeight; ++y) {
input.read(tempBytes);
if (y < starty || y % currentSubsampling > 0) {
continue;
}
for (int x = startx; x < startx + scanWidth; x += currentSubsampling) {
int tempBytesIdx = x * numComponents;
if (numComponents == 3) {
banks[i] = Color.argb(255, tempBytes[tempBytesIdx] & 0xFF, tempBytes[tempBytesIdx + 1] & 0xFF, tempBytes[tempBytesIdx + 2] & 0xFF);
} else if (numComponents == 1) {
int in = tempBytes[tempBytesIdx] & 0xFF;
banks[i] = Color.argb(in, in, in, in);
}
++i;
}
}
Bitmap raster = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
raster.setPixels(banks, 0, width, 0, 0, width, height);
// return pdImage.getColorSpace().toRGBImage(raster); TODO: PdfBox-Android
return raster;
} finally {
IOUtils.closeQuietly(input);
}
}
Aggregations