Displaying Source Code(s)
|
|
C program to display bitmap images(*.bmp)
--------------------------------------------------------------------------------
Description : This program works well in windows 98.check out
for WinXp and server2003. enter the name of the bitmap file. tht
file should be present in the current directory.
Code :
#include <stdio.h>
#include <conio.h>
#include <dos.h>
#define DECLARE
#define VGALOW 0x101
typedef unsigned int UINT;
typedef unsigned char UCHAR;
struct VgaInfoBlock {
char signature[4];
short version;
char far *oemname;
long capabilities;
unsigned far *modes;
char buffer[238];
};
typedef struct
{
char red;
char green;
char blue;
}RGB;
struct VgaModeInfoBlock
{
UINT ModeAttributes;
UCHAR WinAAttributes;
UCHAR WinBAttributes;
UINT WindowGranularity;
UINT WinSize;
UINT WinASegment;
UINT WinBSegment;
void (far *WinFuncPtr)(void);
UINT BytesperScanLine;
UINT XResolution;
UINT YResolution;
UCHAR XCharSize;
UCHAR YCharSize;
UCHAR NumberOfPlanes;
UCHAR BitsPerPixel;
UCHAR NumberOfBanks;
UCHAR MemoryModel;
UCHAR BankSize;
UCHAR NumberOfImagePages;
UCHAR Reserved1;
UCHAR RedMaskSize;
UCHAR RedMaskPosition;
UCHAR GreenMaskSize;
UCHAR GreenMaskPosition;
UCHAR BlueMaskSize;
UCHAR BlueMaskPosition;
UCHAR ReservedMaskSize;
UCHAR ReservedMskPosition;
UCHAR DirectScreenModeInfo;
UCHAR Reserved2[216];
}modeinfo;
typedef enum
{
memPL = 3,
memPK = 4,
memRGB = 6,
memYUV = 7
}memModels;
typedef struct tagBMPHEADER
{
unsigned char bftype[2];
unsigned long bfsize;
unsigned int bfres1,bfres2;
unsigned long bfoffbits;
unsigned long bisize,biwidth,biheight;
unsigned int biplanes,bibitcount;
unsigned long
bicompression,bisizeimage,bixpelspermeter,biypelspermeter;
unsigned long biclrused,biclrimportant;
}BMPHEADER;
typedef struct tagRGBQUAD
{
unsigned char blue,green,red,rgbreserved;
}RGBQUAD;
typedef struct tagBMPINFO
{
BMPHEADER bmiheader;
RGBQUAD bmicolors[256];
}BMPINFO;
DECLARE int maxx,maxy;
DECLARE int xres,yres;
DECLARE int bytesperline;
DECLARE int curbank;
DECLARE unsigned int bankshift;
DECLARE int oldmode;
DECLARE char far *screenptr;
DECLARE void (far *bankswitch)(void);
DECLARE int pcolor,xp,yp;
DECLARE int ccolor;
DECLARE int GetVesaMode(void);
DECLARE void SetVseaMode(int);
DECLARE void setbank(int);
DECLARE void SetPalette(RGB pal[256]);
DECLARE void vinitgraph(int);
DECLARE void setwidth(int);
DECLARE void vclosegraph(void);
DECLARE void startaddr(int *,int *,int);
DECLARE void vputpixel(int,int,int);
DECLARE void SetPalette(RGB color[256]);
DECLARE char *ReadMemString(char far *);
DECLARE void showbitmap(char *infname,int xs,int ys);
void Vesa(int state)
{
union REGS reg;
reg.x.ax=0x4FFF;
reg.h.dl=(char ) state;
int86(0x10,®,®);
return ;
}
int GetSvgaInfo(struct VgaInfoBlock far *buffer)
{
struct REGPACK reg;
reg.r_ax = 0x4F00;
reg.r_es = FP_SEG(buffer);
reg.r_di = FP_OFF(buffer);
intr(0x10,®);
if(reg.r_ax==0x004F)
return 0;
else
return 1;
}
char *ReadMemString(char far *pointer)
{
char string[200];
int i=0;
while(*pointer)
{
string[i]=*pointer;
pointer++;
i++;
}
string[i]=0;
return string;
}
int GetSvgaModeInfo(int mode,struct VgaModeInfoBlock far
*buffer)
{
struct REGPACK reg;
reg.r_ax = 0x4F01;
reg.r_es = FP_SEG(buffer);
reg.r_di = FP_OFF(buffer);
reg.r_cx=mode;
intr(0x10,®);
if(reg.r_ax!=0x004F)
return 1;
else
return 0;
}
int GetVesaMode(void)
{
union REGS in,out;
in.x.ax=0x4F03;
int86(0x10,&in,&out);
return out.x.bx;
}
void SetVesaMode(int mode)
{
struct REGPACK reg;
oldmode = GetVesaMode();
reg.r_ax = 0x4F02;
reg.r_bx=mode;
intr(0x10,®);
GetSvgaModeInfo(GetVesaMode(), &modeinfo);
xres = modeinfo.XResolution;
yres = modeinfo.YResolution;
maxx=xres;
bytesperline = modeinfo.BytesperScanLine;
bankshift = 0;
while((unsigned ) (64 >> bankshift)!= modeinfo.WindowGranularity)
bankshift++;
bankswitch = modeinfo.WinFuncPtr;
curbank=-1;
screenptr = (char far *)( ((long) 0xA000 )<<16 | 0);
return ;
}
void setbank(int bank)
{
if(bank==curbank)
return;
curbank = bank;
bank<<=bankshift;
_BX=0;
_DX=bank;
bankswitch();
_BX=1;
bankswitch();
return ;
}
void SetPalette(RGB pal[256])
{
union REGS reg;
struct SREGS inreg;
reg.x.ax=0x1012;
segread(&inreg);
inreg.es = inreg.ds;
reg.x.bx=0;
reg.x.cx=256;
reg.x.dx=(int ) &pal[0];
int86x(0x10,®,®,&inreg);
return ;
}
void vputpixel(int x,int y,int c)
{
long addr = (long ) y * bytesperline + x;
setbank((int) (addr>>16));
*(screenptr+(addr & 0xFFFF))=(char) c;
return;
}
void setwidth(int width)
{
union REGS in,out;
in.x.ax = 0x4F06;
in.x.bx=0x0000;
in.x.cx=width;
int86(0x10,&in,&out);
bytesperline = (int ) out.x.bx;
maxy = (int ) out.x.dx;
maxx = (int ) out.x.cx;
return ;
}
void vinitgraph(int mode)
{
SetVesaMode(mode);
setwidth(xres);
return ;
}
void vclosegraph(void)
{
vinitgraph(oldmode);
/* union REGS regs;
regs.h.ah = 0x00;
regs.h.al = 0x03;
int86(0x10, ®s, ®s);*/
maxx=xres;
}
void startaddr(int *xs,int *ys,int mode)
{
union REGS in,out;
in.x.ax = 0x4F07;
if(mode==0)
{
in.x.bx=0x0000;
in.x.cx=*xs;
in.x.dx = *ys;
}
else
in.x.bx = 0x0001;
int86(0x10,&in,&out);
if(mode==1)
{
*xs = out.x.cx;
*ys = out.x.dx;
}
return ;
}
char ISValidBitmap(char *fname)
{
BMPINFO bmpinfo;
FILE *fp;
if((fp = fopen(fname,"rb+"))==NULL)
{
printf("
Unable open the file %s",fname,"!!");
return 0;
}
fread(&bmpinfo,sizeof(bmpinfo),1,fp);
fclose(fp);
if(!(bmpinfo.bmiheader.bftype[0]=='B' &&
bmpinfo.bmiheader.bftype[1]=='M'))
{
printf("
can't read the file: not a valid BMP file!");
return 0;
}
if(!bmpinfo.bmiheader.bicompression==0)
{
printf("
can't read the file: should not be a RLR encoded!!");
return 0;
}
if(!bmpinfo.bmiheader.bibitcount==8)
{
printf("can't read the file: should be 8-bit per color
format!!");
return 0;
}
return 1;
}
void showbitmap(char *infname,int xs,int ys)
{
BMPINFO bmpinfo;
RGB pal[256];
FILE *fpt;
int i,j,w,h,c,bank;
unsigned char byte[1056];
long addr;
unsigned int k;
if((fpt=fopen(infname,"rb+"))==NULL)
{
printf("
Error opening file ");
getch();
return 1;
}
fread(&bmpinfo,sizeof(bmpinfo),1,fpt);
fseek(fpt,bmpinfo.bmiheader.bfoffbits,SEEK_SET);
w = bmpinfo.bmiheader.biwidth;
h = bmpinfo.bmiheader.biheight;
for(i=0;i<=255;i++)
{
pal[i].red = bmpinfo.bmicolors[i].red/4;
pal[i].green = bmpinfo.bmicolors[i].green/4;
pal[i].blue = bmpinfo.bmicolors[i].blue/4;
}
vinitgraph(VGALOW);
setwidth(1000);
SetPalette(pal);
for(i=0;i<h;i++)
{
fread(&byte[0],sizeof(unsigned char),w,fpt);
for(j=0;j<w;j++)
{
c= (int ) byte[j];
addr= (long) (ys+h-i)*bytesperline+xs+j;
bank = (int ) (addr >>16);
if(curbank!= bank)
{
curbank =bank;
bank<<=bankshift;
_BX=0;
_DX=bank;
bankswitch();
_BX=1;
bankswitch();
}
*(screenptr+(addr & 0xFFFF)) = (char ) c;
}
}
fclose(fpt);
getch();
vclosegraph();
return 0;
}
int ColorToGrey(char *infname,int xs,int ys)
{
BMPINFO bmpinfo;
FILE *fpt1,*fpt2;
char fname[13];
unsigned char r,g,b,byte[1056],pal[256];
double e,grey;
int i,j,h,w,pcnt=0;
long size,curpos;
strcpy(fname,infname);
fpt2=fopen("Grey.bmp","wb");
if((fpt1=fopen(fname,"rb+"))==NULL)
{
printf("can't open the file %s",infname);
getch();
return 1;
}
clrscr();
printf("Preparing taget file..");
fseek(fpt1,0,SEEK_END);
size = ftell(fpt1) + 256;
fseek(fpt1,0,SEEK_SET);
fread(&bmpinfo,sizeof(bmpinfo),1,fpt1);
curpos=ftell(fpt1);
pcnt = (int )ceil((float) curpos *100.0/(float) size);
gotoxy(25,1);
printf("%d completed",pcnt);
for(i=0;i<=255;i++)
{
r = bmpinfo.bmicolors[i].red;
g = bmpinfo.bmicolors[i].green;
b = bmpinfo.bmicolors[i].blue;
grey = (double) 0.3 * (double ) r+ (double ) 0.11 * (double ) b
+
(double ) 0.59 * (double) g;
if(grey-(int) grey >=0.5)
grey++;
if(grey>255)
grey=255;
pal[i]=(unsigned char ) grey;
bmpinfo.bmicolors[i].red = (unsigned char ) i;
bmpinfo.bmicolors[i].green = (unsigned char ) i;
bmpinfo.bmicolors[i].blue = (unsigned char ) i;
curpos++;
}
bmpinfo.bmiheader.biclrused=0;
i = bmpinfo.bmiheader.bfoffbits;
bmpinfo.bmiheader.bfoffbits=1078;
fwrite(&bmpinfo,sizeof(bmpinfo),1,fpt2);
fseek(fpt1,i,SEEK_SET);
fseek(fpt2,bmpinfo.bmiheader.bfoffbits,SEEK_SET);
w=bmpinfo.bmiheader.biwidth;
h = bmpinfo.bmiheader.biheight;
curpos = ftell(fpt1) + 256;
for(i=0;i<h;i++)
{
fread(&byte[0],sizeof(unsigned char),w,fpt2);
for(j=0;j<w;j++)
byte[j]=pal[byte[j]];
fwrite(&byte[0],sizeof(unsigned char ) , w,fpt2);
curpos+=w;
pcnt = (int )ceil((float) curpos *100.0/(float) size);
gotoxy(25,1);
printf("%d completed",pcnt);
}
fclose(fpt1);
fclose(fpt2);
showbitmap("Grey.bmp",xs,ys);
return 0;
}
void main()
{
char file[13];
memset(file,0,13);
clrscr();
printf("
Enter the file name[*.bmp]:");
scanf("%s",file);
if(IsValidBitmap(file))
showbitmap(file,0,0);
else
printf("
Not a valid bitmap file");
printf("
That's all folks");
getch();
} |
|
|