Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2006

Tip: Looking for answers? Try searching our database.

Error when creating BufferedImage with IndexColorModel from a byte array.

Thread view: 
weisburd@gmail.com - 11 Oct 2006 19:06 GMT
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);
PofN - 11 Oct 2006 20:51 GMT
> 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.

> (http://javaalmanac.com/egs/java.awt.image/Mandelbrot2.html?l=rel)

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.