sandipan

An old C code to display 256-color image in DOS and perform

Sep 5th, 2018
508
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 6.17 KB | None | 0 0
  1. /*
  2.  C code to display 256-color image in DOS and perform Histogram Equalization
  3.  Sandipan Dey
  4.  BCSE, JU, Kolkata
  5.  2003
  6. */
  7.  
  8. // MUST USE MEDIUM MEMORY MODEL
  9. // USE 256 COLOR BITMAP FILE (WIDTH%4=0)
  10. // INPUT IMAGE BEANS.BMP
  11.  
  12. #include <math.h>
  13. #include <stdio.h>
  14. #include <conio.h>
  15. #include <stdlib.h>
  16. #include <graphics.h>
  17. #include <string.h>
  18. #define DEFAULT "output.bmp"
  19. #define MAX 256
  20. #define PI 3.14159
  21.  
  22. int FindAllRGB(int* b) {
  23.     for(int i=0;i<MAX;++i)
  24.         if(b[i]!=i)return 0;
  25.         else return 1;
  26. }
  27.  
  28. void Help() {
  29.     printf("\nThis Program Takes an Image & Performs Histogram Equalization &");
  30.     printf("\nStores it as another Image.");
  31.     printf("\nTo Run the .Exe File:");
  32.     printf("\nHEQ[.EXE] InputFileName [OutputFileName]");
  33.     printf("\nThe following conditions must be satisfied:");
  34.     printf("\n1)The Input Image must be an 8X8 (256-color) .BMP File");
  35.     printf("\n2)No other Format currently supported.");
  36.     printf("\n3)Default OutputFile is 'Output.BMP'");
  37.     printf("\n4)The Input BMP & EGAVGA.BGI must be in the same Directory as the .EXE File");
  38.     printf("\n5)N.B: For exact & correct Output All the Gray Levels [0..L-1]");
  39.     printf("\n  MUST BE PRESENT in the RGB Palette of the Input BMP ");
  40.     printf("\n  {L=Maximum Gray Level Possible}");
  41.     printf("\n  Written By:-Sandipan Dey,BCSE-IV,Roll-99710,J.U.");
  42. }
  43.  
  44. void main(int argc,char* argv[]) {
  45.     FILE *fp=NULL,*fo=NULL;
  46.     long int n[MAX]={0}, n1[MAX]={0};
  47.     int c, type;
  48.     char INPUTFILE[100], OUTPUTFILE[100];
  49.     long int imgoffset=0;
  50.     double sum=0, p[MAX]={0}, p1=0;
  51.     strcpy(OUTPUTFILE,DEFAULT);
  52.     clrscr();
  53.     if(argc<=1) {
  54.         printf("\nNo Input File!");
  55.         getch();
  56.         exit(0);
  57.     }
  58.     strcpy(INPUTFILE,argv[1]);
  59.     if(argc>2 && strcmp(argv[2],"\0"))
  60.         strcpy(OUTPUTFILE,argv[2]);
  61.     if((fp=fopen(INPUTFILE,"rb"))==NULL) {
  62.         printf("\n File Not Found!");
  63.         fclose(fp);
  64.         getch();
  65.         exit(0);
  66.     }    // File Not Found
  67.     fo = fopen(OUTPUTFILE, "wb");
  68.     if (fgetc(fp)!='B' || fgetc(fp)!='M') {      // Check Signature
  69.         printf("\n Not a valid BMP File!!!");
  70.         fclose(fp);
  71.         fclose(fo);
  72.         getch();
  73.         exit(0);
  74.     }
  75.     fseek(fp,10,0);         // Find Offset of Data
  76.     imgoffset=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);  // Data Offset
  77.     fseek(fp,28,0);         // Find No. of Bits to Represent a Pixel of the Bitmap
  78.     type=fgetc(fp);
  79.     //printf(" %d",type);
  80.     switch(type) {
  81.         case 1:printf("\n A Monochrome (1-Bit) Bitmap!");break;
  82.         case 4:printf("\n A 16-Color (4-Bit) Bitmap!");break;
  83.         case 8:printf("\n A 256-Color (8-Bit) Bitmap");break;
  84.         default:printf("\n Not a 1,4 Or 8-Bit Bitmap!");break;
  85.     }
  86.     if(type!=8) {
  87.         fclose(fp);
  88.         printf("\nAn 8-bit Bitmap expected!!!");
  89.         fclose(fo);
  90.         exit(0);
  91.     }
  92.     fseek(fp,54,0);
  93.     long int sum1=0;
  94.     int r,g,bl,b[MAX]={0};
  95.     for(int t=0;t<MAX;++t) {
  96.         r=fgetc(fp);
  97.         g=fgetc(fp);
  98.         bl=fgetc(fp);
  99.         sum1=(r+g+bl)/3;
  100.         fgetc(fp);
  101.         b[t]=sum1;
  102.     }
  103.     if(!FindAllRGB(b)){
  104.         printf("\nNot All Gray Levels between 0 & %d are Present in RGB Palette!!!",MAX);
  105.         getch();/*exit(0);*/
  106.     }
  107.     fseek(fp,0,0);      // Goto Start of Bitmap
  108.     while(!feof(fp)) {
  109.         fputc(fgetc(fp),fo);
  110.     }
  111.     fclose(fp);
  112.     fp=fopen(INPUTFILE,"rb");
  113.     long int w,h;
  114.     fseek(fp,18,0);
  115.     w=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
  116.     h=fgetc(fp)+(fgetc(fp)<<8)+(fgetc(fp)<<16)+(fgetc(fp)<<24);
  117.     fseek(fp,imgoffset,0);
  118.     int gd=VGA,gm=VGAHI,x=0,y=h;
  119.     struct palettetype pal;
  120.     initgraph(&gd,&gm,"");
  121.     getpalette(&pal);
  122.     for (int i=0; i<16; i++)
  123.         setrgbpalette(pal.colors[i],i*4,i*4,i*4);
  124.     fseek(fp,imgoffset,0);
  125.     while(!feof(fp)) {
  126.         c=fgetc(fp);
  127.         putpixel(x,y,c/16);
  128.         x=(x+1)%w;
  129.         if(!x){
  130.             --y;
  131.         }
  132.         ++n[c%MAX];
  133.     }
  134.     outtextxy(520,450,"Before");
  135.     outtextxy(520,460,"Histogram");
  136.     outtextxy(520,470,"Equalization");
  137.     fclose(fp);
  138.     getch();
  139.     cleardevice();
  140.     for(i=0;i<MAX;++i) {
  141.         sum+=n[i];
  142.     }
  143.     for(i=0;i<MAX;++i) {
  144.         p[i]=n[i]/(sum*1.0);
  145.         p1=0;
  146.         for(int j=0;j<=i;++j)
  147.             p1+=p[j];
  148.         n1[i]=(MAX-1)*p1;
  149.     }
  150.     fp=fopen(INPUTFILE,"rb");
  151.     fseek(fp,imgoffset,0);
  152.     fseek(fo,imgoffset,0);
  153.     x=0;y=h;
  154.     while(!feof(fp)) {
  155.         c=fgetc(fp);
  156.         putpixel(x,y,n1[c]/16);
  157.         x=(x+1)%w;
  158.         if(!x){--y;}
  159.         fputc(n1[c],fo);
  160.     }
  161.     fclose(fp);
  162.     fclose(fo);
  163.     outtextxy(520,400,"After");
  164.     outtextxy(520,410,"Histogram");
  165.     outtextxy(520,419,"Equalization");
  166.     outtextxy(520,445,"Sandipan Dey");
  167.     outtextxy(520,455,"BCSE-IV");
  168.     outtextxy(520,465,"Roll-99710");
  169.     getch();
  170.     cleardevice();
  171.     //closegraph();
  172.     gd=DETECT;
  173.     initgraph(&gd,&gm,"");
  174.     int xstart=10,ystart=470;
  175.     line(xstart,ystart,620,ystart);
  176.     line(xstart,ystart-470,xstart,ystart);
  177.     char s[5];
  178.     setcolor(14);
  179.     for(i=0;i<MAX;++i) {
  180.         line(xstart+2*i,ystart-5000*p[i],xstart+2*i,ystart);
  181.         if(!(i%15)){
  182.             setcolor(getmaxcolor());
  183.             line(xstart+2*i,ystart-5,xstart+2*i,ystart+1);
  184.             sprintf(s,"%d",i);
  185.             outtextxy(xstart+2*i-7,ystart+2,s);
  186.             setcolor(14);
  187.         }
  188.     }
  189.     fflush(stdin);
  190.     getch();
  191.     for(i=0;i<MAX;++i)n[i]=0;
  192.     fo=fopen(OUTPUTFILE,"rb");
  193.     fseek(fo,imgoffset,0);
  194.     while(!feof(fo)){
  195.         c=fgetc(fp);
  196.         ++n[c%MAX];
  197.     }
  198.     fclose(fo);
  199.     sum=0;
  200.     for(i=0;i<MAX;++i) {
  201.         sum+=n[i];
  202.     }
  203.     for(i=0;i<MAX;++i)
  204.     p[i]=n[i]/(sum*1.0);
  205.     setcolor(12);
  206.     for(i=0;i<MAX;++i)  {
  207.         line(xstart+2*i,ystart-5000*p[i],xstart+2*i,ystart);
  208.         if(!(i%15)){
  209.             setcolor(getmaxcolor());
  210.             line(xstart+2*i,ystart-5,xstart+2*i,ystart+1);
  211.             sprintf(s,"%d",i);
  212.             outtextxy(xstart+2*i-7,ystart+2,s);
  213.             setcolor(12);
  214.         }
  215.     }
  216.     getch();
  217.     closegraph();
  218. }
Add Comment
Please, Sign In to add comment