Skip to main content
gamedev.net
🔒 Locked

Bitmap loading problem for my software rasterizer

Started by Citron 2 replies 900+ views
Original Post
Citron
Citron
HI! First of all, I would like to apologize for my bad english. I'll try my best to explain my problem. Me and my pal have been working on a software rasterizer as a school project for over a half year now. we are working on a simple texturemapper at the moment and are trying to load a bitmap into an array and display it. I found the How to Load a Bitmap tutorial on gamedev, which helped us a lot, but still theres one problem... We've loaded the bitmap and draws it on the screen using gdi and the array that contains the actual picture. but the rgb colorvalues loaded from the bitmap isn't equal to the ones in the actual bitmapfile. ill include the code for my loading function below: int LoadBMP(char filename[], HDC hDC) { BITMAPFILEHEADER bmfh;//bitmapsfilhuvud BITMAPINFOHEADER bmfi;//bitmapsfilinfo fstream fin; fin.open(filename, ios::in|ios::binary); //öppna bitmapsfilen if(!(fin.read((char*)&bmfh, sizeof(bmfh)))) cout << "fel"; if(!(fin.read((char *)&bmfi, sizeof(bmfi)))) cout << "fel i läsandet av bmfi!" << endl; int dataSize = (bmfi.biWidth*bmfi.biHeight*(unsigned int)(bmfi.biBitCount/8.0)); char* ImageBitmap = new char[dataSize]; cout << dataSize << "b stor fil" << endl; cout << sizeof(ImageBitmap) << endl; fin.read((char*)ImageBitmap, (unsigned int)ImageBitmap); cout << "R: " << (int)ImageBitmap[5] << " G: " << (int)ImageBitmap[4] << " B: " << (int)ImageBitmap[3] << endl; if(ImageBitmap == NULL) { cout << "fuck"; } fin.close(); cout << "Bitmaptype: " << bmfh.bfType << endl; cout << "Storlek: " << bmfi.biSizeImage << " bytes" << endl; cout << "Bitdjup: " << bmfi.biBitCount << " bpp" << endl; cout << "Hojd(i pixlar): " << bmfi.biHeight << " px" << endl; cout << "Bredd(i pixlar): " << bmfi.biWidth << " px" << endl; int trace = 0; for(int y = 0; y < bmfi.biHeight; y++) { for(int x = 0; x < bmfi.biWidth ; x++) { SetPixel(hDC, x, y, RGB(SIGN((int)ImageBitmap[trace + 2]), SIGN((int)ImageBitmap[trace + 1]), SIGN((int)ImageBitmap[trace]))); trace += 3; } } SetPixel(hDC, 125,125, RGB(255,255,255)); ofstream fout; fout.open("BMPTEXT.txt", ios_base::out); trace = 0; for(int h = 0; h < bmfi.biHeight; h++) { for(int w = 0; w < bmfi.biWidth ; w++) { fout << "<" << (int)ImageBitmap[trace + 2] << "," << (int)ImageBitmap[trace + 1] << "," << (int)ImageBitmap[trace] << ">"; trace += 3; } fout << endl; } fout.close(); delete ImageBitmap; return 1; } I would be very happy if someone could tell me why my rgb values are wrong. it's hard to find a good tutorial on plotting bitmaps onto a screen. As most of you know, I can't use bitblt() because it doesn't work with texture interpolation. If anyone knows of any good tutorials on the topic you're welcome to post them:)
JoeJ
JoeJ
Hi, I can't help you loading a bmp file.

An alternative wolud be to save the bitmap as a raw file from Photoshop.
This saves no header, only the bytes of color.
You can choose the option af saving RGB triples or R array first, then G array...
Disadvantage: The loading App needs to know image resolution and number of color channels.

Another easy to load image format is Targa, which also supports a variable number of color channels.
Note that bmp does not support RGBA for example, so you will want to switch to a more flexible format when using transparancy.

Hope this helps.
Ashkan
Ashkan
Quote:

if(ImageBitmap == NULL)
{
cout << "fuck";
}


This is my favorite line :). Your teacher would be playing with the code trying to load some bitmaps and suddenly a pretty nice tiny f*ck would pop up and he would be like "OHHH MY GGGOD!" and you would be like "Errrr, emmm, see you next term, Sir!".
jsaade
jsaade
char* ImageBitmap = new char[dataSize];
use unsigned char to represent bytes.
i assume you are talking about 24-bits bitmaps, if so,
the size would be unsigned char * bmp = new unsigned char[width * height * 3];
if not you will have to first read color palette and then the indices to that palette.
for 24-bits bitmaps, just loop on the width*height (if i remember correctly)
for(int i =0; i < width*height i+=3){
ImageBitmap = readbyte();
ImageBitmap[i+1] = readbyte();
ImageBitmap[i+2] = readbyte();
}

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.