use of java.awt.image.ComponentColorModel in project javacv by bytedeco.
the class Java2DFrameConverter method getBufferedImage.
public BufferedImage getBufferedImage(Frame frame, double gamma, boolean flipChannels, ColorSpace cs) {
if (frame == null || frame.image == null) {
return null;
}
int type = getBufferedImageType(frame);
if (bufferedImage == null || bufferedImage.getWidth() != frame.imageWidth || bufferedImage.getHeight() != frame.imageHeight || bufferedImage.getType() != type) {
bufferedImage = type == BufferedImage.TYPE_CUSTOM || cs != null ? null : new BufferedImage(frame.imageWidth, frame.imageHeight, type);
}
if (bufferedImage == null) {
boolean alpha = false;
int[] offsets = null;
if (frame.imageChannels == 1) {
alpha = false;
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
}
offsets = new int[] { 0 };
} else if (frame.imageChannels == 3) {
alpha = false;
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
}
// raster in "BGR" order like OpenCV..
offsets = new int[] { 2, 1, 0 };
} else if (frame.imageChannels == 4) {
alpha = true;
if (cs == null) {
cs = ColorSpace.getInstance(ColorSpace.CS_LINEAR_RGB);
}
// raster in "RGBA" order for OpenCL.. alpha needs to be last
offsets = new int[] { 0, 1, 2, 3 };
} else {
assert false;
}
ColorModel cm = null;
WritableRaster wr = null;
if (frame.imageDepth == Frame.DEPTH_UBYTE || frame.imageDepth == Frame.DEPTH_BYTE) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_BYTE, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else if (frame.imageDepth == Frame.DEPTH_USHORT) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_USHORT, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else if (frame.imageDepth == Frame.DEPTH_SHORT) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_SHORT, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else if (frame.imageDepth == Frame.DEPTH_INT) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_INT);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_INT, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else if (frame.imageDepth == Frame.DEPTH_FLOAT) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_FLOAT);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_FLOAT, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else if (frame.imageDepth == Frame.DEPTH_DOUBLE) {
cm = new ComponentColorModel(cs, alpha, false, Transparency.OPAQUE, DataBuffer.TYPE_DOUBLE);
wr = Raster.createWritableRaster(new ComponentSampleModel(DataBuffer.TYPE_DOUBLE, frame.imageWidth, frame.imageHeight, frame.imageChannels, frame.imageStride, offsets), null);
} else {
assert false;
}
bufferedImage = new BufferedImage(cm, wr, false, null);
}
if (bufferedImage != null) {
copy(frame, bufferedImage, gamma, flipChannels, null);
}
return bufferedImage;
}
use of java.awt.image.ComponentColorModel in project com.revolsys.open by revolsys.
the class Gdal method getBufferedImage.
/**
* <p>
* Convert the overview raster from {@link Dataset} to a
* {@link BufferedImage} . The raster will be clipped to the
* sourceOffsetX,sourceOffsetY -> sourceWidth, sourceHeight rectangle. The
* clip rectangle will be adjusted to fit inside the bounds of the source
* image. The result image will scaled to the dimensions of targetWidth,
* targetHeight.
* </p>
*
* @param dataset
* The image dataset.
* @param overviewIndex
* The index of the overview raster data. Use -1 for the whole
* image.
* @param sourceOffsetX
* The x location of the clip rectangle.
* @param sourceOffsetY
* The y location of the clip rectangle.
* @param sourceWidth
* The width of the clip rectangle. Use -1 to auto calculate.
* @param sourceHeight
* The height of the clip rectangle. Use -1 to auto calculate.
* @param targetWidth
* The width of the result image. Use -1 to auto calculate.
* @param targetHeight
* The height of the result image. Use -1 to auto calculate.
* @return The buffered image.
*/
public static BufferedImage getBufferedImage(final Dataset dataset, final int overviewIndex, int sourceOffsetX, int sourceOffsetY, int sourceWidth, int sourceHeight, int targetWidth, int targetHeight) {
synchronized (dataset) {
final int bandCount = dataset.getRasterCount();
final ByteBuffer[] bandData = new ByteBuffer[bandCount];
final int[] banks = new int[bandCount];
final int[] offsets = new int[bandCount];
int pixels = 0;
int bandDataType = 0;
int rasterColorInterpretation = -1;
ColorTable rasterColorTable = null;
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
final Band band = dataset.GetRasterBand(bandIndex + 1);
try {
Band overviewBand;
if (overviewIndex == -1) {
overviewBand = band;
} else {
overviewBand = band.GetOverview(overviewIndex);
}
try {
if (rasterColorTable == null) {
rasterColorTable = band.GetRasterColorTable();
rasterColorInterpretation = band.GetRasterColorInterpretation();
bandDataType = band.getDataType();
final int overviewWidth = overviewBand.getXSize();
final int overviewHeight = overviewBand.getYSize();
if (sourceOffsetX < 0) {
sourceOffsetX = 0;
}
if (sourceOffsetY < 0) {
sourceOffsetY = 0;
}
if (sourceOffsetX >= overviewWidth || sourceOffsetY >= overviewHeight) {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
if (sourceWidth < 0) {
sourceWidth = overviewWidth;
}
if (sourceOffsetX + sourceWidth > overviewWidth) {
sourceWidth = overviewWidth - sourceOffsetX;
}
if (targetWidth < 0) {
targetWidth = sourceWidth;
}
if (sourceHeight < 0) {
sourceHeight = overviewHeight;
}
if (sourceOffsetY + sourceHeight > overviewHeight) {
sourceHeight = overviewHeight - sourceOffsetY;
}
if (targetHeight < 0) {
targetHeight = sourceHeight;
}
pixels = targetWidth * targetHeight;
}
if (pixels > 0 && sourceHeight > 0 && sourceWidth > 0) {
final int bufferSize = pixels * gdal.GetDataTypeSize(bandDataType) / 8;
final ByteBuffer data = ByteBuffer.allocateDirect(bufferSize);
data.order(ByteOrder.nativeOrder());
final int result = overviewBand.ReadRaster_Direct(sourceOffsetX, sourceOffsetY, sourceWidth, sourceHeight, targetWidth, targetHeight, bandDataType, data);
if (result == gdalconstConstants.CE_None) {
bandData[bandIndex] = data;
} else {
throw new RuntimeException("Error converting image");
}
} else {
return new BufferedImage(1, 1, BufferedImage.TYPE_INT_ARGB);
}
banks[bandIndex] = bandIndex;
offsets[bandIndex] = 0;
} finally {
overviewBand.delete();
}
} finally {
band.delete();
}
}
DataBuffer imageBuffer = null;
SampleModel sampleModel = null;
int dataType = 0;
int dataBufferType = 0;
if (bandDataType == gdalconstConstants.GDT_Byte) {
final byte[][] bytes = new byte[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
bytes[bandIndex] = new byte[pixels];
bandData[bandIndex].get(bytes[bandIndex]);
}
imageBuffer = new DataBufferByte(bytes, pixels);
dataBufferType = DataBuffer.TYPE_BYTE;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex ? BufferedImage.TYPE_BYTE_INDEXED : BufferedImage.TYPE_BYTE_GRAY;
} else if (bandDataType == gdalconstConstants.GDT_Int16) {
final short[][] shorts = new short[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
shorts[bandIndex] = new short[pixels];
bandData[bandIndex].asShortBuffer().get(shorts[bandIndex]);
}
imageBuffer = new DataBufferShort(shorts, pixels);
dataBufferType = DataBuffer.TYPE_USHORT;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = BufferedImage.TYPE_USHORT_GRAY;
} else if (bandDataType == gdalconstConstants.GDT_Int32) {
final int[][] ints = new int[bandCount][];
for (int bandIndex = 0; bandIndex < bandCount; bandIndex++) {
ints[bandIndex] = new int[pixels];
bandData[bandIndex].asIntBuffer().get(ints[bandIndex]);
}
imageBuffer = new DataBufferInt(ints, pixels);
dataBufferType = DataBuffer.TYPE_INT;
sampleModel = new BandedSampleModel(dataBufferType, targetWidth, targetHeight, targetWidth, banks, offsets);
dataType = BufferedImage.TYPE_CUSTOM;
}
final WritableRaster raster = Raster.createWritableRaster(sampleModel, imageBuffer, null);
BufferedImage image = null;
ColorModel colorModel = null;
if (rasterColorInterpretation == gdalconstConstants.GCI_PaletteIndex) {
dataType = BufferedImage.TYPE_BYTE_INDEXED;
colorModel = rasterColorTable.getIndexColorModel(gdal.GetDataTypeSize(bandDataType));
image = new BufferedImage(colorModel, raster, false, null);
} else {
ColorSpace colorSpace = null;
if (bandCount > 2) {
colorSpace = ColorSpace.getInstance(ColorSpace.CS_sRGB);
colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, dataBufferType);
image = new BufferedImage(colorModel, raster, true, null);
} else {
image = new BufferedImage(targetWidth, targetHeight, dataType);
image.setData(raster);
}
}
return image;
}
}
use of java.awt.image.ComponentColorModel in project pivot by apache.
the class GrayscaleDecorator method prepare.
@Override
public Graphics2D prepare(Component component, Graphics2D graphicsValue) {
this.graphics = graphicsValue;
int width = component.getWidth();
int height = component.getHeight();
if (bufferedImage == null || bufferedImage.getWidth() < width || bufferedImage.getHeight() < height) {
ColorSpace gsColorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ComponentColorModel ccm = new ComponentColorModel(gsColorSpace, true, false, Transparency.TRANSLUCENT, DataBuffer.TYPE_BYTE);
WritableRaster raster = ccm.createCompatibleWritableRaster(width, height);
bufferedImage = new BufferedImage(ccm, raster, ccm.isAlphaPremultiplied(), null);
}
bufferedImageGraphics = bufferedImage.createGraphics();
bufferedImageGraphics.setClip(graphicsValue.getClip());
return bufferedImageGraphics;
}
use of java.awt.image.ComponentColorModel in project pdfbox by apache.
the class PDColorSpace method toRGBImageAWT.
/**
* Returns the (A)RGB equivalent of the given raster, using the given AWT color space
* to perform the conversion.
* @param raster the source raster
* @param colorSpace the AWT
* @return an (A)RGB buffered image
*/
protected BufferedImage toRGBImageAWT(WritableRaster raster, ColorSpace colorSpace) {
//
// WARNING: this method is performance sensitive, modify with care!
//
// ICC Profile color transforms are only fast when performed using ColorConvertOp
ColorModel colorModel = new ComponentColorModel(colorSpace, false, false, Transparency.OPAQUE, raster.getDataBuffer().getDataType());
BufferedImage src = new BufferedImage(colorModel, raster, false, null);
BufferedImage dest = new BufferedImage(raster.getWidth(), raster.getHeight(), BufferedImage.TYPE_INT_RGB);
ColorConvertOp op = new ColorConvertOp(null);
op.filter(src, dest);
return dest;
}
use of java.awt.image.ComponentColorModel in project scriptographer by scriptographer.
the class Raster method getColorModel.
/**
* The Java2D color model of the raster.
* @jshide
*/
public ColorModel getColorModel() {
ColorType type = getType();
ColorModel cm = null;
switch(type) {
case RGB:
case ARGB:
cm = new ComponentColorModel(RGBColor.getColorSpace(), type.alpha ? new int[] { 8, 8, 8, 8 } : new int[] { 8, 8, 8 }, type.alpha, false, type.alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
break;
case CMYK:
case ACMYK:
cm = new ComponentColorModel(CMYKColor.getColorSpace(), type.alpha ? new int[] { 8, 8, 8, 8, 8 } : new int[] { 8, 8, 8, 8 }, type.alpha, false, type.alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
break;
case GRAY:
case AGRAY:
cm = new ComponentColorModel(GrayColor.getColorSpace(), type.alpha ? new int[] { 8, 8 } : new int[] { 8 }, type.alpha, false, type.alpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
break;
case BITMAP:
case ABITMAP:
// create an IndexColorModel with two colors, black and white:
// black is the transparent color in case of an alpha image
cm = new IndexColorModel(2, 2, new byte[] { 0, (byte) 255 }, new byte[] { 0, (byte) 255 }, new byte[] { 0, (byte) 255 }, type.alpha ? 0 : -1);
break;
}
return cm;
}
Aggregations