A simple bmp class

Started by
11 comments, last by Oberon_Command 18 years, 9 months ago
Where can I find a very simple class for C++ that opens a .bmp file, and will allow me to easily see the scan through the pixels of the bitmap in sequential order to see what their values are? I've tried looking but I end up with GDI+ classes, or classes that implement all sorts of complex features, but so far have come up with nothing that allows me to simply check the raw pixel data. Thanks.
Advertisement
That's because doing that sort of operation is time consuming and usually unnecissary for games. You might want to look for paint/drawing APIs rather than game APIs since that sort of thing does require pixel data, and doesn't have the time contraints games usually carry with them.
Actually, looking over GDI, it has some useful functions for doing what I want.

Does anyone know, however, how to create a CImage from a CBitmap?
I prefer to mess around with the low level stuff :) You can wrap it in class if you want to.
I created this function some time ago to mess around directly with the pixels. bitsOut is a one dimensional array and doesn't need allocating/deallocating. Keep in mind that the width in bytes is always made to be divisible by 4.
But as Telastyn said, the performance is not exactly stellar.

HBITMAP CreateAccessableBitmap(HDC compat, int width, int height, BYTE** bitsOut){	BITMAPINFO binf;	BITMAPINFOHEADER bhdr;	RGBQUAD dummy;	HBITMAP result;	if(bitsOut){		*bitsOut = NULL;	}	bhdr.biBitCount = 24;	bhdr.biClrImportant = 0;	bhdr.biClrUsed = 0;	bhdr.biCompression = BI_RGB;	bhdr.biHeight = -height;	bhdr.biPlanes = 1;	bhdr.biSize = sizeof(BITMAPINFOHEADER);	bhdr.biSizeImage = 0;	bhdr.biWidth = width;	bhdr.biXPelsPerMeter = 0;	bhdr.biYPelsPerMeter = 0;	binf.bmiColors[0] = dummy;	binf.bmiHeader = bhdr;	if(bitsOut){		result = CreateDIBSection(compat, &binf, DIB_RGB_COLORS, (void**)bitsOut, NULL, NULL);	}else{		result = CreateDIBSection(compat, &binf, DIB_RGB_COLORS, NULL, NULL, NULL);	}	return result;}
You could take a look at SDL it loads BMPs and you can access the pixel data if you so desire. It's also pretty easy to use.
If you want to make your own class, or whatever, that deals with bitmaps, I'd suggest reading up on the two structs BITMAPINFO and BITMAPINFOHEADER. Those once loaded correctly will tell you what you need to do with the data.

Be warned: There is some funnyness with bitmaps. They can have negative height, need to have certain dimensions, and will pad bytes in order to reach them. Be sure to try out reading and displaying on multiple different bmps, before deciding your done with it.
I don't seem to be able to post the code here... I keep getting an empty post :-/
I was trying to post this: http://www.xzist.org/files/bitmap.txt
I've read through your code, while I'm far from the best to judge your work, I think it looked pretty solid. I do have a quick question though, and its not criticism, more of a curiosity. You emulated the BITMAPINFO and BITMAPINFOHEADER structs. I'm guessing that is because you didn't want to include the windows libraries that define it. Then later you use the draw function that requires portions of those libraries. Is it because you wanted a quick way to test if it worked correctly?
...yes. It was originally for DOS. The draw function was just for testing.
allo

to read bmp file you must be define a structure like Image with width and Heigth, the image infos will be find when you start read.

see how i read bmp file very simple, not use MSDN library. Only basic C routine.
i make simple image manipulation: reverse color, you can make your fun very easy.

Note must be put an Image in the directory where your excutable. Use excute with argument to input your bmp file, or change main argument for appropriate use.(give pointer argv="filename.bmp" your image's name, and argc= 3 in your code)

thank,

if u are some pb, send me mail at: assoumemba_AT_HOTMAIL_DOT_COM


#include
#include
#include

/*-------STRUCTURES---------*/
typedef struct {int rows; int cols; unsigned char* data;} sImage;

/*-------PROTOTYPES---------*/
long getImageInfo(FILE*, long, int);
void copyImageInfo(FILE* inputFile, FILE* outputFile);
void copyColorTable(FILE* inputFile, FILE* outputFile, int nColors);

int main(int argc, char* argv[])
{
FILE *bmpInput, *bmpOutput;
sImage originalImage;
unsigned char someChar;
unsigned char* pChar;
int nColors; /* BMP number of colors */
long fileSize; /* BMP file size */
int vectorSize; /* BMP vector size */
int r, c; /* r = rows, c = cols */


/* initialize pointer */
someChar = '0';
pChar = &someChar

if(argc <

This topic is closed to new replies.

Advertisement