-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHistogramEq.c
236 lines (186 loc) · 9.59 KB
/
HistogramEq.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
/*
Copyright (C) 2016-2020 Digital Image Processing Team
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define DEBUG 0
#define MAX_INTENSITY 256
/*Structure for BMP Header*/
struct bmpheader {
unsigned char id1; // first 2-bytes for BM ID field [1-byte]
unsigned char id2; // ;; [1-byte]
unsigned int size; // Size of the BMP file
unsigned short int app_spec_1; // Application specific
unsigned short int app_spec_2; // Application specific
unsigned int offset; // Offset where the pixel array (bitmap data) can be found
};
/*Structure for DIB [Device Independent Bitmap] Header*/
struct dibheader {
unsigned int size; // Number of bytes in the DIB header (from this point)
int width; // Width of the bitmap in pixels
int height; // Height of the bitmap in pixels. Positive for bottom to top pixel order
unsigned short int color_planes; // Number of color planes being used
unsigned short int bits_per_pixel; // Number of bits per pixel
unsigned int compression; // BI_RGB, pixel array compression used
unsigned int size_with_padding; // Size of the raw bitmap data (including padding)
unsigned int resolution_horizontal; // resolution of the image (horizontal)
unsigned int resolution_vertical; // resolution of the image (vertical)
unsigned int color_palette; // Number of colors in the palette
unsigned int important_colors; // Number of important colors; 0 means all colors are important
};
struct color {
unsigned char r;
unsigned char g;
unsigned char b;
};
int BMPHistEq(struct color *image, int width, int height, struct bmpheader h0, struct dibheader h1) {
int *pixel_arr = (int *) malloc(width*height*sizeof(int));
// int *mod_pixel_arr = (int *) malloc(width*height*sizeof(int));
int i, j, max_pixel_value=0;
double temp=0.0;
int color_map[MAX_INTENSITY] = {0}; // map of intensity values from 0-255
double PMF[MAX_INTENSITY] = {0.0};
double CDF[MAX_INTENSITY] = {0.0};
int DF[MAX_INTENSITY] = {0};
int total_pixels = width*height;
FILE *fp;
char filename[100];
#ifdef DEBUG
// printf("Size of pixel array: %d\n\n", sizeof(*pixel_arr));
#endif
for(i=0;i<height*width; i++) {
*(pixel_arr+i) = image[i].r; // red pixel only
}
// Count frequencies of pixel values in 0-255
// and finding max pixel
for(i=0; i<height*width; i++) {
if(max_pixel_value < *(pixel_arr+i))
max_pixel_value = *(pixel_arr+i);
color_map[*(pixel_arr+i)]++;
}
printf("\nMax pixel value is %d", max_pixel_value);
for(i=0; i<MAX_INTENSITY; i++) {
PMF[i] = (double) color_map[i] / (double) total_pixels;
}
// Calculate CDF (Cumulative Distributive Function)
for(i=0; i<MAX_INTENSITY; i++) {
temp = temp + PMF[i];
CDF[i] = temp;
}
// CDF value with (Gray levels (minus) 1)
for(i=0; i<MAX_INTENSITY; i++) {
temp = temp + PMF[*(pixel_arr+i)];
DF[i] = (int) round(max_pixel_value * CDF[i]);
}
for(i=0; i<width*height; i++) {
image[i].r = DF[*(pixel_arr+i)];
}
// Write equalized image to file...
printf("\nEnter name of histogram equalized image file: ");
scanf("%s", filename);
if((fp=fopen(filename, "wb")) == NULL) {
printf("\nError, creating BMP file\n");
return -1;
}
fwrite(&h0.id1, 1, sizeof(h0.id1), fp);
fwrite(&h0.id2, 1, sizeof(h0.id2), fp);
fwrite(&h0.size, 1, sizeof(h0.size), fp);
fwrite(&h0.app_spec_1, 1, sizeof(h0.app_spec_1), fp);
fwrite(&h0.app_spec_2, 1, sizeof(h0.app_spec_2), fp);
fwrite(&h0.offset, 1, sizeof(h0.offset), fp);
fwrite(&h1.size, 1, sizeof(h1.size), fp);
fwrite(&h1.width, 1, sizeof(h1.width), fp);
fwrite(&h1.height, 1, sizeof(h1.height), fp);
fwrite(&h1.color_planes, 1, sizeof(h1.color_planes), fp);
fwrite(&h1.bits_per_pixel, 1, sizeof(h1.bits_per_pixel), fp);
fwrite(&h1.compression, 1, sizeof(h1.compression), fp);
fwrite(&h1.size_with_padding, 1, sizeof(h1.size_with_padding), fp);
fwrite(&h1.resolution_horizontal, 1, sizeof(h1.resolution_horizontal), fp);
fwrite(&h1.resolution_vertical, 1, sizeof(h1.resolution_vertical), fp);
fwrite(&h1.color_palette, 1, sizeof(h1.color_palette), fp);
fwrite(&h1.important_colors, 1, sizeof(h1.important_colors), fp);
printf("Debug: File pointer is at %d bytes\n", ftell(fp));
// Copy modified pixels...
fseek(fp,54,SEEK_SET);
for(i=0; i<height; i++)
for(j=0; j<width; j++)
fwrite((image+i*width+j),1,sizeof(struct color),fp);
free(pixel_arr);
return 0;
}
void printColor(struct color *image, int width, int height) {
long long int image_size = sizeof(*image)*width*height;
int i=0;
while(i<image_size) {
printf("%d ", (image+i)->r);
i++;
}
printf("\n\nTotal count of pixels: %d\n", i);
}
int main() {
char filename[100]; // Holds input BMP Image filename
FILE *fp;
int i,j; // Loop variables
int status;
printf("*****************************************************************\n");
printf("\tHistogram Equalization on BMP Image (24-Bit non alpha)\n");
printf("*****************************************************************\n\n");
printf("\nEnter a BMP image filename: ");
scanf("%s", filename);
if((fp = fopen(filename, "rb"))==NULL) { // Open file in read as byte mode
fclose(fp);
printf("\nError, cannot open file.\nTerminating...\n");
return 0;
}
struct bmpheader header0;
struct dibheader header1;
struct color *image;
fread(&header0.id1, sizeof(header0.id1), 1, fp);
fread(&header0.id2, sizeof(header0.id2), 1, fp);
fread(&header0.size, sizeof(header0.size), 1, fp);
fread(&header0.app_spec_1, sizeof(header0.app_spec_1), 1, fp);
fread(&header0.app_spec_2, sizeof(header0.app_spec_2), 1, fp);
fread(&header0.offset, sizeof(header0.offset), 1, fp);
fread(&header1.size, sizeof(header1.size), 1, fp);
fread(&header1.width, sizeof(header1.width), 1, fp);
fread(&header1.height, sizeof(header1.height), 1, fp);
fread(&header1.color_planes, sizeof(header1.color_planes), 1, fp);
fread(&header1.bits_per_pixel, sizeof(header1.bits_per_pixel), 1, fp);
fread(&header1.compression, sizeof(header1.compression), 1, fp);
fread(&header1.size_with_padding, sizeof(header1.size_with_padding), 1, fp);
fread(&header1.resolution_horizontal, sizeof(header1.resolution_horizontal), 1, fp);
fread(&header1.resolution_vertical, sizeof(header1.resolution_vertical), 1, fp);
fread(&header1.color_palette, sizeof(header1.color_palette), 1, fp);
fread(&header1.important_colors, sizeof(header1.important_colors), 1, fp);
printf("\n\nBMP Header Statistics:");
printf("\nID feild = %c%c\nSize of BMP file = %lu\nApplication specific = %d\nApplication specific = %d\nOffset where the pixel array (bitmap data) can be found = %lu\n", header0.id1, header0.id2, header0.size, header0.app_spec_1, header0.app_spec_2, header0.offset);
printf("\n\nDIB Header Statistics:");
printf("\nNumber of bytes in the DIB header = %lu\nWidth of the bitmap in pixels = %d\nHeight of the bitmap in pixels = %d\nNumber of color planes being used = %d\nNumber of bits per pixel = %d\nNumber of color planes being used = %d\nNumber of bits per pixel = %d\nBI_RGB, pixel array compression used = %lu\nSize of the raw bitmap data (including padding) = %lu\nResolution of the image (horizontal) = %lu\nResolution of the image (vertical) = %lu\nNumber of colors in the palette = %lu\nNumber of important colors; 0 means all colors are important = %lu\n", header1.size, header1.width, header1.height, header1.color_planes, header1.bits_per_pixel, header1.size_with_padding, header1.resolution_horizontal, header1.resolution_vertical, header1.color_palette, header1.important_colors);
printf("Debug: File pointer is at %d bytes\n", ftell(fp));
image=(struct color *)malloc(header1.width*header1.height*sizeof(struct color)); // Allocate memory for pixel array
fseek(fp,54,SEEK_SET); // Start of pixel array (bitmap data) -- Optional statement; already at 54th bytes
// Populating image data...
for(i=0;i<header1.height;i++)
for(j=0;j<header1.width;j++)
fread((image+i*header1.width+j),sizeof(struct color),1,fp);
fclose(fp); // Close file pointer
#ifdef DEBUG
// printColor(image, header1.width, header1.height);
#endif
status = BMPHistEq(image, header1.width, header1.height, header0, header1);
if(status == -1) printf("\nFailed to successfully convert image\n");
else printf("\nSuccessfully equalized image\n");
return 0;
}