-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathimage_arithmetic.c
109 lines (97 loc) · 3.7 KB
/
image_arithmetic.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
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
#include "image.h"
/* img1 + img2 */
Image* add(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = MIN(img1->data[i] + img2->data[i], MAX_COLOR);
return img;
}
/* img1 - img2 */
Image* sub(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = MAX(img1->data[i] - img2->data[i], MIN_COLOR);
return img;
}
/* |img1 - img2| */
Image* diff(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = abs(img1->data[i] - img2->data[i]);
return img;
}
/* img1 * img2 */
Image* mul(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = (unsigned char)(img1->data[i] * img2->data[i] / 255.0);
return img;
}
/* (img1 + img2) / 2 */
Image* average(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = (img1->data[i] + img2->data[i]) / 2;
return img;
}
/* img1 * weight + img2 * (1 - weight), 0 <= weight <= 1 */
Image* cross_fade(Image *img1, Image *img2, double weight) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = (unsigned char)(img1->data[i] * weight + img2->data[i] * (1 - weight));
return img;
}
/* min(img1, img2) */
Image* minimum(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = MIN(img1->data[i], img2->data[i]);
return img;
}
/* max(img1, img2) */
Image* maximum(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = MAX(img1->data[i], img2->data[i]);
return img;
}
/* sqrt(img1^2 + img2^2) */
Image* amplitude(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = (unsigned char)(sqrt(img1->data[i] * img1->data[i] + img2->data[i] * img2->data[i]) / sqrt(2.0));
return img;
}
/* img1 & img2 */
Image* and(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = img1->data[i] & img2->data[i];
return img;
}
/* img1 | img2 */
Image* or(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = img1->data[i] | img2->data[i];
return img;
}
/* img1 ^ img2 */
Image* xor(Image *img1, Image *img2) {
if (img1->width != img2->width || img1->height != img2->height || img1->size != img2->size) error(3);
Image *img = copy(img1, 0);
for (int i = 0; i < img->size; i++)
img->data[i] = img1->data[i] ^ img2->data[i];
return img;
}