sixel_write.c

filter: Statementstmt 91.4% 425 / 465 Branchbran 81.9% 236 / 288 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 87.7% 661 / 753 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« sixel_read.c ↑ Summary sixel_quant.c »
1 /*
2 * Imager::File::SIXEL - SIXEL encoder.
3 *
4 * Copyright (C) 2026 davenonymous.
5 *
6 * This library is free software; you can redistribute it and/or modify
7 * it under the same terms as Perl itself.
8 *
9 * An image is encoded in three stages:
10 *
11 * 1. the image is reduced to a palette of at most 256 colours and an
12 * index map holding one palette index (or SIXEL_TRANSPARENT) per
13 * pixel,
14 * 2. the colour registers used by the map are defined,
15 * 3. the map is emitted band by band, six rows per band.
16 *
17 * Within a band every column contributes one entry per distinct colour
18 * holding that colour's six bit mask. The entries are bucketed by
19 * colour with a counting sort, which keeps them in column order. Each
20 * colour's entries are split into segments at long gaps, and the
21 * segments are packed greedily onto as few run length encoded sixel
22 * lines as possible. The cost of stage 3 is therefore linear in the
23 * number of pixels and independent of the palette size.
24 */
25 #include "imext.h"
26 #include "imsixel.h"
27 #include "sixel_quant.h"
28  
29 #include <errno.h>
30 #include <stdint.h>
31 #include <stdlib.h>
32 #include <string.h>
33  
34 #define OUTPUT_CAPACITY 65536
35 /* the longest token: "#255;2;100;100;100" or a run "!<20 digits>c" */
36 #define OUTPUT_TOKEN_MAX 64
37  
38 #define DEFAULT_ALPHA_THRESHOLD 128
39  
40 typedef enum {
41 PALETTE_ADAPTIVE,
42 PALETTE_WEBMAP
43 } palette_kind;
44  
45 typedef struct {
46 int max_colors;
47 palette_kind palette;
48 sixel_dither dither;
49 int alpha_threshold;
50 int pan;
51 int pad;
52 } write_options;
53  
54 /* ------------------------------------------------------------------ */
55 /* options */
56  
57 /* Parses a tag holding a decimal integer, with an optional sign and no
58 * other characters.
59 */
60 static int
61 28 tag_integer(const i_img_tag *tag, long *value) {
62 28 const char *text = tag->data;
63 char *end;
64  
65 28 ▶ if (!text) {
Branch: 1 branch
BranchTrueFalseTotal
if (!text) { 25 3 28
66 25 *value = tag->idata;
67 25 return 1;
68 }
69 3 ▶ if (!(text[0] == '-' || text[0] == '+' || (text[0] >= '0' && text[0] <= '9')))
Branch: 4 branches
BranchTrueFalseTotal
if (!(text[0] == '-' || text[0] == '+' || (text[0] >= '0' && text[0] <= '9'))) 3 0 3
if (!(text[0] == '-' || text[0] == '+' || (text[0] >= '0' && text[0] <= '9'))) 3 0 3
if (!(text[0] == '-' || text[0] == '+' || (text[0] >= '0' && text[0] <= '9'))) 3 0 3
if (!(text[0] == '-' || text[0] == '+' || (text[0] >= '0' && text[0] <= '9'))) 1 2 3
70 1 return 0;
71 2 errno = 0;
72 2 *value = strtol(text, &end, 10);
73 2 ▶ return errno == 0 && end != text && end == text + tag->size;
Branch: 3 branches
BranchTrueFalseTotal
return errno == 0 && end != text && end == text + tag->size; 2 0 2
return errno == 0 && end != text && end == text + tag->size; 2 0 2
return errno == 0 && end != text && end == text + tag->size; 0 2 2
74 }
75  
76 static int
77 249 read_int_option(i_img *im, const char *name, int minimum, int maximum, int *value) {
78 long found;
79 int entry;
80  
81 249 ▶ if (!i_tags_find(&im->tags, name, 0, &entry))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_tags_find(&im->tags, name, 0, &entry)) 221 28 249
82 221 return 1;
83 28 ▶ if (!tag_integer(im->tags.tags + entry, &found) || found < minimum || found > maximum) {
Branch: 3 branches
BranchTrueFalseTotal
if (!tag_integer(im->tags.tags + entry, &found) || found < minimum || found > maximum) { 25 3 28
if (!tag_integer(im->tags.tags + entry, &found) || found < minimum || found > maximum) { 22 3 25
if (!tag_integer(im->tags.tags + entry, &found) || found < minimum || found > maximum) { 2 20 22
84 8 i_push_errorf(0, "%s must be an integer from %d to %d", name, minimum, maximum);
85 8 return 0;
86 }
87 20 *value = (int)found;
88 20 return 1;
89 }
90  
91 /* Reads a keyword option, storing the index of the matching keyword.
92 * The whole tag value must match, so an embedded NUL byte or trailing
93 * text makes it unknown.
94 */
95 static int
96 114 read_keyword_option(i_img *im, const char *name, const char *const *keywords, int *value) {
97 const i_img_tag *tag;
98 int entry;
99 int i;
100  
101 114 ▶ if (!i_tags_find(&im->tags, name, 0, &entry))
Branch: 1 branch
BranchTrueFalseTotal
if (!i_tags_find(&im->tags, name, 0, &entry)) 90 24 114
102 90 return 1;
103 24 tag = im->tags.tags + entry;
104 24 ▶ if (!tag->data) {
Branch: 1 branch
BranchTrueFalseTotal
if (!tag->data) { 0 24 24
105 0 i_push_errorf(0, "unknown %s value '%d'", name, tag->idata);
106 0 return 0;
107 }
108 45 ▶ for (i = 0; keywords[i]; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; keywords[i]; ++i) { 41 4 45
109 41 ▶ if ((size_t)tag->size == strlen(keywords[i]) && memcmp(tag->data, keywords[i], (size_t)tag->size) == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if ((size_t)tag->size == strlen(keywords[i]) && memcmp(tag->data, keywords[i], (size_t)tag->size) == 0) { 21 20 41
if ((size_t)tag->size == strlen(keywords[i]) && memcmp(tag->data, keywords[i], (size_t)tag->size) == 0) { 20 1 21
110 20 *value = i;
111 20 return 1;
112 }
113 }
114 4 i_push_errorf(0, "unknown %s value '%.*s'", name, tag->size, tag->data);
115 4 return 0;
116 }
117  
118 static int
119 66 read_write_options(i_img *im, write_options *opts) {
120 static const char *const palette_names[] = { "adaptive", "webmap", NULL };
121 static const char *const dither_names[] = { "none", "ordered", "diffusion", NULL };
122 66 int palette = PALETTE_ADAPTIVE;
123 66 int dither = SIXEL_DITHER_DIFFUSION;
124  
125 66 opts->max_colors = SIXEL_MAX_PALETTE;
126 66 opts->alpha_threshold = DEFAULT_ALPHA_THRESHOLD;
127 66 opts->pan = 1;
128 66 opts->pad = 1;
129  
130 66 ▶ if (!read_int_option(im, "sixel_max_colors", 1, SIXEL_MAX_PALETTE, &opts->max_colors)
Branch: 1 branch
BranchTrueFalseTotal
if (!read_int_option(im, "sixel_max_colors", 1, SIXEL_MAX_PALETTE, &opts->max_colors) 63 3 66
131 63 ▶ || !read_int_option(im, "sixel_alpha_threshold", 0, 255, &opts->alpha_threshold)
Branch: 1 branch
BranchTrueFalseTotal
|| !read_int_option(im, "sixel_alpha_threshold", 0, 255, &opts->alpha_threshold) 61 2 63
132 61 ▶ || !read_int_option(im, "sixel_pan", 1, 0x7FFFFFFF, &opts->pan)
Branch: 1 branch
BranchTrueFalseTotal
|| !read_int_option(im, "sixel_pan", 1, 0x7FFFFFFF, &opts->pan) 59 2 61
133 59 ▶ || !read_int_option(im, "sixel_pad", 1, 0x7FFFFFFF, &opts->pad)
Branch: 1 branch
BranchTrueFalseTotal
|| !read_int_option(im, "sixel_pad", 1, 0x7FFFFFFF, &opts->pad) 58 1 59
134 58 ▶ || !read_keyword_option(im, "sixel_palette", palette_names, &palette)
Branch: 1 branch
BranchTrueFalseTotal
|| !read_keyword_option(im, "sixel_palette", palette_names, &palette) 56 2 58
135 56 ▶ || !read_keyword_option(im, "sixel_dither", dither_names, &dither))
Branch: 1 branch
BranchTrueFalseTotal
|| !read_keyword_option(im, "sixel_dither", dither_names, &dither)) 2 54 56
136 12 return 0;
137  
138 54 opts->palette = (palette_kind)palette;
139 54 opts->dither = (sixel_dither)dither;
140 54 return 1;
141 }
142  
143 /* ------------------------------------------------------------------ */
144 /* stage 1: palette and index map */
145  
146 typedef struct {
147 sixel_palette palette;
148 uint16_t *map;
149 i_img_dim width;
150 i_img_dim height;
151 int transparent;
152 } indexed_image;
153  
154 static void
155 53 indexed_release(indexed_image *indexed) {
156 53 free(indexed->map);
157 53 indexed->map = NULL;
158 53 }
159  
160 /* The number of pixels, or 0 for an image too large to encode: the
161 * band scratch space indexes the up to 6 entries per column of a band
162 * and its segments with signed 32 bit numbers, and the RGBA copy must
163 * fit in memory.
164 */
165 static size_t
166 145 pixel_count_of(i_img *im) {
167 145 size_t width = (size_t)im->xsize;
168 145 size_t height = (size_t)im->ysize;
169  
170 145 ▶ if (width == 0 || height == 0 || width > INT32_MAX / 6 || height > SIZE_MAX / 4 / width)
Branch: 4 branches
BranchTrueFalseTotal
if (width == 0 || height == 0 || width > INT32_MAX / 6 || height > SIZE_MAX / 4 / width) 145 0 145
if (width == 0 || height == 0 || width > INT32_MAX / 6 || height > SIZE_MAX / 4 / width) 145 0 145
if (width == 0 || height == 0 || width > INT32_MAX / 6 || height > SIZE_MAX / 4 / width) 145 0 145
if (width == 0 || height == 0 || width > INT32_MAX / 6 || height > SIZE_MAX / 4 / width) 0 145 145
171 0 return 0;
172 145 return width * height;
173 }
174  
175 /* Reads the image as packed RGBA with the alpha byte reduced to 0
176 * (unpainted) or 255 (painted).
177 */
178 static unsigned char *
179 46 fetch_rgba(i_img *im, int alpha_threshold) {
180 static const int gray_channels[4] = { 0, 0, 0, 1 };
181 46 size_t width = (size_t)im->xsize;
182 46 size_t pixel_count = pixel_count_of(im);
183 46 int alpha_channel = -1;
184 46 int has_alpha = i_img_alpha_channel(im, &alpha_channel);
185 int channels[4];
186 46 ▶ int sample_count = has_alpha ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int sample_count = has_alpha ? 4 : 3; 7 39 46
187 unsigned char *rgba;
188 46 unsigned char *row = NULL;
189 i_img_dim y;
190  
191 46 ▶ if (i_img_color_channels(im) < 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_img_color_channels(im) < 3) { 2 44 46
192 2 memcpy(channels, gray_channels, sizeof(channels));
193 }
194 else {
195 44 channels[0] = 0;
196 44 channels[1] = 1;
197 44 channels[2] = 2;
198 }
199 46 channels[3] = alpha_channel;
200  
201 46 rgba = malloc(pixel_count * 4);
202 46 ▶ if (!has_alpha)
Branch: 1 branch
BranchTrueFalseTotal
if (!has_alpha) 39 7 46
203 39 row = malloc(width * 3);
204 46 ▶ if (!rgba || (!has_alpha && !row)) {
Branch: 3 branches
BranchTrueFalseTotal
if (!rgba || (!has_alpha && !row)) { 46 0 46
if (!rgba || (!has_alpha && !row)) { 39 7 46
if (!rgba || (!has_alpha && !row)) { 0 39 39
205 0 free(rgba);
206 0 free(row);
207 0 i_push_error(0, "out of memory");
208 0 return NULL;
209 }
210  
211 822 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 776 46 822
212 776 unsigned char *out = rgba + (size_t)y * width * 4;
213 size_t x;
214  
215 776 ▶ if (has_alpha) {
Branch: 1 branch
BranchTrueFalseTotal
if (has_alpha) { 39 737 776
216 39 i_gsamp(im, 0, im->xsize, y, out, channels, sample_count);
217 268 ▶ for (x = 0; x < width; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) 229 39 268
218 229 ▶ out[x * 4 + 3] = out[x * 4 + 3] < alpha_threshold ? 0 : 255;
Branch: 1 branch
BranchTrueFalseTotal
out[x * 4 + 3] = out[x * 4 + 3] < alpha_threshold ? 0 : 255; 108 121 229
219 }
220 else {
221 737 i_gsamp(im, 0, im->xsize, y, row, channels, sample_count);
222 47786 ▶ for (x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) { 47049 737 47786
223 47049 out[x * 4] = row[x * 3];
224 47049 out[x * 4 + 1] = row[x * 3 + 1];
225 47049 out[x * 4 + 2] = row[x * 3 + 2];
226 47049 out[x * 4 + 3] = 255;
227 }
228 }
229 }
230  
231 46 free(row);
232 46 return rgba;
233 }
234  
235 /* Fills register_of with the register each palette entry is written
236 * to: its own index, or that of the first entry with the same colour.
237 */
238 static void
239 7 share_identical_registers(const sixel_palette *palette, uint16_t *register_of) {
240 int i, j;
241  
242 490 ▶ for (i = 0; i < palette->size; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < palette->size; ++i) { 483 7 490
243 52886 ▶ for (j = 0; memcmp(palette->rgb[j], palette->rgb[i], 3) != 0; ++j)
Branch: 1 branch
BranchTrueFalseTotal
for (j = 0; memcmp(palette->rgb[j], palette->rgb[i], 3) != 0; ++j) 52403 483 52886
244 ;
245 483 register_of[i] = (uint16_t)j;
246 }
247 7 }
248  
249 /* Paletted images whose palette fits are encoded with that palette.
250 * Entries that round to the same SIXEL colour share a register.
251 */
252 static int
253 7 index_paletted(i_img *im, const write_options *opts, indexed_image *indexed) {
254 i_color colors[SIXEL_MAX_PALETTE];
255 int painted[SIXEL_MAX_PALETTE];
256 uint16_t register_of[SIXEL_MAX_PALETTE];
257 7 ▶ int color_count = i_colorcount(im);
Branch: 1 branch
BranchTrueFalseTotal
int color_count = i_colorcount(im); 7 0 7
258 7 int alpha_channel = -1;
259 7 int has_alpha = i_img_alpha_channel(im, &alpha_channel);
260 7 int gray = i_img_color_channels(im) < 3;
261 7 size_t width = (size_t)im->xsize;
262 i_palidx *row;
263 i_img_dim y;
264 int i;
265  
266 7 ▶ if (!i_getcolors(im, 0, colors, color_count)) {
Branch: 2 branches
BranchTrueFalseTotal
if (!i_getcolors(im, 0, colors, color_count)) { 7 0 7
if (!i_getcolors(im, 0, colors, color_count)) { 0 7 7
267 0 i_push_error(0, "cannot read the image palette");
268 0 return 0;
269 }
270 7 row = malloc(width * sizeof(*row));
271 7 ▶ if (!row) {
Branch: 1 branch
BranchTrueFalseTotal
if (!row) { 0 7 7
272 0 i_push_error(0, "out of memory");
273 0 return 0;
274 }
275  
276 490 ▶ for (i = 0; i < color_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < color_count; ++i) { 483 7 490
277 483 const unsigned char *channel = colors[i].channel;
278 483 ▶ painted[i] = !has_alpha || channel[alpha_channel] >= opts->alpha_threshold;
Branch: 2 branches
BranchTrueFalseTotal
painted[i] = !has_alpha || channel[alpha_channel] >= opts->alpha_threshold; 2 481 483
painted[i] = !has_alpha || channel[alpha_channel] >= opts->alpha_threshold; 1 1 2
279 483 indexed->palette.rgb[i][0] = channel[0];
280 483 indexed->palette.rgb[i][1] = channel[gray ? 0 : 1];
281 483 ▶ indexed->palette.rgb[i][2] = channel[gray ? 0 : 2];
Branch: 1 branch
BranchTrueFalseTotal
indexed->palette.rgb[i][2] = channel[gray ? 0 : 2]; 0 483 483
282 }
283 7 indexed->palette.size = color_count;
284 7 sixel_palette_snap(&indexed->palette);
285 7 share_identical_registers(&indexed->palette, register_of);
286  
287 107 ▶ for (y = 0; y < im->ysize; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < im->ysize; ++y) { 100 7 107
288 100 uint16_t *out = indexed->map + (size_t)y * width;
289 size_t x;
290  
291 100 ▶ i_gpal(im, 0, im->xsize, y, row);
Branch: 1 branch
BranchTrueFalseTotal
i_gpal(im, 0, im->xsize, y, row); 100 0 100
292 4464 ▶ for (x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) { 4364 100 4464
293 4364 int index = row[x];
294 4364 ▶ out[x] = index < color_count && painted[index] ? register_of[index] : SIXEL_TRANSPARENT;
Branch: 2 branches
BranchTrueFalseTotal
out[x] = index < color_count && painted[index] ? register_of[index] : SIXEL_TRANSPARENT; 4364 0 4364
out[x] = index < color_count && painted[index] ? register_of[index] : SIXEL_TRANSPARENT; 4363 1 4364
295 }
296 }
297  
298 7 free(row);
299 7 return 1;
300 }
301  
302 static int
303 46 index_rgba(i_img *im, const write_options *opts, const i_color *custom, int custom_size,
304 indexed_image *indexed) {
305 46 size_t pixel_count = pixel_count_of(im);
306 46 sixel_palette *palette = &indexed->palette;
307 46 unsigned char *rgba = fetch_rgba(im, opts->alpha_threshold);
308 int i;
309  
310 46 ▶ if (!rgba)
Branch: 1 branch
BranchTrueFalseTotal
if (!rgba) 0 46 46
311 0 return 0;
312  
313 46 ▶ if (custom) {
Branch: 1 branch
BranchTrueFalseTotal
if (custom) { 4 42 46
314 12 ▶ for (i = 0; i < custom_size; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < custom_size; ++i) 8 4 12
315 8 memcpy(palette->rgb[i], custom[i].channel, 3);
316 4 palette->size = custom_size;
317 4 sixel_palette_snap(palette);
318 }
319 42 ▶ else if (opts->palette == PALETTE_WEBMAP) {
Branch: 1 branch
BranchTrueFalseTotal
else if (opts->palette == PALETTE_WEBMAP) { 4 38 42
320 4 sixel_palette_webmap(palette);
321 }
322 else {
323 38 ▶ if (sixel_palette_exact(rgba, pixel_count, opts->max_colors, palette, indexed->map)) {
Branch: 1 branch
BranchTrueFalseTotal
if (sixel_palette_exact(rgba, pixel_count, opts->max_colors, palette, indexed->map)) { 26 12 38
324 26 free(rgba);
325 26 return 1;
326 }
327 12 ▶ if (!sixel_palette_adaptive(rgba, pixel_count, opts->max_colors, palette)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sixel_palette_adaptive(rgba, pixel_count, opts->max_colors, palette)) { 0 12 12
328 0 free(rgba);
329 0 i_push_error(0, "out of memory");
330 0 return 0;
331 }
332 }
333  
334 20 ▶ if (!sixel_map_pixels(rgba, (size_t)im->xsize, (size_t)im->ysize, palette, opts->dither, indexed->map)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!sixel_map_pixels(rgba, (size_t)im->xsize, (size_t)im->ysize, palette, opts->dither, indexed->map)) { 0 20 20
335 0 free(rgba);
336 0 i_push_error(0, "out of memory");
337 0 return 0;
338 }
339  
340 20 free(rgba);
341 20 return 1;
342 }
343  
344 static int
345 53 index_image(i_img *im, const write_options *opts, const i_color *custom, int custom_size,
346 indexed_image *indexed) {
347 53 size_t pixel_count = pixel_count_of(im);
348 int uses_own_palette;
349  
350 53 indexed->width = im->xsize;
351 53 indexed->height = im->ysize;
352 53 indexed->transparent = i_img_has_alpha(im);
353 53 indexed->map = NULL;
354  
355 53 ▶ if (pixel_count == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (pixel_count == 0) { 0 53 53
356 0 i_push_error(0, "image too large to encode");
357 0 return 0;
358 }
359 53 indexed->map = malloc(pixel_count * sizeof(uint16_t));
360 53 ▶ if (!indexed->map) {
Branch: 1 branch
BranchTrueFalseTotal
if (!indexed->map) { 0 53 53
361 0 i_push_error(0, "out of memory");
362 0 return 0;
363 }
364  
365 53 uses_own_palette = !custom
366 49 ▶ && opts->palette == PALETTE_ADAPTIVE
Branch: 1 branch
BranchTrueFalseTotal
&& opts->palette == PALETTE_ADAPTIVE 45 4 49
367 45 ▶ && i_img_type(im) == i_palette_type
Branch: 1 branch
BranchTrueFalseTotal
&& i_img_type(im) == i_palette_type 8 37 45
368 102 ▶ && i_colorcount(im) <= opts->max_colors;
Branch: 3 branches
BranchTrueFalseTotal
&& i_colorcount(im) <= opts->max_colors; 49 4 53
&& i_colorcount(im) <= opts->max_colors; 8 0 8
&& i_colorcount(im) <= opts->max_colors; 7 1 8
369  
370 99 ▶ if (uses_own_palette ? index_paletted(im, opts, indexed)
Branch: 2 branches
BranchTrueFalseTotal
if (uses_own_palette ? index_paletted(im, opts, indexed) 7 46 53
if (uses_own_palette ? index_paletted(im, opts, indexed) 53 0 53
371 46 : index_rgba(im, opts, custom, custom_size, indexed))
372 53 return 1;
373  
374 0 indexed_release(indexed);
375 0 return 0;
376 }
377  
378 /* ------------------------------------------------------------------ */
379 /* output buffering */
380  
381 typedef struct {
382 io_glue *io;
383 unsigned char *buffer;
384 size_t length;
385 int failed;
386 } output;
387  
388 static void
389 53 output_flush(output *out) {
390 53 ▶ if (out->length && !out->failed) {
Branch: 2 branches
BranchTrueFalseTotal
if (out->length && !out->failed) { 53 0 53
if (out->length && !out->failed) { 53 0 53
391 53 ▶ if (i_io_write(out->io, out->buffer, out->length) != (ssize_t)out->length) {
Branch: 1 branch
BranchTrueFalseTotal
if (i_io_write(out->io, out->buffer, out->length) != (ssize_t)out->length) { 0 53 53
392 0 i_push_error(0, "write failed");
393 0 out->failed = 1;
394 }
395 }
396 53 out->length = 0;
397 53 }
398  
399 /* Returns where a token of at most OUTPUT_TOKEN_MAX bytes may be stored. */
400 static inline unsigned char *
401 34764 output_reserve(output *out) {
402 34764 ▶ if (out->length > OUTPUT_CAPACITY - OUTPUT_TOKEN_MAX)
Branch: 1 branch
BranchTrueFalseTotal
if (out->length > OUTPUT_CAPACITY - OUTPUT_TOKEN_MAX) 0 34764 34764
403 0 output_flush(out);
404 34764 return out->buffer + out->length;
405 }
406  
407 static inline unsigned char *
408 19032 format_number(unsigned char *p, uint64_t value) {
409 unsigned char digits[20];
410 19032 int n = 0;
411  
412 do {
413 38021 digits[n++] = (unsigned char)('0' + value % 10);
414 38021 value /= 10;
415 38021 ▶ } while (value);
Branch: 1 branch
BranchTrueFalseTotal
} while (value); 18989 19032 38021
416 57053 ▶ while (n)
Branch: 1 branch
BranchTrueFalseTotal
while (n) 38021 19032 57053
417 38021 *p++ = digits[--n];
418 19032 return p;
419 }
420  
421 static inline void
422 868 output_byte(output *out, unsigned char c) {
423 868 output_reserve(out)[0] = c;
424 868 out->length++;
425 868 }
426  
427 static void
428 53 output_text(output *out, const char *text) {
429 53 unsigned char *start = output_reserve(out);
430 53 size_t length = strlen(text);
431  
432 53 memcpy(start, text, length);
433 53 out->length += length;
434 53 }
435  
436 /* Writes count copies of the sixel character c. */
437 static inline void
438 25705 output_run(output *out, unsigned char c, size_t count) {
439 25705 unsigned char *start = output_reserve(out);
440 25705 unsigned char *p = start;
441  
442 25705 ▶ if (count > 3) {
Branch: 1 branch
BranchTrueFalseTotal
if (count > 3) { 2920 22785 25705
443 2920 *p++ = '!';
444 2920 p = format_number(p, count);
445 2920 *p++ = c;
446 }
447 else {
448 49229 ▶ while (count--)
Branch: 1 branch
BranchTrueFalseTotal
while (count--) 26444 22785 49229
449 26444 *p++ = c;
450 }
451 25705 out->length += (size_t)(p - start);
452 25705 }
453  
454 /* ------------------------------------------------------------------ */
455 /* stages 2 and 3: SIXEL stream */
456  
457 static void
458 53 write_header(output *out, const write_options *opts, const indexed_image *indexed) {
459 53 unsigned char *start = output_reserve(out);
460 53 unsigned char *p = start;
461  
462 /* DCS P1;P2;P3 q - P2 = 1 leaves unpainted pixels transparent */
463 53 *p++ = 0x1B;
464 53 *p++ = 'P';
465 53 *p++ = '0';
466 53 *p++ = ';';
467 53 ▶ *p++ = indexed->transparent ? '1' : '0';
Branch: 1 branch
BranchTrueFalseTotal
*p++ = indexed->transparent ? '1' : '0'; 8 45 53
468 53 *p++ = ';';
469 53 *p++ = '0';
470 53 *p++ = 'q';
471 /* raster attributes " Pan ; Pad ; Ph ; Pv */
472 53 *p++ = '"';
473 53 p = format_number(p, (uint64_t)opts->pan);
474 53 *p++ = ';';
475 53 p = format_number(p, (uint64_t)opts->pad);
476 53 *p++ = ';';
477 53 p = format_number(p, (uint64_t)indexed->width);
478 53 *p++ = ';';
479 53 p = format_number(p, (uint64_t)indexed->height);
480 53 out->length += (size_t)(p - start);
481 53 }
482  
483 static void
484 53 write_registers(output *out, const indexed_image *indexed) {
485 53 const sixel_palette *palette = &indexed->palette;
486 char used[SIXEL_MAX_PALETTE];
487 53 size_t pixel_count = (size_t)indexed->width * (size_t)indexed->height;
488 size_t i;
489 int index;
490  
491 53 memset(used, 0, sizeof(used));
492 51695 ▶ for (i = 0; i < pixel_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < pixel_count; ++i) { 51642 53 51695
493 51642 ▶ if (indexed->map[i] != SIXEL_TRANSPARENT)
Branch: 1 branch
BranchTrueFalseTotal
if (indexed->map[i] != SIXEL_TRANSPARENT) 51533 109 51642
494 51533 used[indexed->map[i]] = 1;
495 }
496  
497 3136 ▶ for (index = 0; index < palette->size; ++index) {
Branch: 1 branch
BranchTrueFalseTotal
for (index = 0; index < palette->size; ++index) { 3083 53 3136
498 unsigned char *start, *p;
499 int c;
500  
501 3083 ▶ if (!used[index])
Branch: 1 branch
BranchTrueFalseTotal
if (!used[index]) 478 2605 3083
502 478 continue;
503 2605 start = p = output_reserve(out);
504 2605 *p++ = '#';
505 2605 p = format_number(p, (uint64_t)index);
506 2605 *p++ = ';';
507 2605 *p++ = '2';
508 10420 ▶ for (c = 0; c < 3; ++c) {
Branch: 1 branch
BranchTrueFalseTotal
for (c = 0; c < 3; ++c) { 7815 2605 10420
509 7815 *p++ = ';';
510 7815 p = format_number(p, (uint64_t)sixel_percent_from_sample(palette->rgb[index][c]));
511 }
512 2605 out->length += (size_t)(p - start);
513 }
514 53 }
515  
516 /* A colour's columns are split into segments wherever SEGMENT_GAP or
517 * more columns lack the colour; shorter gaps are cheaper to skip with
518 * '?' than to start a new segment.
519 */
520 #define SEGMENT_GAP 10
521  
522 /* A run of one colour's column entries within a band. */
523 typedef struct {
524 uint32_t first; /* first entry in line_x / line_mask */
525 uint32_t count;
526 uint32_t start; /* first column */
527 uint32_t end; /* one past the last column */
528 uint16_t color;
529 int32_t next; /* next segment starting at the same column */
530 } segment;
531  
532 /* Per band scratch space, sized for the worst case of six distinct
533 * colours in every column.
534 */
535 typedef struct {
536 uint16_t *entry_color;
537 uint32_t *entry_x;
538 unsigned char *entry_mask;
539 uint32_t *line_x;
540 unsigned char *line_mask;
541 segment *segments;
542 int32_t *starting_at; /* first segment starting at each column */
543 uint32_t *next_start; /* union-find: next column with a segment */
544 uint32_t color_count[SIXEL_MAX_PALETTE];
545 uint32_t color_end[SIXEL_MAX_PALETTE];
546 uint16_t band_colors[SIXEL_MAX_PALETTE];
547 } band_scratch;
548  
549 static int
550 53 band_scratch_init(band_scratch *scratch, size_t width) {
551 53 size_t capacity = width * 6;
552  
553 53 scratch->entry_color = malloc(capacity * sizeof(uint16_t));
554 53 scratch->entry_x = malloc(capacity * sizeof(uint32_t));
555 53 scratch->entry_mask = malloc(capacity);
556 53 scratch->line_x = malloc(capacity * sizeof(uint32_t));
557 53 scratch->line_mask = malloc(capacity);
558 53 scratch->segments = malloc(capacity * sizeof(segment));
559 53 scratch->starting_at = malloc(width * sizeof(int32_t));
560 53 scratch->next_start = malloc((width + 1) * sizeof(uint32_t));
561 53 memset(scratch->color_count, 0, sizeof(scratch->color_count));
562 53 ▶ return scratch->entry_color && scratch->entry_x && scratch->entry_mask
Branch: 2 branches
BranchTrueFalseTotal
return scratch->entry_color && scratch->entry_x && scratch->entry_mask 53 0 53
return scratch->entry_color && scratch->entry_x && scratch->entry_mask 53 0 53
563 53 ▶ && scratch->line_x && scratch->line_mask && scratch->segments
Branch: 3 branches
BranchTrueFalseTotal
&& scratch->line_x && scratch->line_mask && scratch->segments 53 0 53
&& scratch->line_x && scratch->line_mask && scratch->segments 53 0 53
&& scratch->line_x && scratch->line_mask && scratch->segments 53 0 53
564 106 ▶ && scratch->starting_at && scratch->next_start;
Branch: 3 branches
BranchTrueFalseTotal
&& scratch->starting_at && scratch->next_start; 53 0 53
&& scratch->starting_at && scratch->next_start; 53 0 53
&& scratch->starting_at && scratch->next_start; 53 0 53
565 }
566  
567 static void
568 53 band_scratch_release(band_scratch *scratch) {
569 53 free(scratch->entry_color);
570 53 free(scratch->entry_x);
571 53 free(scratch->entry_mask);
572 53 free(scratch->line_x);
573 53 free(scratch->line_mask);
574 53 free(scratch->segments);
575 53 free(scratch->starting_at);
576 53 free(scratch->next_start);
577 53 }
578  
579 /* Groups the band's pixels into per colour column entries, ordered by
580 * colour and then column. Returns the number of colours; their entries
581 * end at color_end[band_colors[i]]. Sets *opaque when every pixel of
582 * the band is painted.
583 */
584 static int
585 169 collect_band(band_scratch *scratch, const indexed_image *indexed, i_img_dim top, int rows, int *opaque) {
586 169 size_t width = (size_t)indexed->width;
587 169 const uint16_t *band = indexed->map + (size_t)top * width;
588 169 size_t entry_count = 0;
589 169 int band_color_count = 0;
590 169 size_t offset = 0;
591 size_t x, e;
592 int i;
593  
594 169 *opaque = 1;
595 9157 ▶ for (x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) { 8988 169 9157
596 uint16_t column_color[6];
597 unsigned char column_mask[6];
598 8988 int distinct = 0;
599 int row, k;
600  
601 60630 ▶ for (row = 0; row < rows; ++row) {
Branch: 1 branch
BranchTrueFalseTotal
for (row = 0; row < rows; ++row) { 51642 8988 60630
602 51642 uint16_t color = band[(size_t)row * width + x];
603 51642 ▶ if (color == SIXEL_TRANSPARENT) {
Branch: 1 branch
BranchTrueFalseTotal
if (color == SIXEL_TRANSPARENT) { 109 51533 51642
604 109 *opaque = 0;
605 109 continue;
606 }
607 103361 ▶ for (k = 0; k < distinct && column_color[k] != color; ++k)
Branch: 2 branches
BranchTrueFalseTotal
for (k = 0; k < distinct && column_color[k] != color; ++k) 76144 27217 103361
for (k = 0; k < distinct && column_color[k] != color; ++k) 51828 24316 76144
608 ;
609 51533 ▶ if (k == distinct) {
Branch: 1 branch
BranchTrueFalseTotal
if (k == distinct) { 27217 24316 51533
610 27217 column_color[distinct] = color;
611 27217 column_mask[distinct] = 0;
612 27217 ++distinct;
613 }
614 51533 column_mask[k] |= (unsigned char)(1 << row);
615 }
616  
617 36205 ▶ for (k = 0; k < distinct; ++k) {
Branch: 1 branch
BranchTrueFalseTotal
for (k = 0; k < distinct; ++k) { 27217 8988 36205
618 27217 uint16_t color = column_color[k];
619 27217 ▶ if (scratch->color_count[color]++ == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (scratch->color_count[color]++ == 0) 5028 22189 27217
620 5028 scratch->band_colors[band_color_count++] = color;
621 27217 scratch->entry_color[entry_count] = color;
622 27217 scratch->entry_x[entry_count] = (uint32_t)x;
623 27217 scratch->entry_mask[entry_count] = column_mask[k];
624 27217 ++entry_count;
625 }
626 }
627  
628 /* counting sort by colour, which keeps each colour's columns in order */
629 5197 ▶ for (i = 0; i < band_color_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < band_color_count; ++i) { 5028 169 5197
630 5028 uint16_t color = scratch->band_colors[i];
631 5028 scratch->color_end[color] = (uint32_t)offset;
632 5028 offset += scratch->color_count[color];
633 }
634 27386 ▶ for (e = 0; e < entry_count; ++e) {
Branch: 1 branch
BranchTrueFalseTotal
for (e = 0; e < entry_count; ++e) { 27217 169 27386
635 27217 uint32_t slot = scratch->color_end[scratch->entry_color[e]]++;
636 27217 scratch->line_x[slot] = scratch->entry_x[e];
637 27217 scratch->line_mask[slot] = scratch->entry_mask[e];
638 }
639 169 return band_color_count;
640 }
641  
642 /* Splits the band's colours into segments and files each under the
643 * column it starts at. Returns the number of segments.
644 */
645 static size_t
646 169 build_segments(band_scratch *scratch, int band_color_count, size_t width) {
647 169 size_t segment_count = 0;
648 size_t x;
649 int i;
650  
651 9157 ▶ for (x = 0; x < width; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) 8988 169 9157
652 8988 scratch->starting_at[x] = -1;
653  
654 5197 ▶ for (i = 0; i < band_color_count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < band_color_count; ++i) { 5028 169 5197
655 5028 uint16_t color = scratch->band_colors[i];
656 5028 uint32_t end = scratch->color_end[color];
657 5028 uint32_t k = end - scratch->color_count[color];
658  
659 5028 scratch->color_count[color] = 0;
660 10522 ▶ while (k < end) {
Branch: 1 branch
BranchTrueFalseTotal
while (k < end) { 5494 5028 10522
661 5494 segment *seg = scratch->segments + segment_count;
662 5494 uint32_t last = k;
663  
664 27217 ▶ while (last + 1 < end && scratch->line_x[last + 1] - scratch->line_x[last] - 1 < SEGMENT_GAP)
Branch: 2 branches
BranchTrueFalseTotal
while (last + 1 < end && scratch->line_x[last + 1] - scratch->line_x[last] - 1 < SEGMENT_GAP) 22189 5028 27217
while (last + 1 < end && scratch->line_x[last + 1] - scratch->line_x[last] - 1 < SEGMENT_GAP) 21723 466 22189
665 21723 ++last;
666 5494 seg->first = k;
667 5494 seg->count = last - k + 1;
668 5494 seg->start = scratch->line_x[k];
669 5494 seg->end = scratch->line_x[last] + 1;
670 5494 seg->color = color;
671 5494 seg->next = scratch->starting_at[seg->start];
672 5494 scratch->starting_at[seg->start] = (int32_t)segment_count;
673 5494 ++segment_count;
674 5494 k = last + 1;
675 }
676 }
677  
678 /* a column without segments points to the next column */
679 9157 ▶ for (x = 0; x < width; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) 8988 169 9157
680 8988 ▶ scratch->next_start[x] = scratch->starting_at[x] >= 0 ? (uint32_t)x : (uint32_t)x + 1;
Branch: 1 branch
BranchTrueFalseTotal
scratch->next_start[x] = scratch->starting_at[x] >= 0 ? (uint32_t)x : (uint32_t)x + 1; 3927 5061 8988
681 169 scratch->next_start[width] = (uint32_t)width;
682 169 return segment_count;
683 }
684  
685 /* The first column at or after x where a segment starts, or the width. */
686 static uint32_t
687 6411 find_next_start(uint32_t *next_start, uint32_t x) {
688 14566 ▶ while (next_start[x] != x) {
Branch: 1 branch
BranchTrueFalseTotal
while (next_start[x] != x) { 8155 6411 14566
689 8155 next_start[x] = next_start[next_start[x]];
690 8155 x = next_start[x];
691 }
692 6411 return x;
693 }
694  
695 /* Writes a segment's sixels from its first column on; columns of the
696 * segment without the colour are skipped with '?'.
697 */
698 static void
699 4262 write_segment_data(output *out, const band_scratch *scratch, const segment *seg) {
700 4262 const uint32_t *xs = scratch->line_x + seg->first;
701 4262 const unsigned char *masks = scratch->line_mask + seg->first;
702 4262 size_t next_x = seg->start;
703 4262 unsigned char run_char = 0;
704 4262 size_t run_length = 0;
705 uint32_t i;
706  
707 24714 ▶ for (i = 0; i < seg->count; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < seg->count; ++i) { 20452 4262 24714
708 20452 unsigned char c = (unsigned char)('?' + masks[i]);
709  
710 20452 ▶ if (xs[i] != next_x) {
Branch: 1 branch
BranchTrueFalseTotal
if (xs[i] != next_x) { 2959 17493 20452
711 2959 ▶ if (run_length)
Branch: 1 branch
BranchTrueFalseTotal
if (run_length) 2959 0 2959
712 2959 output_run(out, run_char, run_length);
713 2959 output_run(out, '?', xs[i] - next_x);
714 2959 run_length = 0;
715 }
716 20452 ▶ if (run_length && c == run_char) {
Branch: 2 branches
BranchTrueFalseTotal
if (run_length && c == run_char) { 13231 7221 20452
if (run_length && c == run_char) { 1974 11257 13231
717 1974 ++run_length;
718 }
719 else {
720 18478 ▶ if (run_length)
Branch: 1 branch
BranchTrueFalseTotal
if (run_length) 11257 7221 18478
721 11257 output_run(out, run_char, run_length);
722 18478 run_char = c;
723 18478 run_length = 1;
724 }
725 20452 next_x = (size_t)xs[i] + 1;
726 }
727 4262 ▶ if (run_length)
Branch: 1 branch
BranchTrueFalseTotal
if (run_length) 4262 0 4262
728 4262 output_run(out, run_char, run_length);
729 4262 }
730  
731 /* Writes one band. Segments are packed greedily, left to right, into
732 * as few sixel lines as possible. In an opaque band the segments of the
733 * first line paint every row of their columns: the pixels of other
734 * colours are painted over by later lines, and the full columns
735 * compress into long runs.
736 *
737 * *active is the colour register currently selected, or -1. Bands
738 * without pixels are written only as the '-' before the next band
739 * with pixels, counted in *pending_newlines.
740 */
741 static void
742 169 write_band(output *out, band_scratch *scratch, const indexed_image *indexed, i_img_dim top,
743 int *active, int *pending_newlines) {
744 169 size_t width = (size_t)indexed->width;
745 169 ▶ int rows = indexed->height - top < 6 ? (int)(indexed->height - top) : 6;
Branch: 1 branch
BranchTrueFalseTotal
int rows = indexed->height - top < 6 ? (int)(indexed->height - top) : 6; 38 131 169
746 169 unsigned char full_column = (unsigned char)('?' + (1 << rows) - 1);
747 int opaque;
748 169 int band_color_count = collect_band(scratch, indexed, top, rows, &opaque);
749 size_t remaining;
750 169 int fill = opaque;
751  
752 169 remaining = build_segments(scratch, band_color_count, width);
753 169 ▶ if (!remaining) {
Branch: 1 branch
BranchTrueFalseTotal
if (!remaining) { 4 165 169
754 4 ++*pending_newlines;
755 4 return;
756 }
757 279 ▶ for (; *pending_newlines; --*pending_newlines)
Branch: 1 branch
BranchTrueFalseTotal
for (; *pending_newlines; --*pending_newlines) 114 165 279
758 114 output_byte(out, '-');
759 165 ++*pending_newlines;
760  
761 1082 ▶ while (remaining) {
Branch: 1 branch
BranchTrueFalseTotal
while (remaining) { 917 165 1082
762 917 uint32_t x = 0;
763 uint32_t column;
764  
765 6411 ▶ while ((column = find_next_start(scratch->next_start, x)) < width) {
Branch: 1 branch
BranchTrueFalseTotal
while ((column = find_next_start(scratch->next_start, x)) < width) { 5494 917 6411
766 5494 int32_t index = scratch->starting_at[column];
767 5494 segment *seg = scratch->segments + index;
768  
769 5494 scratch->starting_at[column] = seg->next;
770 5494 ▶ if (seg->next < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (seg->next < 0) 3927 1567 5494
771 3927 scratch->next_start[column] = column + 1;
772  
773 5494 ▶ if (seg->color != *active) {
Branch: 1 branch
BranchTrueFalseTotal
if (seg->color != *active) { 5480 14 5494
774 5480 unsigned char *start = output_reserve(out);
775 5480 unsigned char *p = start;
776 5480 *p++ = '#';
777 5480 p = format_number(p, seg->color);
778 5480 out->length += (size_t)(p - start);
779 5480 *active = seg->color;
780 }
781 5494 ▶ if (seg->start > x)
Branch: 1 branch
BranchTrueFalseTotal
if (seg->start > x) 3036 2458 5494
782 3036 output_run(out, '?', seg->start - x);
783 5494 ▶ if (fill)
Branch: 1 branch
BranchTrueFalseTotal
if (fill) 1232 4262 5494
784 1232 output_run(out, full_column, seg->end - seg->start);
785 else
786 4262 write_segment_data(out, scratch, seg);
787 5494 x = seg->end;
788 5494 --remaining;
789 }
790 917 ▶ if (remaining)
Branch: 1 branch
BranchTrueFalseTotal
if (remaining) 752 165 917
791 752 output_byte(out, '$');
792 917 fill = 0;
793 }
794 }
795  
796 static int
797 53 write_indexed(io_glue *ig, const write_options *opts, const indexed_image *indexed) {
798 band_scratch scratch;
799 output out;
800 i_img_dim top;
801 53 int active = -1;
802 53 int pending_newlines = 0;
803 int allocated;
804  
805 53 out.io = ig;
806 53 out.length = 0;
807 53 out.failed = 0;
808 53 out.buffer = malloc(OUTPUT_CAPACITY);
809 /* band_scratch_init leaves every member releasable, even on failure */
810 53 ▶ allocated = band_scratch_init(&scratch, (size_t)indexed->width) && out.buffer;
Branch: 2 branches
BranchTrueFalseTotal
allocated = band_scratch_init(&scratch, (size_t)indexed->width) && out.buffer; 53 0 53
allocated = band_scratch_init(&scratch, (size_t)indexed->width) && out.buffer; 53 0 53
811 53 ▶ if (!allocated) {
Branch: 1 branch
BranchTrueFalseTotal
if (!allocated) { 0 53 53
812 0 band_scratch_release(&scratch);
813 0 free(out.buffer);
814 0 i_push_error(0, "out of memory");
815 0 return 0;
816 }
817  
818 53 write_header(&out, opts, indexed);
819 53 write_registers(&out, indexed);
820 222 ▶ for (top = 0; top < indexed->height && !out.failed; top += 6)
Branch: 2 branches
BranchTrueFalseTotal
for (top = 0; top < indexed->height && !out.failed; top += 6) 169 53 222
for (top = 0; top < indexed->height && !out.failed; top += 6) 169 0 169
821 169 write_band(&out, &scratch, indexed, top, &active, &pending_newlines);
822 /* without any sixel data some decoders, libsixel among them, ignore
823 * the raster attributes; a carriage return makes them apply */
824 53 ▶ if (active < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (active < 0) 2 51 53
825 2 output_byte(&out, '$');
826 53 output_text(&out, "\x1b\\");
827 53 output_flush(&out);
828  
829 53 band_scratch_release(&scratch);
830 53 free(out.buffer);
831 53 return !out.failed;
832 }
833  
834 /* ------------------------------------------------------------------ */
835 /* public interface */
836  
837 static int
838 53 write_image(io_glue *ig, i_img *im, const write_options *opts, const i_color *palette, int palette_size) {
839 indexed_image indexed;
840 int ok;
841  
842 53 ▶ if (!index_image(im, opts, palette, palette_size, &indexed))
Branch: 1 branch
BranchTrueFalseTotal
if (!index_image(im, opts, palette, palette_size, &indexed)) 0 53 53
843 0 return 0;
844  
845 53 ok = write_indexed(ig, opts, &indexed);
846 53 indexed_release(&indexed);
847 53 return ok;
848 }
849  
850 int
851 61 i_writesixel(io_glue *ig, i_img *im, const i_color *palette, int palette_size) {
852 61 return i_writesixel_multi(ig, &im, 1, palette, palette_size);
853 }
854  
855 /* Every option is validated before anything is written, so invalid
856 * options never leave a partial stream behind.
857 */
858 int
859 63 i_writesixel_multi(io_glue *ig, i_img **imgs, int count, const i_color *palette, int palette_size) {
860 write_options *opts;
861 63 int ok = 1;
862 int i;
863  
864 63 i_clear_error();
865 63 ▶ if (count < 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (count < 1) { 0 63 63
866 0 i_push_error(0, "no images to write");
867 0 return 0;
868 }
869 63 ▶ if (palette && (palette_size < 1 || palette_size > SIXEL_MAX_PALETTE)) {
Branch: 3 branches
BranchTrueFalseTotal
if (palette && (palette_size < 1 || palette_size > SIXEL_MAX_PALETTE)) { 4 59 63
if (palette && (palette_size < 1 || palette_size > SIXEL_MAX_PALETTE)) { 4 0 4
if (palette && (palette_size < 1 || palette_size > SIXEL_MAX_PALETTE)) { 0 4 4
870 0 i_push_errorf(0, "the palette must hold from 1 to %d colors", SIXEL_MAX_PALETTE);
871 0 return 0;
872 }
873  
874 63 opts = malloc(sizeof(*opts) * (size_t)count);
875 63 ▶ if (!opts) {
Branch: 1 branch
BranchTrueFalseTotal
if (!opts) { 0 63 63
876 0 i_push_error(0, "out of memory");
877 0 return 0;
878 }
879 129 ▶ for (i = 0; i < count && ok; ++i)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < count && ok; ++i) 66 63 129
for (i = 0; i < count && ok; ++i) 66 0 66
880 66 ok = read_write_options(imgs[i], opts + i);
881 116 ▶ for (i = 0; i < count && ok; ++i)
Branch: 2 branches
BranchTrueFalseTotal
for (i = 0; i < count && ok; ++i) 65 51 116
for (i = 0; i < count && ok; ++i) 53 12 65
882 53 ok = write_image(ig, imgs[i], opts + i, palette, palette_size);
883 63 free(opts);
884  
885 63 ▶ if (ok && i_io_close(ig)) {
Branch: 2 branches
BranchTrueFalseTotal
if (ok && i_io_close(ig)) { 51 12 63
if (ok && i_io_close(ig)) { 0 51 51
886 0 i_push_error(0, "error closing output");
887 0 ok = 0;
888 }
889 63 return ok;
890 }
« sixel_read.c ↑ Summary sixel_quant.c »