Loading a Bitmap and Displaying Bitmap Stored in an Array

Ok , I can load a 2 colour bitmap into an array of GLubyte 's .  then display it using glBitmap() . How do I do the same with a bitmap with more colours say 16 or 268 or higher ?? Please say everything I need to change .

Thanks Paladin
LVL 1
Paladin_VBAsked:
Who is Participating?
 
_corey_Connect With a Mentor Commented:
You will need to use the function glDrawPixels for more depth.

http://developer.3dlabs.com/GLmanpages/gldrawpixels.htm ["glDrawPixels"]

Make this call in the same place as glBitmap, but use glDrawPixels with *probably* GL_RGB format and GL_UNSIGNED_BYTE type.

You can see other formats supposed on the website, but GL_RGB generic is looking for 3-bytes per pixel.

corey
0
 
furqanchandioCommented:
the following coding loads a 24 bit bitmap and compresses it  but it is in C

#include<stdio.h>
#include<math.h>
#include<windows.h>  // for the bitmap structs

#define BITMAP_ID 0x4D42

void compress_bitmap(char* filename,char* destfilename)
{
FILE *fileptr,*dest_ptr;
BITMAPFILEHEADER bitmapfileheader, dest_header;
BITMAPINFOHEADER bminfo, dest_info;

unsigned char *bitmapimage, *destimage, tempRGB;
unsigned int index=0,dest_index=0;


//open file
fileptr=fopen(filename,"rb");
dest_ptr=fopen(destfilename,"wb");
if(!fileptr || !dest_ptr) { printf("\n Cant Open File \n "); return;}

//read the bitmapfile header
fread(&bitmapfileheader, sizeof(bitmapfileheader),1,fileptr);
//write to dest
dest_header.bfSize=sizeof(BITMAPFILEHEADER);
dest_header.bfType=BITMAP_ID;
dest_header.bfReserved1=NULL;
dest_header.bfReserved2=NULL;
dest_header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


//verify bitmap is true ie equal to 0x4D42
if (bitmapfileheader.bfType != BITMAP_ID )
{
      fclose(fileptr);  fclose(dest_ptr);
      printf("\n Improper File Format \n ");
      return;
}// if not bitmap

//read bitmap info header
fread(&bminfo,sizeof(bminfo),1,fileptr);
dest_info.biSize = sizeof(BITMAPINFOHEADER);
dest_info.biPlanes=1;
dest_info.biBitCount=24;
dest_info.biCompression= BI_RGB;
dest_info.biSizeImage= bminfo.biSizeImage/2;
dest_info.biXPelsPerMeter=0;
dest_info.biYPelsPerMeter=0;
dest_info.biClrUsed=0;
dest_info.biClrImportant=0;
dest_info.biWidth=bminfo.biWidth/2;
dest_info.biHeight=bminfo.biHeight;


//allocate memory
bitmapimage=(unsigned char*) malloc (bminfo.biSizeImage);
destimage =(unsigned char*) malloc (bminfo.biSizeImage/2);


//verify allocation()
if (!bitmapimage  || !destimage)
{ printf(" \n Cant allocate memory \n");
      free(bitmapimage); free(destimage); fclose(fileptr);fclose(dest_ptr); return ;}// kachra collection


//move file pointer to beginning of data;
fseek(fileptr,bitmapfileheader.bfOffBits,SEEK_SET);
// read the bitmap data
fread (bitmapimage,bminfo.biSizeImage,1, fileptr);

// make sure image was read
if (!bitmapimage) {
      printf("\n Data was not read \n ");
      fclose(fileptr); return;}

//write the data
for (index; index < bminfo.biSizeImage ; index++)
if (index%2==0) destimage[dest_index++] = bitmapimage[index];

//swap blue and green values
for (dest_index=0; dest_index < dest_info.biSizeImage; dest_index+=3)
{
tempRGB=destimage[dest_index+1];
destimage[dest_index+1] = destimage[dest_index+2];
destimage[dest_index+2]=tempRGB;
}//for loop

//write destination file header
fwrite(&dest_header,1,sizeof(BITMAPFILEHEADER),dest_ptr);

//write destination file info
fwrite(&dest_info,1,sizeof(BITMAPINFOHEADER),dest_ptr);

//write destination file data
fwrite(destimage,1,dest_info.biSizeImage,dest_ptr);

fclose (fileptr);
fclose (dest_ptr);
free(bitmapimage);
free(destimage);
}//////////////////////////////////////////////compress_bitmap
//////////////////////////////////////////////////////////////

/////////////////////////////////////////////////
////////// UNCOMPRESS the file //////////////////////////
void uncompress_bitmap(char* filename,char* destfilename)
{
FILE *fileptr,*dest_ptr;
BITMAPFILEHEADER bitmapfileheader, dest_header;
BITMAPINFOHEADER bminfo, dest_info;

unsigned char *bitmapimage, *destimage, tempRGB;
unsigned int index=0,dest_index=0;


//open file
fileptr=fopen(filename,"rb");
dest_ptr=fopen(destfilename,"wb");
if(!fileptr || !dest_ptr) { printf("\n Cant Open File \n "); return;}

//read the bitmapfile header
fread(&bitmapfileheader, sizeof(bitmapfileheader),1,fileptr);
//write to dest
dest_header.bfSize=sizeof(BITMAPFILEHEADER);
dest_header.bfType=BITMAP_ID;
dest_header.bfReserved1=NULL;
dest_header.bfReserved2=NULL;
dest_header.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);


//verify bitmap is true ie equal to 0x4D42
if (bitmapfileheader.bfType != BITMAP_ID )
{
      fclose(fileptr);  fclose(dest_ptr);
      printf("\n Improper File Format \n ");
      return;
}// if not bitmap

//read bitmap info header
fread(&bminfo,sizeof(bminfo),1,fileptr);
dest_info.biSize = sizeof(BITMAPINFOHEADER);
dest_info.biPlanes=1;
dest_info.biBitCount=24;
dest_info.biCompression= BI_RGB;
dest_info.biSizeImage= bminfo.biSizeImage*2;
dest_info.biXPelsPerMeter=0;
dest_info.biYPelsPerMeter=0;
dest_info.biClrUsed=0;
dest_info.biClrImportant=0;
dest_info.biWidth=bminfo.biWidth*2;
dest_info.biHeight=bminfo.biHeight;


//allocate memory
bitmapimage=(unsigned char*) malloc (bminfo.biSizeImage);
destimage =(unsigned char*) malloc (bminfo.biSizeImage*2);


//verify allocation()
if (!bitmapimage  || !destimage)
{ printf(" \n Cant allocate memory \n");
      free(bitmapimage); free(destimage); fclose(fileptr);fclose(dest_ptr); return ;}// kachra collection


//move file pointer to beginning of data;
fseek(fileptr,bitmapfileheader.bfOffBits,SEEK_SET);
// read the bitmap data
fread (bitmapimage,bminfo.biSizeImage,1, fileptr);

// make sure image was read
if (!bitmapimage) {
      printf("\n Data was not read \n ");
      fclose(fileptr); return;}

//write the data
for (index; index < bminfo.biSizeImage ; index++)
{
destimage[dest_index] = bitmapimage[index];
dest_index+=2;
//destimage[dest_index] = bitmapimage[index];
}//uncompress

//swap blue and green values
for (dest_index=0; dest_index < dest_info.biSizeImage; dest_index+=3)
{
tempRGB=destimage[dest_index+1];
destimage[dest_index+1] = destimage[dest_index+2];
destimage[dest_index+2]=tempRGB;
}//for loop    

//write destination file header
fwrite(&dest_header,1,sizeof(BITMAPFILEHEADER),dest_ptr);

//write destination file info
fwrite(&dest_info,1,sizeof(BITMAPINFOHEADER),dest_ptr);

//write destination file data
fwrite(destimage,1,dest_info.biSizeImage,dest_ptr);



fclose (fileptr);
fclose (dest_ptr);
free(bitmapimage);
free(destimage);
}//compress_bitmap



///////////////////////////
//////////// Main Asli function
int main()
{

char source_file[256]="multi.bmp";
char dest_file[256]="dest.bmp";
char dest2_file[256]="dest2.bmp";

compress_bitmap(source_file, dest_file);
uncompress_bitmap(dest_file,dest2_file);
      
printf("\n Kindly Use Paint Or Some Other Utility to View Files ");
printf("\n multi.bmp, dest.bmp, dest2.bmp ");

printf("\n Little Wonder It Is Called Lossy  ");
printf("\n\n Coding To Load Bitmap From Book ");
printf("\n OpenGL Game Programming by  ");
printf("\n Kevin Hawkins and  Dave Astle \n\n");
return 0;
}//main
0
 
furqanchandioCommented:
0
 
Paladin_VBAuthor Commented:
I was using draw Pixels before , but it wasn't working  lols just found  I had the dimensions for the height and the width the wrong way round !! :P . Thank you Corey . Points to you
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.