use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.
the class ImageFactory method createDCMImage.
public static BufferedImage createDCMImage(int type, int cs) {
if (type == BufferedImage.TYPE_INT_RGB && cs == ColorSpace.CS_sRGB) {
BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
DataBuffer data = image.getData().getDataBuffer();
fill(image);
return image;
}
ColorSpace cSpace = ColorSpace.getInstance(cs);
DirectColorModel dcm = null;
switch(type) {
case BufferedImage.TYPE_INT_ARGB:
dcm = new DirectColorModel(cSpace, 32, rMask, gMask, bMask, aMask, false, DataBuffer.TYPE_INT);
break;
case BufferedImage.TYPE_INT_RGB:
dcm = new DirectColorModel(cSpace, 24, rMask, gMask, bMask, 0, false, DataBuffer.TYPE_INT);
break;
case BufferedImage.TYPE_INT_BGR:
dcm = new DirectColorModel(cSpace, 24, rMask, gMask, bMask, 0, false, DataBuffer.TYPE_INT);
break;
case BufferedImage.TYPE_USHORT_555_RGB:
dcm = new DirectColorModel(cSpace, 15, r555Mask, g555Mask, b555Mask, 0, false, DataBuffer.TYPE_USHORT);
break;
case BufferedImage.TYPE_USHORT_565_RGB:
dcm = new DirectColorModel(cSpace, 16, r565Mask, g565Mask, b565Mask, 0, false, DataBuffer.TYPE_USHORT);
break;
}
SampleModel sm = dcm.createCompatibleSampleModel(WIDTH, HEIGHT);
WritableRaster raster = dcm.createCompatibleWritableRaster(WIDTH, HEIGHT);
switch(type) {
case BufferedImage.TYPE_INT_ARGB:
fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
break;
case BufferedImage.TYPE_INT_RGB:
fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
break;
case BufferedImage.TYPE_INT_BGR:
fillDCM(raster.getDataBuffer(), sm, cSpace.getType());
break;
case BufferedImage.TYPE_USHORT_555_RGB:
fillDCM(raster.getDataBuffer(), sm, cSpace.getType(), 5, 5, 5);
break;
case BufferedImage.TYPE_USHORT_565_RGB:
fillDCM(raster.getDataBuffer(), sm, cSpace.getType(), 5, 6, 5);
break;
}
return new BufferedImage(dcm, raster, false, null);
}
use of java.awt.color.ColorSpace in project frostwire by frostwire.
the class CMYKJPEGImageReader method createRGBImageFromCMYK.
/**
* Creates a buffered image from a raster in the CMYK 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 cmykRaster A raster with (at least) 4 bands of samples.
* @param cmykProfile 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 createRGBImageFromCMYK(Raster cmykRaster, ICC_Profile cmykProfile) {
BufferedImage image;
int w = cmykRaster.getWidth();
int h = cmykRaster.getHeight();
if (cmykProfile != null) {
ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
WritableRaster rgbRaster = image.getRaster();
ColorSpace rgbCS = image.getColorModel().getColorSpace();
ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
cmykToRgb.filter(cmykRaster, rgbRaster);
} else {
int[] rgb = new int[w * h];
int[] C = cmykRaster.getSamples(0, 0, w, h, 0, (int[]) null);
int[] M = cmykRaster.getSamples(0, 0, w, h, 1, (int[]) null);
int[] Y = cmykRaster.getSamples(0, 0, w, h, 2, (int[]) null);
int[] K = cmykRaster.getSamples(0, 0, w, h, 3, (int[]) null);
for (int i = 0, imax = C.length; i < imax; i++) {
int k = K[i];
rgb[i] = (255 - Math.min(255, C[i] + k)) << 16 | (255 - Math.min(255, M[i] + k)) << 8 | (255 - Math.min(255, Y[i] + k));
}
Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
}
return image;
}
use of java.awt.color.ColorSpace in project frostwire by frostwire.
the class JPEGImageIO method createRGBImageFromCMYK.
/**
* Creates a buffered image from a raster in the CMYK 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 cmykRaster A raster with (at least) 4 bands of samples.
* @param cmykProfile 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 createRGBImageFromCMYK(Raster cmykRaster, ICC_Profile cmykProfile) {
BufferedImage image;
int w = cmykRaster.getWidth();
int h = cmykRaster.getHeight();
if (cmykProfile != null) {
ColorSpace cmykCS = new ICC_ColorSpace(cmykProfile);
image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
WritableRaster rgbRaster = image.getRaster();
ColorSpace rgbCS = image.getColorModel().getColorSpace();
ColorConvertOp cmykToRgb = new ColorConvertOp(cmykCS, rgbCS, null);
cmykToRgb.filter(cmykRaster, rgbRaster);
} else {
int[] rgb = new int[w * h];
int[] C = cmykRaster.getSamples(0, 0, w, h, 0, (int[]) null);
int[] M = cmykRaster.getSamples(0, 0, w, h, 1, (int[]) null);
int[] Y = cmykRaster.getSamples(0, 0, w, h, 2, (int[]) null);
int[] K = cmykRaster.getSamples(0, 0, w, h, 3, (int[]) null);
for (int i = 0, imax = C.length; i < imax; i++) {
int k = K[i];
rgb[i] = (255 - Math.min(255, C[i] + k)) << 16 | (255 - Math.min(255, M[i] + k)) << 8 | (255 - Math.min(255, Y[i] + k));
}
Raster rgbRaster = Raster.createPackedRaster(new DataBufferInt(rgb, rgb.length), w, h, w, new int[] { 0xff0000, 0xff00, 0xff }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new DirectColorModel(cs, 24, 0xff0000, 0xff00, 0xff, 0x0, false, DataBuffer.TYPE_INT);
image = new BufferedImage(cm, (WritableRaster) rgbRaster, true, null);
}
return image;
}
use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.
the class X11GraphicsConfig method createABGRCCM.
public static ComponentColorModel createABGRCCM() {
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8, 8 };
int[] bOffs = { 3, 2, 1, 0 };
return new ComponentColorModel(cs, nBits, true, true, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
}
use of java.awt.color.ColorSpace in project jdk8u_jdk by JetBrains.
the class HTMLCodec method imageToPlatformBytes.
@Override
protected byte[] imageToPlatformBytes(Image image, long format) throws IOException {
String mimeType = null;
if (format == CF_PNG) {
mimeType = "image/png";
} else if (format == CF_JFIF) {
mimeType = "image/jpeg";
}
if (mimeType != null) {
return imageToStandardBytes(image, mimeType);
}
int width = 0;
int height = 0;
if (image instanceof ToolkitImage) {
ImageRepresentation ir = ((ToolkitImage) image).getImageRep();
ir.reconstruct(ImageObserver.ALLBITS);
width = ir.getWidth();
height = ir.getHeight();
} else {
width = image.getWidth(null);
height = image.getHeight(null);
}
// Fix for 4919639.
// Some Windows native applications (e.g. clipbrd.exe) do not handle
// 32-bpp DIBs correctly.
// As a workaround we switched to 24-bpp DIBs.
// MSDN prescribes that the bitmap array for a 24-bpp should consist of
// 3-byte triplets representing blue, green and red components of a
// pixel respectively. Additionally each scan line must be padded with
// zeroes to end on a LONG data-type boundary. LONG is always 32-bit.
// We render the given Image to a BufferedImage of type TYPE_3BYTE_BGR
// with non-default scanline stride and pass the resulting data buffer
// to the native code to fill the BITMAPINFO structure.
int mod = (width * 3) % 4;
int pad = mod > 0 ? 4 - mod : 0;
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
int[] nBits = { 8, 8, 8 };
int[] bOffs = { 2, 1, 0 };
ColorModel colorModel = new ComponentColorModel(cs, nBits, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, width, height, width * 3 + pad, 3, bOffs, null);
BufferedImage bimage = new BufferedImage(colorModel, raster, false, null);
// Some Windows native applications (e.g. clipbrd.exe) do not understand
// top-down DIBs.
// So we flip the image vertically and create a bottom-up DIB.
AffineTransform imageFlipTransform = new AffineTransform(1, 0, 0, -1, 0, height);
Graphics2D g2d = bimage.createGraphics();
try {
g2d.drawImage(image, imageFlipTransform, null);
} finally {
g2d.dispose();
}
DataBufferByte buffer = (DataBufferByte) raster.getDataBuffer();
byte[] imageData = buffer.getData();
return imageDataToPlatformImageBytes(imageData, width, height, format);
}
Aggregations