Archived

This topic is now archived and is closed to further replies.

Bitmap

This topic is 6020 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

How the heck do you display bitmaps? I tried using Lamothe''s TOTWGPG but his examples were wrong. i tried using the directX documentation but it didn''t work either.Does anyone have a programthey can e-mail me or a function thay can paste here that loads a bitmap to a ofscrn surface then blits to the back buffer then flips it. any help would be greatly appreciated. X4J

Share this post


Link to post
Share on other sites
Hi,
I can put one here. Its tested and well commented, by the way it does not do transparency.

/*****Bitmap.h****************/
#ifndef MY_BITMAP_H
#define MY_BITMAP_H

#include

typedef struct BMP {

BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bmiHeader;
GLubyte *image; // image data, change this to
//UCHAR if you don''t want GLubyte

} BMP_FILE, *BMP_FILE_PTR;

extern GLubyte *Load_BMP(BMP_FILE_PTR bitmap, char *filename);
extern GLubyte *Unload_BMP(BMP_FILE_PTR bitmap);

#endif


/************** Bitmap.cpp *************/
// Load bitmap routines

// fread reads 1-2 bytes packing at a time, but bitmaps are packed
// at 8 bytes at a time, so we need to make fread give right values
//#pragma pack 2

// include header files
#include
#include
#include
#include
#include "bitmap.h"

int Load_BMP(BMP_FILE_PTR bitmap, char *filename)
{
FILE *fp; // file pointer
int bitsize; // bitmap size info

// first read the file to see if it exists
if((fp = fopen(filename, "rb")) == NULL)
{
//cerr << "file not found";
return NULL;
}

// read in the file header
fread(&bitmap->bmfHeader, sizeof(BITMAPFILEHEADER),
1, fp);

// check if file is a bitmap file - check
// for ''BM''reversed(ie is ''MB'')
// or check if the value is 0x4d42

if(bitmap->bmfHeader.bfType != ''MB'')
{
fclose(fp); // close the file as non-bmp
//files are not of our interest here
return NULL;
}



// now read in the info header
fread(&bitmap->bmiHeader, sizeof(BITMAPINFOHEADER), 1,
fp);

// read the image size to check if we need to compute
//the value
bitsize = bitmap->bmiHeader.biSizeImage;
if(bitsize == NULL)
{
bitsize = bitmap->bmiHeader.biWidth *
((bitmap->bmiHeader.biBitCount) / 8) *
bitmap->bmiHeader.biHeight;
}

// allocate memory for image
if((bitmap->image = (GLubyte *)malloc(bitsize)) == NULL)
{
fclose(fp); // due to lack of
//memory image cannot be put in buffer, so close
//file and return NULL
return NULL;
}

// read in the image
fread(bitmap->image, bitsize, 1, fp);

// now we have everything so close file and return 1
fclose(fp);

// this function was succesful if it reached till here
return 1;
}

int Unload_BMP(BMP_FILE_PTR bitmap)
{
if(bitmap->image)
{
free(bitmap->image);
bitmap->image = NULL;
}

// shouldn''t this be succesful whatever happens :-)
return 1;
}

I guess this looks more like a bitmap function how it is in Lamothe''s Book(Its not wrong though).

The above one I tested it using OpenGL, ofcourse while editing here there may be mistakes.

Load_BMP(&bmp24bit, "file.bmp");
glRasterPos2i(-1, -1);

Then use glDrawPixels
glDrawPixels( bitmap24bit.bmiHeader.biWidth,
bitmap24bit.bmiHeader.biHeight,
GL_BGR_EXT, GL_UNSIGNED_BYTE, bitmap24bit.image);


This works :-)

How good it is ? I don''t know, but it looks simpler.

Share this post


Link to post
Share on other sites
Hi,

First of all a few corrections.
Change all prototype declarations to extern int instead of extern GLubyte *.


/******************************
Isn't the actual width of the bitmap in multiple of 4 rounded up? So don't you have to adjust?
/******************************

What exactly do you mean ? This code potentially is for 24 bit bitmaps only. Don't even bother to try it for 8 or 16 bit because it wouldn't work.



Edited by - flame_warrior on June 21, 2001 7:54:51 PM

Share this post


Link to post
Share on other sites
Hi,

For 8 or 16 bit bitmaps using DirectX what you have to do is read everything as as usual in the code but after reading the info header check the bitCount for 8 or 16 bit, and read in the palette if its 8 or 16. The bitmaps are stored as BGR so you need to reverse the colors and convert it into RGB, so use a for loop where the max value would be biWidth * biHeight. Its done on similar lines but you need to twist a little.

Share this post


Link to post
Share on other sites
by multiple of 4 I mean this. Let''s say the bitmap width is 50. For 24 bit bitmap each line will occupy (50 * 3) 150 bytes. In windows I header the true "pitch" is rounded up to a multiple of 4. So the actually bytes is 152, which divides evenly to 4.

Share this post


Link to post
Share on other sites
Hi,

It would be a multiple of 4 if we wanted to simulate an alpha channel, not necssary in what I tried to accomplish. While trying to simulate the alpha channel I make it a multiple of 4, while just loading a simple bitmap it was unnecessary.

Share this post


Link to post
Share on other sites
Have a look at this code:

  
HANDLE hFile;
DWORD BytesRead;

///Open the file to the handle

hFile = CreateFile(szFilename, GENERIC_READ, 0, NULL,
OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
if (hFile == INVALID_HANDLE_VALUE)
{
return(1);
}//end if

///


///Read some information from the file

// Read BITMAPFILEHEADER

if (!(ReadFile(hFile, &FileHeader, sizeof(BITMAPFILEHEADER),
&BytesRead, NULL)))
{
CloseHandle(hFile);
return(2);
}//end if

// Read BITMAPINFOHEADER

if (!(ReadFile(hFile, &InfoHeader, sizeof(BITMAPINFOHEADER),
&BytesRead, NULL)))
{
CloseHandle(hFile);
return(3);
}//end if

///


// Read the pixel data

int Size = FileHeader.bfSize -
sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER);
pbPixelData = (PBYTE)malloc(Size);
if (!(ReadFile(hFile, pbPixelData, FileHeader.bfSize -
sizeof(BITMAPFILEHEADER) - sizeof(BITMAPINFOHEADER),
&BytesRead, NULL)))
{

delete [] pbPixelData;
CloseHandle(hFile);
return(4);
}//end if



Assume no error reading the data from the files. When I go back to check the pixel data from pbPixelData like
WriteDebug(pbPixelData[0]); or something like that I get a null value. How come?

Share this post


Link to post
Share on other sites