forked from smcameron/space-nerds-in-space
-
Notifications
You must be signed in to change notification settings - Fork 0
/
png_utils.c
239 lines (204 loc) · 6.58 KB
/
png_utils.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
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
239
/*
Copyright (C) 2015 Stephen M. Cameron
Author: Stephen M. Cameron
This file is part of Spacenerds In Space.
Spacenerds in Space is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
Spacenerds in Space is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Spacenerds in Space; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <png.h>
int png_utils_write_png_image(const char *filename, unsigned char *pixels, int w, int h, int has_alpha, int invert)
{
png_structp png_ptr;
png_infop info_ptr;
png_byte **row;
int x, y, rc, colordepth = 8;
int bytes_per_pixel = has_alpha ? 4 : 3;
FILE *f;
f = fopen(filename, "w");
if (!f) {
fprintf(stderr, "fopen: %s:%s\n", filename, strerror(errno));
return -1;
}
png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (!png_ptr)
goto cleanup1;
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr)
goto cleanup2;
if (setjmp(png_jmpbuf(png_ptr))) /* oh libpng, you're old as dirt, aren't you. */
goto cleanup2;
png_set_IHDR(png_ptr, info_ptr, (size_t) w, (size_t) h, colordepth,
has_alpha ? PNG_COLOR_TYPE_RGBA : PNG_COLOR_TYPE_RGB,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
row = png_malloc(png_ptr, h * sizeof(*row));
if (!invert) {
for (y = 0; y < h; y++) {
row[y] = png_malloc(png_ptr, w * bytes_per_pixel);
for (x = 0; x < w; x++) {
unsigned char *r = (unsigned char *) row[y];
unsigned char *src = (unsigned char *)
&pixels[y * w * bytes_per_pixel + x * bytes_per_pixel];
unsigned char *dest = &r[x * bytes_per_pixel];
memcpy(dest, src, bytes_per_pixel);
}
}
} else {
for (y = 0; y < h; y++) {
row[h - y - 1] = png_malloc(png_ptr, w * bytes_per_pixel);
for (x = 0; x < w; x++) {
unsigned char *r = (unsigned char *) row[h - y - 1];
unsigned char *src = (unsigned char *)
&pixels[y * w * bytes_per_pixel + x * bytes_per_pixel];
unsigned char *dest = &r[x * bytes_per_pixel];
memcpy(dest, src, bytes_per_pixel);
}
}
}
png_init_io(png_ptr, f);
png_set_rows(png_ptr, info_ptr, row);
png_write_png(png_ptr, info_ptr, PNG_TRANSFORM_PACKING, NULL);
for (y = 0; y < h; y++)
png_free(png_ptr, row[y]);
png_free(png_ptr, row);
rc = 0;
cleanup2:
png_destroy_write_struct(&png_ptr, &info_ptr);
cleanup1:
fclose(f);
return rc;
}
char *png_utils_read_png_image(const char *filename, int flipVertical, int flipHorizontal,
int pre_multiply_alpha,
int *w, int *h, int *hasAlpha, char *whynot, int whynotlen)
{
int i, j, bit_depth, color_type, row_bytes, image_data_row_bytes;
png_byte header[8];
png_uint_32 tw, th;
png_structp png_ptr = NULL;
png_infop info_ptr = NULL;
png_infop end_info = NULL;
png_byte *image_data = NULL;
FILE *fp = fopen(filename, "rb");
if (!fp) {
snprintf(whynot, whynotlen, "Failed to open '%s': %s",
filename, strerror(errno));
return 0;
}
if (fread(header, 1, 8, fp) != 8) {
snprintf(whynot, whynotlen, "Failed to read 8 byte header from '%s'\n",
filename);
goto cleanup;
}
if (png_sig_cmp(header, 0, 8)) {
snprintf(whynot, whynotlen, "'%s' isn't a png file.",
filename);
goto cleanup;
}
png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING,
NULL, NULL, NULL);
if (!png_ptr) {
snprintf(whynot, whynotlen,
"png_create_read_struct() returned NULL");
goto cleanup;
}
info_ptr = png_create_info_struct(png_ptr);
if (!info_ptr) {
snprintf(whynot, whynotlen,
"png_create_info_struct() returned NULL");
goto cleanup;
}
end_info = png_create_info_struct(png_ptr);
if (!end_info) {
snprintf(whynot, whynotlen,
"2nd png_create_info_struct() returned NULL");
goto cleanup;
}
if (setjmp(png_jmpbuf(png_ptr))) {
snprintf(whynot, whynotlen, "libpng encounted an error");
goto cleanup;
}
png_init_io(png_ptr, fp);
png_set_sig_bytes(png_ptr, 8);
/*
* PNG_TRANSFORM_STRIP_16 |
* PNG_TRANSFORM_PACKING forces 8 bit
* PNG_TRANSFORM_EXPAND forces to expand a palette into RGB
*/
png_read_png(png_ptr, info_ptr, PNG_TRANSFORM_STRIP_16 | PNG_TRANSFORM_PACKING | PNG_TRANSFORM_EXPAND, NULL);
png_get_IHDR(png_ptr, info_ptr, &tw, &th, &bit_depth, &color_type, NULL, NULL, NULL);
if (bit_depth != 8) {
snprintf(whynot, whynotlen, "load_png_texture only supports 8-bit image channel depth");
goto cleanup;
}
if (color_type != PNG_COLOR_TYPE_RGB && color_type != PNG_COLOR_TYPE_RGB_ALPHA) {
snprintf(whynot, whynotlen, "load_png_texture only supports RGB and RGBA");
goto cleanup;
}
if (w)
*w = tw;
if (h)
*h = th;
int has_alpha = (color_type == PNG_COLOR_TYPE_RGB_ALPHA);
if (hasAlpha)
*hasAlpha = has_alpha;
row_bytes = png_get_rowbytes(png_ptr, info_ptr);
image_data_row_bytes = row_bytes;
/* align to 4 byte boundary */
if (image_data_row_bytes & 0x03)
image_data_row_bytes += 4 - (image_data_row_bytes & 0x03);
png_bytepp row_pointers = png_get_rows(png_ptr, info_ptr);
image_data = malloc(image_data_row_bytes * th * sizeof(png_byte) + 15);
if (!image_data) {
snprintf(whynot, whynotlen, "malloc failed in load_png_texture");
goto cleanup;
}
int bytes_per_pixel = (color_type == PNG_COLOR_TYPE_RGB_ALPHA ? 4 : 3);
for (i = 0; i < th; i++) {
png_byte *src_row;
png_byte *dest_row = image_data + i * image_data_row_bytes;
if (flipVertical)
src_row = row_pointers[th - i - 1];
else
src_row = row_pointers[i];
if (flipHorizontal) {
for (j = 0; j < tw; j++) {
png_byte *src = src_row + bytes_per_pixel * j;
png_byte *dest = dest_row + bytes_per_pixel * (tw - j - 1);
memcpy(dest, src, bytes_per_pixel);
}
} else {
memcpy(dest_row, src_row, row_bytes);
}
if (has_alpha && pre_multiply_alpha) {
for (j = 0; j < tw; j++) {
png_byte *pixel = dest_row + bytes_per_pixel * j;
float alpha = pixel[3] / 255.0;
pixel[0] = pixel[0] * alpha;
pixel[1] = pixel[1] * alpha;
pixel[2] = pixel[2] * alpha;
}
}
}
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return (char *) image_data;
cleanup:
if (image_data)
free(image_data);
png_destroy_read_struct(&png_ptr, &info_ptr, &end_info);
fclose(fp);
return 0;
}