use of java.awt.image.PixelInterleavedSampleModel in project jdk8u_jdk by JetBrains.
the class GIFImageReader method createIndexed.
// We don't check all parameters as ImageTypeSpecifier.createIndexed do
// since this method is private and we pass consistent data here
private ImageTypeSpecifier createIndexed(byte[] r, byte[] g, byte[] b, int bits) {
ColorModel colorModel;
if (imageMetadata.transparentColorFlag) {
// Some files erroneously have a transparent color index
// of 255 even though there are fewer than 256 colors.
int idx = Math.min(imageMetadata.transparentColorIndex, r.length - 1);
colorModel = new IndexColorModel(bits, r.length, r, g, b, idx);
} else {
colorModel = new IndexColorModel(bits, r.length, r, g, b);
}
SampleModel sampleModel;
if (bits == 8) {
int[] bandOffsets = { 0 };
sampleModel = new PixelInterleavedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, 1, 1, bandOffsets);
} else {
sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, 1, 1, bits);
}
return new ImageTypeSpecifier(colorModel, sampleModel);
}
use of java.awt.image.PixelInterleavedSampleModel in project Purus-Pasta by puruscor.
the class TexI method detectfmt.
/* Java's image model is a little bit complex, so these may not be
* entirely correct. They should be corrected if oddities are
* detected. */
public static int detectfmt(BufferedImage img) {
ColorModel cm = img.getColorModel();
if (!(img.getSampleModel() instanceof PixelInterleavedSampleModel))
return (-1);
PixelInterleavedSampleModel sm = (PixelInterleavedSampleModel) img.getSampleModel();
int[] cs = cm.getComponentSize();
int[] off = sm.getBandOffsets();
/*
System.err.print(this + ": " + cm.getNumComponents() + ", (");
for(int i = 0; i < off.length; i++)
System.err.print(((i > 0)?" ":"") + off[i]);
System.err.print("), (");
for(int i = 0; i < off.length; i++)
System.err.print(((i > 0)?" ":"") + cs[i]);
System.err.print(")");
System.err.println();
*/
if ((cm.getNumComponents() == 4) && (off.length == 4)) {
if (((cs[0] == 8) && (cs[1] == 8) && (cs[2] == 8) && (cs[3] == 8)) && (cm.getTransferType() == DataBuffer.TYPE_BYTE) && (cm.getTransparency() == java.awt.Transparency.TRANSLUCENT)) {
if ((off[0] == 0) && (off[1] == 1) && (off[2] == 2) && (off[3] == 3))
return (GL.GL_RGBA);
if ((off[0] == 2) && (off[1] == 1) && (off[2] == 0) && (off[3] == 3))
return (GL.GL_BGRA);
}
} else if ((cm.getNumComponents() == 3) && (off.length == 3)) {
if (((cs[0] == 8) && (cs[1] == 8) && (cs[2] == 8)) && (cm.getTransferType() == DataBuffer.TYPE_BYTE) && (cm.getTransparency() == java.awt.Transparency.OPAQUE)) {
if ((off[0] == 0) && (off[1] == 1) && (off[2] == 2))
return (GL.GL_RGB);
if ((off[0] == 2) && (off[1] == 1) && (off[2] == 0))
return (GL2.GL_BGR);
}
}
return (-1);
}
use of java.awt.image.PixelInterleavedSampleModel in project imageio-ext by geosolutions-it.
the class PNMImageReader method getImageTypes.
public Iterator getImageTypes(int imageIndex) throws IOException {
checkIndex(imageIndex);
readHeader();
int tmp = (variant - '1') % 3;
ArrayList list = new ArrayList(1);
int dataType = DataBuffer.TYPE_INT;
// Determine data type based on maxValue.
if (maxValue < 0x100) {
dataType = DataBuffer.TYPE_BYTE;
} else if (maxValue < 0x10000) {
dataType = DataBuffer.TYPE_USHORT;
}
// Choose an appropriate SampleModel.
SampleModel sampleModel = null;
ColorModel colorModel = null;
if ((variant == PBM_ASCII) || (variant == PBM_RAW)) {
// Each pixel takes 1 bit, pack 8 pixels into a byte.
sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height, 1);
byte[] color = { (byte) 0xFF, (byte) 0 };
colorModel = new IndexColorModel(1, 2, color, color, color);
} else {
sampleModel = new PixelInterleavedSampleModel(dataType, width, height, tmp == 1 ? 1 : 3, width * (tmp == 1 ? 1 : 3), tmp == 1 ? new int[] { 0 } : new int[] { 0, 1, 2 });
colorModel = ImageUtil.createColorModel(null, sampleModel);
}
list.add(new ImageTypeSpecifier(colorModel, sampleModel));
return list.iterator();
}
use of java.awt.image.PixelInterleavedSampleModel in project imageio-ext by geosolutions-it.
the class PNMImageReader method read.
public BufferedImage read(int imageIndex, ImageReadParam param) throws IOException {
checkIndex(imageIndex);
clearAbortRequest();
processImageStarted(imageIndex);
if (param == null)
param = getDefaultReadParam();
// read header
readHeader();
Rectangle sourceRegion = new Rectangle(0, 0, 0, 0);
Rectangle destinationRegion = new Rectangle(0, 0, 0, 0);
computeRegions(param, this.width, this.height, param.getDestination(), sourceRegion, destinationRegion);
int scaleX = param.getSourceXSubsampling();
int scaleY = param.getSourceYSubsampling();
// If the destination band is set used it
int[] sourceBands = param.getSourceBands();
int[] destBands = param.getDestinationBands();
boolean seleBand = (sourceBands != null) && (destBands != null);
boolean noTransform = destinationRegion.equals(new Rectangle(0, 0, width, height)) || seleBand;
// base the maxValue on variant.
if (isRaw(variant) && maxValue >= 0x100) {
maxValue = 0xFF;
}
int numBands = 1;
// bitmap (PBM) and greymap (PGM) are 1 band.
if (variant == PPM_ASCII || variant == PPM_RAW) {
numBands = 3;
}
if (!seleBand) {
sourceBands = new int[numBands];
destBands = new int[numBands];
for (int i = 0; i < numBands; i++) destBands[i] = sourceBands[i] = i;
}
int dataType = DataBuffer.TYPE_INT;
// Determine data type based on maxValue.
if (maxValue < 0x100) {
dataType = DataBuffer.TYPE_BYTE;
} else if (maxValue < 0x10000) {
dataType = DataBuffer.TYPE_USHORT;
}
// Choose an appropriate SampleModel.
SampleModel sampleModel = null;
ColorModel colorModel = null;
if ((variant == PBM_ASCII) || (variant == PBM_RAW)) {
// Each pixel takes 1 bit, pack 8 pixels into a byte.
sampleModel = new MultiPixelPackedSampleModel(DataBuffer.TYPE_BYTE, destinationRegion.width, destinationRegion.height, 1);
byte[] color = { (byte) 0xFF, (byte) 0 };
colorModel = new IndexColorModel(1, 2, color, color, color);
} else {
sampleModel = new PixelInterleavedSampleModel(dataType, destinationRegion.width, destinationRegion.height, sourceBands.length, destinationRegion.width * sourceBands.length, destBands);
colorModel = ImageUtil.createColorModel(null, sampleModel);
}
// If the destination is provided, then use it. Otherwise, create new
// one
BufferedImage bi = param.getDestination();
// Get the image data.
WritableRaster raster = null;
if (bi == null) {
sampleModel = sampleModel.createCompatibleSampleModel(destinationRegion.x + destinationRegion.width, destinationRegion.y + destinationRegion.height);
if (seleBand)
sampleModel = sampleModel.createSubsetSampleModel(sourceBands);
raster = Raster.createWritableRaster(sampleModel, new Point());
bi = new BufferedImage(colorModel, raster, false, null);
} else {
raster = bi.getWritableTile(0, 0);
sampleModel = bi.getSampleModel();
colorModel = bi.getColorModel();
noTransform &= destinationRegion.equals(raster.getBounds());
}
final int destinationRegionHeight = destinationRegion.height;
final int destinationRegionWidth = destinationRegion.width;
final int numSourceBands = sourceBands.length;
int j = 0;
int k = 0;
int i = 0;
switch(variant) {
case PBM_RAW:
{
// SampleModel for these cases should be MultiPixelPacked.
DataBuffer dataBuffer = raster.getDataBuffer();
// Read the entire image.
byte[] buf = ((DataBufferByte) dataBuffer).getData();
if (noTransform) {
iis.readFully(buf, 0, buf.length);
processImageUpdate(bi, 0, 0, width, height, 1, 1, destBands);
processImageProgress(100.0F);
} else if (scaleX == 1 && sourceRegion.x % 8 == 0) {
int skip = sourceRegion.x >> 3;
int originalLS = width + 7 >> 3;
int destLS = raster.getWidth() + 7 >> 3;
int readLength = sourceRegion.width + 7 >> 3;
int offset = sourceRegion.y * originalLS;
iis.skipBytes(offset + skip);
offset = originalLS * (scaleY - 1) + originalLS - readLength;
byte[] lineData = new byte[readLength];
int bitoff = destinationRegion.x & 7;
boolean reformat = !(bitoff == 0);
for (i = 0, j = 0, k = destinationRegion.y * destLS + (destinationRegion.x >> 3); i < destinationRegionHeight; i++, j += scaleY) {
if (reformat) {
iis.read(lineData, 0, readLength);
int mask1 = (255 << bitoff) & 255;
int mask2 = ~mask1 & 255;
int shift = 8 - bitoff;
int n = 0;
int m = k;
for (; n < readLength - 1; n++, m++) buf[m] = (byte) (((lineData[n] & mask2) << shift) | (lineData[n + 1] & mask1) >> bitoff);
buf[m] = (byte) ((lineData[n] & mask2) << shift);
} else {
iis.read(buf, k, readLength);
}
iis.skipBytes(offset);
k += destLS;
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
} else {
int originalLS = width + 7 >> 3;
byte[] data = new byte[originalLS];
iis.skipBytes(sourceRegion.y * originalLS);
int destLS = bi.getWidth() + 7 >> 3;
int offset = originalLS * (scaleY - 1);
int dsx = destLS * destinationRegion.y + (destinationRegion.x >> 3);
int n = dsx;
for (i = 0, j = 0; i < destinationRegionHeight; i++, j += scaleY) {
iis.read(data, 0, originalLS);
iis.skipBytes(offset);
int b = 0;
int pos = 7 - (destinationRegion.x & 7);
for (int m = sourceRegion.x; m < sourceRegion.x + sourceRegion.width; m += scaleX) {
b |= (data[m >> 3] >> (7 - (m & 7)) & 1) << pos;
pos--;
if (pos == -1) {
buf[n++] = (byte) b;
b = 0;
pos = 7;
}
}
if (pos != 7)
buf[n++] = (byte) b;
n += destinationRegion.x >> 3;
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
}
break;
}
case PBM_ASCII:
{
DataBuffer dataBuffer = raster.getDataBuffer();
byte[] buf = ((DataBufferByte) dataBuffer).getData();
i = 0;
if (noTransform)
for (int n = 0; i < height; i++) {
int b = 0;
int pos = 7;
for (j = 0; j < width; j++) {
b |= (readInteger(iis) & 1) << pos;
pos--;
if (pos == -1) {
buf[n++] = (byte) b;
b = 0;
pos = 7;
}
}
if (pos != 7)
buf[n++] = (byte) b;
processImageUpdate(bi, 0, i, width, 1, 1, 1, destBands);
processImageProgress(100.0F * i / height);
}
else {
skipInteger(iis, sourceRegion.y * width + sourceRegion.x);
int skipX = scaleX - 1;
int skipY = (scaleY - 1) * width + width - destinationRegionWidth * scaleX;
int dsx = (bi.getWidth() + 7 >> 3) * destinationRegion.y + (destinationRegion.x >> 3);
i = 0;
for (int n = dsx; i < destinationRegionHeight; i++) {
int b = 0;
int pos = 7 - (destinationRegion.x & 7);
for (j = 0; j < destinationRegionWidth; j++) {
b |= (readInteger(iis) & 1) << pos;
pos--;
if (pos == -1) {
buf[n++] = (byte) b;
b = 0;
pos = 7;
}
skipInteger(iis, skipX);
}
if (pos != 7)
buf[n++] = (byte) b;
n += destinationRegion.x >> 3;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
}
break;
}
case PGM_ASCII:
case PGM_RAW:
case PPM_ASCII:
case PPM_RAW:
// SampleModel for these cases should be PixelInterleaved.
int skipX = (scaleX - 1) * numBands;
int skipY = (scaleY * width - destinationRegion.width * scaleX) * numBands;
int dsx = (bi.getWidth() * destinationRegion.y + destinationRegion.x) * numBands;
switch(dataType) {
case DataBuffer.TYPE_BYTE:
DataBufferByte bbuf = (DataBufferByte) raster.getDataBuffer();
byte[] byteArray = bbuf.getData();
if (isRaw(variant)) {
if (noTransform) {
iis.readFully(byteArray);
processImageUpdate(bi, 0, 0, width, height, 1, 1, destBands);
processImageProgress(100.0F);
} else {
iis.skipBytes(sourceRegion.y * width * numBands);
int skip = (scaleY - 1) * width * numBands;
byte[] data = new byte[width * numBands];
int pixelStride = scaleX * numBands;
int sx = sourceRegion.x * numBands;
i = 0;
for (int n = dsx; i < destinationRegionHeight; i++) {
iis.read(data);
for (j = sourceRegion.x, k = sx; j < sourceRegion.x + sourceRegion.width; j += scaleX, k += pixelStride) {
for (int m = 0; m < numSourceBands; m++) byteArray[n + destBands[m]] = data[k + sourceBands[m]];
n += numSourceBands;
}
n += destinationRegion.x * numBands;
iis.skipBytes(skip);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
}
} else {
skipInteger(iis, (sourceRegion.y * width + sourceRegion.x) * numBands);
i = 0;
if (seleBand) {
byte[] data = new byte[numBands];
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numBands; k++) data[k] = (byte) readInteger(iis);
for (k = 0; k < numSourceBands; k++) byteArray[n + destBands[k]] = data[sourceBands[k]];
n += numSourceBands;
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
} else
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numBands; k++) byteArray[n++] = (byte) readInteger(iis);
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
}
break;
case DataBuffer.TYPE_USHORT:
DataBufferUShort sbuf = (DataBufferUShort) raster.getDataBuffer();
short[] shortArray = sbuf.getData();
skipInteger(iis, sourceRegion.y * width * numBands + sourceRegion.x);
i = 0;
if (seleBand) {
short[] data = new short[numBands];
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numBands; k++) data[k] = (short) readInteger(iis);
for (k = 0; k < numSourceBands; k++) shortArray[n + destBands[k]] = data[sourceBands[k]];
n += numSourceBands;
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
} else
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numBands; k++) shortArray[n++] = (short) readInteger(iis);
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
break;
case DataBuffer.TYPE_INT:
DataBufferInt ibuf = (DataBufferInt) raster.getDataBuffer();
int[] intArray = ibuf.getData();
skipInteger(iis, sourceRegion.y * width * numBands + sourceRegion.x);
i = 0;
if (seleBand) {
int[] data = new int[numBands];
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numBands; k++) data[k] = readInteger(iis);
for (k = 0; k < numSourceBands; k++) intArray[n + destBands[k]] = data[sourceBands[k]];
n += numSourceBands;
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
} else {
for (int n = dsx; i < destinationRegionHeight; i++) {
for (j = 0; j < destinationRegionWidth; j++) {
for (k = 0; k < numSourceBands; k++) intArray[n++] = readInteger(iis);
skipInteger(iis, skipX);
}
n += destinationRegion.x * numSourceBands;
skipInteger(iis, skipY);
processImageUpdate(bi, 0, i, destinationRegionWidth, 1, 1, 1, destBands);
processImageProgress(100.0F * i / destinationRegionHeight);
}
}
break;
}
break;
}
if (abortRequested())
processReadAborted();
else
processImageComplete();
return bi;
}
use of java.awt.image.PixelInterleavedSampleModel in project imageio-ext by geosolutions-it.
the class ImageIOUtils method makeGenericPixelInterleavedWritableRaster.
/**
* Returns a generic pixel interleaved WritableRaster
*
* @param numElems
* @param numLines
* @param bandOffsets
* @param dataType
* @return
*/
public static WritableRaster makeGenericPixelInterleavedWritableRaster(int numElems, int numLines, int numBands, int dataType) {
int[] bandOffsets = new int[numBands];
for (int i = 0; i < numBands; ++i) bandOffsets[i] = i;
DataBuffer d = null;
if (dataType == DataBuffer.TYPE_BYTE)
d = new DataBufferByte(numElems * numLines * numBands);
else if (dataType == DataBuffer.TYPE_SHORT)
d = new DataBufferShort(numElems * numLines * numBands);
else if (dataType == DataBuffer.TYPE_USHORT)
d = new DataBufferUShort(numElems * numLines * numBands);
else if (dataType == DataBuffer.TYPE_FLOAT)
d = new DataBufferFloat(numElems * numLines * numBands);
else if (dataType == DataBuffer.TYPE_DOUBLE)
d = new DataBufferDouble(numElems * numLines * numBands);
else
throw new IllegalArgumentException("Invalid datatype: " + dataType);
PixelInterleavedSampleModel pism = new PixelInterleavedSampleModel(dataType, numElems, numLines, bandOffsets.length, numElems * bandOffsets.length, bandOffsets);
SunWritableRaster ras = new SunWritableRaster(pism, d, new Point(0, 0));
return ras;
}
Aggregations