use of java.awt.image.ComponentColorModel in project quick-media by liuyueyi.
the class PNGRed method createComponentColorModel.
/**
* A convenience method to create an instance of
* <code>ComponentColorModel</code> suitable for use with the
* given <code>SampleModel</code>. The <code>SampleModel</code>
* should have a data type of <code>DataBuffer.TYPE_BYTE</code>,
* <code>TYPE_USHORT</code>, or <code>TYPE_INT</code> and between
* 1 and 4 bands. Depending on the number of bands of the
* <code>SampleModel</code>, either a gray, gray+alpha, rgb, or
* rgb+alpha <code>ColorModel</code> is returned.
*/
public static ColorModel createComponentColorModel(SampleModel sm) {
int type = sm.getDataType();
int bands = sm.getNumBands();
ComponentColorModel cm = null;
if (type == DataBuffer.TYPE_BYTE) {
switch(bands) {
case 1:
cm = colorModelGray8;
break;
case 2:
cm = colorModelGrayAlpha8;
break;
case 3:
cm = colorModelRGB8;
break;
case 4:
cm = colorModelRGBA8;
break;
}
} else if (type == DataBuffer.TYPE_USHORT) {
switch(bands) {
case 1:
cm = colorModelGray16;
break;
case 2:
cm = colorModelGrayAlpha16;
break;
case 3:
cm = colorModelRGB16;
break;
case 4:
cm = colorModelRGBA16;
break;
}
} else if (type == DataBuffer.TYPE_INT) {
switch(bands) {
case 1:
cm = colorModelGray32;
break;
case 2:
cm = colorModelGrayAlpha32;
break;
case 3:
cm = colorModelRGB32;
break;
case 4:
cm = colorModelRGBA32;
break;
}
}
return cm;
}
use of java.awt.image.ComponentColorModel in project quick-media by liuyueyi.
the class PNGImage method createComponentColorModel.
/**
* A convenience method to create an instance of
* <code>ComponentColorModel</code> suitable for use with the
* given <code>SampleModel</code>. The <code>SampleModel</code>
* should have a data type of <code>DataBuffer.TYPE_BYTE</code>,
* <code>TYPE_USHORT</code>, or <code>TYPE_INT</code> and between
* 1 and 4 bands. Depending on the number of bands of the
* <code>SampleModel</code>, either a gray, gray+alpha, rgb, or
* rgb+alpha <code>ColorModel</code> is returned.
*/
public static ColorModel createComponentColorModel(SampleModel sm) {
int type = sm.getDataType();
int bands = sm.getNumBands();
ComponentColorModel cm = null;
if (type == DataBuffer.TYPE_BYTE) {
switch(bands) {
case 1:
cm = colorModelGray8;
break;
case 2:
cm = colorModelGrayAlpha8;
break;
case 3:
cm = colorModelRGB8;
break;
case 4:
cm = colorModelRGBA8;
break;
}
} else if (type == DataBuffer.TYPE_USHORT) {
switch(bands) {
case 1:
cm = colorModelGray16;
break;
case 2:
cm = colorModelGrayAlpha16;
break;
case 3:
cm = colorModelRGB16;
break;
case 4:
cm = colorModelRGBA16;
break;
}
} else if (type == DataBuffer.TYPE_INT) {
switch(bands) {
case 1:
cm = colorModelGray32;
break;
case 2:
cm = colorModelGrayAlpha32;
break;
case 3:
cm = colorModelRGB32;
break;
case 4:
cm = colorModelRGBA32;
break;
}
}
return cm;
}
use of java.awt.image.ComponentColorModel in project nutz by nutzam.
the class Images method createJPEG4.
/**
* Java's ImageIO can't process 4-component images and Java2D can't apply
* AffineTransformOp either, so convert raster data to RGB. Technique due to
* MArk Stephens. Free for any use.
*/
private static BufferedImage createJPEG4(Raster raster) {
int w = raster.getWidth();
int h = raster.getHeight();
byte[] rgb = new byte[w * h * 3];
float[] Y = raster.getSamples(0, 0, w, h, 0, (float[]) null);
float[] Cb = raster.getSamples(0, 0, w, h, 1, (float[]) null);
float[] Cr = raster.getSamples(0, 0, w, h, 2, (float[]) null);
float[] K = raster.getSamples(0, 0, w, h, 3, (float[]) null);
for (int i = 0, imax = Y.length, base = 0; i < imax; i++, base += 3) {
float k = 220 - K[i], y = 255 - Y[i], cb = 255 - Cb[i], cr = 255 - Cr[i];
double val = y + 1.402 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
val = y - 0.34414 * (cb - 128) - 0.71414 * (cr - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 1] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
val = y + 1.772 * (cb - 128) - k;
val = (val - 128) * .65f + 128;
rgb[base + 2] = val < 0.0 ? (byte) 0 : val > 255.0 ? (byte) 0xff : (byte) (val + 0.5);
}
raster = Raster.createInterleavedRaster(new DataBufferByte(rgb, rgb.length), w, h, w * 3, 3, new int[] { 0, 1, 2 }, null);
ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
ColorModel cm = new ComponentColorModel(cs, false, true, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
return new BufferedImage(cm, (WritableRaster) raster, true, null);
}
use of java.awt.image.ComponentColorModel in project jwt by emweb.
the class WServerGLWidget method texImage2D.
@Override
public void texImage2D(WGLWidget.GLenum target, int level, WGLWidget.GLenum internalformat, WGLWidget.GLenum format, WGLWidget.GLenum type, WGLWidget.Texture texture) {
BufferedImage initialImage = null;
try {
initialImage = ImageIO.read(new File(texture.getUrl()));
} catch (IOException e) {
logger.error("IOException reading {}", texture.getUrl(), e);
}
int openGlInternalFormat = serverGLenum(internalformat);
int openGlImageFormat = serverGLenum(format);
WritableRaster raster = Raster.createInterleavedRaster(DataBuffer.TYPE_BYTE, initialImage.getWidth(), initialImage.getHeight(), 4, null);
ComponentColorModel colorModel = new ComponentColorModel(ColorSpace.getInstance(ColorSpace.CS_sRGB), new int[] { 8, 8, 8, 8 }, true, false, ComponentColorModel.TRANSLUCENT, DataBuffer.TYPE_BYTE);
BufferedImage bufImg = new BufferedImage(colorModel, raster, false, null);
Graphics2D g = bufImg.createGraphics();
g.translate(0, initialImage.getHeight());
g.scale(1, -1);
g.drawImage(initialImage, null, null);
DataBufferByte imgBuf = (DataBufferByte) raster.getDataBuffer();
byte[] bytes = imgBuf.getData();
ByteBuffer buffer = ByteBuffer.wrap(bytes);
g.dispose();
glCtx_.glTexImage2D(GL2.GL_TEXTURE_2D, 0, openGlInternalFormat, initialImage.getWidth(), initialImage.getHeight(), 0, openGlImageFormat, GL2.GL_UNSIGNED_BYTE, buffer);
}
Aggregations