PDA

View Full Version : Loading bmps



tmwp197
11-28-2000, 09:34 AM
Does anybody have some simple code for loading bitmaps to textures which doesn't use glaux.h and is machine independant (even though bmps are not http://www.opengl.org/discussion_boards/ubb/confused.gif )

Cheers, Tom

MrShoe
11-28-2000, 01:19 PM
Hello there, funny isn't it, I have the SAME problem! I asked for the SIMPLEST sample code of how to load a texture onto a quad from a .bmp file and the code that I got was GOOD but it was for WINDOWS and I use Linux, it also uses GLAUX. SO I guess we are in the same boat! If you get any good code can you please send it to me at
osushkov@hotmail.com
I would really appreciate the help!

Moz
11-28-2000, 04:28 PM
Hi guys,
try that, this code is taken from my tga loader and converted to a BMP loader (did this in 2-3 minutes, so don't expect it to be too clean).
I tested it and it seemed to work. It's simple c++ code that should work on any platform (for windows bitmaps, hé http://www.opengl.org/discussion_boards/ubb/smile.gif). If you want it in c, just remove the throw stuff and replace the operator new with calls to the malloc function.

unsigned char* LoadBMP(char *file_name, int &size, int bytes_to_store)
{
FILE *image_file; // Image file's handle
char file_header[54]; // BMP file's header
int image_size; // Size of the image in pixels
unsigned char *file_buffer; // Buffer that temporarily holds the data from the file
int pixpos; // Position of the pixel currently copied
int copied; // Number of bytes copied for the current pixel
int i, j; // Counter variables
unsigned char *dest; // Destination buffer
int file_name_length; // Length of the file's name

// Try to open the file and check if it exists
image_file = fopen(file_name, "rb");
file_name_length = strlen(file_name);
if(image_file == NULL)
{
throw "no such file";
}

fread(file_header, 54, 1, image_file);
// Parse the header
image_size = (file_header[18] + 256 * file_header[19]) * (file_header[22] + 256 * file_header[23]);
int bpp = (int)file_header[28];
if(bpp < 24)
{
throw "file w/ -24bpp not supported";
}

// check the storage format to be a supported one
if((bytes_to_store != GL_RGB) && (bytes_to_store != GL_LUMINANCE))
{
throw "The storage format specified is not valid";
}

// Allocate memory
size = image_size * ((bytes_to_store == GL_RGB) ? 3 : 1);
dest = new unsigned char[size];
file_buffer = new unsigned char[image_size * 3];
fread(file_buffer, (image_size * 3), 1, image_file);
fclose(image_file);
// copying image data to memory
for(i = 0; i < image_size; i++)
{
// move current position
pixpos = i * 3;
// Compute and store Y
if(bytes_to_store == GL_LUMINANCE)
{
dest[i] = (unsigned char)(0.299 * file_buffer[pixpos + 2]) +
(0.587 * file_buffer[pixpos + 1]) + (0.114 * file_buffer[pixpos]);
}
else
{
// init number of bytes copied from current pixel
copied = 0;
// Copy BGR to RGB (swapping)
for(j = 2; j >= 0; j--)
{
dest[i * 3 + (copied++)] = file_buffer[pixpos + j];
}
}
}
// Free memory
delete [] file_buffer;
return dest;
}

you can call it with GL_RGB or GL_LUMINANCE as third param (with GL_LUMINANCE, it calculates the luminance of each pixel and store the image as 8bpp).

PS: you should consider using tga files as they can store an alpha channel.

Moz



[This message has been edited by Moz (edited 11-29-2000).]

funk
11-28-2000, 10:44 PM
http://nehe.gamedev.net/opengl.asp

These tutorials (at least many of them) use bmp textures

Nutty
11-29-2000, 03:22 AM
www.openil.org (http://www.openil.org)

Open Image Library. Cross platform, and works under many API's. Does lots of file formats.

Cheers,
Nutty