use of java.awt.color.ColorSpace in project digilib by robcast.
the class ImageLoaderDocuImage method loadSubimage.
/*
* Load an image file into the Object.
*
* @see digilib.image.DocuImageImpl#loadSubimage(digilib.io.ImageInput, java.awt.Rectangle, int)
*/
public void loadSubimage(ImageInput ii, Rectangle region, int prescale) throws FileOpException {
logger.debug("loadSubimage");
this.input = ii;
// ImageReader reader = null;
try {
reader = getReader(ii);
/*
* set up reader parameters
*/
ImageReadParam readParam = reader.getDefaultReadParam();
readParam.setSourceRegion(region);
if (prescale > 1) {
readParam.setSourceSubsampling(prescale, prescale, 0, 0);
}
if (imageHacks.get(Hacks.setDestSrgb)) {
/*
* try to set target color space to sRGB
*/
for (Iterator<ImageTypeSpecifier> i = reader.getImageTypes(0); i.hasNext(); ) {
ImageTypeSpecifier type = (ImageTypeSpecifier) i.next();
ColorModel cm = type.getColorModel();
ColorSpace cs = cm.getColorSpace();
logger.debug("loadSubimage: possible color model:" + cm + " color space:" + cs);
if (cs.getNumComponents() < 3 && !imageHacks.get(Hacks.setDestSrgbForNonRgb)) {
// if the first type is not RGB do nothing
logger.debug("loadSubimage: image is not RGB " + type);
break;
}
if (cs.isCS_sRGB()) {
logger.debug("loadSubimage: substituted sRGB destination type " + type);
readParam.setDestinationType(type);
break;
}
}
}
/*
* read image
*/
logger.debug("loadSubimage: loading..");
img = reader.read(0, readParam);
logger.debug("loadSubimage: loaded " + img);
// invalidate image size if it was set
imageSize = null;
/*
* downconvert highcolor images
*/
if (img.getColorModel().getComponentSize(0) > 8) {
logger.debug("loadSubimage: converting to 8bit");
int type = BufferedImage.TYPE_INT_RGB;
if (img.getColorModel().hasAlpha()) {
type = BufferedImage.TYPE_INT_ARGB;
}
BufferedImage lcImg = new BufferedImage(img.getWidth(), img.getHeight(), type);
lcImg.createGraphics().drawImage(img, null, 0, 0);
img = lcImg;
}
} catch (IOException e) {
throw new FileOpException("Unable to load File!", e);
} finally {
if (!reuseReader && reader != null) {
reader.dispose();
}
}
}
use of java.awt.color.ColorSpace in project pdfbox by apache.
the class PDDeviceCMYK method toRGBImageAWT.
@Override
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace) {
if (usePureJavaCMYKConversion) {
BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_RGB);
ColorSpace destCS = dest.getColorModel().getColorSpace();
WritableRaster destRaster = dest.getRaster();
float[] srcValues = new float[4];
float[] lastValues = new float[] { -1.0f, -1.0f, -1.0f, -1.0f };
float[] destValues = new float[3];
int startX = raster.getMinX();
int startY = raster.getMinY();
int endX = raster.getWidth() + startX;
int endY = raster.getHeight() + startY;
for (int x = startX; x < endX; x++) {
for (int y = startY; y < endY; y++) {
raster.getPixel(x, y, srcValues);
// check if the last value can be reused
if (!Arrays.equals(lastValues, srcValues)) {
lastValues[0] = srcValues[0];
srcValues[0] = srcValues[0] / 255f;
lastValues[1] = srcValues[1];
srcValues[1] = srcValues[1] / 255f;
lastValues[2] = srcValues[2];
srcValues[2] = srcValues[2] / 255f;
lastValues[3] = srcValues[3];
srcValues[3] = srcValues[3] / 255f;
// use CIEXYZ as intermediate format to optimize the color conversion
destValues = destCS.fromCIEXYZ(colorSpace.toCIEXYZ(srcValues));
for (int k = 0; k < destValues.length; k++) {
destValues[k] = destValues[k] * 255f;
}
}
destRaster.setPixel(x, y, destValues);
}
}
return dest;
} else {
return super.toRGBImageAWT(raster, colorSpace);
}
}
use of java.awt.color.ColorSpace in project pdfbox by apache.
the class LosslessFactoryTest method testCreateLosslessFrom16Bit.
@Test
void testCreateLosslessFrom16Bit() throws IOException {
PDDocument document = new PDDocument();
BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
ColorSpace targetCS = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int dataBufferType = DataBuffer.TYPE_USHORT;
final ColorModel colorModel = new ComponentColorModel(targetCS, false, false, ColorModel.OPAQUE, dataBufferType);
WritableRaster targetRaster = Raster.createInterleavedRaster(dataBufferType, image.getWidth(), image.getHeight(), targetCS.getNumComponents(), new Point(0, 0));
BufferedImage img16Bit = new BufferedImage(colorModel, targetRaster, false, new Hashtable<>());
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
op.filter(image, img16Bit);
PDImageXObject ximage = LosslessFactory.createFromImage(document, img16Bit);
validate(ximage, 16, img16Bit.getWidth(), img16Bit.getHeight(), "png", PDDeviceRGB.INSTANCE.getName());
checkIdent(image, ximage.getImage());
doWritePDF(document, ximage, TESTRESULTSDIR, "misc-16bit.pdf");
}
use of java.awt.color.ColorSpace in project pdfbox by apache.
the class LosslessFactoryTest method testCreateLosslessFromImageCMYK.
/**
* Test lossless encoding of CMYK images
*/
@Test
void testCreateLosslessFromImageCMYK() throws IOException {
PDDocument document = new PDDocument();
BufferedImage image = ImageIO.read(this.getClass().getResourceAsStream("png.png"));
final ColorSpace targetCS = new ICC_ColorSpace(ICC_Profile.getInstance(this.getClass().getResourceAsStream("/org/apache/pdfbox/resources/icc/ISOcoated_v2_300_bas.icc")));
ColorConvertOp op = new ColorConvertOp(image.getColorModel().getColorSpace(), targetCS, null);
BufferedImage imageCMYK = op.filter(image, null);
PDImageXObject ximage = LosslessFactory.createFromImage(document, imageCMYK);
validate(ximage, 8, imageCMYK.getWidth(), imageCMYK.getHeight(), "png", "ICCBased");
doWritePDF(document, ximage, TESTRESULTSDIR, "cmyk.pdf");
// still slight difference of 1 color level
// checkIdent(imageCMYK, ximage.getImage());
}
use of java.awt.color.ColorSpace in project sis by apache.
the class Colorizer method initialize.
/**
* Uses the given color model for mapping range of values to new colors. The colors in the given color model
* are ignored (because they will be replaced by colors specified by this {@code Colorizer}); only the range
* of values will be fetched, if such range exists.
*
* @param source the color model from which to get a range of values, or {@code null}.
* @return {@code true} on success, or {@code false} if no range of values has been found.
* @throws IllegalStateException if a sample dimension is already defined on this colorizer.
*/
public boolean initialize(final ColorModel source) {
checkInitializationStatus(false);
if (source != null) {
final ColorSpace cs = source.getColorSpace();
if (cs instanceof ScaledColorSpace) {
final ScaledColorSpace scs = (ScaledColorSpace) cs;
initialize(scs.offset, scs.maximum);
return true;
}
}
return false;
}
Aggregations