-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.c
More file actions
238 lines (174 loc) · 6.34 KB
/
Copy pathmain.c
File metadata and controls
238 lines (174 loc) · 6.34 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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#include <math.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <stdbool.h>
#include <png.h>
#define HEADER_SIZE 8
static png_uint_32 height;
static png_uint_32 width;
static int bit_depth;
static int color_type;
static png_bytep* row_pointers;
static int8_t factor;
void read_png(char const* const filename);
void transform_png();
void write_png(char const* const filename);
int main(int argc, char** argv) {
if (argc < 4) {
fprintf(stdout, "provide a png file and output name and scale factor you dumass!\n");
exit(EXIT_FAILURE);
}
factor = strtol(argv[3], NULL, 10);
read_png(argv[1]);
transform_png(NULL, 10);
write_png(argv[2]);
return EXIT_SUCCESS;
}
void read_png(char const* const filename) {
FILE *fp = fopen(filename, "r"); // add "b" if windows but WINDOWS SUCKS SO NO!!!
if (!fp) {
fprintf(stdout, "couldn't open the file!\n");
exit(EXIT_FAILURE);
}
uint8_t header[HEADER_SIZE] = {0, 0, 0, 0, 0, 0, 0, 0}; // i mean
if (fread(header, sizeof(header[0]), HEADER_SIZE, fp) != HEADER_SIZE) {
fprintf(stdout, "error reading header bytes\n");
exit(EXIT_FAILURE);
}
/* check whether file is a png */
const bool is_png = (png_sig_cmp(header, 0, HEADER_SIZE) == 0);
if (!is_png) {
fprintf(stdout, "not a png!\n");
exit(EXIT_FAILURE);
}
/* basic info */
fprintf(stdout, " Compiled with libpng %s. \n", PNG_LIBPNG_VER_STRING);
/* initializing the necessary structures */
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fprintf(stdout, "failed to create a png struct!\n");
exit(EXIT_FAILURE);
}
png_infop png_info_ptr = png_create_info_struct(png_ptr);
if (!png_info_ptr) {
fprintf(stdout, "couldn't create a png info struct!\n");
png_destroy_read_struct(&png_ptr, NULL, NULL);
exit(EXIT_FAILURE);
}
/* jumping when libpng encounters an error */
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stdout, "jumping from libpng, caused by an error!\n");
png_destroy_read_struct(&png_ptr, &png_info_ptr, NULL);
fclose(fp);
exit(EXIT_FAILURE);
}
/* setting up the input code */
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, HEADER_SIZE);
/* reading, finally! */
png_read_info(png_ptr, png_info_ptr);
/* getting some info about the file */
png_get_IHDR(png_ptr, png_info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
fprintf(stdout, " Width: %d Height: %d Bit depth: %d Color type: %d\n", width, height, bit_depth, color_type);
/* reading rows */
row_pointers = malloc(sizeof(png_bytep) * height);
for (size_t row = 0; row < height; row++) row_pointers[row] = NULL;
for (size_t row = 0; row < height; row++) row_pointers[row] = png_malloc(png_ptr, png_get_rowbytes(png_ptr, png_info_ptr));
png_read_image(png_ptr, row_pointers);
png_destroy_read_struct(&png_ptr, &png_info_ptr, NULL);
fclose(fp);
}
const uint8_t quantize(uint8_t const factor, uint8_t colour) {
return (uint8_t)(round((double)(colour * factor) / 255.0) * (255.0/(double)factor));
}
const uint8_t apply_error(int16_t const error, uint16_t const colour, uint16_t const scale) {
uint16_t res = colour + error * scale / 16;
if (res > 255) res = round(res / 255.0);
return res;
}
void prop_error(png_bytep colour, int8_t errors[3], uint8_t const scale) {
colour[0] = apply_error(errors[0], colour[0], scale);
colour[1] = apply_error(errors[1], colour[1], scale);
colour[2] = apply_error(errors[2], colour[2], scale);
}
void transform_png() {
/* png_set_quantize? FOR N00BS! */
for(int y = 0; y < height - 1; y++) {
png_bytep row = row_pointers[y];
for(int x = 1; x < width - 1; x++) {
png_bytep px = &(row[x * 4]);
unsigned char red = px[0];
unsigned char green = px[1];
unsigned char blue = px[2];
/* quantize */
red = quantize(factor, red);
green = quantize(factor, green);
blue = quantize(factor, blue);
/* errors */
int8_t errors[3] = {
px[0] - red,
px[1] - green,
px[2] - blue
};
px[0] = red;
px[1] = green;
px[2] = blue;
png_bytep px_r = &(row[(x+1) * 4]);
prop_error(px_r, errors, 7);
png_bytep new_row = row_pointers[y+1];
png_bytep px_lb = &(new_row[(x-1) * 4]);
prop_error(px_lb, errors, 3);
png_bytep px_b = &(new_row[x * 4]);
prop_error(px_b, errors, 5);
png_bytep px_rb = &(new_row[(x+1) * 4]);
prop_error(px_rb, errors, 1);
}
}
}
void write_png(char const* const filename) {
FILE *fp = fopen(filename, "w");
if (!fp) {
fprintf(stdout, "couldn't open the file!\n");
exit(EXIT_FAILURE);
}
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr) {
fprintf(stdout, "failed to create a png struct!\n");
exit(EXIT_FAILURE);
}
png_infop png_info_ptr = png_create_info_struct(png_ptr);
if (!png_info_ptr) {
fprintf(stdout, "failed to create an info struct!\n");
exit(EXIT_FAILURE);
}
if (setjmp(png_jmpbuf(png_ptr))) {
fprintf(stdout, "jumping from libpng, caused by an error!\n");
png_destroy_write_struct(&png_ptr, &png_info_ptr);
fclose(fp);
exit(EXIT_FAILURE);
}
png_init_io(png_ptr, fp);
png_set_IHDR(
png_ptr,
png_info_ptr,
width, height,
8,
PNG_COLOR_TYPE_RGBA,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT
);
png_write_info(png_ptr, png_info_ptr);
if (!row_pointers) {
fprintf(stdout, "where are the row pointers?!\n");
exit(EXIT_FAILURE);
}
png_write_image(png_ptr, row_pointers);
png_write_end(png_ptr, NULL);
/* clean up */
for(int y = 0; y < height; y++) free(row_pointers[y]);
free(row_pointers);
fclose(fp);
png_destroy_write_struct(&png_ptr, &png_info_ptr);
}