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 / GUI / November 2004

Tip: Looking for answers? Try searching our database.

short array as BufferedImage

Thread view: 
Lothar Leidner - 29 Nov 2004 11:14 GMT
Hello,

a small problem turns out to be a big problem for me: I want to
display a two-dimensional short array (values <= 255) as a gray value
image. Where can I find help for DataBuffer, ColorModel, Raster etc.
and some example code? Thanks in advance.

Lothar Leidner
Andrei Kouznetsov - 29 Nov 2004 19:56 GMT
> a small problem turns out to be a big problem for me: I want to
> display a two-dimensional short array (values <= 255) as a gray value
> image.

> Where can I find help for DataBuffer, ColorModel, Raster etc.
at the source

> and some example code? here it is:

at first you should create ColorMdel:

byte[] r = new byte[256];
for (int i = 0; i < r.length; i++) {
   r[i] = (byte) i;
}
ColorModel icm = new IndexColorModel(8, 256, r, r, r);

there many ways to create (Buffered)Image:

first way:
^^^^^^
Before you can use DataBuffer you should copy all data to one array.

then create DataBufferByte:
DataBuffer buffer = new DataBufferByte(dataArray, dataArray.length, 0);

create Raster:

WritableRaster wr = Raster.createBandedRaster(buffer, width, height, width,
new int[] {0}, new int [] {0}, new Point(0,0));

create BufferedImage:

BufferedImage bi = new BufferedImage(icm, wr, false, new Hashtable());

second way (better):
^^^^^^^^^

create BufferedImage:
BufferedImage bi = new BufferedImage(width, height, TYPE_BYTE_INDEXED, icm);

I assume that your data is in 2D byte array:
byte [][] data = new byte[height][width];

then create int array to hold temporary data:
int [] buf = new int[width];

transfer your data into BufferedImage:

for(int i = 0; i < height; i++) {
   //create ABGR pixel array
   for(int j = 0; j < width; j++) {
       int a = data[i][j] & 0xFF;
       buf[j] = (0xFF << 24) | (a << 16) | (a << 8) || a;
   }
   //set pixels
   bi.setRGB(0, j, width, 1, buf, 0, width);
}

> Thanks in advance.
you are welcome

Signature

Andrei Kouznetsov
http://uio.dev.java.net Unified I/O for Java
http://reader.imagero.com Java image reader
http://jgui.imagero.com Java GUI components and utilities

Lothar Leidner - 29 Nov 2004 21:15 GMT
Thank you again for your help, it worked.

Lothar


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.