use of java.awt.image.ColorModel in project bioformats by openmicroscopy.
the class ImagePlusReader method createVirtualStack.
private ImageStack createVirtualStack(ImportProcess process, int s, List<LUT> luts) throws FormatException, IOException {
final ImporterOptions options = process.getOptions();
final ImageProcessorReader reader = process.getReader();
reader.setSeries(s);
final int zCount = process.getZCount(s);
final int cCount = process.getCCount(s);
final int tCount = process.getTCount(s);
final IMetadata meta = process.getOMEMetadata();
final int imageCount = reader.getImageCount();
// CTR FIXME: Make virtual stack work with different color modes?
final BFVirtualStack virtualStack = new BFVirtualStack(options.getId(), reader, false, false, false);
for (int i = 0; i < imageCount; i++) {
String label = constructSliceLabel(i, reader, meta, s, zCount, cCount, tCount);
virtualStack.addSlice(label);
}
if (luts != null) {
for (int c = 0; c < cCount; c++) {
int index = reader.getIndex(0, c, 0);
ImageProcessor ip = reader.openProcessors(index)[0];
final ColorModel cm = ip.getColorModel();
final LUT lut = cm instanceof LUT ? (LUT) cm : null;
luts.add(lut);
}
}
return virtualStack;
}
use of java.awt.image.ColorModel in project bioformats by openmicroscopy.
the class AWTImageTools method convertRenderedImage.
// -- Image conversion --
// NB: The commented out makeType method below is broken in that it results
// in rescaled data in some circumstances. We were using it for getBytes and
// getShorts, but due to this problem we implemented a different solution
// using Raster.getPixels instead. But we have left the makeType method here
// in case we decide to explore this issue any further in the future.
// /** Copies the given image into a result with the specified data type. */
// public static BufferedImage makeType(BufferedImage image, int type) {
// WritableRaster r = image.getRaster();
// int w = image.getWidth(), h = image.getHeight(), c = r.getNumBands();
// ColorModel colorModel = makeColorModel(c, type);
// if (colorModel == null) return null;
//
// int s = w * h;
// DataBuffer buf = null;
// if (type == DataBuffer.TYPE_BYTE) buf = new DataBufferByte(s, c);
// else if (type == DataBuffer.TYPE_USHORT) buf = new DataBufferUShort(s, c);
// else if (type == DataBuffer.TYPE_INT) buf = new DataBufferInt(s, c);
// else if (type == DataBuffer.TYPE_SHORT) buf = new DataBufferShort(s, c);
// else if (type == DataBuffer.TYPE_FLOAT) buf = new DataBufferFloat(s, c);
// else if (type == DataBuffer.TYPE_DOUBLE) buf = new DataBufferDouble(s, c);
// if (buf == null) return null;
//
// SampleModel model = new BandedSampleModel(type, w, h, c);
// WritableRaster raster = Raster.createWritableRaster(model, buf, null);
// BufferedImage target = new BufferedImage(colorModel, raster, false, null);
// Graphics2D g2 = target.createGraphics();
// g2.drawRenderedImage(image, null);
// g2.dispose();
// return target;
// }
/**
* Converts a java.awt.image.RenderedImage into a
* java.awt.image.BufferedImage.
*
* This code was adapted from
* <a href="http://www.jguru.com/faq/view.jsp?EID=114602">a jGuru post</a>.
*/
public static BufferedImage convertRenderedImage(RenderedImage img) {
if (img instanceof BufferedImage)
return (BufferedImage) img;
ColorModel cm = img.getColorModel();
int width = img.getWidth();
int height = img.getHeight();
WritableRaster raster = cm.createCompatibleWritableRaster(width, height);
boolean isAlphaPremultiplied = cm.isAlphaPremultiplied();
Hashtable<String, Object> properties = new Hashtable<String, Object>();
String[] keys = img.getPropertyNames();
if (keys != null) {
for (int i = 0; i < keys.length; i++) {
properties.put(keys[i], img.getProperty(keys[i]));
}
}
BufferedImage result = new BufferedImage(cm, raster, isAlphaPremultiplied, properties);
img.copyData(raster);
return result;
}
use of java.awt.image.ColorModel in project bioformats by openmicroscopy.
the class AWTImageTools method openImage.
/**
* Creates an image from the given byte array, using the given
* IFormatReader to retrieve additional information.
* The floating point normalization setting is specified by 'normal',
* which allows the reader's normalization setting to be overridden.
*/
public static BufferedImage openImage(byte[] buf, IFormatReader r, int w, int h, boolean normal) throws FormatException, IOException {
int pixelType = r.getPixelType();
boolean little = r.isLittleEndian();
int rgbChanCount = r.getRGBChannelCount();
boolean interleaved = r.isInterleaved();
boolean indexed = r.isIndexed();
if (pixelType == FormatTools.FLOAT) {
float[] f = (float[]) DataTools.makeDataArray(buf, 4, true, little);
if (normal)
f = DataTools.normalizeFloats(f);
return makeImage(f, w, h, rgbChanCount, interleaved);
} else if (pixelType == FormatTools.DOUBLE) {
double[] d = (double[]) DataTools.makeDataArray(buf, 8, true, little);
if (normal)
d = DataTools.normalizeDoubles(d);
return makeImage(d, w, h, rgbChanCount, interleaved);
}
boolean signed = FormatTools.isSigned(pixelType);
ColorModel model = null;
if (signed) {
if (pixelType == FormatTools.INT8) {
model = new SignedColorModel(8, DataBuffer.TYPE_BYTE, rgbChanCount);
} else if (pixelType == FormatTools.INT16) {
model = new SignedColorModel(16, DataBuffer.TYPE_SHORT, rgbChanCount);
} else if (pixelType == FormatTools.INT32) {
model = new SignedColorModel(32, DataBuffer.TYPE_INT, rgbChanCount);
}
}
int bpp = FormatTools.getBytesPerPixel(pixelType);
BufferedImage b = makeImage(buf, w, h, rgbChanCount, interleaved, bpp, false, little, signed);
if (b == null) {
throw new FormatException("Could not construct BufferedImage");
}
if (indexed && rgbChanCount == 1) {
if (pixelType == FormatTools.UINT8 || pixelType == FormatTools.INT8) {
byte[][] table = r.get8BitLookupTable();
if (table != null && table.length > 0 && table[0] != null) {
int len = table[0].length;
byte[] dummy = table.length < 3 ? new byte[len] : null;
byte[] red = table.length >= 1 ? table[0] : dummy;
byte[] green = table.length >= 2 ? table[1] : dummy;
byte[] blue = table.length >= 3 ? table[2] : dummy;
model = new IndexColorModel(8, len, red, green, blue);
}
} else if (pixelType == FormatTools.UINT16 || pixelType == FormatTools.INT16) {
short[][] table = r.get16BitLookupTable();
if (table != null && table.length > 0 && table[0] != null) {
model = new Index16ColorModel(16, table[0].length, table, r.isLittleEndian());
}
}
}
if (model != null) {
WritableRaster raster = Raster.createWritableRaster(b.getSampleModel(), b.getRaster().getDataBuffer(), null);
b = new BufferedImage(model, raster, false, null);
}
return b;
}
use of java.awt.image.ColorModel in project bioformats by openmicroscopy.
the class AWTImageTools method scale.
/**
* Scales the image using the most appropriate API, with the resultant image
* having the same color model as the original image.
*/
public static BufferedImage scale(BufferedImage source, int width, int height, boolean pad) {
int w = source.getWidth();
int h = source.getHeight();
if (w == width && h == height)
return source;
int finalWidth = width, finalHeight = height;
if (pad) {
// keep aspect ratio the same
double r = (double) w / h;
double ratio = (double) width / height;
if (r > ratio) {
// bounded by width; adjust height
height = (h * width) / w;
} else {
// bounded by height; adjust width
width = (w * height) / h;
}
}
int pixelType = getPixelType(source);
BufferedImage result = null;
ColorModel sourceModel = source.getColorModel();
if ((sourceModel instanceof Index16ColorModel) || (sourceModel instanceof IndexColorModel) || (sourceModel instanceof SignedColorModel) || FormatTools.isFloatingPoint(pixelType)) {
DataBuffer buffer = source.getData().getDataBuffer();
WritableRaster raster = Raster.createWritableRaster(source.getSampleModel(), buffer, null);
ColorModel model = makeColorModel(1, buffer.getDataType());
if (sourceModel instanceof SignedColorModel) {
model = sourceModel;
}
source = new BufferedImage(model, raster, false, null);
Image scaled = scaleAWT(source, width, height, Image.SCALE_AREA_AVERAGING);
result = makeBuffered(scaled, sourceModel);
raster = Raster.createWritableRaster(result.getSampleModel(), result.getData().getDataBuffer(), null);
result = new BufferedImage(sourceModel, raster, false, null);
} else {
if (FormatTools.isSigned(pixelType)) {
source = makeUnsigned(source);
sourceModel = null;
}
Image scaled = scaleAWT(source, width, height, Image.SCALE_AREA_AVERAGING);
result = makeBuffered(scaled, sourceModel);
}
return padImage(result, finalWidth, finalHeight);
}
use of java.awt.image.ColorModel in project whole by wholeplatform.
the class ICNSData method convertToRaw.
protected byte[] convertToRaw(ICNSType iconType, RenderedImage image) {
int width = image.getWidth();
int height = image.getHeight();
Components reds = new Components();
Components greens = new Components();
Components blues = new Components();
Raster raster = image.getData();
ColorModel colorModel = image.getColorModel();
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int rgb = colorModel.getRGB(raster.getDataElements(x, y, null));
reds.add((byte) ((rgb >> 16) & 0xFF));
greens.add((byte) ((rgb >> 8) & 0xFF));
blues.add((byte) (rgb & 0xFF));
}
}
byte[] redArray = reds.toByteArray();
byte[] greenArray = greens.toByteArray();
byte[] blueArray = blues.toByteArray();
int offset = iconType == ICNSType.IT32 ? 4 : 0;
byte[] result = new byte[offset + redArray.length + greenArray.length + blueArray.length];
System.arraycopy(redArray, 0, result, offset, redArray.length);
System.arraycopy(greenArray, 0, result, offset + redArray.length, greenArray.length);
System.arraycopy(blueArray, 0, result, offset + redArray.length + greenArray.length, blueArray.length);
return result;
}
Aggregations