sandipan

Old C code for bi-level display of an image in DOS

Sep 6th, 2018
208
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.11 KB | None | 0 0
  1. /*
  2.  C program for bi-level display of an 8-bit BMP image in DOS
  3.  Sandipan Dey
  4.  BCSE, JU, Kolkata
  5.  2003
  6. */
  7.  
  8. // MUST USE MEDIUM MEMORY MODEL
  9. // INPUT IMAGE EINSTEIN.BMP GIRL.BMP: use a small image
  10. #include <stdio.h>
  11. #include <conio.h>
  12. #include <stdlib.h>
  13. #include <graphics.h>
  14. #include <string.h>
  15. #define DEFAULT "output.bmp"
  16. #define MAX 256
  17.  
  18. void fillmatrix(unsigned char a[8],int n) {
  19.     int c=0,x,y;
  20.     int k;
  21.     while(c < n) {
  22.         x=random(8);
  23.         y=random(8);
  24.         k=1;
  25.         for(int j=0;j<y;++j,k<<=1);
  26.         if(!(a[x] & k)){a[x]+=k;++c;}
  27.     }
  28. }
  29.  
  30. int FindAllRGB(int* b) {
  31.     for(int i=0;i<MAX;++i)
  32.     if(b[i]!=i)return 0;
  33.     else return 1;
  34. }
  35.  
  36. void main(int argc,char* argv[]) {
  37.     FILE* fp=NULL;
  38.     int c,type;
  39.     char INPUTFILE[100];
  40.     long int imgoffset=0;
  41.     if(argc<=1) {
  42.         printf("\nNo Input File!");
  43.         getch();
  44.         exit(0);
  45.     }
  46.     strcpy(INPUTFILE,argv[1]);
  47.     if((fp=fopen(INPUTFILE,"rb"))==NULL) {
  48.         printf("\n File Not Found!");
  49.         fclose(fp);
  50.         getch();
  51.         exit(0);
  52.     }    // File Not Found
  53.     if(fgetc(fp)!='B' || fgetc(fp)!='M') {      // Check BMP Signature
  54.         printf("\n Not a valid BMP File!!!");
  55.         fclose(fp);
  56.         getch();
  57.         exit(0);
  58.     }
  59.     fseek(fp,10,0);                             // Find Offset of Data
  60.     imgoffset=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);  // Data Offset
  61.     fseek(fp,28,0);                             // Find No. of Bits to Represent a Pixel of the Bitmap
  62.     type=fgetc(fp);
  63.     switch(type) {
  64.         case 1:printf("\n A Monochrome (1-Bit) Bitmap!");break;
  65.         case 4:printf("\n A 16-Color (4-Bit) Bitmap!");break;
  66.         case 8:printf("\n A 256-Color (8-Bit) Bitmap");break;
  67.         default:printf("\n Not a 1,4 Or 8-Bit Bitmap!");break;
  68.     }
  69.     if(type!=8){
  70.         fclose(fp);
  71.         printf("\nAn 8-bit Bitmap expected!!!");
  72.         exit(0);
  73.     }
  74.     fseek(fp,54,0);
  75.     long int sum1=0;
  76.     int r,g,bl,b[MAX]={0};
  77.     for(int t=0;t<MAX;++t) {
  78.         r=fgetc(fp);
  79.         g=fgetc(fp);
  80.         bl=fgetc(fp);
  81.         sum1=(r+g+bl)/3;
  82.         fgetc(fp);
  83.         b[t]=sum1;
  84.     }
  85.     if(!FindAllRGB(b)){
  86.         printf("\nNot All Gray Levels between 0 & %d are Present in RGB Palette!!!",MAX);
  87.         getch();/*exit(0);*/
  88.     }
  89.     long int w,h;
  90.     fseek(fp,18,0);
  91.     w=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
  92.     h=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
  93.     fseek(fp,imgoffset,0);
  94.     int gd=VGA,gm=VGAHI;
  95.     long int x=0,y=h;
  96.     struct palettetype pal;
  97.     initgraph(&gd,&gm,"");
  98.     getpalette(&pal);
  99.     for (int i=0; i<16; i++)
  100.     setrgbpalette(pal.colors[i],i*4,i*4,i*4);
  101.     fseek(fp,imgoffset,0);
  102.     while(!feof(fp)) {
  103.         c=fgetc(fp);
  104.         putpixel(x,y,c/16);
  105.         x=(x+1)%w;
  106.         if(!x){
  107.             --y;
  108.         }
  109.     }
  110.     outtextxy(500,450,"Gray Level Image");
  111.     getch();
  112.     cleardevice();
  113.     unsigned char a[8]={0};
  114.     fseek(fp,imgoffset,0);
  115.     x=0;y=472;
  116.     randomize();
  117.     while(!feof(fp)) {
  118.         for(int i=0;i<8;++i)
  119.             a[i]=0;
  120.         c=fgetc(fp);
  121.         fillmatrix(a,c/8);
  122.         for(i=0;i<8;++i) {
  123.             for(int j=1,k=0;k<8;j<<=1,++k)
  124.                 if((a[i] & j)==j)
  125.                     putpixel(x+k,y+i,WHITE);
  126.         }
  127.         x=(x+8)%(w*8);
  128.         if(!x)y-=8;
  129.     }
  130.     outtextxy(485,420,"Bilevel Display");
  131.     outtextxy(485,441,"Sandipan Dey");
  132.     outtextxy(485,451,"BCSE-IV");
  133.     outtextxy(485,461,"Roll-99710");
  134.     outtextxy(485,471,"Jadavpur University");
  135.     fclose(fp);
  136.     getch();
  137.     closegraph();
  138. }
Add Comment
Please, Sign In to add comment