-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathequalize.cpp
More file actions
153 lines (123 loc) · 3.67 KB
/
Copy pathequalize.cpp
File metadata and controls
153 lines (123 loc) · 3.67 KB
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
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <cassert>
#include "Timer.h"
extern "C" {
#include "ppmb_io.h"
}
struct img {
int xsize;
int ysize;
int maxrgb;
unsigned char *r;
unsigned char *g;
unsigned char *b;
};
int alloc_img(struct img *p) {
p->r = (unsigned char *) malloc(p->xsize * p->ysize * sizeof(char));
p->g = (unsigned char *) malloc(p->xsize * p->ysize * sizeof(char));
p->b = (unsigned char *) malloc(p->xsize * p->ysize * sizeof(char));
if(!p->r || !p->g || !p->b) {
fprintf(stderr, "Failed to allocate memory for image\n");
return 0;
}
return 1;
}
void read_histogram(FILE *f, int **hist, int *N)
{
int j;
if(fscanf(f, "%d", N) != 1) {
fprintf(stderr, "Unable to read length of histogram\n");
exit(1);
};
fprintf(stderr, "Allocating histogram for %d entries\n", *N);
*hist = (int *) calloc(*N, sizeof(int));
for(int i = 0; i < *N; i++) {
fscanf(f, "%d %d", &j, *hist + i);
if(i != j) {
fprintf(stderr, "Histogram indices corrupt!\n");
exit(1);
}
}
}
void gen_xlat(int pixels, int N, int *hist, int **xlat)
{
*xlat = (int *) calloc(N, sizeof(int));
if(!*xlat) {
fprintf(stderr, "Unable to allocate memory for xlat\n");
exit(1);
}
int *xl = *xlat;
int cdf_min = 0;
// first calculate cdf using xl as a temporary space
xl[0] = hist[0];
for(int i = 1; i < N; i++) {
xl[i] = hist[i] + xl[i - 1];
}
// then calculate cdf_min
for(int i = 0; i < N; i++) {
cdf_min = xl[i];
if(cdf_min > 0)
break;
}
// then calculate xlat
xl[0] = hist[0];
for(int i = 1; i < N; i++) {
xl[i] = ((xl[i] - cdf_min) * (N - 1)) / (pixels - cdf_min);
//xl[i] = ((float) (xl[i] - cdf_min) / (pixels - cdf_min)) * (N - 1);
if(xl[i] > 255) xl[i] = 255;
if(xl[i] < 0) xl[i] = 0;
}
}
void equalize(struct img *in, struct img *out, int *xlat_r, int *xlat_g, int *xlat_b) {
out->xsize = in->xsize;
out->ysize = in->ysize;
alloc_img(out);
for(int pix = 0; pix < in->xsize * in->ysize; pix++) {
out->r[pix] = xlat_r[in->r[pix]];
out->g[pix] = xlat_g[in->g[pix]];
out->b[pix] = xlat_b[in->b[pix]];
}
}
int main(int argc, char *argv[]) {
if(argc != 4) {
printf("Usage: %s input-file histogram output-file\n", argv[0]);
exit(1);
}
struct img input, output;
if(!ppmb_read(argv[1], &input.xsize, &input.ysize, &input.maxrgb,
&input.r, &input.g, &input.b)) {
if(input.maxrgb > 255) {
printf("Maxrgb %d not supported\n", input.maxrgb);
exit(1);
}
int *hist_r, *hist_g, *hist_b;
int *xlat_r, *xlat_g, *xlat_b;
int N;
FILE *hist;
hist = fopen(argv[2], "r");
if(!hist) {
fprintf(stderr, "Unable to read histogram file '%s'\n", argv[2]);
exit(1);
}
read_histogram(hist, &hist_r, &N);
if(N != input.maxrgb+1) { fprintf(stderr, "maxrgb != red histogram length\n"); exit(1); }
read_histogram(hist, &hist_g, &N);
if(N != input.maxrgb+1) { fprintf(stderr, "maxrgb != green histogram length\n"); exit(1); }
read_histogram(hist, &hist_b, &N);
if(N != input.maxrgb+1) { fprintf(stderr, "maxrgb != blue histogram length\n"); exit(1); }
gen_xlat(input.xsize * input.ysize, N, hist_r, &xlat_r);
gen_xlat(input.xsize * input.ysize, N, hist_g, &xlat_g);
gen_xlat(input.xsize * input.ysize, N, hist_b, &xlat_b);
ggc::Timer t("equalize");
t.start();
equalize(&input, &output, xlat_r, xlat_g, xlat_b);
t.stop();
if(ppmb_write(argv[3], output.xsize, output.ysize, output.r, output.g, output.b)) {
fprintf(stderr, "Unable to write output\n");
exit(1);
}
printf("Time: %llu ns\n", t.duration());
}
}