Error when creating BufferedImage with IndexColorModel from a byte array.

W

weisburd

Hi, I have a 1-dimentional byte array and an IndexColorTable, and I
can't figure out how to combine the 2 into an BufferedImage without
unnecessary copying/reallocating of the image buffer.

The color model I have is:

int [] cmap = new int [numColors];
cmap[i++] = 0xffa0f000; /etc.
....
new IndexColorModel(8, 22, cmap, 0, true, transparentIndex,
DataBuffer.TYPE_BYTE );




Thanks for your help
-Ben


Ps.

I tried to do this based on some example code
(http://javaalmanac.com/egs/java.awt.image/Mandelbrot2.html?l=rel), but
can't figure out how to go from the color model they're using to the
one I have (the 8 bit one specified above). When I replace the 4bit
colormodel in the code below with the 8bit color model specified above,
I get the following error:

[java] java.lang.IllegalArgumentException: Raster
ByteInterleavedRaster: width = 5120 height = 3520 #numDataElements 1
dataOff[0] = 0 is incompatible with ColorModel IndexColorModel:
#pixelBits = 8 numComponents = 4 color space =
java.awt.color.ICC_ColorSpace@c51355 transparency = 2 transIndex = 22
has alpha = true isAlphaPre = false
[java] at java.awt.image.BufferedImage.<init>(BufferedImage.java:613)


Code:

byte[] pixelArray = (byte[]) getData_CHAR();
int width = 5120;
int height = 3520;
int numbytes = width*height;

//create DataBuffer using byte buffer of pixel data.
DataBuffer dataBuffer = new DataBufferByte(pixelArray, numbytes, 0);

//prepare a sample model that specifies a storage 8-bits of pixel data
in an 8-bit data element
int bitMasks[] = new int[]{0xf};
SinglePixelPackedSampleModel sampleModel = new
SinglePixelPackedSampleModel(DataBuffer.TYPE_BYTE, width, height,
bitMasks);

//create a raster using the sample model and data buffer
WritableRaster writableRaster =
Raster.createWritableRaster(sampleModel, dataBuffer, new Point(0,0));

//generate 16-color model
byte[] r = new byte[16];
byte[] g = new byte[16];
byte[] b = new byte[16];

r[0] = 0; g[0] = 0; b[0] = 0;
r[1] = 0; g[1] = 0; b[1] = (byte)192;
r[2] = 0; g[2] = 0; b[2] = (byte)255;
r[3] = 0; g[3] = (byte)192; b[3] = 0;
r[4] = 0; g[4] = (byte)255; b[4] = 0;
r[5] = 0; g[5] = (byte)192; b[5] = (byte)192;
r[6] = 0; g[6] = (byte)255; b[6] = (byte)255;
r[7] = (byte)192; g[7] = 0; b[7] = 0;
r[8] = (byte)255; g[8] = 0; b[8] = 0;
r[9] = (byte)192; g[9] = 0; b[9] = (byte)192;
r[10] = (byte)255; g[10] = 0; b[10] = (byte)255;
r[11] = (byte)192; g[11] = (byte)192; b[11] = 0;
r[12] = (byte)255; g[12] = (byte)255; b[12] = 0;
r[13] = (byte)80; g[13] = (byte)80; b[13] = (byte)80;
r[14] = (byte)192; g[14] = (byte)192; b[14] = (byte)192;
r[15] = (byte)255; g[15] = (byte)255; b[15] = (byte)255;

//create buffered image
ColorModel colorModel = new IndexColorModel(4, 16, r, g, b);
BufferedImage image = new BufferedImage(colorModel, writableRaster,
false, null);
 
P

PofN

Hi, I have a 1-dimentional byte array and an IndexColorTable, and I
can't figure out how to combine the 2 into an BufferedImage without
unnecessary copying/reallocating of the image buffer.

Try MemoryImageSource. It is supposed to require the least copying.

That's the most complicated way to build an image. Someone at Sun sure
got a hard one when comming up with that image structure.

PofN
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,968
Messages
2,570,152
Members
46,697
Latest member
AugustNabo

Latest Threads

Top