-
Notifications
You must be signed in to change notification settings - Fork 6
/
intensities.c
108 lines (85 loc) · 2.87 KB
/
intensities.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
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stddef.h>
#include <fcntl.h>
#include <string.h>
#include <jpeglib.h>
#include "definitions.h"
quadrant_sums rgb_sums(rgb_pixel *restrict pixels, uint32_t width, uint32_t height)
{
quadrant_sums sums = {{}};
for (uint32_t i = 0; i < height; ++i) {
for (uint32_t j = 0; j < width; ++j) {
int nw = (i <= height / 2) && (j <= width / 2);
int ne = (i <= height / 2) && (j >= width / 2);
int sw = (i >= height / 2) && (j <= width / 2);
int se = (i >= height / 2) && (j >= width / 2);
rgb_pixel pix = pixels[i * width + j];
if (nw) {
sums.nw.r += pix.r;
sums.nw.g += pix.g;
sums.nw.b += pix.b;
}
if (ne) {
sums.ne.r += pix.r;
sums.ne.g += pix.g;
sums.ne.b += pix.b;
}
if (sw) {
sums.sw.r += pix.r;
sums.sw.g += pix.g;
sums.sw.b += pix.b;
}
if (se) {
sums.se.r += pix.r;
sums.se.g += pix.g;
sums.se.b += pix.b;
}
}
}
return sums;
}
static intensity_data rgb_to_luma(quadrant_sums sums, raster_data data)
{
double dim = ((data.width + 1lu) & ~1lu) * ((data.height + 1lu) & ~1lu) / 4;
double nw_luma = ((sums.nw.r / dim * sRGB_R_Y) +
(sums.nw.g / dim * sRGB_G_Y) +
(sums.nw.b / dim * sRGB_B_Y));
double ne_luma = ((sums.ne.r / dim * sRGB_R_Y) +
(sums.ne.g / dim * sRGB_G_Y) +
(sums.ne.b / dim * sRGB_B_Y));
double sw_luma = ((sums.sw.r / dim * sRGB_R_Y) +
(sums.sw.g / dim * sRGB_G_Y) +
(sums.sw.b / dim * sRGB_B_Y));
double se_luma = ((sums.se.r / dim * sRGB_R_Y) +
(sums.se.g / dim * sRGB_G_Y) +
(sums.se.b / dim * sRGB_B_Y));
return (struct intensity_data) {
.nw = nw_luma,
.ne = ne_luma,
.sw = sw_luma,
.se = se_luma,
.error = 0
};
}
intensity_data jpeg_intensities(const char *file_name)
{
raster_data data = read_jpeg_file(file_name);
if (data.error)
return (struct intensity_data) { .error = 1 };
quadrant_sums sums = rgb_sums(data.pixels, data.width, data.height);
intensity_data ins = rgb_to_luma(sums, data);
free(data.pixels);
return ins;
}
intensity_data png_intensities(const char *file_name)
{
raster_data data = read_png_file(file_name);
if (data.error)
return (struct intensity_data) { .error = 1 };
quadrant_sums sums = rgb_sums(data.pixels, data.width, data.height);
intensity_data ins = rgb_to_luma(sums, data);
free(data.pixels);
return ins;
}