use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.
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 (cache != null && !(colorSpace instanceof PDPattern)) {
cache.put(indirect, colorSpace);
}
return colorSpace;
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.
the class SetNonStrokingDeviceGrayColor method process.
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICEGRAY);
context.getGraphicsState().setNonStrokingColorSpace(cs);
super.process(operator, arguments);
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.
the class SetNonStrokingDeviceRGBColor method process.
@Override
public void process(Operator operator, List<COSBase> arguments) throws IOException {
PDColorSpace cs = context.getResources().getColorSpace(COSName.DEVICERGB);
context.getGraphicsState().setNonStrokingColorSpace(cs);
super.process(operator, arguments);
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.
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
* @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 BufferedImage getRGBImage(PDImage pdImage, Rectangle region, int subsampling, COSArray colorKey) throws IOException {
if (pdImage.isEmpty()) {
throw new IOException("Image stream is empty");
}
Rectangle 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.getWidth() / subsampling);
final int height = (int) Math.ceil(clipped.getHeight() / 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");
}
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.
// Interleaved raster allows chunk-copying for 8-bit images.
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, numComponents, new Point(0, 0));
final float[] defaultDecode = pdImage.getColorSpace().getDefaultDecode(8);
if (bitsPerComponent == 8 && Arrays.equals(decode, defaultDecode) && colorKey == null) {
// convert image, faster path for non-decoded, non-colormasked 8-bit images
return from8bit(pdImage, raster, clipped, subsampling, width, height);
}
return fromAny(pdImage, raster, colorKey, clipped, subsampling, width, height);
}
use of org.apache.pdfbox.pdmodel.graphics.color.PDColorSpace in project pdfbox by apache.
the class SampledImageReader method from1Bit.
private static BufferedImage from1Bit(PDImage pdImage, Rectangle 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);
BufferedImage bim = null;
WritableRaster raster;
byte[] output;
DecodeOptions options = new DecodeOptions(currentSubsampling);
options.setSourceRegion(clipped);
// read bit stream
try (InputStream 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.x;
starty = clipped.y;
scanWidth = clipped.width;
scanHeight = clipped.height;
}
if (colorSpace instanceof PDDeviceGray) {
// TYPE_BYTE_GRAY and not TYPE_BYTE_BINARY because this one is handled
// without conversion to RGB by Graphics.drawImage
// this reduces the memory footprint, only one byte per pixel instead of three.
bim = new BufferedImage(width, height, BufferedImage.TYPE_BYTE_GRAY);
raster = bim.getRaster();
} else {
raster = Raster.createBandedRaster(DataBuffer.TYPE_BYTE, width, height, 1, new Point(0, 0));
}
output = ((DataBufferByte) raster.getDataBuffer()).getData();
final boolean isIndexed = colorSpace instanceof PDIndexed;
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.warn("premature EOF, image will be incomplete");
break;
}
}
if (bim != null) {
return bim;
}
// use the color space to convert the image to RGB
return colorSpace.toRGBImage(raster);
}
}
Aggregations