-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpixel_tools.cpp
115 lines (101 loc) · 3.43 KB
/
pixel_tools.cpp
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
#include "rect.h"
#include "pixel_tools.h"
namespace pixel_tools {
unsigned int clr_to_int(png::rgba_pixel clr) {
unsigned int result = 0;
for (int i = 0; i < 8; ++i) {
result += (clr.blue & 1) << (3*i);
clr.blue = clr.blue >> 1;
result += (clr.green & 1) << (3*i + 1);
clr.green = clr.green >> 1;
result += (clr.red & 1) << (3*i + 2);
clr.red = clr.red >> 1;
}
result += clr.alpha << 24;
return result;
}
png::rgba_pixel int_to_clr(unsigned int iii) {
png::rgba_pixel result(0, 0, 0, 0);
result.alpha = iii >> 24;
iii &= 0x00FFFFFF;
for (int j = 23; j >= 0; --j) {
switch (j % 3) {
case 0:
result.blue = result.blue << 1;
result.blue |= (iii & (1 << j)) >> j;
break;
case 1:
result.green = result.green << 1;
result.green |= (iii & (1 << j)) >> j;
break;
case 2:
result.red = result.red << 1;
result.red |= (iii & (1 << j)) >> j;
break;
default:
break;
}
}
return result;
}
void derivate_image(png::image<png::rgba_pixel>& img) {
unsigned int h = img.get_height(), w = img.get_width();
for (unsigned int y = h-1; y; --y) {
for (unsigned int x = w-1; x; --x) {
img[y][x].red = img[y][x].red - img[y-1][x].red - img[y][x-1].red + img[y-1][x-1].red;
img[y][x].green = img[y][x].green - img[y-1][x].green - img[y][x-1].green + img[y-1][x-1].green;
img[y][x].blue = img[y][x].blue - img[y-1][x].blue - img[y][x-1].blue + img[y-1][x-1].blue;
img[y][x].alpha = img[y][x].alpha - img[y-1][x].alpha - img[y][x-1].alpha + img[y-1][x-1].alpha;
}
}
for (unsigned int x = w-1; x; --x) {
img[0][x].red -= img[0][x-1].red;
img[0][x].green -= img[0][x-1].green;
img[0][x].blue -= img[0][x-1].blue;
img[0][x].alpha -= img[0][x-1].alpha;
}
for (unsigned int y = h-1; y; --y) {
img[y][0].red -= img[y-1][0].red;
img[y][0].green -= img[y-1][0].green;
img[y][0].blue -= img[y-1][0].blue;
img[y][0].alpha -= img[y-1][0].alpha;
}
}
void integrate_image(png::image<png::rgba_pixel>& img) {
unsigned int h = img.get_height(), w = img.get_width();
for (unsigned int x = 1; x < w; ++x) {
img[0][x].red += img[0][x-1].red;
img[0][x].green += img[0][x-1].green;
img[0][x].blue += img[0][x-1].blue;
img[0][x].alpha += img[0][x-1].alpha;
}
for (unsigned int y = 1; y < h; ++y) {
img[y][0].red += img[y-1][0].red;
img[y][0].green += img[y-1][0].green;
img[y][0].blue += img[y-1][0].blue;
img[y][0].alpha += img[y-1][0].alpha;
}
for (unsigned int y = 1; y < h; ++y) {
for (unsigned int x = 1; x < w; ++x) {
img[y][x].red = img[y][x].red + img[y-1][x].red + img[y][x-1].red - img[y-1][x-1].red;
img[y][x].green = img[y][x].green + img[y-1][x].green + img[y][x-1].green - img[y-1][x-1].green;
img[y][x].blue = img[y][x].blue + img[y-1][x].blue + img[y][x-1].blue - img[y-1][x-1].blue;
img[y][x].alpha = img[y][x].alpha + img[y-1][x].alpha + img[y][x-1].alpha - img[y-1][x-1].alpha;
}
}
}
void copy_rect(png::image<png::rgba_pixel>& source,
png::image<png::rgba_pixel>& dest, RectMovement rm) {
int x2 = rm.dest.x1;
for (int x1 = rm.source.x1; x1 < rm.source.x2; ++x1) {
int y2 = rm.dest.y1;
for (int y1 = rm.source.y1; y1 < rm.source.y2; ++y1) {
if (source[y1][x1].alpha != 0) {
dest[y2][x2] = source[y1][x1];
}
y2 += rm.dest.y2;
}
x2 += rm.dest.x2;
}
}
}