Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- C code to display 256-color image in DOS and perform Histogram Equalization
- Sandipan Dey
- BCSE, JU, Kolkata
- 2003
- */
- // MUST USE MEDIUM MEMORY MODEL
- // USE 256 COLOR BITMAP FILE (WIDTH%4=0)
- // INPUT IMAGE BEANS.BMP
- #include <math.h>
- #include <stdio.h>
- #include <conio.h>
- #include <stdlib.h>
- #include <graphics.h>
- #include <string.h>
- #define DEFAULT "output.bmp"
- #define MAX 256
- #define PI 3.14159
- int FindAllRGB(int* b) {
- for(int i=0;i<MAX;++i)
- if(b[i]!=i)return 0;
- else return 1;
- }
- void Help() {
- printf("\nThis Program Takes an Image & Performs Histogram Equalization &");
- printf("\nStores it as another Image.");
- printf("\nTo Run the .Exe File:");
- printf("\nHEQ[.EXE] InputFileName [OutputFileName]");
- printf("\nThe following conditions must be satisfied:");
- printf("\n1)The Input Image must be an 8X8 (256-color) .BMP File");
- printf("\n2)No other Format currently supported.");
- printf("\n3)Default OutputFile is 'Output.BMP'");
- printf("\n4)The Input BMP & EGAVGA.BGI must be in the same Directory as the .EXE File");
- printf("\n5)N.B: For exact & correct Output All the Gray Levels [0..L-1]");
- printf("\n MUST BE PRESENT in the RGB Palette of the Input BMP ");
- printf("\n {L=Maximum Gray Level Possible}");
- printf("\n Written By:-Sandipan Dey,BCSE-IV,Roll-99710,J.U.");
- }
- void main(int argc,char* argv[]) {
- FILE *fp=NULL,*fo=NULL;
- long int n[MAX]={0}, n1[MAX]={0};
- int c, type;
- char INPUTFILE[100], OUTPUTFILE[100];
- long int imgoffset=0;
- double sum=0, p[MAX]={0}, p1=0;
- strcpy(OUTPUTFILE,DEFAULT);
- clrscr();
- if(argc<=1) {
- printf("\nNo Input File!");
- getch();
- exit(0);
- }
- strcpy(INPUTFILE,argv[1]);
- if(argc>2 && strcmp(argv[2],"\0"))
- strcpy(OUTPUTFILE,argv[2]);
- if((fp=fopen(INPUTFILE,"rb"))==NULL) {
- printf("\n File Not Found!");
- fclose(fp);
- getch();
- exit(0);
- } // File Not Found
- fo = fopen(OUTPUTFILE, "wb");
- if (fgetc(fp)!='B' || fgetc(fp)!='M') { // Check Signature
- printf("\n Not a valid BMP File!!!");
- fclose(fp);
- fclose(fo);
- 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);
- //printf(" %d",type);
- 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!!!");
- fclose(fo);
- 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);*/
- }
- fseek(fp,0,0); // Goto Start of Bitmap
- while(!feof(fp)) {
- fputc(fgetc(fp),fo);
- }
- fclose(fp);
- fp=fopen(INPUTFILE,"rb");
- 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,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;
- }
- ++n[c%MAX];
- }
- outtextxy(520,450,"Before");
- outtextxy(520,460,"Histogram");
- outtextxy(520,470,"Equalization");
- fclose(fp);
- getch();
- cleardevice();
- for(i=0;i<MAX;++i) {
- sum+=n[i];
- }
- for(i=0;i<MAX;++i) {
- p[i]=n[i]/(sum*1.0);
- p1=0;
- for(int j=0;j<=i;++j)
- p1+=p[j];
- n1[i]=(MAX-1)*p1;
- }
- fp=fopen(INPUTFILE,"rb");
- fseek(fp,imgoffset,0);
- fseek(fo,imgoffset,0);
- x=0;y=h;
- while(!feof(fp)) {
- c=fgetc(fp);
- putpixel(x,y,n1[c]/16);
- x=(x+1)%w;
- if(!x){--y;}
- fputc(n1[c],fo);
- }
- fclose(fp);
- fclose(fo);
- outtextxy(520,400,"After");
- outtextxy(520,410,"Histogram");
- outtextxy(520,419,"Equalization");
- outtextxy(520,445,"Sandipan Dey");
- outtextxy(520,455,"BCSE-IV");
- outtextxy(520,465,"Roll-99710");
- getch();
- cleardevice();
- //closegraph();
- gd=DETECT;
- initgraph(&gd,&gm,"");
- int xstart=10,ystart=470;
- line(xstart,ystart,620,ystart);
- line(xstart,ystart-470,xstart,ystart);
- char s[5];
- setcolor(14);
- for(i=0;i<MAX;++i) {
- line(xstart+2*i,ystart-5000*p[i],xstart+2*i,ystart);
- if(!(i%15)){
- setcolor(getmaxcolor());
- line(xstart+2*i,ystart-5,xstart+2*i,ystart+1);
- sprintf(s,"%d",i);
- outtextxy(xstart+2*i-7,ystart+2,s);
- setcolor(14);
- }
- }
- fflush(stdin);
- getch();
- for(i=0;i<MAX;++i)n[i]=0;
- fo=fopen(OUTPUTFILE,"rb");
- fseek(fo,imgoffset,0);
- while(!feof(fo)){
- c=fgetc(fp);
- ++n[c%MAX];
- }
- fclose(fo);
- sum=0;
- for(i=0;i<MAX;++i) {
- sum+=n[i];
- }
- for(i=0;i<MAX;++i)
- p[i]=n[i]/(sum*1.0);
- setcolor(12);
- for(i=0;i<MAX;++i) {
- line(xstart+2*i,ystart-5000*p[i],xstart+2*i,ystart);
- if(!(i%15)){
- setcolor(getmaxcolor());
- line(xstart+2*i,ystart-5,xstart+2*i,ystart+1);
- sprintf(s,"%d",i);
- outtextxy(xstart+2*i-7,ystart+2,s);
- setcolor(12);
- }
- }
- getch();
- closegraph();
- }
Add Comment
Please, Sign In to add comment