Skip to content

Instantly share code, notes, and snippets.

@KevinKu
Created October 29, 2015 14:41
Show Gist options
  • Save KevinKu/f59f9fdc25cb5f569f40 to your computer and use it in GitHub Desktop.
Save KevinKu/f59f9fdc25cb5f569f40 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
int Hist_Eq(unsigned char *,unsigned char *, long , long );
int bmp_write(unsigned char *, unsigned char *,int , int , char *);
int bmp_read(unsigned char *,unsigned char *, int xsize, int , const char *);
int main() {
unsigned char *image;
unsigned char *newimage;
unsigned char header[54];
int xsize = 256;
int ysize = 256;
image = (unsigned char *)malloc((size_t)(xsize+2) * (ysize+2) );
newimage = (unsigned char *)malloc((size_t)(xsize+2) * (ysize+2) );
if (image == NULL)
return -1;
bmp_read(image,header, xsize, ysize, "lena");
Hist_Eq(image,newimage,xsize,ysize);
bmp_write(newimage,header, xsize, ysize, "NEW_lena");
free(image);
free(newimage);
image = (unsigned char *)malloc((size_t)(xsize+2) * (ysize+2) );
newimage = (unsigned char *)malloc((size_t)(xsize+2) * (ysize+2) );
bmp_read(image,header, xsize, ysize, "HH-60");
Hist_Eq(image,newimage,xsize,ysize);
bmp_write(newimage,header, xsize, ysize, "NEW_HH-60");
free(image);
free(newimage);
}
int Hist_Eq(unsigned char *image,unsigned char *newimage, long xsize, long ysize)
{
int i,j;
int x=xsize+2;
int y=ysize+2;
int temp=0;
int gray[256]={0};
double hist_array[256]={0},totalp=0;
double hist_array_sum[256]={0};
totalp=(double) x * (double) y;
for(i=0;i<x*y;i++){
gray[image[i]]++;
}
for(i=0;i<256;i++){
hist_array[i]=(double)gray[i]/(double) totalp;
}
hist_array_sum[0]=hist_array[0];
for(i=1;i<256;i++){
hist_array_sum[i]=hist_array_sum[i-1]+hist_array[i];
}
for(i=0;i<x*y;i++){
newimage[i]=hist_array_sum[image[i]]*255;
}
return 0;
}
int bmp_read(unsigned char *image,unsigned char *header, int xsize, int ysize, const char *filename) {
char fname_bmp[128];
FILE *fp;
sprintf(fname_bmp, "%s.bmp", filename);
if (!(fp = fopen(fname_bmp, "rb")))
return -1;
fread(header, sizeof(unsigned char), 54, fp);
fread(image, sizeof(unsigned char), (size_t)(long)(xsize+2) * (ysize+2), fp);
fclose(fp);
return 0;
}
int bmp_write(unsigned char *image, unsigned char *header,int xsize, int ysize, char *filename) {
/*unsigned char header[54] = {
0x42, 0x4d, 0, 0, 0, 0, 0, 0, 0, 0,
54, 0, 0, 0, 40, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 0, 24, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0
};
*/
long file_size = (long)(xsize+2) * (long)(ysize+2) + 54;
long width, height;
char fname_bmp[128];
FILE *fp;
/*header[2] = (unsigned char)(file_size &0x000000ff);
header[3] = (file_size >> 8) & 0x000000ff;
header[4] = (file_size >> 16) & 0x000000ff;
header[5] = (file_size >> 24) & 0x000000ff;
width = xsize;
header[18] = width & 0x000000ff;
header[19] = (width >> 8) &0x000000ff;
header[20] = (width >> 16) &0x000000ff;
header[21] = (width >> 24) &0x000000ff;
height = ysize;
header[22] = height &0x000000ff;
header[23] = (height >> 8) &0x000000ff;
header[24] = (height >> 16) &0x000000ff;
header[25] = (height >> 24) &0x000000ff;
*/
sprintf(fname_bmp, "%s.bmp", filename);
if (!(fp = fopen(fname_bmp, "wb")))
return -1;
fwrite(header, sizeof(unsigned char), 54, fp);
fwrite(image, sizeof(unsigned char), (size_t)(long)(xsize+2) * (ysize+2), fp);
fclose(fp);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment