-
Notifications
You must be signed in to change notification settings - Fork 7
/
imgify.c
267 lines (220 loc) · 7.37 KB
/
imgify.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
/*
imgify - Convert any file to PNG and back.
imgify.c
Copyright (C) 2015 - Jardel Weyrich <[email protected]>
This program 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.
This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include <assert.h>
#include <errno.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <png.h>
static uint8_t* pixel_at(uint8_t *data, uint32_t row, uint32_t col, uint32_t width, uint32_t height, uint8_t channels) {
assert(data != NULL);
assert(row >= 0 && row < height);
assert(col >= 0 && col < width);
const uint32_t offset = (row * width + col) * channels;
//printf("DEBUG: [pixel_at] row %d -> offset %u\n", row+1, offset);
return &data[offset];
}
#define PNG_SIG_SIZE 8
bool png_load(const char *filename, uint8_t **out_buffer, size_t *out_buffer_size, uint32_t *out_width, uint32_t *out_height, uint8_t *out_channels, uint32_t *out_padding, uint8_t pad_byte) {
// Open the file.
FILE *fp = fopen(filename, "rb");
if (fp == NULL) {
perror("fopen");
return false;
}
// Check for the signature.
uint8_t sig[PNG_SIG_SIZE];
fread(sig, 1, sizeof(sig), fp);
if (!png_check_sig(sig, sizeof(sig))) {
fclose(fp);
perror("png_check_sig: failed");
return false;
}
// Setup PNG structs.
png_structp png_ptr = png_create_read_struct(PNG_LIBPNG_VER_STRING, NULL, NULL, NULL);
if (png_ptr == NULL) {
fclose(fp);
perror("png_create_read_struct: out of memory");
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_read_struct(&png_ptr, (png_infopp) NULL, (png_infopp) NULL);
fclose(fp);
perror("png_create_info_struct: out of memory");
return false;
}
// Handle libpng errors.
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
perror("setjmp: failed");
return false;
}
// Inform our file stream pointer for later use.
png_init_io(png_ptr, fp);
// Let the libpng know that we already checked the signature bytes
// so it doesn't expect to find it at the current file position.
png_set_sig_bytes(png_ptr, PNG_SIG_SIZE);
// Read PNG info up to image data.
png_read_info(png_ptr, info_ptr);
int color_type;
uint32_t width;
uint32_t height;
int bit_depth;
// Read header data.
png_get_IHDR(png_ptr, info_ptr, &width, &height, &bit_depth, &color_type, NULL, NULL, NULL);
// Create buffer to hold row pointers.
png_bytepp row_pointers = malloc(height * sizeof(png_bytep));
if (row_pointers == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
perror("malloc: out of memory");
return false;
}
// Get the number of channels of info for the color type
const uint8_t channels = png_get_channels(png_ptr, info_ptr);
// Calculate how much memory it will take.
const size_t data_size = width * height * bit_depth * channels / 8;
// Allocate the data buffer.
uint8_t *data = malloc(data_size);
if (data == NULL) {
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
free(row_pointers);
perror("malloc: out of memory");
return false;
}
// The length of one row in bytes - the seme as `width * bit_depth * channels / 8`.
const uint32_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
// Set the row pointers.
for (size_t i = 0; i < height; ++i) {
row_pointers[i] = data + i * rowbytes;
}
// Read the entire image.
png_read_image(png_ptr, row_pointers);
// Clean up.
free(row_pointers);
row_pointers = NULL;
png_destroy_read_struct(&png_ptr, &info_ptr, NULL);
fclose(fp);
// Calculate padding.
const uint8_t *last_row = pixel_at(data, height-1, 0, width, height, channels);
uint32_t padding = 0;
for (uint32_t i = 0; i < rowbytes; ++i) {
uint32_t column = rowbytes - i;
// Stop when the padding byte is no longer found.
if (last_row[column-1] != pad_byte) {
padding = i;
// printf("DEBUG: [png_load] Found %u pixels of padding (%u bytes), starting at row %u column %u\n",
// padding / channels, padding, height, column);
break;
}
}
if (out_buffer != NULL)
*out_buffer = data;
if (out_buffer_size != NULL)
*out_buffer_size = rowbytes * height - padding;
if (out_width != NULL)
*out_width = width;
if (out_height != NULL)
*out_height = height;
if (out_channels != NULL)
*out_channels = channels;
if (out_padding != NULL)
*out_padding = padding;
return true;
}
bool png_save(const char *filename, uint8_t *data, uint32_t width, uint32_t height, uint8_t channels, uint32_t padding, uint8_t pad_byte) {
assert(width > 0); // 1 column at least
assert(height > 0); // 1 row at least
const int bit_depth = 8;
int color_type;
switch (channels) {
case 3: color_type = PNG_COLOR_TYPE_RGB; break;
case 4: color_type = PNG_COLOR_TYPE_RGBA; break;
default:
fprintf(stderr, "Unknown image color type\n");
return false;
}
// Open the file.
FILE *fp = fopen(filename, "wb");
if (fp == NULL) {
perror("fopen");
return false;
}
// Setup PNG structs.
png_structp png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, 0, NULL, 0);
if (png_ptr == NULL) {
fclose(fp);
perror("png_create_write_struct: out of memory");
return false;
}
png_infop info_ptr = png_create_info_struct(png_ptr);
if (info_ptr == NULL) {
png_destroy_write_struct(&png_ptr, NULL);
fclose(fp);
perror("png_create_info_struct: out of memory");
return false;
}
// Handle libpng errors.
if (setjmp(png_jmpbuf(png_ptr))) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
perror("setjmp: failed");
return false;
}
// Inform our file stream pointer for later use.
png_init_io(png_ptr, fp);
// Set the PNG header info.
png_set_IHDR(png_ptr, info_ptr, width, height, bit_depth, color_type,
PNG_INTERLACE_NONE, PNG_COMPRESSION_TYPE_BASE, PNG_FILTER_TYPE_BASE);
// Write the info.
png_write_info(png_ptr, info_ptr);
// The length of one row in bytes - the seme as `width * bit_depth * channels / 8`.
const uint32_t rowbytes = png_get_rowbytes(png_ptr, info_ptr);
uint8_t *padded_row_ptr = malloc(rowbytes);
if (padded_row_ptr == NULL) {
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
perror("malloc");
return false;
}
const bool needs_padding = padding > 0;
for (uint32_t i=0; i < height; i++) {
const bool is_lastrow = i == height - 1;
const bool is_padding_now = needs_padding && is_lastrow;
const uint8_t *row_ptr = pixel_at(data, i, 0, width, height, channels);
//printf("DEBUG: [png_save] row=%u width=%u rowbytes=%u\n", i+1, width, rowbytes);
if (!is_padding_now) {
png_write_row(png_ptr, row_ptr);
} else {
memset(padded_row_ptr, pad_byte, rowbytes);
memcpy(padded_row_ptr, row_ptr, rowbytes - padding);
png_write_row(png_ptr, padded_row_ptr);
free(padded_row_ptr);
}
}
free(padded_row_ptr);
// Signal we have finishing writing.
png_write_end(png_ptr, NULL);
// Cleanup.
png_destroy_write_struct(&png_ptr, &info_ptr);
fclose(fp);
return true;
}