Image data loading

Hi

I was wondering what people do, when they need to load image data for textures. I sthere some common library that one can use, or do everybody write their own loaders?

Check this out: http://openil.sourceforge.net

Some used every thing from the BMP loader in the glaux to custom TGA loader routines, or a image loading library.

The easest is just plain RAW image data, very simple to load.
The TGA uncompressed format also is very easy to load also lot’s of examples of loading TGA. TGA support alpha channel which comes in handy with making transparency maps.

nehe.gamedev.net has tutors on loading textures BMP to TGA

Originally posted by Anders:
[b]Hi

I was wondering what people do, when they need to load image data for textures. I sthere some common library that one can use, or do everybody write their own loaders?[/b]

If you’re making a very simple app or game, where you know only need a few textures, the raw format is very easy to load and use. The main problem with the raw format, however, is that you must know the dimensions of the texture before you load it. With any other format(.bmp’s, tga’s, etc), you can pull the size of the image from the image format. Uncompressed bmps and tga’s have very easy-to-understand file-formats.

You can find the specifications for most file formats at http://www.wotsit.org

Now, you can cheat a bit with raw files. You CAN store the size and format of the texture in the filename.

Examples:
sky32x32rgb.raw
grass256x256rgba.raw

When you load the file, you can parse out the texture dimensions and pixelformat from the filename, and you’re good to go. It all depends on the size of your project. If you’re going to use 4-5 textures, stick with the raw format. If you want to use more, look into bmp’s and tga’s. There are libraries that let you load .jpgs, etc., but I haven’t been too impressed with the loss of quality that jpgegs tend to display when used as textures.

If you do insist on using raw textures, Photoshop and Paintshop Pro have a .raw option. When you save the file, make sure it has a header size of 0 and is in interleaved format. From that point, this is all you need to do to load the file into a texture:

//this function returns 1 if it successfully loads a texture, 0 if it failed to load the file
int LoadTexture(void){
FILE *ifile;
// the size of a raw texture is equal to:
//it’s height x it’s width x the amount of bytes per pixel
//A 256x256 rgb texture is 256x256x3 bytes(196,608 bytes)
GLubyte texture[196608];
if(ifile=fopen(“mytexture.raw”,“rb”)){
fread(texture,(GLubyte)1,196608,ifile);
fclose(ifile);
glBindTexture(GL_TEXTURE_2D,texname[1]);
glPixelStorei(GL_UNPACK_ALIGNMENT, 1);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_S,GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_WRAP_T,GL_REPEAT);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);

glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR);

gluBuild2DMipmaps(GL_TEXTURE_2D,3,256,256,GL_RGB, GL_UNSIGNED_BYTE, &texture[0]);
return 1;
}
return 0;
}

That should just about cover loading .raws as textures.

[This message has been edited by delsydsoft (edited 02-15-2003).]