?
Solved

Loading a Bitmap and Displaying Bitmap Stored in an Array

Posted on 2005-04-30
4
Medium Priority
?
504 Views
Last Modified: 2013-12-03
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
0
Comment
Question by:Paladin_VB
  • 2
4 Comments
 
LVL 8

Accepted Solution

by:
_corey_ earned 320 total points
ID: 13901565
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
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13903657
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
 
LVL 4

Expert Comment

by:furqanchandio
ID: 13903667
0
 
LVL 1

Author Comment

by:Paladin_VB
ID: 13903905
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

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Suggested Courses

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question