This source file includes following definitions.
- main
#include <stdio.h>
#include "Halide.h"
using namespace Halide;
int main(int argc, char **argv) {
int W = 1000, H = 1000;
Buffer<uint8_t> in(W, H);
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
unsigned r1 = rand();
r1 = r1 & 0xff;
in(x, y) = r1/2 + 64;
}
}
Func hist, cdf, equalized, rescaled;
RDom r(in), ri(0, 255);
Var x, y, i;
hist(in(r.x, r.y)) += 1;
cdf(i) = 0;
cdf(ri.x) = cdf(ri.x-1) + hist(ri.x);
equalized(x, y) = cdf(in(x, y));
hist.compute_root();
cdf.compute_root();
int pixels = in.extent(0)*in.extent(1);
rescaled(i, _) = cast<uint8_t>((equalized(i, _)*256)/pixels);
Buffer<uint8_t> out = rescaled.realize(in.width(), in.height());
int out_hist[16], in_hist[16];
for (int i = 0; i < 16; i++) {
out_hist[i] = in_hist[i] = 0;
}
for (int y = 0; y < out.height(); y++) {
for (int x = 0; x < out.width(); x++) {
out_hist[out(x, y)/16]++;
in_hist[in(x, y)/16]++;
}
}
for (int i = 0; i < 16; i++) {
int correct = (in.width()*in.height())/16;
if (out_hist[i] < correct/2 || out_hist[i] > 2*correct) {
printf("Expected histogram entries of ~ %d\n", correct);
return -1;
}
}
printf("Success!\n");
return 0;
}