use of java.awt.color.ICC_ColorSpace in project acs-aem-commons by Adobe-Consulting-Services.
the class CMYKJPEGImageReader method createRGBAImageFromRGBA.
/**
* Creates a buffered image from a raster in the RGBA color space, converting
* the colors to RGB using the provided CMYK ICC_Profile.
*
* As seen from a comment made by 'phelps' at
* http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=4799903
*
* @param rgbaRaster A raster with (at least) 4 bands of samples.
* @param rgbaProfile An ICC_Profile for conversion from the CMYK color space
* to the RGB color space. If this parameter is null, a default profile is used.
* @return a BufferedImage in the RGB color space.
*/
public static BufferedImage createRGBAImageFromRGBA(Raster rgbaRaster, ICC_Profile rgbaProfile) {
BufferedImage image;
int w = rgbaRaster.getWidth();
int h = rgbaRaster.getHeight();
// ICC_Profile currently not supported
rgbaProfile = null;
if (rgbaProfile != null) {
ColorSpace rgbaCS = new ICC_ColorSpace(rgbaProfile);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
WritableRaster rgbRaster = image.getRaster();
ColorSpace rgbCS = image.getColorModel().getColorSpace();
ColorConvertOp cmykToRgb = new ColorConvertOp(rgbaCS, rgbCS, null);
cmykToRgb.filter(rgbaRaster, rgbRaster);
} else {
int[] rgb = new int[w * h];
int[] R = rgbaRaster.getSamples(0, 0, w, h, 0, (int[]) null);
int[] G = rgbaRaster.getSamples(0, 0, w, h, 1, (int[]) null);
int[] B = rgbaRaster.getSamples(0, 0, w, h, 2, (int[]) null);
int[] A = rgbaRaster.getSamples(0, 0, w, h, 3, (int[]) null);
for (int i = 0, imax = R.length; i < imax; i++) {
rgb[i] = A[i] << 24 | R[i] << 16 | G[i] << 8 | B[i];
}
Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff, 0xff000000 }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new DirectColorModel(cs, 32, 0xff0000, 0xff00, 0xff, 0x0ff000000, false, DataBuffer.TYPE_INT);
image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
}
return image;
}
use of java.awt.color.ICC_ColorSpace in project pdfbox by apache.
the class PNGConverterTest method getImageWithProfileData.
public static BufferedImage getImageWithProfileData(BufferedImage sourceImage, ICC_Profile realProfile) {
Hashtable<String, Object> properties = new Hashtable<>();
String[] propertyNames = sourceImage.getPropertyNames();
if (propertyNames != null) {
for (String propertyName : propertyNames) {
properties.put(propertyName, sourceImage.getProperty(propertyName));
}
}
ComponentColorModel oldColorModel = (ComponentColorModel) sourceImage.getColorModel();
boolean hasAlpha = oldColorModel.hasAlpha();
int transparency = oldColorModel.getTransparency();
boolean alphaPremultiplied = oldColorModel.isAlphaPremultiplied();
WritableRaster raster = sourceImage.getRaster();
int dataType = raster.getDataBuffer().getDataType();
int[] componentSize = oldColorModel.getComponentSize();
final ColorModel colorModel = new ComponentColorModel(new ICC_ColorSpace(realProfile), componentSize, hasAlpha, alphaPremultiplied, transparency, dataType);
return new BufferedImage(colorModel, raster, sourceImage.isAlphaPremultiplied(), properties);
}
use of java.awt.color.ICC_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());
}
Aggregations