image loader

Mar 5, 2011 at 7:50pm
I need to load an image from a file into an array. I would perfer a small snippet of code, but an image loading library would work too.
Mar 5, 2011 at 7:58pm
I know that Allegro can load bitmaps, but I don't think you would need somthing that large just to use one or two functions.
Mar 5, 2011 at 8:24pm
A few questions:

a) Which image format?
b) Do you need some conversion of the image data?

Mar 5, 2011 at 8:32pm
All I want is to take an image of any format (Not just one format, I will be reading many images of diferent fomats and I don't want to have to convert each one to any one format) and convert it to a 2 dimentional array of pixels.
Mar 5, 2011 at 8:45pm
You need a library, or libraries.
You must also choose a specific subset of image types you plan to support, because each one is different. (You cannot just load any image.)
Good luck!
Mar 5, 2011 at 8:52pm
I suppose DevIL might do a good job for you. Check out http://openil.sourceforge.net/.

You can retrieve a raw pointer to your currently loaded image. The only caveat: As far as I know there isn't an explicit copying facility. In practice this isn't a problem since everything you need to iterate over the memory pointed to can be queried as well, i.e. width and height if the image. If you need some code samples I can show you how I do it within my OBJ loading library.

Thomas
Mar 5, 2011 at 11:40pm
You could also try making your own loader, but almost every file type other than BMP requires that you know fairly complicated algorithms.
Mar 6, 2011 at 12:38am
Besides, maybe you should learn something about how images are stored. Just saying, because normally you wouldn't say something as outrageous as
ll I want is to take an image of any format
Mar 6, 2011 at 12:55am
@thokra
Could you show me the code to load an image and get it into an array? Thanks.
I have been looking on the internet and I can use the image as a texture, but I need to read it as an array to process the data.
Mar 6, 2011 at 10:15am
Here's what my MTL loader does when processing the specific line.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
ILuint img = ilGenImage();
ilBindImage(img);

if(!ilLoadImage(file_name.c_str()))
{
    std::cout << "[MTLLoader] ERROR: Could not load image " << file_name
                 << "! " << ilGetString(ilGetError()) << std::endl;
}

if(!ilConvertImage(IL_RGB, IL_UNSIGNED_BYTE))
{
    std::cout << "[MTLLoader] ERROR: Could not convert image " << file_name
                 << "! " << ilGetString(ilGetError()) << std::endl;
    return;
}

int image_width = ilGetInteger(IL_IMAGE_WIDTH);
int image_height = ilGetInteger(IL_IMAGE_HEIGHT);
int image_size = ilGetInteger(IL_IMAGE_BYTES_PER_PIXEL) * image_width *
                        image_height;

unsigned char *image_data = ilGetData();

Texture *tex = new Texture;

for(int i = 0; i < image_size; ++i)
    tex->data.push_back(image_data[i]);

tex->width = image_width;
tex->height = image_height;

materials_[current_mat_].setTexture(tex);

ilDeleteImage(img);


As you can see, DevIL is intentionally built like OpenGL. First you let the IL generate a handle for your image to load. Then you bind to that image and load it subsequently. In my code I convert the data to an array of IL_UNSIGNED_BYTE, effectively leaving you with 8 bits per channel. If you need an alpha channel you can also try the IL_RGBA costant instead of IL_RGB. Then I duplicate the whole array and put it in a simple struct Texture. You don't need to do that. You could as well say:

1
2
3
4
unsigned char* my_img_data = my_class.getPtrToData():

for(int i = 0; i < image_size; ++i)
    my_img_data[i] = image_data[i];


Don't forget to release all allocated memory by calling glDeleteImage() on your image handle! And that's it. HTH!

BTW, may I ask what you're trying to do with the data? Reading the word "texture" made me curious... ;)

Thomas
Mar 6, 2011 at 8:38pm
Thank you all for your help and time!
Topic archived. No new replies allowed.