seh4b
YaBB Newbies
Offline
Posts: 13
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])); }