-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpng_write.c
228 lines (188 loc) · 5.49 KB
/
png_write.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
#include "impy.h"
#include "private.h"
#include <assert.h>
#include <png.h>
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#include <string.h>
static void pre_img(im_write* wr);
static void emit_header(im_write* wr);
static void emit_rows(im_write *wr, unsigned int num_rows, const void *data, int stride);
static void post_img(im_write* wr);
static void finish(im_write* wr);
static void emit_palette(im_write* wr);
static void custom_write(png_structp png_ptr,
png_bytep data, png_size_t length);
static void custom_flush(png_structp png_ptr);
typedef struct ipng_writer {
// embedded im_write
im_write base;
// png-specific
png_structp png_ptr;
png_infop info_ptr;
} ipng_writer;
static struct write_handler ipng_write_handler = {
IM_FILETYPE_PNG,
pre_img,
emit_header,
emit_rows,
post_img,
finish
};
im_write* ipng_new_writer(im_out* out, ImErr* err)
{
ipng_writer* pw = imalloc(sizeof(ipng_writer));
if (!pw) {
*err = IM_ERR_NOMEM;
return NULL;
}
i_write_init(&pw->base);
pw->base.handler = &ipng_write_handler;
pw->base.out = out;
// PNG-specific
pw->png_ptr = NULL;
pw->info_ptr = NULL;
*err = IM_ERR_NONE;
return (im_write*)pw;
}
static void pre_img(im_write* wr)
{
if (wr->num_frames>0) {
wr->err = IM_ERR_ANIM_UNSUPPORTED; // animation not supported.
return;
}
}
static void emit_header(im_write* wr)
{
ipng_writer* pw = (ipng_writer*)wr;
assert(pw->png_ptr == NULL);
assert(pw->info_ptr == NULL);
pw->png_ptr = png_create_write_struct(PNG_LIBPNG_VER_STRING, NULL,NULL,NULL);
if (!pw->png_ptr) {
wr->err = IM_ERR_NOMEM;
return;
}
pw->info_ptr = png_create_info_struct(pw->png_ptr);
if (!pw->info_ptr)
{
wr->err = IM_ERR_NOMEM;
return;
}
// TODO - cover other functions which call png_* routines!
if (setjmp(png_jmpbuf(pw->png_ptr)))
{
png_destroy_write_struct(&pw->png_ptr, &pw->info_ptr);
// TODO: check error in im_out?
wr->err = IM_ERR_EXTLIB;
return;
}
png_set_write_fn(pw->png_ptr,
(png_voidp)wr->out, custom_write, custom_flush);
// work out which format we'll be writing (and also which format
// we'd like to receive from im_write_rows()).
int color_type;
if (im_fmt_is_indexed(wr->fmt)) {
color_type = PNG_COLOR_TYPE_PALETTE;
i_write_set_internal_fmt(wr, IM_FMT_INDEX8);
} else if (im_fmt_has_rgb(wr->fmt)) {
if (im_fmt_has_alpha(wr->fmt)) {
color_type = PNG_COLOR_TYPE_RGB_ALPHA;
i_write_set_internal_fmt(wr, IM_FMT_RGBA);
} else {
color_type = PNG_COLOR_TYPE_RGB;
i_write_set_internal_fmt(wr, IM_FMT_RGB);
}
} else {
wr->err = IM_ERR_UNSUPPORTED; // unsupported fmt
return;
}
png_set_IHDR(pw->png_ptr, pw->info_ptr,
(png_uint_32)wr->w,
(png_uint_32)wr->h,
8, //bit_depth
color_type,
PNG_INTERLACE_NONE,
PNG_COMPRESSION_TYPE_DEFAULT,
PNG_FILTER_TYPE_DEFAULT);
if (wr->pal_num_colours > 0) {
emit_palette(wr);
}
// If there are any string key-value pairs lined up, write them out as
// text chunks.
if (wr->kv.num_entries > 0) {
png_text *tmp = imalloc(sizeof(png_text) * wr->kv.num_entries);
size_t i;
for (i = 0; i < wr->kv.num_entries; ++i) {
tmp[i].compression = PNG_TEXT_COMPRESSION_NONE;
tmp[i].key = (char*)wr->kv.entries[i].key;
tmp[i].text = (char*)wr->kv.entries[i].value;
tmp[i].text_length = strlen(wr->kv.entries[i].value);
tmp[i].itxt_length = 0;
tmp[i].lang = NULL;
tmp[i].lang_key = NULL;
}
png_set_text(pw->png_ptr, pw->info_ptr, tmp, wr->kv.num_entries);
ifree(tmp);
}
// write the header chunks
png_write_info(pw->png_ptr, pw->info_ptr);
}
static void emit_rows(im_write *wr, unsigned int num_rows, const void *data, int stride)
{
ipng_writer* pw = (ipng_writer*)wr;
unsigned int i;
for (i = 0; i < num_rows; ++i) {
png_write_row(pw->png_ptr, (png_const_bytep)data);
data += stride;
}
}
static void emit_palette(im_write* wr)
{
png_color rgb[256] = {0};
png_byte trans[256] = {0};
uint8_t const* src;
int maxtrans = -1;
int i;
ipng_writer* pw = (ipng_writer*)wr;
src = wr->pal_data;
for (i = 0; i < wr->pal_num_colours; ++i) {
rgb[i].red = *src++;
rgb[i].green = *src++;
rgb[i].blue = *src++;
trans[i] = *src++;
if( trans[i] != 255) {
maxtrans=i;
}
}
png_set_PLTE(pw->png_ptr, pw->info_ptr, rgb, wr->pal_num_colours);
if (maxtrans!=-1) {
png_set_tRNS(pw->png_ptr, pw->info_ptr, trans, maxtrans+1, NULL);
}
}
// called after last row written
static void post_img(im_write* wr)
{
ipng_writer* pw = (ipng_writer*)wr;
png_write_end(pw->png_ptr, pw->info_ptr);
}
static void finish(im_write* wr)
{
ipng_writer* pw = (ipng_writer*)wr;
if (pw->png_ptr) {
png_destroy_write_struct(&pw->png_ptr, &pw->info_ptr);
}
}
static void custom_write(png_structp png_ptr,
png_bytep data, png_size_t length)
{
im_out* w = (im_out*)png_get_io_ptr(png_ptr);
size_t n = im_out_write(w, data, length);
if (n!=length) {
png_error(png_ptr, "write error");
}
}
static void custom_flush(png_structp png_ptr)
{
// TODO
}