Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C program for bi-level display of an 8-bit BMP image in DOS
- Sandipan Dey
- BCSE, JU, Kolkata
- 2003
- */
- // MUST USE MEDIUM MEMORY MODEL
- // INPUT IMAGE EINSTEIN.BMP GIRL.BMP: use a small image
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include <string.h>
- #define DEFAULT "output.bmp"
- #define MAX 256
- void fillmatrix(unsigned char a[8],int n) {
- int c=0,x,y;
- int k;
- while(c < n) {
- x=random(8);
- y=random(8);
- k=1;
- for(int j=0;j<y;++j,k<<=1);
- if(!(a[x] & k)){a[x]+=k;++c;}
- }
- }
- int FindAllRGB(int* b) {
- for(int i=0;i<MAX;++i)
- if(b[i]!=i)return 0;
- else return 1;
- }
- void main(int argc,char* argv[]) {
- FILE* fp=NULL;
- int c,type;
- char INPUTFILE[100];
- long int imgoffset=0;
- if(argc<=1) {
- printf("\nNo Input File!");
- getch();
- exit(0);
- }
- strcpy(INPUTFILE,argv[1]);
- if((fp=fopen(INPUTFILE,"rb"))==NULL) {
- printf("\n File Not Found!");
- fclose(fp);
- getch();
- exit(0);
- } // File Not Found
- if(fgetc(fp)!='B' || fgetc(fp)!='M') { // Check BMP Signature
- printf("\n Not a valid BMP File!!!");
- fclose(fp);
- getch();
- exit(0);
- }
- fseek(fp,10,0); // Find Offset of Data
- imgoffset=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24); // Data Offset
- fseek(fp,28,0); // Find No. of Bits to Represent a Pixel of the Bitmap
- type=fgetc(fp);
- switch(type) {
- case 1:printf("\n A Monochrome (1-Bit) Bitmap!");break;
- case 4:printf("\n A 16-Color (4-Bit) Bitmap!");break;
- case 8:printf("\n A 256-Color (8-Bit) Bitmap");break;
- default:printf("\n Not a 1,4 Or 8-Bit Bitmap!");break;
- }
- if(type!=8){
- fclose(fp);
- printf("\nAn 8-bit Bitmap expected!!!");
- exit(0);
- }
- fseek(fp,54,0);
- long int sum1=0;
- int r,g,bl,b[MAX]={0};
- for(int t=0;t<MAX;++t) {
- r=fgetc(fp);
- g=fgetc(fp);
- bl=fgetc(fp);
- sum1=(r+g+bl)/3;
- fgetc(fp);
- b[t]=sum1;
- }
- if(!FindAllRGB(b)){
- printf("\nNot All Gray Levels between 0 & %d are Present in RGB Palette!!!",MAX);
- getch();/*exit(0);*/
- }
- long int w,h;
- fseek(fp,18,0);
- w=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
- h=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
- fseek(fp,imgoffset,0);
- int gd=VGA,gm=VGAHI;
- long int x=0,y=h;
- struct palettetype pal;
- initgraph(&gd,&gm,"");
- getpalette(&pal);
- for (int i=0; i<16; i++)
- setrgbpalette(pal.colors[i],i*4,i*4,i*4);
- fseek(fp,imgoffset,0);
- while(!feof(fp)) {
- c=fgetc(fp);
- putpixel(x,y,c/16);
- x=(x+1)%w;
- if(!x){
- --y;
- }
- }
- outtextxy(500,450,"Gray Level Image");
- getch();
- cleardevice();
- unsigned char a[8]={0};
- fseek(fp,imgoffset,0);
- x=0;y=472;
- randomize();
- while(!feof(fp)) {
- for(int i=0;i<8;++i)
- a[i]=0;
- c=fgetc(fp);
- fillmatrix(a,c/8);
- for(i=0;i<8;++i) {
- for(int j=1,k=0;k<8;j<<=1,++k)
- if((a[i] & j)==j)
- putpixel(x+k,y+i,WHITE);
- }
- x=(x+8)%(w*8);
- if(!x)y-=8;
- }
- outtextxy(485,420,"Bilevel Display");
- outtextxy(485,441,"Sandipan Dey");
- outtextxy(485,451,"BCSE-IV");
- outtextxy(485,461,"Roll-99710");
- outtextxy(485,471,"Jadavpur University");
- fclose(fp);
- getch();
- closegraph();
- }
Add Comment
Please, Sign In to add comment