Loading images memory using C
Hello All,
I've been searching for how to load an image into memory using C for quite a while, but all searches have been fruitless.
Does anybody know how to do this?
Thanks in advance.
void* loadImage(const char* szFilename){ FILE* pFile = fopen(szFilename, "rb"); fseek(pFile, 0, SEEK_END); long lLen = ftell(pFile); fseek(pFile, 0, SEEK_SET); void* pBuffer = malloc(lLen); fread(pBuffer, 1, lLen, pFile); fclose(pFile); return pBuffer;}
Thanks for that, I assume it's straight forward to get data out if the data is from a bitmap file?
Yes, it's relatively easy to load a bitmap.
The bitmap is composed of a header and the data (the exact header is surely documented online, just google it up). The header contains information about the width and height, but also the color depth (it could be a grayscale 8bit bitmap or a 24bit one, possibly others as well).
Once you've identified the header, you can access the data of the bitmap just like a 2 dimensional array:
A 24bit bitmap will work in the same way, but every pixel occupies 3 byte, each byte for either red, green or blue channel.
The bitmap is composed of a header and the data (the exact header is surely documented online, just google it up). The header contains information about the width and height, but also the color depth (it could be a grayscale 8bit bitmap or a 24bit one, possibly others as well).
Once you've identified the header, you can access the data of the bitmap just like a 2 dimensional array:
// This code assumes this is a greyscale bitmap.unsigned char getPixel( unsigned int x, unsigned int y ){ return data[ x * image_width + y ];}
A 24bit bitmap will work in the same way, but every pixel occupies 3 byte, each byte for either red, green or blue channel.
Check out wikipedia for the BMP file-format: http://en.wikipedia.org/wiki/BMP_file_format.
Usually you don't have to bother with most details, if you just want to read a simple bitmap.
Something like the following is usually enough, at least on a system with the appropriate endianess, and a bitmap where each line data is a number of bytes divisible by 4. (You can read more on that on wikipedia)
Check the bitDepth, which should probably be 24, if you want one byte for each red, green and blue for each pixel. Or 8 for grayscale. Then imageData is simply the pointer to the actual image-data, and you have the image width/height.
So to get a pixel you use:
If you have a normal 24-bit image. It's usually stored as BGR, not RGB. Also it's usually stored bottom-up, not top-down, but you can read all such details also on wikipedia. It won't matter except for that your image might be upside-down =)
Usually you don't have to bother with most details, if you just want to read a simple bitmap.
Something like the following is usually enough, at least on a system with the appropriate endianess, and a bitmap where each line data is a number of bytes divisible by 4. (You can read more on that on wikipedia)
char *theBitmapData;unsigned int offset = *(unsigned int*)(theBitmapData + 10);int width = *(int*)(theBitmapData + 18);int height = *(int*)(theBitmapData + 22);int bitDepth = *(unsigned short*)(theBitmapData + 28);unsigned char *imageData = (unsigned char*)(theBitmapData + offset);
Check the bitDepth, which should probably be 24, if you want one byte for each red, green and blue for each pixel. Or 8 for grayscale. Then imageData is simply the pointer to the actual image-data, and you have the image width/height.
So to get a pixel you use:
blue = *(imageData + y*width*3 + x*3 + 0);green = *(imageData + y*width*3 + x*3 + 1);red = *(imageData + y*width*3 + x*3 + 2);
If you have a normal 24-bit image. It's usually stored as BGR, not RGB. Also it's usually stored bottom-up, not top-down, but you can read all such details also on wikipedia. It won't matter except for that your image might be upside-down =)
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement