-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab1_atkinson.c
68 lines (53 loc) · 1.52 KB
/
lab1_atkinson.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
#include <stdlib.h>
#include <string.h>
#include "png_wrapper.h"
static inline void add_sat(struct Image img, size_t x, size_t y, double a)
{
if (x < 0 || y < 0 || x >= img.width || y >= img.height)
return;
int sum = img.pixels[y][x] + a + 0.5;
if (sum < 0)
sum = 0;
else if (sum > 255)
sum = 255;
img.pixels[y][x] = sum;
}
static void process_image(struct Image img)
{
for (size_t y = 0; y < img.height; y++)
for (size_t x = 0; x < img.width; x++)
{
unsigned char old_val = img.pixels[y][x];
unsigned char new_val = (old_val / 128) * 255;
img.pixels[y][x] = new_val;
int err = old_val - new_val;
add_sat(img, x + 1, y + 0, err / 8.0);
add_sat(img, x + 2, y + 0, err / 8.0);
add_sat(img, x - 1, y + 1, err / 8.0);
add_sat(img, x + 0, y + 1, err / 8.0);
add_sat(img, x + 1, y + 1, err / 8.0);
add_sat(img, x + 0, y + 2, err / 8.0);
}
}
int main(int argc, char * const argv[])
{
if (argc < 2 || argc > 3)
error("usage: %s <input_file> [<output_file>]", argv[0]);
char *input_filename = argv[1];
char *output_filename;
if (argc == 3)
output_filename = argv[2];
else
{
output_filename = alloca(strlen(input_filename) + sizeof("_out.png"));
strcpy(output_filename, input_filename);
strcat(output_filename, "_out.png");
}
struct Image img = read_grayscale_png(input_filename);
printf("Input file \"%s\" opened (width = %u, height = %u)\n",
input_filename, img.width, img.height);
process_image(img);
write_grayscale_png(img, output_filename);
free_pixels(img);
return 0;
}