-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpng2rgb.c
170 lines (132 loc) · 4.26 KB
/
png2rgb.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
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
#include <stdio.h>
#include <stdlib.h>
#include <png.h>
#define MIN(x,y) ((x) < (y) ? (x) : (y))
int main(int argc, char **argv) {
unsigned int max_desired_width;
if (!(((argc == 3) && (sscanf(argv[2], "%u", &max_desired_width) == 1)) || (argc == 2))) {
fprintf(stderr, "Usage: %s <png file path> [maximum width]\n", argv[0]);
exit(1);
}
if (argc == 2) {
max_desired_width = 0;
}
FILE *infile = fopen(argv[1], "rb");
if (!infile) {
fprintf(stderr,"Cannot read file '%s'\n", argv[1]);
exit(1);
}
unsigned char sig[8];
if (fread(sig, 1, sizeof(sig), infile) < 8) {
fclose(infile);
fprintf(stderr,"Invalid PNG file\n");
exit(1);
}
if (png_sig_cmp(sig, 0, 8)) {
fclose(infile);
fprintf(stderr,"Invalid PNG file\n");
exit(1);
}
png_structp png = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png == NULL) {
fclose(infile);
fprintf(stderr,"Cannot allocate read struct\n");
exit(1);
}
png_infop info = png_create_info_struct(png);
if (info == NULL) {
png_destroy_read_struct(&png, NULL, NULL);
fclose(infile);
fprintf(stderr,"Cannot allocate info struct\n");
exit(1);
}
// pass open file to png struct
png_init_io(png, infile);
// skip signature bytes (we already read those)
png_set_sig_bytes(png, sizeof(sig));
// get image information
png_read_info(png, info);
png_uint_32 w = png_get_image_width(png, info);
png_uint_32 h = png_get_image_height(png, info);
// set least one byte per channel
if (png_get_bit_depth(png, info) < 8) {
png_set_packing(png);
}
// if transparency, convert it to alpha
if (png_get_valid(png, info, PNG_INFO_tRNS)) {
png_set_tRNS_to_alpha(png);
}
switch(png_get_color_type(png, info)) {
case PNG_COLOR_TYPE_RGB:
case PNG_COLOR_TYPE_RGBA:
break;
case PNG_COLOR_TYPE_GRAY:
case PNG_COLOR_TYPE_GRAY_ALPHA:
case PNG_COLOR_TYPE_PALETTE:
default:
png_destroy_read_struct(&png, &info, NULL);
fclose(infile);
fprintf(stderr,"Cannot determine color type\n");
exit(1);
}
png_uint_32 bpp = png_get_rowbytes(png, info) / w;
png_set_interlace_handling(png);
png_read_update_info(png, info);
// allocate pixel buffer
unsigned char *pixels = (unsigned char*)calloc(w*h*bpp, sizeof(unsigned char));
// setup array with row pointers into pixel buffer
png_bytep rows[h];
unsigned char *p = pixels;
for(int i = 0; i < h; i++) {
rows[i] = p;
p += w * bpp;
}
// read all rows (data goes into 'pixels' buffer)
png_read_image(png, rows);
png_read_end(png, NULL);
png_destroy_read_struct(&png, &info, NULL);
// Do the scaling
int desired_width;
int desired_height;
int stride_width;
int stride_height;
if (max_desired_width > 0) {
double aspect_ratio = (double)(w) / (double)(h);
desired_width = MIN(max_desired_width, w);
desired_height = (int)((double)(desired_width) / aspect_ratio);
stride_width = (int)(w / desired_width);
stride_height = (int)(h / desired_height);
} else {
desired_width = w;
desired_height = h;
stride_width = 1;
stride_height = 1;
}
desired_width = w / stride_width;
desired_height = h / stride_height;
fprintf(stdout, "%d\n", desired_width);
fprintf(stdout, "%d\n", desired_height);
for (int row = 0; row < desired_height; row++) {
for (int col = 0; col < desired_width; col++) {
int count = 0;
unsigned int new_red = 0;
unsigned int new_green = 0;
unsigned int new_blue = 0;
for (int srow=MIN(row * stride_height, h); srow < MIN((row+1)*stride_height, h); srow++) {
for (int scol=MIN(col * stride_width, w); scol < MIN((col+1)*stride_width, w); scol++) {
new_red += pixels[srow*w*bpp + scol*bpp];
new_green += pixels[srow*w*bpp + scol*bpp + 1];
new_blue += pixels[srow*w*bpp + scol*bpp + 2];
count++;
}
}
new_red = (unsigned int)((double)new_red / (double)count);
new_green = (unsigned int)((double)new_green / (double)count);
new_blue = (unsigned int)((double)new_blue / (double)count);
fprintf(stdout,"%u\n%u\n%u\n", new_red, new_green, new_blue);
}
}
fclose(infile);
// memory leaks
exit(0);
}