-
Notifications
You must be signed in to change notification settings - Fork 35
/
color.c
57 lines (53 loc) · 1.36 KB
/
color.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
#include <stdlib.h>
#include <math.h>
#include <magick/api.h>
#include "macros.h"
#define HISTOGRAM_SIZE 768
float
calculate_image_entropy_rect(const Image *image, const RectangleInfo *rect)
{
register long y;
register long x;
register const PixelPacket *p;
ExceptionInfo ex;
unsigned int histogram[HISTOGRAM_SIZE] = {0,};
long sx;
long sy;
unsigned int width;
unsigned int height;
if (rect && rect->width > 0 && rect->height > 0) {
sx = rect->x;
sy = rect->y;
width = rect->width;
height = rect->height;
} else {
sx = 0;
sy = 0;
width = image->columns;
height = image->rows;
}
unsigned int ey = sy + height;
int total = 0;
for(y = sy; y < ey; ++y) {
p = ACQUIRE_IMAGE_PIXELS(image, sx, y, width, 1, &ex);
if (!p) {
continue;
}
total += width;
for (x=0; x < width; x++, p++) {
histogram[ScaleQuantumToChar(p->red)]++;
histogram[ScaleQuantumToChar(p->green) + 256]++;
histogram[ScaleQuantumToChar(p->blue) + 512]++;
}
}
float t = (float)(total * 3);
float entropy = 0.0f;
int ii;
for (ii = 0; ii < HISTOGRAM_SIZE; ++ii) {
float p = histogram[ii] / t;
if (p) {
entropy += p * log2f(p);
}
}
return -entropy;
}