We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpOpenGL and 3D Libraries › Code to Convert Image to Binary data for OpenGL
Page Index Toggle Pages: 1
Code to Convert Image to Binary data for OpenGL (Read 4228 times)
Code to Convert Image to Binary data for OpenGL
Mar 12th, 2007, 6:46am
 
Hi Guys,
Just thought I'd share some code in case some of you are porting your apps over to C++ openGL for efficiency. I'm working with particle systems and am struggling through trying to get an image to load and ended up writing a converter in processing, because I find it so much easier to do things here.

There are two handy int to byte and byte to int functions here for other purposes too. thanks to andrew otto for those.

-Seth

//NOTES -
// Program written by Seth Hunter and Andrew Otto to convert any image loaded into Processing to a .bin file
// WHY? OpenGL examples particularly in the "Red book" Open GL Programming Guide, 5th edition
// Require an image to be formatted as binary data in the following format:
// A 32 bit int with the width value
// A 32 bit in with the height value
// R Byte, G Byte, B Bytes - for the whole image pixel by pixel
// this program converts them from the bottom left corner row by row going up.
// and outputs a .bin file with the same name as the image file.
// Good luck with all this. I wasn't sure how else to do the conversion.  --Seth


//change these to your own variables
//update width, height, and the file name and extention of your file
//takes jpeg, tga, png types
int image_width = 1149;
int image_height = 840;
String filename = "north_stars";
String ext = ".jpg";

void setup() {
 size(image_width,image_height);
 byte[] width_bytes = int2bytes(image_width);
 byte[] height_bytes = int2bytes(image_height);
 //println("WIDTH: " + bytes2int(width_bytes) + "\n" + width_bytes + "\n");
 //println("HEIGHT: " + bytes2int(height_bytes) + "\n" + height_bytes + "\n");

 PImage image_load;
 image_load = loadImage(filename + ext);
 image(image_load,0,0);

 //adding 8 bytes for the width and height ints.
 byte[] output_bytes = new byte[(image_width * image_height * 3) + 8];
 //loading first 8 bytes
 for(int i=0; i<4; i++) {
   output_bytes[i] = width_bytes[i];
   output_bytes[i+4] = height_bytes[i];
 }

 int counter = 0;
 //loop through from the bottom left of the image
 //because OPEN GL and C++ have a bottom left 0,0 origin in the viewport
 for(int i=image_height-1; i>0; i--) {
   for(int j=0; j<image_width; j++) {
     int index = i*image_width + j;
     //place the bytes into a 4 byte array
     byte[] color_bytes = int2bytes(image_load.pixels[index]);
     //parse the last 3 RGB bytes from the byte array
     for(int k=0; k<3; k++) {
       output_bytes[counter + 8] = color_bytes[k+1];
       counter++;
     }
   }
 }
 //save all this to a file
 saveBytes(filename + ".bin", output_bytes);
}

//int2bytes()
//  returns a 4 element byte array containing each seperate byte of number
byte[] int2bytes(int number)
{
 byte[] bytes = new byte[4];
 bytes[0] = (byte)((number >> 24) & 255);
 bytes[1] = (byte)((number >> 16) & 255);
 bytes[2] = (byte)((number >> 8) & 255);
 bytes[3] = (byte)((number) & 255);

 return bytes;
}

//bytes2int()
//  converts a 4 element byte array into an integer.
int bytes2int(byte[] bytes)
{
 return (int)(  (bytes[0] << 24) |
   (bytes[1] << 16) |
   (bytes[2] << 8) |
   (bytes[3]));
}
Re: Code to Convert Image to Binary data for OpenG
Reply #1 - Apr 4th, 2007, 8:25am
 
Hi seh4b,

  Could you tell what are the improvment to convert Image 2 bin for OpenGL? Is it to have a better loading at start? Isn't already inside the conversion of an Image to a texture with OPENGL inside processing?



BTW: Is there a way to reduce texture size in the OpenGL mode (for example, I have a B+W movie (aka many frames) in 8bit)? Should I look in JOGL for this?
Re: Code to Convert Image to Binary data for OpenG
Reply #2 - Apr 27th, 2007, 3:17am
 
bump, would love to see an answer on this one.
Page Index Toggle Pages: 1