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