use of java.awt.color.ICC_ColorSpace in project pdfbox by apache.
the class ImageIOUtil method writeImage.
/**
* Writes a buffered image to a file using the given image format.
* Compression is fixed for PNG, GIF, BMP and WBMP, dependent of the compressionQuality
* parameter for JPG, and dependent of bit count for TIFF (a bitonal image
* will be compressed with CCITT G4, a color image with LZW). Creating a
* TIFF image is only supported if the jai_imageio library is in the class
* path.
*
* @param image the image to be written
* @param formatName the target format (ex. "png")
* @param output the output stream to be used for writing
* @param dpi the resolution in dpi (dots per inch) to be used in metadata
* @param compressionQuality quality to be used when compressing the image (0 <
* compressionQuality < 1.0f). See {@link ImageWriteParam#setCompressionQuality(float)} for
* more details.
* @param compressionType Advanced users only, and only relevant for TIFF
* files: If null, save uncompressed; if empty string, use logic explained
* above; other valid values are found in the javadoc of
* <a href="https://download.java.net/media/jai-imageio/javadoc/1.1/com/sun/media/imageio/plugins/tiff/TIFFImageWriteParam.html">TIFFImageWriteParam</a>.
* @return true if the image file was produced, false if there was an error.
* @throws IOException if an I/O error occurs
*/
public static boolean writeImage(BufferedImage image, String formatName, OutputStream output, int dpi, float compressionQuality, String compressionType) throws IOException {
ImageOutputStream imageOutput = null;
ImageWriter writer = null;
try {
// find suitable image writer
Iterator<ImageWriter> writers = ImageIO.getImageWritersByFormatName(formatName);
ImageWriteParam param = null;
IIOMetadata metadata = null;
// accept a driver that can't, if a better one can't be found
while (writers.hasNext()) {
if (writer != null) {
writer.dispose();
}
writer = writers.next();
if (writer != null) {
param = writer.getDefaultWriteParam();
metadata = writer.getDefaultImageMetadata(new ImageTypeSpecifier(image), param);
if (metadata != null && !metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
break;
}
}
}
if (writer == null) {
LOG.error("No ImageWriter found for '" + formatName + "' format");
LOG.error("Supported formats: " + Arrays.toString(ImageIO.getWriterFormatNames()));
return false;
}
boolean isTifFormat = formatName.toLowerCase().startsWith("tif");
// compression
if (param != null && param.canWriteCompressed()) {
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
if (isTifFormat) {
if ("".equals(compressionType)) {
// default logic
TIFFUtil.setCompressionType(param, image);
} else {
param.setCompressionType(compressionType);
if (compressionType != null) {
param.setCompressionQuality(compressionQuality);
}
}
} else {
param.setCompressionType(param.getCompressionTypes()[0]);
param.setCompressionQuality(compressionQuality);
}
}
if (metadata != null) {
if (isTifFormat) {
// TIFF metadata
TIFFUtil.updateMetadata(metadata, image, dpi);
} else if ("jpeg".equalsIgnoreCase(formatName) || "jpg".equalsIgnoreCase(formatName)) {
// This segment must be run before other meta operations,
// or else "IIOInvalidTreeException: Invalid node: app0JFIF"
// The other (general) "meta" methods may not be used, because
// this will break the reading of the meta data in tests
JPEGUtil.updateMetadata(metadata, dpi);
} else {
// write metadata is possible
if (!metadata.isReadOnly() && metadata.isStandardMetadataFormatSupported()) {
setDPI(metadata, dpi, formatName);
}
}
}
if (metadata != null && formatName.equalsIgnoreCase("png") && hasICCProfile(image)) {
// add ICC profile
IIOMetadataNode iccp = new IIOMetadataNode("iCCP");
ICC_Profile profile = ((ICC_ColorSpace) image.getColorModel().getColorSpace()).getProfile();
iccp.setUserObject(getAsDeflatedBytes(profile));
iccp.setAttribute("profileName", "unknown");
iccp.setAttribute("compressionMethod", "deflate");
Node nativeTree = metadata.getAsTree(metadata.getNativeMetadataFormatName());
nativeTree.appendChild(iccp);
metadata.mergeTree(metadata.getNativeMetadataFormatName(), nativeTree);
}
// write
imageOutput = ImageIO.createImageOutputStream(output);
writer.setOutput(imageOutput);
writer.write(null, new IIOImage(image, null, metadata), param);
} finally {
if (writer != null) {
writer.dispose();
}
if (imageOutput != null) {
imageOutput.close();
}
}
return true;
}
use of java.awt.color.ICC_ColorSpace in project jdk8u_jdk by JetBrains.
the class CMMTests method getColorSpace.
protected static ColorSpace getColorSpace(TestEnvironment env) {
ColorSpace cs;
//(Boolean)env.getModifier(usePlatfromProfiles);
Boolean usePlatfrom = true;
int cs_code = env.getIntValue(csList);
if (usePlatfrom) {
cs = ColorSpace.getInstance(cs_code);
} else {
String resource = "profiles/";
switch(cs_code) {
case ColorSpace.CS_CIEXYZ:
resource += "CIEXYZ.pf";
break;
case ColorSpace.CS_GRAY:
resource += "GRAY.pf";
break;
case ColorSpace.CS_LINEAR_RGB:
resource += "LINEAR_RGB.pf";
break;
case ColorSpace.CS_PYCC:
resource += "PYCC.pf";
break;
case ColorSpace.CS_sRGB:
resource += "sRGB.pf";
break;
default:
throw new RuntimeException("Unknown color space: " + cs_code);
}
try {
InputStream is = CMMTests.class.getResourceAsStream(resource);
ICC_Profile p = ICC_Profile.getInstance(is);
cs = new ICC_ColorSpace(p);
} catch (IOException e) {
throw new RuntimeException("Unable load profile from resource " + resource, e);
}
}
return cs;
}
use of java.awt.color.ICC_ColorSpace in project jdk8u_jdk by JetBrains.
the class InvalidRenderIntentTest method main.
public static void main(String[] args) {
ICC_Profile pSRGB = ICC_Profile.getInstance(CS_sRGB);
byte[] raw_data = pSRGB.getData();
setRenderingIntent(0x1000000, raw_data);
ICC_Profile p = ICC_Profile.getInstance(raw_data);
ICC_ColorSpace cs = new ICC_ColorSpace(p);
// perfrom test color conversion
ColorConvertOp op = new ColorConvertOp(cs, ColorSpace.getInstance(CS_sRGB), null);
BufferedImage src = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
BufferedImage dst = new BufferedImage(1, 1, TYPE_3BYTE_BGR);
try {
op.filter(src.getRaster(), dst.getRaster());
} catch (CMMException e) {
throw new RuntimeException("Test failed.", e);
}
System.out.println("Test passed.");
}
use of java.awt.color.ICC_ColorSpace in project jdk8u_jdk by JetBrains.
the class ColorModel method getGray16TosRGB8LUT.
/*
* Return a byte LUT that converts 16-bit gray values in the grayCS
* ColorSpace to the appropriate 8-bit sRGB value. I.e., if lut
* is the byte array returned by this method and sval = lut[gval],
* then the sRGB triple (sval,sval,sval) is the best match to gval.
* Cache references to any computed LUT in a Map.
*/
static byte[] getGray16TosRGB8LUT(ICC_ColorSpace grayCS) {
if (isLinearGRAYspace(grayCS)) {
return getLinearRGB16TosRGB8LUT();
}
if (g16Tos8Map != null) {
byte[] g16Tos8LUT = g16Tos8Map.get(grayCS);
if (g16Tos8LUT != null) {
return g16Tos8LUT;
}
}
short[] tmp = new short[65536];
for (int i = 0; i <= 65535; i++) {
tmp[i] = (short) i;
}
ColorTransform[] transformList = new ColorTransform[2];
PCMM mdl = CMSManager.getModule();
ICC_ColorSpace srgbCS = (ICC_ColorSpace) ColorSpace.getInstance(ColorSpace.CS_sRGB);
transformList[0] = mdl.createTransform(grayCS.getProfile(), ColorTransform.Any, ColorTransform.In);
transformList[1] = mdl.createTransform(srgbCS.getProfile(), ColorTransform.Any, ColorTransform.Out);
ColorTransform t = mdl.createTransform(transformList);
tmp = t.colorConvert(tmp, null);
byte[] g16Tos8LUT = new byte[65536];
for (int i = 0, j = 2; i <= 65535; i++, j += 3) {
// All three components of tmp should be equal, since
// the input color space to colorConvert is a gray scale
// space. However, there are slight anomalies in the results.
// Copy tmp starting at index 2, since colorConvert seems
// to be slightly more accurate for the third component!
// scale unsigned short (0 - 65535) to unsigned byte (0 - 255)
g16Tos8LUT[i] = (byte) (((float) (tmp[j] & 0xffff)) * (1.0f / 257.0f) + 0.5f);
}
if (g16Tos8Map == null) {
g16Tos8Map = Collections.synchronizedMap(new WeakHashMap<ICC_ColorSpace, byte[]>(2));
}
g16Tos8Map.put(grayCS, g16Tos8LUT);
return g16Tos8LUT;
}
use of java.awt.color.ICC_ColorSpace in project jdk8u_jdk by JetBrains.
the class ColorModel method getLinearGray16ToOtherGray8LUT.
/*
* Return a byte LUT that converts 16-bit gray values in the CS_GRAY
* linear gray ColorSpace to the appropriate 8-bit value in the
* grayCS ColorSpace. Cache references to any computed LUT in a Map.
*/
static byte[] getLinearGray16ToOtherGray8LUT(ICC_ColorSpace grayCS) {
if (lg16Toog8Map != null) {
byte[] lg16Toog8LUT = lg16Toog8Map.get(grayCS);
if (lg16Toog8LUT != null) {
return lg16Toog8LUT;
}
}
short[] tmp = new short[65536];
for (int i = 0; i <= 65535; i++) {
tmp[i] = (short) i;
}
ColorTransform[] transformList = new ColorTransform[2];
PCMM mdl = CMSManager.getModule();
ICC_ColorSpace lgCS = (ICC_ColorSpace) ColorSpace.getInstance(ColorSpace.CS_GRAY);
transformList[0] = mdl.createTransform(lgCS.getProfile(), ColorTransform.Any, ColorTransform.In);
transformList[1] = mdl.createTransform(grayCS.getProfile(), ColorTransform.Any, ColorTransform.Out);
ColorTransform t = mdl.createTransform(transformList);
tmp = t.colorConvert(tmp, null);
byte[] lg16Toog8LUT = new byte[65536];
for (int i = 0; i <= 65535; i++) {
// scale unsigned short (0 - 65535) to unsigned byte (0 - 255)
lg16Toog8LUT[i] = (byte) (((float) (tmp[i] & 0xffff)) * (1.0f / 257.0f) + 0.5f);
}
if (lg16Toog8Map == null) {
lg16Toog8Map = Collections.synchronizedMap(new WeakHashMap<ICC_ColorSpace, byte[]>(2));
}
lg16Toog8Map.put(grayCS, lg16Toog8LUT);
return lg16Toog8LUT;
}
Aggregations