Add to Favorites    Make Home Page 3006 Online  
 Language Categories  
 Our Services  

Home » C Home » Multimedia and Sound Home » C program to display bitmap images(*.bmp)


Search Projects & Source Codes:

Title C program to display bitmap images(*.bmp)
Author Krishna Chaitanya
Author Email krishnaiete2003 [at]
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.
Category C » Multimedia and Sound
Hits 379171
Code Select and Copy the Code
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.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;; 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;; segread(&inreg); = inreg.ds; reg.x.bx=0;; 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; = 0x4F06; in.x.bx=0x0000;; int86(0x10,&in,&out); bytesperline = (int ) out.x.bx; maxy = (int ) out.x.dx; maxx = (int ); return ; } void vinitgraph(int mode) { SetVesaMode(mode); setwidth(xres); return ; } void vclosegraph(void) { vinitgraph(oldmode); /* union REGS regs; regs.h.ah = 0x00; = 0x03; int86(0x10, ®s, ®s);*/ maxx=xres; } void startaddr(int *xs,int *ys,int mode) { union REGS in,out; = 0x4F07; if(mode==0) { in.x.bx=0x0000;*xs; in.x.dx = *ys; } else in.x.bx = 0x0001; int86(0x10,&in,&out); if(mode==1) { *xs =; *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(); }

Related Source Codes

Script Name Author
The Game Opposite as seen on Nokia 2300 Mobile Manikanta
Radix Sort ashish
Change your mouse pointer Ashim
The blinking star Shashank
Data Validation Crylittlebaby
To search a file by giving file type like mp3 or mpeg or doc Prashanth SR
Menus Demonstration B.Chidhambaram
Employee Database Project Using C. Reenku Raman Nayak
Creating a Lexical Analyzer in c fahad bader al-buhairi դ ? ??
Calendar Program Omkar & Devendra
Stop double Process for start in C Cedrik Jurak
Stop double Process for start in C Cedrik Jurak
Time Scheduler Atiq Anwar
A timepass game between atmost two players Rahul Roy


Google Groups Subscribe to SourceCodesWorld - Techies Talk

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!

ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page


Google Search


Source Codes is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024, All Rights Reserved.
Page URL:

Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review