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.
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!
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.
@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.
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;
unsignedchar *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
unsignedchar* 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... ;)