-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimg_utils.c
More file actions
73 lines (56 loc) · 1.74 KB
/
img_utils.c
File metadata and controls
73 lines (56 loc) · 1.74 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
#include "img_utils.h"
void
normalize(MagickWand *wand, int image_size, double threshold)
{
PixelIterator *iterator = NULL;
PixelWand **pixels = NULL;
MagickPixelPacket pixel;
size_t width, height;
size_t x, y;
size_t min_x, max_x, min_y, max_y;
int th = 65535 * (1 - threshold);
width = MagickGetImageWidth(wand);
height = MagickGetImageHeight(wand);
min_x = width - 1;
max_x = 0;
min_y = height - 1;
max_y = 0;
MagickGaussianBlurImage(wand, 1, 1);
MagickModulateImage(wand, 100, 0, 100);
MagickAutoLevelImage(wand);
iterator = NewPixelIterator(wand);
for (y = 0; y < height; ++y) {
pixels = PixelGetNextIteratorRow(iterator, &width);
for (x = 0; x < width; ++x) {
PixelGetMagickColor(pixels[x], &pixel);
if (pixel.red < th) {
if (x < min_x) min_x = x;
if (x > max_x) max_x = x;
if (y < min_y) min_y = y;
if (y > max_y) max_y = y;
}
}
}
if (max_x - min_x + 1 > 0 && max_y - min_y + 1 > 0)
MagickCropImage(wand, max_x - min_x + 1, max_y - min_y + 1, min_x, min_y);
MagickResizeImage(wand, image_size, image_size, LanczosFilter, 1.0);
}
void
get_input_data(MagickWand *wand, nfloat_t *data)
{
PixelIterator *iterator = NULL;
PixelWand **pixels = NULL;
MagickPixelPacket pixel;
size_t width = MagickGetImageWidth(wand);
size_t height = MagickGetImageHeight(wand);
iterator = NewPixelIterator(wand);
for (size_t y = 0; y < height; ++y) {
pixels = PixelGetNextIteratorRow(iterator, &width);
for (size_t x = 0; x < width; ++x) {
PixelGetMagickColor(pixels[x], &pixel);
*data = 1.0 - ((nfloat_t) pixel.red / 65535.0) * 2.0;
++data;
}
}
DestroyPixelIterator(iterator);
}