Click to See Complete Forum and Search --> : Reading bitmaps into array


galon1
February 19th, 2001, 09:09 AM
Hello U all.
I need to read a bitmap(8bits grayscale)
into an array (like BYTE Array[PIC_SIZE];)
How can I do that
Thanks
Galon

ravindra_span
October 12th, 2001, 09:26 AM
Hey! This is the code in C to read the 8 bit bitmaps.
Ravindra.


#include <stdio.h>
#include <graphics.h>
#define BYTE unsigned char
#define WORD unsigned int
#define DWORD unsigned long

struct iden
{
int x;
int y;
int cl;
};
struct fileheader
{
char bftype[2];
DWORD bfsize;
WORD bfreserved1;
WORD bfreserved2;
DWORD bfoffbits;
};

struct bitmapinfoheader
{
DWORD bisize;
DWORD biwidth;
DWORD biheight;
WORD biplanes;
WORD bibitcount;
DWORD bicompression;
DWORD bisizeimage;
DWORD bixpelsperimeter;
DWORD biypelsperimeter;
DWORD biclrused;
DWORD biclrimportant;
};
/*paLLETE header*/
struct colortable
{
BYTE rgbblue;
BYTE rgbgreen;
BYTE rgbred;
BYTE rgbreserved;
};

void main()
{
struct iden iden1;
int gd=DETECT,gm,e;
int i=0,colorno,loop,x,y,higher,lower,color,i1,j1,k1;
int rs1,ls1,v1,code1,px,py;
char flag;
unsigned long size;
unsigned char *ptr;
FILE *fp;
struct fileheader header;
struct bitmapinfoheader info;
struct colortable table;
struct palettetype pal;

char *str;
fp=fopen("file.bmp","rb");
fseek(fp,0,SEEK_END);
size=ftell(fp);
fseek(fp,0,SEEK_SET);
fread(&header,1,sizeof(header),fp);
clrscr();
fread(&info,1,sizeof(info),fp);
printf("\n\n bitcount=%d\n",info.bibitcount);
getchar();
if(info.bibitcount==1)
colorno=2;
if(info.bibitcount==4)
colorno=16;
if(info.bibitcount==8)
colorno=256;
if(info.bibitcount==0)
colorno=0;
if(info.biclrused!=0)
colorno=info.biclrused;
initgraph(&gd,&gm,"c:\\tc\bgi");
printf("\n%d y=%ld x=%ld ",colorno,info.biheight,info.biwidth);
getchar();
getpalette(&pal);
for(loop=0;loop<colorno;loop++)
{
fread(&table,1,sizeof(table),fp);
setrgbpalette(pal.colors[loop],table.rgbred,table.rgbgreen,table.rgbblue);
}
cleardevice();
if(colorno==256)
{
for(i=0;i<16;i++)
setrgbpalette(pal.colors[i],i*4,i*4,i*4);
}

if(info.bibitcount==4)
{

for(y=info.biheight;y>=1;y=y-1)
{
for(x=1;x<=info.biwidth;x++)
{
fread(ptr,1,1,fp);
color=*ptr;
lower=color&15;
higher=color&240;higher=higher>>4;

if( (x+20)< 325 && (y+20)<250 ) // to decrease
{ //the area
putpixel(x+20,y+20,higher); //of
x++; // the pic
putpixel(x+20,y+20,lower); // a.bmp
}
else x++;
}
}
}


if(info.bibitcount==8)
{
for(y=info.biheight;y>=1;y--)
{
for(x=1;x<=info.biwidth;x++)
{
fread(ptr,1,1,fp);
color=*ptr;
putpixel(x,y,color/16-1);
}

}
}

getchar();
closegraph();
fclose(fp);


}

BernhardPreis
October 15th, 2001, 03:59 PM
More general, the bitmap file has a header containing various info. The thing you basicly need is the heigth and width and the color format (all in the header). After the header (which has a fixed size) there is the picture information. with 8 bit grayscale each byte contains the color information of a single pixel. You can get the pixel position from the current file position. (x=curpos-sizeof(header)%picwidth;y=curpos-sizeof(header)/picwidth (don't use floats or rounding)).

Tell me if that helped,

Bernhard


*** This message was posted using 100% recycled electrons ***