use of java.awt.image.ComponentColorModel in project imageio-ext by geosolutions-it.
the class JP2KKakaduWriteTest method test16BitGray.
public static void test16BitGray() throws IOException {
if (!isKakaduAvailable) {
LOGGER.warning("Kakadu libs not found: test are skipped ");
return;
}
final ColorSpace cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
ColorModel cm = new ComponentColorModel(cs, new int[] { 16 }, false, false, Transparency.OPAQUE, DataBuffer.TYPE_USHORT);
final int w = 512;
final int h = 512;
SampleModel sm = cm.createCompatibleSampleModel(w, h);
final int bufferSize = w * h;
final short[] bufferValues = new short[bufferSize];
for (int i = 0; i < h; i++) {
for (int j = 0; j < w; j++) // bufferValues[j + (i * h)] = (short) ((j + i) * (65536 /
// 1024));
bufferValues[j + (i * h)] = (short) (Math.random() * 65535);
}
DataBuffer imageBuffer = new DataBufferUShort(bufferValues, bufferSize);
BufferedImage bi = new BufferedImage(cm, Raster.createWritableRaster(sm, imageBuffer, null), false, null);
JP2KKakaduImageWriteParam param = new JP2KKakaduImageWriteParam();
param.setSourceSubsampling(2, 3, 0, 0);
write(outputFileName + "_gray16", bi, true, lossLessQuality);
write(outputFileName + "_gray16", bi, false, lossLessQuality);
write(outputFileName + "_gray16", bi, true, lossyQuality);
write(outputFileName + "_gray16", bi, false, lossyQuality);
write(outputFileName + "_JAI_gray16", bi, true, lossLessQuality, true);
write(outputFileName + "_JAI_gray16", bi, false, lossLessQuality, true);
write(outputFileName + "_JAI_subSampled_gray16", bi, true, lossyQuality, true, param);
write(outputFileName + "_JAI_subSampled_gray16", bi, false, lossyQuality, true, param);
LOGGER.info(writeOperations + " write operations performed");
}
use of java.awt.image.ComponentColorModel in project imageio-ext by geosolutions-it.
the class TIFFBaseJPEGCompressor method encode.
public final int encode(byte[] b, int off, int width, int height, int[] bitsPerSample, int scanlineStride) throws IOException {
if (this.JPEGWriter == null) {
throw new IIOException("JPEG writer has not been initialized!");
}
if (!((bitsPerSample.length == 3 && bitsPerSample[0] == 8 && bitsPerSample[1] == 8 && bitsPerSample[2] == 8) || (bitsPerSample.length == 1 && bitsPerSample[0] == 8))) {
throw new IIOException("Can only JPEG compress 8- and 24-bit images!");
}
// Set the stream.
ImageOutputStream ios;
// usingCodecLib && !writeAbbreviatedStream
long initialStreamPosition;
if (usingCodecLib && !writeAbbreviatedStream) {
ios = stream;
initialStreamPosition = stream.getStreamPosition();
} else {
// is using a stream on the native side which cannot be reset.
if (baos == null) {
baos = new IIOByteArrayOutputStream();
} else {
baos.reset();
}
ios = new MemoryCacheImageOutputStream(baos);
initialStreamPosition = 0L;
}
JPEGWriter.setOutput(ios);
// Create a DataBuffer.
DataBufferByte dbb;
if (off == 0 || usingCodecLib) {
dbb = new DataBufferByte(b, b.length);
} else {
//
// Workaround for bug in core Java Image I/O JPEG
// ImageWriter which cannot handle non-zero offsets.
//
int bytesPerSegment = scanlineStride * height;
byte[] btmp = new byte[bytesPerSegment];
System.arraycopy(b, off, btmp, 0, bytesPerSegment);
dbb = new DataBufferByte(btmp, bytesPerSegment);
off = 0;
}
// Set up the ColorSpace.
int[] offsets;
ColorSpace cs;
if (bitsPerSample.length == 3) {
offsets = new int[] { off, off + 1, off + 2 };
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
} else {
offsets = new int[] { off };
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
}
// Create the ColorModel.
ColorModel cm = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_BYTE);
// Create the SampleModel.
SampleModel sm = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, width, height, bitsPerSample.length, scanlineStride, offsets);
// Create the WritableRaster.
WritableRaster wras = Raster.createWritableRaster(sm, dbb, new Point(0, 0));
// Create the BufferedImage.
BufferedImage bi = new BufferedImage(cm, wras, false, null);
// Get the pruned JPEG image metadata (may be null).
IIOMetadata imageMetadata = getImageMetadata(writeAbbreviatedStream);
// Compress the image into the output stream.
int compDataLength;
if (usingCodecLib && !writeAbbreviatedStream) {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
compDataLength = (int) (stream.getStreamPosition() - initialStreamPosition);
} else {
if (writeAbbreviatedStream) {
// Write abbreviated JPEG stream
// First write the tables-only data.
JPEGWriter.prepareWriteSequence(JPEGStreamMetadata);
ios.flush();
// Rewind to the beginning of the byte array.
baos.reset();
// Write the abbreviated image data.
IIOImage image = new IIOImage(bi, null, imageMetadata);
JPEGWriter.writeToSequence(image, JPEGParam);
JPEGWriter.endWriteSequence();
} else {
// Write complete JPEG stream
JPEGWriter.write(null, new IIOImage(bi, null, imageMetadata), JPEGParam);
}
compDataLength = baos.size();
baos.writeTo(stream);
baos.reset();
}
return compDataLength;
}
use of java.awt.image.ComponentColorModel in project imageio-ext by geosolutions-it.
the class ScanlineProviderFactory method getProvider.
public static ScanlineProvider getProvider(RenderedImage image) {
ColorModel cm = image.getColorModel();
SampleModel sm = image.getSampleModel();
Raster raster;
if (image instanceof BufferedImage) {
raster = ((BufferedImage) image).getRaster();
// a copy of the raster to get a data buffer we can scroll over without issues
if (raster.getParent() != null) {
raster = image.getData(new Rectangle(0, 0, raster.getWidth(), raster.getHeight()));
}
} else {
// TODO: we could build a tile oriented reader that fetches tiles in parallel here
raster = image.getData();
}
// grab the right scanline extractor based on image features
if (cm instanceof ComponentColorModel && sm.getDataType() == DataBuffer.TYPE_BYTE) {
if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
return new RasterByteABGRProvider(raster, cm.hasAlpha());
} else if (sm.getNumBands() == 2 && cm.hasAlpha()) {
return new RasterByteGrayAlphaProvider(raster);
} else if (sm.getNumBands() == 1) {
if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
if (sm instanceof MultiPixelPackedSampleModel) {
if (cm.getPixelSize() == 8) {
return new RasterByteSingleBandProvider(raster, 8, raster.getWidth());
} else if (cm.getPixelSize() == 4) {
int scanlineLength = (raster.getWidth() + 1) / 2;
return new RasterByteSingleBandProvider(raster, 4, scanlineLength);
} else if (cm.getPixelSize() == 2) {
int scanlineLength = (raster.getWidth() + 2) / 4;
return new RasterByteSingleBandProvider(raster, 2, scanlineLength);
} else if (cm.getPixelSize() == 1) {
int scanlineLength = (raster.getWidth() + 4) / 8;
return new RasterByteSingleBandProvider(raster, 1, scanlineLength);
}
} else {
if (cm.getPixelSize() == 8) {
return new RasterByteSingleBandProvider(raster, 8, raster.getWidth());
} else if (cm.getPixelSize() == 4) {
int scanlineLength = (raster.getWidth() + 1) / 2;
return new RasterByteRepackSingleBandProvider(raster, 4, scanlineLength);
} else if (cm.getPixelSize() == 2) {
int scanlineLength = (raster.getWidth() + 2) / 4;
return new RasterByteRepackSingleBandProvider(raster, 2, scanlineLength);
} else if (cm.getPixelSize() == 1) {
int scanlineLength = (raster.getWidth() + 4) / 8;
return new RasterByteRepackSingleBandProvider(raster, 1, scanlineLength);
}
}
}
}
} else if (cm instanceof ComponentColorModel && sm.getDataType() == DataBuffer.TYPE_USHORT) {
if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
return new RasterShortABGRProvider(raster, cm.hasAlpha());
} else if (sm.getNumBands() == 2 && cm.hasAlpha()) {
return new RasterShortGrayAlphaProvider(raster);
} else if (sm.getNumBands() == 1) {
return new RasterShortSingleBandProvider(raster);
}
} else if (cm instanceof DirectColorModel && sm.getDataType() == DataBuffer.TYPE_INT) {
if (sm.getNumBands() == 3 || sm.getNumBands() == 4) {
return new RasterIntABGRProvider(raster, cm.hasAlpha());
}
} else if (cm instanceof IndexColorModel) {
IndexColorModel icm = (IndexColorModel) cm;
int pixelSize = icm.getPixelSize();
// re-align to powers of two
if ((pixelSize & (pixelSize - 1)) != 0) {
int nextPower = (int) (Math.floor(Math.log(pixelSize) / Math.log(2)) + 1);
pixelSize = (int) Math.pow(2, nextPower);
}
if (sm.getDataType() == DataBuffer.TYPE_BYTE) {
if (sm instanceof MultiPixelPackedSampleModel) {
if (pixelSize == 8) {
return new RasterByteSingleBandProvider(raster, 8, raster.getWidth(), icm);
} else if (pixelSize == 4) {
int scanlineLength = (raster.getWidth() + 1) / 2;
return new RasterByteSingleBandProvider(raster, 4, scanlineLength, icm);
} else if (pixelSize == 2) {
int scanlineLength = (raster.getWidth() + 2) / 4;
return new RasterByteSingleBandProvider(raster, 2, scanlineLength, icm);
} else if (pixelSize == 1) {
int scanlineLength = (raster.getWidth() + 4) / 8;
return new RasterByteSingleBandProvider(raster, 1, scanlineLength, icm);
}
} else {
if (pixelSize == 8) {
return new RasterByteSingleBandProvider(raster, 8, raster.getWidth(), icm);
} else if (pixelSize == 4) {
int scanlineLength = (raster.getWidth() + 1) / 2;
return new RasterByteRepackSingleBandProvider(raster, 4, scanlineLength, icm);
} else if (pixelSize == 2) {
int scanlineLength = (raster.getWidth() + 2) / 4;
return new RasterByteRepackSingleBandProvider(raster, 2, scanlineLength, icm);
} else if (pixelSize == 1) {
int scanlineLength = (raster.getWidth() + 4) / 8;
return new RasterByteRepackSingleBandProvider(raster, 1, scanlineLength, icm);
}
}
} else if (sm.getDataType() == DataBuffer.TYPE_USHORT) {
if (sm instanceof MultiPixelPackedSampleModel) {
if (pixelSize == 16) {
int scanlineLength = raster.getWidth() * 2;
return new RasterShortSingleBandProvider(raster, 16, scanlineLength, icm);
} else if (pixelSize == 8) {
int scanlineLength = raster.getWidth() + ((raster.getWidth() % 2 == 0) ? 0 : 1);
return new RasterShortSingleBandProvider(raster, 8, scanlineLength, icm);
} else if (pixelSize == 4) {
int scanlineLength = (raster.getWidth() + 1) / 2;
return new RasterShortSingleBandProvider(raster, 4, scanlineLength, icm);
} else if (pixelSize == 2) {
int scanlineLength = (raster.getWidth() + 2) / 4;
return new RasterShortSingleBandProvider(raster, 2, scanlineLength, icm);
} else if (pixelSize == 1) {
int scanlineLength = (raster.getWidth() + 4) / 8;
return new RasterShortSingleBandProvider(raster, 1, scanlineLength, icm);
}
}
}
}
return null;
}
use of java.awt.image.ComponentColorModel in project imageio-ext by geosolutions-it.
the class JP2KKakaduImageReader method parseBoxes.
/**
* Get basic image properties by querying several JP2Boxes. Then, properly
* set the ColorModel of the input object.
*
* @param codestreamP
*/
private void parseBoxes(JP2KCodestreamProperties codestreamP) {
if (isRawSource)
return;
short numComp = 1;
byte[] bitDepths = null;
byte[] maps = null;
int bitDepth = -1;
ICC_Profile profile = null;
int colorSpaceType = -1;
// //
//
// ImageHeader Box
//
// //
final ImageHeaderBox ihBox = (ImageHeaderBox) getJp2Box(ImageHeaderBox.BOX_TYPE);
if (ihBox != null) {
numComp = ihBox.getNumComponents();
bitDepth = ihBox.getBitDepth();
}
// //
//
// ColorSpecification Box
//
// //
final ColorSpecificationBox csBox = (ColorSpecificationBox) getJp2Box(ColorSpecificationBox.BOX_TYPE);
if (csBox != null) {
profile = csBox.getICCProfile();
colorSpaceType = csBox.getEnumeratedColorSpace();
}
// //
//
// ComponentMapping Box
//
// //
final ComponentMappingBox cmBox = (ComponentMappingBox) getJp2Box(ComponentMappingBox.BOX_TYPE);
if (cmBox != null) {
maps = cmBox.getComponentAssociation();
}
// //
//
// Palette Box
//
// //
final PaletteBox palBox = (PaletteBox) getJp2Box(PaletteBox.BOX_TYPE);
if (palBox != null) {
byte[][] lookUpTable = palBox.getLUT();
if (lookUpTable != null && numComp == 1) {
int tableComps = lookUpTable.length;
int maxDepth = 1 + (bitDepth & 0x7F);
if (maps == null) {
maps = new byte[tableComps];
for (int i = 0; i < tableComps; i++) maps[i] = (byte) i;
}
if (tableComps == 3) {
codestreamP.setColorModel(new IndexColorModel(maxDepth, lookUpTable[0].length, lookUpTable[maps[0]], lookUpTable[maps[1]], lookUpTable[maps[2]]));
return;
} else if (tableComps == 4) {
codestreamP.setColorModel(new IndexColorModel(maxDepth, lookUpTable[0].length, lookUpTable[maps[0]], lookUpTable[maps[1]], lookUpTable[maps[2]], lookUpTable[maps[3]]));
return;
}
}
}
// //
//
// BitsPerComponent Box
//
// //
final BitsPerComponentBox bpcBox = (BitsPerComponentBox) getJp2Box(BitsPerComponentBox.BOX_TYPE);
if (bpcBox != null) {
bitDepths = bpcBox.getBitDepth();
}
// //
//
// ChannelDefinition Box
//
// //
final ChannelDefinitionBox chBox = (ChannelDefinitionBox) getJp2Box(ChannelDefinitionBox.BOX_TYPE);
if (chBox != null) {
final short[] channels = chBox.getChannel();
final short[] associations = chBox.getAssociation();
final int[] cType = chBox.getTypes();
boolean hasAlpha = false;
final int alphaChannel = numComp - 1;
for (int i = 0; i < channels.length; i++) {
if (cType[i] == 1 && channels[i] == alphaChannel)
hasAlpha = true;
}
boolean[] isPremultiplied = new boolean[] { false };
if (hasAlpha) {
isPremultiplied = new boolean[alphaChannel];
for (int i = 0; i < alphaChannel; i++) isPremultiplied[i] = false;
for (int i = 0; i < channels.length; i++) {
if (cType[i] == 2)
isPremultiplied[associations[i] - 1] = true;
}
for (int i = 1; i < alphaChannel; i++) isPremultiplied[0] &= isPremultiplied[i];
}
ColorSpace cs = null;
// RGBN Workaround
if (associations.length == 4) /* && associations[0]==1 && associations[1]==2 && associations[2]==3*/
{
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
hasAlpha = true;
} else if (profile != null)
cs = new ICC_ColorSpace(profile);
else if (colorSpaceType == ColorSpecificationBox.ECS_sRGB)
cs = ColorSpace.getInstance(ColorSpace.CS_sRGB);
else if (colorSpaceType == ColorSpecificationBox.ECS_GRAY)
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
else if (colorSpaceType == ColorSpecificationBox.ECS_YCC)
cs = ColorSpace.getInstance(ColorSpace.CS_PYCC);
else
LOGGER.warning("JP2 type only handle sRGB, GRAY and YCC Profiles");
// TODO: Check these settings
int[] bits = new int[numComp];
for (int i = 0; i < numComp; i++) if (bitDepths != null)
bits[i] = (bitDepths[i] & 0x7F) + 1;
else
bits[i] = (bitDepth & 0x7F) + 1;
int maxBitDepth = 1 + (bitDepth & 0x7F);
boolean isSigned = (bitDepth & 0x80) == 0x80;
if (bitDepths != null)
isSigned = (bitDepths[0] & 0x80) == 0x80;
if (bitDepths != null)
for (int i = 0; i < numComp; i++) if (bits[i] > maxBitDepth)
maxBitDepth = bits[i];
int type = -1;
if (maxBitDepth <= 8)
type = DataBuffer.TYPE_BYTE;
else if (maxBitDepth <= 16)
type = isSigned ? DataBuffer.TYPE_SHORT : DataBuffer.TYPE_USHORT;
else if (maxBitDepth <= 32)
type = DataBuffer.TYPE_INT;
if (type == -1)
return;
if (cs != null) {
codestreamP.setColorModel(new ComponentColorModel(cs, bits, hasAlpha, isPremultiplied[0], hasAlpha ? Transparency.TRANSLUCENT : Transparency.OPAQUE, type));
}
}
}
use of java.awt.image.ComponentColorModel in project imageio-ext by geosolutions-it.
the class GDALUtilities method buildColorModel.
/**
* Builds a proper <code>ColorModel</code> for a specified
* <code>SampleModel</code>
*
* @param sampleModel
* the sampleModel to be used as reference.
* @return a proper <code>ColorModel</code> for the input
* <code>SampleModel</code>
*/
public static ColorModel buildColorModel(final SampleModel sampleModel) {
ColorSpace cs = null;
ColorModel colorModel = null;
final int buffer_type = sampleModel.getDataType();
final int numBands = sampleModel.getNumBands();
if (numBands > 1) {
//
// Number of Bands > 1.
// ImageUtil.createColorModel provides to Creates a
// ColorModel that may be used with the specified
// SampleModel
//
colorModel = ImageUtil.createColorModel(sampleModel);
if (colorModel == null) {
LOGGER.severe("No ColorModels found");
}
} else if ((buffer_type == DataBuffer.TYPE_BYTE) || (buffer_type == DataBuffer.TYPE_USHORT) || (buffer_type == DataBuffer.TYPE_INT) || (buffer_type == DataBuffer.TYPE_FLOAT) || (buffer_type == DataBuffer.TYPE_DOUBLE)) {
// Just one band. Using the built-in Gray Scale Color Space
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
colorModel = // dataType
RasterFactory.createComponentColorModel(// dataType
buffer_type, // color space
cs, // has alpha
false, // is alphaPremultiplied
false, // transparency
Transparency.OPAQUE);
} else {
if (buffer_type == DataBuffer.TYPE_SHORT) {
// Just one band. Using the built-in Gray Scale Color
// Space
cs = ColorSpace.getInstance(ColorSpace.CS_GRAY);
colorModel = new ComponentColorModel(cs, false, false, Transparency.OPAQUE, DataBuffer.TYPE_SHORT);
}
}
return colorModel;
}
Aggregations