sixel_read.c

filter: Statementstmt 90.3% 525 / 581 Branchbran 78.6% 313 / 398 Conditioncond n/a MC/DCmcdc n/a Subroutinesub n/a Podpod n/a totaltotal 85.6% 838 / 979 CC 0 SCAR 0
CC
0
Cov
0%
CRAP 0
« SIXEL.xs ↑ Summary sixel_write.c »
1 /*
2 * Imager::File::SIXEL - SIXEL decoder.
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 * The input is scanned for device control strings (DCS) whose final
10 * character is 'q' and which have no intermediate characters; every
11 * other byte, including other control strings, is skipped. Each sixel
12 * control string becomes one image.
13 *
14 * Pixels keep the colour their register held when they were painted,
15 * so streams that redefine registers to show more colours than there
16 * are registers decode as intended. The image covers every painted
17 * pixel and at least the size declared by the raster attributes.
18 */
19 #include "imext.h"
20 #include "imsixel.h"
21  
22 #include <math.h>
23 #include <stdint.h>
24 #include <stdlib.h>
25 #include <string.h>
26  
27 #define INPUT_BUFFER_SIZE 65536
28  
29 #define CHAR_EOF (-1)
30 #define CHAR_CAN 0x18
31 #define CHAR_SUB 0x1A
32 #define CHAR_ESC 0x1B
33 #define CHAR_DCS 0x90
34 #define CHAR_ST 0x9C
35  
36 #define REGISTER_COUNT 1024
37 #define INITIAL_REGISTER 15
38 /* pixels hold colour numbers below MAX_COLORS, or UNPAINTED */
39 #define MAX_COLORS 0xFFFE
40 #define UNPAINTED 0xFFFF
41 #define MAX_PARAMS 16
42 /* parameters saturate here, as do cursor coordinates */
43 #define PARAM_LIMIT 0x3FFFFFFF
44 /* Painting may write each pixel of the canvas this many times, plus
45 * PAINT_ALLOWANCE writes, before decoding stops: legitimate images
46 * paint most pixels once or twice, while the repeat command lets a few
47 * bytes paint over the same pixels endlessly.
48 */
49 #define PAINT_FACTOR 16
50 #define PAINT_ALLOWANCE (1u << 24)
51  
52 /* ------------------------------------------------------------------ */
53 /* input */
54  
55 typedef struct {
56 io_glue *io;
57 unsigned char buffer[INPUT_BUFFER_SIZE];
58 size_t position;
59 size_t length;
60 int pushed_back[2];
61 int pushed_count;
62 int at_end;
63 int failed;
64 } input;
65  
66 static void
67 92 input_init(input *in, io_glue *io) {
68 92 in->io = io;
69 92 in->position = 0;
70 92 in->length = 0;
71 92 in->pushed_count = 0;
72 92 in->at_end = 0;
73 92 in->failed = 0;
74 92 }
75  
76 static int
77 105 input_refill(input *in) {
78 ssize_t got;
79  
80 105 ▶ if (in->at_end)
Branch: 1 branch
BranchTrueFalseTotal
if (in->at_end) 1 104 105
81 1 return 0;
82 104 got = i_io_read(in->io, in->buffer, sizeof(in->buffer));
83 104 ▶ if (got <= 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (got <= 0) { 12 92 104
84 12 ▶ if (got < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (got < 0) { 0 12 12
85 0 i_push_error(0, "read failed");
86 0 in->failed = 1;
87 }
88 12 in->at_end = 1;
89 12 return 0;
90 }
91 92 in->position = 0;
92 92 in->length = (size_t)got;
93 92 return 1;
94 }
95  
96 /* Returns the next byte or CHAR_EOF. */
97 static inline int
98 82569 input_next(input *in) {
99 82569 ▶ if (in->pushed_count)
Branch: 1 branch
BranchTrueFalseTotal
if (in->pushed_count) 3 82566 82569
100 3 return in->pushed_back[--in->pushed_count];
101 82566 ▶ if (in->position == in->length && !input_refill(in))
Branch: 2 branches
BranchTrueFalseTotal
if (in->position == in->length && !input_refill(in)) 105 82461 82566
if (in->position == in->length && !input_refill(in)) 13 92 105
102 13 return CHAR_EOF;
103 82553 return in->buffer[in->position++];
104 }
105  
106 /* Returns c to the input; at most two bytes may be pending. */
107 static void
108 3 input_push_back(input *in, int c) {
109 3 in->pushed_back[in->pushed_count++] = c;
110 3 }
111  
112 /* Consumes the byte after an ESC that ends a control string. Unless the
113 * pair is the string terminator ESC \ it begins a new escape sequence,
114 * which is returned to the input for the search for the next image.
115 */
116 static void
117 88 finish_escape(input *in) {
118 88 int c = input_next(in);
119  
120 88 ▶ if (c == '\\' || c == CHAR_EOF)
Branch: 2 branches
BranchTrueFalseTotal
if (c == '\\' || c == CHAR_EOF) 1 87 88
if (c == '\\' || c == CHAR_EOF) 0 1 1
121 87 return;
122 1 input_push_back(in, c);
123 1 input_push_back(in, CHAR_ESC);
124 }
125  
126 /* ------------------------------------------------------------------ */
127 /* control string framing */
128  
129 typedef enum {
130 FOUND_SIXEL,
131 FOUND_NOTHING
132 } search_result;
133  
134 /* count reaches MAX_PARAMS + 1 once further parameters are ignored */
135 typedef struct {
136 int count;
137 int value[MAX_PARAMS];
138 } params;
139  
140 static int
141 58489 is_digit(int c) {
142 58489 ▶ return c >= '0' && c <= '9';
Branch: 2 branches
BranchTrueFalseTotal
return c >= '0' && c <= '9'; 55101 3388 58489
return c >= '0' && c <= '9'; 37544 17557 55101
143 }
144  
145 static void
146 9449 params_reset(params *p) {
147 9449 p->count = 1;
148 9449 p->value[0] = 0;
149 9449 }
150  
151 /* Accumulates a parameter byte (a digit or ';'). */
152 static void
153 49029 params_add(params *p, int c) {
154 int *v;
155  
156 49029 ▶ if (c == ';') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == ';') { 11485 37544 49029
157 11485 ▶ if (p->count < MAX_PARAMS)
Branch: 1 branch
BranchTrueFalseTotal
if (p->count < MAX_PARAMS) 11485 0 11485
158 11485 p->value[p->count] = 0;
159 11485 ▶ if (p->count <= MAX_PARAMS)
Branch: 1 branch
BranchTrueFalseTotal
if (p->count <= MAX_PARAMS) 11485 0 11485
160 11485 p->count++;
161 11485 return;
162 }
163 37544 ▶ if (p->count > MAX_PARAMS)
Branch: 1 branch
BranchTrueFalseTotal
if (p->count > MAX_PARAMS) 0 37544 37544
164 0 return;
165 37544 v = p->value + p->count - 1;
166 37544 ▶ *v = *v > (PARAM_LIMIT - 9) / 10 ? PARAM_LIMIT : *v * 10 + (c - '0');
Branch: 1 branch
BranchTrueFalseTotal
*v = *v > (PARAM_LIMIT - 9) / 10 ? PARAM_LIMIT : *v * 10 + (c - '0'); 37522 22 37544
167 }
168  
169 static int
170 28334 param_or(const params *p, int index, int fallback) {
171 28334 ▶ return index < p->count && index < MAX_PARAMS ? p->value[index] : fallback;
Branch: 2 branches
BranchTrueFalseTotal
return index < p->count && index < MAX_PARAMS ? p->value[index] : fallback; 11591 16743 28334
return index < p->count && index < MAX_PARAMS ? p->value[index] : fallback; 11591 0 11591
172 }
173  
174 /* Skips the rest of a control string. */
175 static void
176 6 skip_control_string(input *in) {
177 58 for (;;) {
178 64 int c = input_next(in);
179 64 switch (c) {
180 0 case CHAR_EOF:
181 case CHAR_ST:
182 case CHAR_CAN:
183 case CHAR_SUB:
184 0 return;
185 6 case CHAR_ESC:
186 6 finish_escape(in);
187 6 return;
188 }
189 }
190 }
191  
192 /* Reads the parameters and final byte of a control string that has
193 * been introduced. Returns 1 for a sixel string, leaving the input at
194 * the start of its data, and 0 after skipping anything else.
195 */
196 static int
197 101 read_dcs_header(input *in, params *header) {
198 101 int plain = 1;
199  
200 101 params_reset(header);
201 171 for (;;) {
202 272 int c = input_next(in);
203  
204 272 ▶ if (c == CHAR_EOF || c == CHAR_CAN || c == CHAR_SUB)
Branch: 3 branches
BranchTrueFalseTotal
if (c == CHAR_EOF || c == CHAR_CAN || c == CHAR_SUB) 272 0 272
if (c == CHAR_EOF || c == CHAR_CAN || c == CHAR_SUB) 272 0 272
if (c == CHAR_EOF || c == CHAR_CAN || c == CHAR_SUB) 0 272 272
205 0 return 0;
206 272 ▶ if (c == CHAR_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == CHAR_ESC) { 0 272 272
207 0 input_push_back(in, c);
208 0 return 0;
209 }
210 272 ▶ if (c == CHAR_ST)
Branch: 1 branch
BranchTrueFalseTotal
if (c == CHAR_ST) 0 272 272
211 0 return 0;
212 272 ▶ if (is_digit(c) || c == ';') {
Branch: 2 branches
BranchTrueFalseTotal
if (is_digit(c) || c == ';') { 167 105 272
if (is_digit(c) || c == ';') { 64 103 167
213 169 params_add(header, c);
214 }
215 103 ▶ else if (c >= 0x20 && c <= 0x2F) {
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0x20 && c <= 0x2F) { 103 0 103
else if (c >= 0x20 && c <= 0x2F) { 1 102 103
216 1 plain = 0; /* intermediate */
217 }
218 102 ▶ else if (c >= 0x3C && c <= 0x3F) {
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0x3C && c <= 0x3F) { 102 0 102
else if (c >= 0x3C && c <= 0x3F) { 1 101 102
219 1 plain = 0; /* private marker */
220 }
221 101 ▶ else if (c >= 0x40 && c <= 0x7E) {
Branch: 2 branches
BranchTrueFalseTotal
else if (c >= 0x40 && c <= 0x7E) { 101 0 101
else if (c >= 0x40 && c <= 0x7E) { 101 0 101
222 101 ▶ if (plain && c == 'q')
Branch: 2 branches
BranchTrueFalseTotal
if (plain && c == 'q') 99 2 101
if (plain && c == 'q') 99 0 99
223 99 return 1;
224 2 skip_control_string(in);
225 2 return 0;
226 }
227 /* other control characters are ignored */
228 }
229 }
230  
231 /* The number of continuation bytes following a UTF-8 lead byte. */
232 static int
233 157 utf8_continuations(int c) {
234 157 ▶ if (c >= 0xC2 && c <= 0xDF)
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xC2 && c <= 0xDF) 3 154 157
if (c >= 0xC2 && c <= 0xDF) 2 1 3
235 2 return 1;
236 155 ▶ if (c >= 0xE0 && c <= 0xEF)
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xE0 && c <= 0xEF) 1 154 155
if (c >= 0xE0 && c <= 0xEF) 1 0 1
237 1 return 2;
238 154 ▶ if (c >= 0xF0 && c <= 0xF4)
Branch: 2 branches
BranchTrueFalseTotal
if (c >= 0xF0 && c <= 0xF4) 0 154 154
if (c >= 0xF0 && c <= 0xF4) 0 0 0
239 0 return 3;
240 154 return 0;
241 }
242  
243 /* Advances to the data of the next sixel control string. A byte 0x90
244 * that continues a UTF-8 character in the surrounding text is not a
245 * control string introducer.
246 */
247 static search_result
248 108 find_sixel(input *in, params *header) {
249 108 int continuations = 0;
250  
251 53 for (;;) {
252 161 int c = input_next(in);
253  
254 161 ▶ if (continuations && c >= 0x80 && c <= 0xBF) {
Branch: 3 branches
BranchTrueFalseTotal
if (continuations && c >= 0x80 && c <= 0xBF) { 4 157 161
if (continuations && c >= 0x80 && c <= 0xBF) { 4 0 4
if (continuations && c >= 0x80 && c <= 0xBF) { 4 0 4
255 4 --continuations;
256 4 continue;
257 }
258 157 continuations = utf8_continuations(c);
259 157 ▶ if (c == CHAR_EOF)
Branch: 1 branch
BranchTrueFalseTotal
if (c == CHAR_EOF) 9 148 157
260 9 return FOUND_NOTHING;
261 148 ▶ if (c == CHAR_ESC) {
Branch: 1 branch
BranchTrueFalseTotal
if (c == CHAR_ESC) { 101 47 148
262 101 c = input_next(in);
263 101 ▶ if (c == 'P') {
Branch: 1 branch
BranchTrueFalseTotal
if (c == 'P') { 100 1 101
264 100 ▶ if (read_dcs_header(in, header))
Branch: 1 branch
BranchTrueFalseTotal
if (read_dcs_header(in, header)) 98 2 100
265 98 return FOUND_SIXEL;
266 }
267 1 ▶ else if (c != CHAR_EOF) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c != CHAR_EOF) { 1 0 1
268 1 input_push_back(in, c);
269 }
270 }
271 47 ▶ else if (c == CHAR_DCS) {
Branch: 1 branch
BranchTrueFalseTotal
else if (c == CHAR_DCS) { 1 46 47
272 1 ▶ if (read_dcs_header(in, header))
Branch: 1 branch
BranchTrueFalseTotal
if (read_dcs_header(in, header)) 1 0 1
273 1 return FOUND_SIXEL;
274 }
275 }
276 }
277  
278 /* ------------------------------------------------------------------ */
279 /* colour registers */
280  
281 /* the VT340 power-on colour map, in percent */
282 static const unsigned char vt340_colors[16][3] = {
283 { 0, 0, 0 }, { 20, 20, 80 }, { 80, 13, 13 }, { 20, 80, 20 },
284 { 80, 20, 80 }, { 20, 80, 80 }, { 80, 80, 20 }, { 53, 53, 53 },
285 { 26, 26, 26 }, { 33, 33, 60 }, { 60, 26, 26 }, { 33, 60, 33 },
286 { 60, 33, 60 }, { 33, 60, 60 }, { 60, 60, 33 }, { 80, 80, 80 },
287 };
288  
289 static unsigned char
290 13011 sample_from_percent(int percent) {
291 13011 ▶ if (percent > 100)
Branch: 1 branch
BranchTrueFalseTotal
if (percent > 100) 1 13010 13011
292 1 percent = 100;
293 13011 return (unsigned char)((percent * 255 + 50) / 100);
294 }
295  
296 static unsigned char
297 18 sample_from_unit(double value) {
298 18 return (unsigned char)floor(value * 255.0 + 0.5);
299 }
300  
301 static double
302 18 hls_component(double low, double high, double hue) {
303 18 ▶ if (hue < 0)
Branch: 1 branch
BranchTrueFalseTotal
if (hue < 0) 1 17 18
304 1 hue += 360;
305 18 ▶ if (hue >= 360)
Branch: 1 branch
BranchTrueFalseTotal
if (hue >= 360) 3 15 18
306 3 hue -= 360;
307 18 ▶ if (hue < 60)
Branch: 1 branch
BranchTrueFalseTotal
if (hue < 60) 5 13 18
308 5 return low + (high - low) * hue / 60;
309 13 ▶ if (hue < 180)
Branch: 1 branch
BranchTrueFalseTotal
if (hue < 180) 6 7 13
310 6 return high;
311 7 ▶ if (hue < 240)
Branch: 1 branch
BranchTrueFalseTotal
if (hue < 240) 1 6 7
312 1 return low + (high - low) * (240 - hue) / 60;
313 6 return low;
314 }
315  
316 /* Converts a DEC HLS colour (hue 0..360 with blue at 0, lightness and
317 * saturation 0..100) to RGB.
318 */
319 static void
320 6 hls_to_rgb(int hue, int lightness, int saturation, unsigned char *rgb) {
321 6 ▶ double l = (lightness > 100 ? 100 : lightness) / 100.0;
Branch: 1 branch
BranchTrueFalseTotal
double l = (lightness > 100 ? 100 : lightness) / 100.0; 6 0 6
322 6 ▶ double s = (saturation > 100 ? 100 : saturation) / 100.0;
Branch: 1 branch
BranchTrueFalseTotal
double s = (saturation > 100 ? 100 : saturation) / 100.0; 6 0 6
323 6 double h = fmod(hue + 240.0, 360.0); /* rotate blue from 0 to 240 */
324 6 ▶ double high = l <= 0.5 ? l * (1 + s) : l + s - l * s;
Branch: 1 branch
BranchTrueFalseTotal
double high = l <= 0.5 ? l * (1 + s) : l + s - l * s; 5 1 6
325 6 double low = 2 * l - high;
326  
327 6 rgb[0] = sample_from_unit(hls_component(low, high, h + 120));
328 6 rgb[1] = sample_from_unit(hls_component(low, high, h));
329 6 rgb[2] = sample_from_unit(hls_component(low, high, h - 120));
330 6 }
331  
332 /* ------------------------------------------------------------------ */
333 /* canvas */
334  
335 /* A colour a register held while painting. Redefining a register that
336 * has painted pixels starts a new colour, so pixels keep the colour
337 * they were painted with.
338 */
339 typedef struct {
340 unsigned char rgb[3];
341 unsigned char painted;
342 } color_entry;
343  
344 typedef struct {
345 uint16_t register_color[REGISTER_COUNT];
346 color_entry *colors;
347 size_t color_count;
348 size_t color_capacity;
349 uint16_t *pixels;
350 i_img_dim allocated_width;
351 i_img_dim allocated_height;
352 i_img_dim painted_width; /* one past the rightmost painted pixel */
353 i_img_dim painted_height; /* one past the lowest painted pixel */
354 i_img_dim declared_width;
355 i_img_dim declared_height;
356 i_img_dim x;
357 i_img_dim y;
358 int current;
359 int transparent;
360 int pan;
361 int pad;
362 int seen_data;
363 uint64_t painted; /* pixel writes so far */
364 /* Imager's file limits, 0 meaning unlimited */
365 i_img_dim limit_width;
366 i_img_dim limit_height;
367 size_t limit_bytes;
368 } canvas;
369  
370 /* The pixel aspect ratio selected by P1 of the control string. */
371 static void
372 95 aspect_from_p1(int p1, int *pan, int *pad) {
373 95 *pad = 1;
374 95 switch (p1) {
375 2 case 2:
376 2 *pan = 5;
377 2 break;
378 1 case 3:
379 case 4:
380 1 *pan = 3;
381 1 break;
382 2 case 7:
383 case 8:
384 case 9:
385 2 *pan = 1;
386 2 break;
387 90 default:
388 90 *pan = 2;
389 90 break;
390 }
391 95 }
392  
393 static int
394 95 canvas_init(canvas *cv, const params *header) {
395 int i;
396  
397 95 cv->pixels = NULL;
398 95 cv->color_capacity = 2 * REGISTER_COUNT;
399 95 cv->colors = calloc(cv->color_capacity, sizeof(*cv->colors));
400 95 ▶ if (!cv->colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cv->colors) { 0 95 95
401 0 i_push_error(0, "out of memory");
402 0 return 0;
403 }
404 95 cv->color_count = REGISTER_COUNT;
405 97375 ▶ for (i = 0; i < REGISTER_COUNT; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < REGISTER_COUNT; ++i) 97280 95 97375
406 97280 cv->register_color[i] = (uint16_t)i;
407 1615 ▶ for (i = 0; i < 16; ++i) {
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < 16; ++i) { 1520 95 1615
408 1520 cv->colors[i].rgb[0] = sample_from_percent(vt340_colors[i][0]);
409 1520 cv->colors[i].rgb[1] = sample_from_percent(vt340_colors[i][1]);
410 1520 cv->colors[i].rgb[2] = sample_from_percent(vt340_colors[i][2]);
411 }
412 95 cv->allocated_width = cv->allocated_height = 0;
413 95 cv->painted_width = cv->painted_height = 0;
414 95 cv->declared_width = cv->declared_height = 0;
415 95 cv->x = cv->y = 0;
416 95 cv->current = INITIAL_REGISTER;
417 95 cv->transparent = param_or(header, 1, 0) == 1;
418 95 cv->seen_data = 0;
419 95 cv->painted = 0;
420 95 aspect_from_p1(param_or(header, 0, 0), &cv->pan, &cv->pad);
421 95 i_get_image_file_limits(&cv->limit_width, &cv->limit_height, &cv->limit_bytes);
422 95 return 1;
423 }
424  
425 static void
426 95 canvas_release(canvas *cv) {
427 95 free(cv->pixels);
428 95 free(cv->colors);
429 95 cv->pixels = NULL;
430 95 cv->colors = NULL;
431 95 }
432  
433 /* Checks a size against the file limits, pushing Imager's error. */
434 static int
435 264 dimensions_allowed(i_img_dim width, i_img_dim height, int transparent) {
436 264 ▶ if (width > PARAM_LIMIT || height > PARAM_LIMIT) {
Branch: 2 branches
BranchTrueFalseTotal
if (width > PARAM_LIMIT || height > PARAM_LIMIT) { 264 0 264
if (width > PARAM_LIMIT || height > PARAM_LIMIT) { 0 264 264
437 0 i_push_error(0, "image dimensions are too large");
438 0 return 0;
439 }
440 264 ▶ return i_int_check_image_file_limits(width, height, transparent ? 4 : 3, sizeof(i_sample_t));
Branch: 1 branch
BranchTrueFalseTotal
return i_int_check_image_file_limits(width, height, transparent ? 4 : 3, sizeof(i_sample_t)); 24 240 264
441 }
442  
443 /* The same check without reporting, for choosing allocation sizes. */
444 static int
445 135 within_limits(const canvas *cv, i_img_dim width, i_img_dim height) {
446 135 ▶ size_t channels = cv->transparent ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
size_t channels = cv->transparent ? 4 : 3; 8 127 135
447  
448 135 ▶ if (width > PARAM_LIMIT || height > PARAM_LIMIT)
Branch: 2 branches
BranchTrueFalseTotal
if (width > PARAM_LIMIT || height > PARAM_LIMIT) 135 0 135
if (width > PARAM_LIMIT || height > PARAM_LIMIT) 0 135 135
449 0 return 0;
450 135 ▶ if ((cv->limit_width && width > cv->limit_width) || (cv->limit_height && height > cv->limit_height))
Branch: 4 branches
BranchTrueFalseTotal
if ((cv->limit_width && width > cv->limit_width) || (cv->limit_height && height > cv->limit_height)) 1 134 135
if ((cv->limit_width && width > cv->limit_width) || (cv->limit_height && height > cv->limit_height)) 1 0 1
if ((cv->limit_width && width > cv->limit_width) || (cv->limit_height && height > cv->limit_height)) 1 134 135
if ((cv->limit_width && width > cv->limit_width) || (cv->limit_height && height > cv->limit_height)) 0 1 1
451 0 return 0;
452 135 ▶ return !cv->limit_bytes || (size_t)width * (size_t)height * channels <= cv->limit_bytes;
Branch: 2 branches
BranchTrueFalseTotal
return !cv->limit_bytes || (size_t)width * (size_t)height * channels <= cv->limit_bytes; 135 0 135
return !cv->limit_bytes || (size_t)width * (size_t)height * channels <= cv->limit_bytes; 134 1 135
453 }
454  
455 /* The largest width within the limits for a canvas of the given height. */
456 static i_img_dim
457 1 largest_width(const canvas *cv, i_img_dim height) {
458 1 i_img_dim largest = PARAM_LIMIT;
459 1 ▶ size_t channels = cv->transparent ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
size_t channels = cv->transparent ? 4 : 3; 0 1 1
460  
461 1 ▶ if (cv->limit_width && cv->limit_width < largest)
Branch: 2 branches
BranchTrueFalseTotal
if (cv->limit_width && cv->limit_width < largest) 0 1 1
if (cv->limit_width && cv->limit_width < largest) 0 0 0
462 0 largest = cv->limit_width;
463 1 ▶ if (cv->limit_bytes && (size_t)largest * (size_t)height * channels > cv->limit_bytes)
Branch: 2 branches
BranchTrueFalseTotal
if (cv->limit_bytes && (size_t)largest * (size_t)height * channels > cv->limit_bytes) 1 0 1
if (cv->limit_bytes && (size_t)largest * (size_t)height * channels > cv->limit_bytes) 1 0 1
464 1 largest = (i_img_dim)(cv->limit_bytes / ((size_t)height * channels));
465 1 return largest;
466 }
467  
468 /* The largest height within the limits for a canvas of the given width. */
469 static i_img_dim
470 1 largest_height(const canvas *cv, i_img_dim width) {
471 1 i_img_dim largest = PARAM_LIMIT;
472 1 ▶ size_t channels = cv->transparent ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
size_t channels = cv->transparent ? 4 : 3; 0 1 1
473  
474 1 ▶ if (cv->limit_height && cv->limit_height < largest)
Branch: 2 branches
BranchTrueFalseTotal
if (cv->limit_height && cv->limit_height < largest) 0 1 1
if (cv->limit_height && cv->limit_height < largest) 0 0 0
475 0 largest = cv->limit_height;
476 1 ▶ if (cv->limit_bytes && (size_t)largest * (size_t)width * channels > cv->limit_bytes)
Branch: 2 branches
BranchTrueFalseTotal
if (cv->limit_bytes && (size_t)largest * (size_t)width * channels > cv->limit_bytes) 1 0 1
if (cv->limit_bytes && (size_t)largest * (size_t)width * channels > cv->limit_bytes) 1 0 1
477 1 largest = (i_img_dim)(cv->limit_bytes / ((size_t)width * channels));
478 1 return largest;
479 }
480  
481 static i_img_dim
482 2 clamp_dimension(i_img_dim value, i_img_dim low, i_img_dim high) {
483 2 ▶ return value > high ? (high > low ? high : low) : value;
Branch: 1 branch
BranchTrueFalseTotal
return value > high ? (high > low ? high : low) : value; 1 1 2
484 }
485  
486 /* Moves the pixels to storage of new_width x new_height. Storage that
487 * only grows in height is reallocated in place.
488 */
489 static int
490 134 canvas_resize(canvas *cv, i_img_dim new_width, i_img_dim new_height) {
491 134 size_t old_count = (size_t)cv->allocated_width * (size_t)cv->allocated_height;
492 size_t new_count;
493 uint16_t *pixels;
494 i_img_dim y;
495  
496 134 ▶ if ((size_t)new_height > SIZE_MAX / sizeof(uint16_t) / (size_t)new_width) {
Branch: 1 branch
BranchTrueFalseTotal
if ((size_t)new_height > SIZE_MAX / sizeof(uint16_t) / (size_t)new_width) { 0 134 134
497 0 i_push_error(0, "image dimensions are too large");
498 0 return 0;
499 }
500 134 new_count = (size_t)new_width * (size_t)new_height;
501  
502 134 ▶ if (new_width == cv->allocated_width) {
Branch: 1 branch
BranchTrueFalseTotal
if (new_width == cv->allocated_width) { 3 131 134
503 3 pixels = realloc(cv->pixels, new_count * sizeof(uint16_t));
504 3 ▶ if (!pixels) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pixels) { 0 3 3
505 0 i_push_error(0, "out of memory");
506 0 return 0;
507 }
508 3 memset(pixels + old_count, 0xFF, (new_count - old_count) * sizeof(uint16_t));
509 }
510 else {
511 131 pixels = malloc(new_count * sizeof(uint16_t));
512 131 ▶ if (!pixels) {
Branch: 1 branch
BranchTrueFalseTotal
if (!pixels) { 0 131 131
513 0 i_push_error(0, "out of memory");
514 0 return 0;
515 }
516 131 memset(pixels, 0xFF, new_count * sizeof(uint16_t));
517 372 ▶ for (y = 0; y < cv->allocated_height; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < cv->allocated_height; ++y) { 241 131 372
518 241 memcpy(pixels + (size_t)y * new_width,
519 241 cv->pixels + (size_t)y * cv->allocated_width,
520 241 (size_t)cv->allocated_width * sizeof(uint16_t));
521 }
522 131 free(cv->pixels);
523 }
524 134 cv->pixels = pixels;
525 134 cv->allocated_width = new_width;
526 134 cv->allocated_height = new_height;
527 134 return 1;
528 }
529  
530 /* Grows the pixel storage to at least width x height. Each growing
531 * axis is doubled or, when that breaks a limit, grown to the largest
532 * size within the limits, so storage is moved O(log n) times. The
533 * first allocation also covers the declared size, so that images
534 * painted within it are never moved.
535 */
536 static int
537 16360 canvas_reserve(canvas *cv, i_img_dim width, i_img_dim height) {
538 i_img_dim needed_width, needed_height, new_width, new_height;
539  
540 16360 ▶ if (width <= cv->allocated_width && height <= cv->allocated_height)
Branch: 2 branches
BranchTrueFalseTotal
if (width <= cv->allocated_width && height <= cv->allocated_height) 16226 134 16360
if (width <= cv->allocated_width && height <= cv->allocated_height) 16222 4 16226
541 16222 return 1;
542 138 ▶ if (!cv->pixels) {
Branch: 1 branch
BranchTrueFalseTotal
if (!cv->pixels) { 93 45 138
543 93 ▶ if (width < cv->declared_width)
Branch: 1 branch
BranchTrueFalseTotal
if (width < cv->declared_width) 28 65 93
544 28 width = cv->declared_width;
545 93 ▶ if (height < cv->declared_height)
Branch: 1 branch
BranchTrueFalseTotal
if (height < cv->declared_height) 22 71 93
546 22 height = cv->declared_height;
547 }
548 138 needed_width = width > cv->allocated_width ? width : cv->allocated_width;
549 138 needed_height = height > cv->allocated_height ? height : cv->allocated_height;
550 138 ▶ if (!dimensions_allowed(needed_width, needed_height, cv->transparent))
Branch: 1 branch
BranchTrueFalseTotal
if (!dimensions_allowed(needed_width, needed_height, cv->transparent)) 4 134 138
551 4 return 0;
552  
553 131 ▶ new_width = needed_width > cv->allocated_width && needed_width < 2 * cv->allocated_width
Branch: 1 branch
BranchTrueFalseTotal
new_width = needed_width > cv->allocated_width && needed_width < 2 * cv->allocated_width 22 109 131
554 265 ▶ ? 2 * cv->allocated_width : needed_width;
Branch: 1 branch
BranchTrueFalseTotal
? 2 * cv->allocated_width : needed_width; 131 3 134
555 94 ▶ new_height = needed_height > cv->allocated_height && needed_height < 2 * cv->allocated_height
Branch: 1 branch
BranchTrueFalseTotal
new_height = needed_height > cv->allocated_height && needed_height < 2 * cv->allocated_height 1 93 94
556 228 ▶ ? 2 * cv->allocated_height : needed_height;
Branch: 1 branch
BranchTrueFalseTotal
? 2 * cv->allocated_height : needed_height; 94 40 134
557 134 ▶ if (!within_limits(cv, new_width, new_height)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!within_limits(cv, new_width, new_height)) { 1 133 134
558 1 new_width = clamp_dimension(new_width, needed_width, largest_width(cv, needed_height));
559 1 new_height = clamp_dimension(new_height, needed_height, largest_height(cv, new_width));
560 1 ▶ if (!within_limits(cv, new_width, new_height)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!within_limits(cv, new_width, new_height)) { 0 1 1
561 0 new_width = needed_width;
562 0 new_height = needed_height;
563 }
564 }
565 134 return canvas_resize(cv, new_width, new_height);
566 }
567  
568 static i_img_dim
569 6628 advance(i_img_dim position, i_img_dim distance) {
570 6628 return position + distance > PARAM_LIMIT ? PARAM_LIMIT : position + distance;
571 }
572  
573 /* Paints the sixel bits at the cursor repeat times and advances it. */
574 static int
575 22825 canvas_paint(canvas *cv, int bits, int repeat) {
576 i_img_dim right, bottom, y;
577 22825 int top_row = 0;
578 22825 int bottom_row = 5;
579  
580 22825 cv->seen_data = 1;
581 22825 ▶ if (bits == 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (bits == 0) { 6551 16274 22825
582 6551 cv->x = advance(cv->x, repeat);
583 6551 return 1;
584 }
585  
586 46000 ▶ while (!(bits & (1 << top_row)))
Branch: 1 branch
BranchTrueFalseTotal
while (!(bits & (1 << top_row))) 29726 16274 46000
587 29726 ++top_row;
588 48723 ▶ while (!(bits & (1 << bottom_row)))
Branch: 1 branch
BranchTrueFalseTotal
while (!(bits & (1 << bottom_row))) 32449 16274 48723
589 32449 --bottom_row;
590  
591 16274 right = cv->x + repeat;
592 16274 bottom = cv->y + bottom_row + 1;
593 16274 ▶ if (right > PARAM_LIMIT || bottom > PARAM_LIMIT) {
Branch: 2 branches
BranchTrueFalseTotal
if (right > PARAM_LIMIT || bottom > PARAM_LIMIT) { 16274 0 16274
if (right > PARAM_LIMIT || bottom > PARAM_LIMIT) { 0 16274 16274
594 0 i_push_error(0, "image dimensions are too large");
595 0 return 0;
596 }
597 16274 ▶ if (!canvas_reserve(cv, right, bottom))
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_reserve(cv, right, bottom)) 4 16270 16274
598 4 return 0;
599  
600 51718 ▶ for (y = top_row; y <= bottom_row; ++y)
Branch: 1 branch
BranchTrueFalseTotal
for (y = top_row; y <= bottom_row; ++y) 35448 16270 51718
601 35448 ▶ cv->painted += (bits >> y) & 1 ? (uint64_t)repeat : 0;
Branch: 1 branch
BranchTrueFalseTotal
cv->painted += (bits >> y) & 1 ? (uint64_t)repeat : 0; 33321 2127 35448
602 16270 ▶ if (cv->painted > PAINT_FACTOR * (uint64_t)cv->allocated_width * (uint64_t)cv->allocated_height + PAINT_ALLOWANCE) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv->painted > PAINT_FACTOR * (uint64_t)cv->allocated_width * (uint64_t)cv->allocated_height + PAINT_ALLOWANCE) { 1 16269 16270
603 1 i_push_error(0, "SIXEL data paints too many pixels");
604 1 return 0;
605 }
606  
607 51711 ▶ for (y = top_row; y <= bottom_row; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = top_row; y <= bottom_row; ++y) { 35442 16269 51711
608 uint16_t *p, *end;
609 35442 ▶ if (!(bits & (1 << y)))
Branch: 1 branch
BranchTrueFalseTotal
if (!(bits & (1 << y))) 2127 33315 35442
610 2127 continue;
611 33315 p = cv->pixels + (size_t)(cv->y + y) * cv->allocated_width + cv->x;
612 33315 end = p + repeat;
613 28294532 ▶ while (p < end)
Branch: 1 branch
BranchTrueFalseTotal
while (p < end) 28261217 33315 28294532
614 28261217 *p++ = cv->register_color[cv->current];
615 }
616 16269 cv->colors[cv->register_color[cv->current]].painted = 1;
617  
618 16269 ▶ if (right > cv->painted_width)
Branch: 1 branch
BranchTrueFalseTotal
if (right > cv->painted_width) 865 15404 16269
619 865 cv->painted_width = right;
620 16269 ▶ if (bottom > cv->painted_height)
Branch: 1 branch
BranchTrueFalseTotal
if (bottom > cv->painted_height) 181 16088 16269
621 181 cv->painted_height = bottom;
622 16269 cv->x = right;
623 16269 return 1;
624 }
625  
626 /* Sets the colour of a register. A register whose colour has painted
627 * pixels gets a new colour, unless MAX_COLORS have been used, in which
628 * case its earlier pixels change as well.
629 */
630 static int
631 2823 canvas_define_register(canvas *cv, int index, const unsigned char *rgb) {
632 2823 color_entry *entry = cv->colors + cv->register_color[index];
633  
634 2823 ▶ if (entry->painted && cv->color_count < MAX_COLORS) {
Branch: 2 branches
BranchTrueFalseTotal
if (entry->painted && cv->color_count < MAX_COLORS) { 301 2522 2823
if (entry->painted && cv->color_count < MAX_COLORS) { 301 0 301
635 301 ▶ if (cv->color_count == cv->color_capacity) {
Branch: 1 branch
BranchTrueFalseTotal
if (cv->color_count == cv->color_capacity) { 0 301 301
636 0 size_t capacity = cv->color_capacity * 2 > MAX_COLORS ? MAX_COLORS : cv->color_capacity * 2;
637 0 color_entry *colors = realloc(cv->colors, capacity * sizeof(*colors));
638 0 ▶ if (!colors) {
Branch: 1 branch
BranchTrueFalseTotal
if (!colors) { 0 0 0
639 0 i_push_error(0, "out of memory");
640 0 return 0;
641 }
642 0 cv->colors = colors;
643 0 cv->color_capacity = capacity;
644 }
645 301 cv->register_color[index] = (uint16_t)cv->color_count;
646 301 entry = cv->colors + cv->color_count++;
647 301 entry->painted = 0;
648 }
649 2823 memcpy(entry->rgb, rgb, 3);
650 2823 return 1;
651 }
652  
653 /* Selects a register, defining its colour first if the colour space
654 * is known.
655 */
656 static int
657 6994 canvas_select_color(canvas *cv, const params *p) {
658 6994 int index = p->value[0] % REGISTER_COUNT;
659 6994 int space = param_or(p, 1, 0);
660 6994 int x = param_or(p, 2, 0);
661 6994 int y = param_or(p, 3, 0);
662 6994 int z = param_or(p, 4, 0);
663 unsigned char rgb[3];
664  
665 6994 cv->current = index;
666 6994 ▶ if (p->count < 5)
Branch: 1 branch
BranchTrueFalseTotal
if (p->count < 5) 4170 2824 6994
667 4170 return 1;
668  
669 2824 ▶ if (space == 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (space == 1) { 6 2818 2824
670 6 hls_to_rgb(x > 360 ? 360 : x, y, z, rgb);
671 }
672 2818 ▶ else if (space == 2) {
Branch: 1 branch
BranchTrueFalseTotal
else if (space == 2) { 2817 1 2818
673 2817 rgb[0] = sample_from_percent(x);
674 2817 rgb[1] = sample_from_percent(y);
675 2817 rgb[2] = sample_from_percent(z);
676 }
677 else {
678 1 return 1; /* other colour spaces leave the register unchanged */
679 }
680 2823 return canvas_define_register(cv, index, rgb);
681 }
682  
683 /* Raster attributes take effect only before the first sixel. */
684 static int
685 42 canvas_raster_attributes(canvas *cv, const params *p) {
686 42 int pan = param_or(p, 0, 0);
687 42 int pad = param_or(p, 1, 0);
688 42 int width = param_or(p, 2, 0);
689 42 int height = param_or(p, 3, 0);
690  
691 42 ▶ if (cv->seen_data)
Branch: 1 branch
BranchTrueFalseTotal
if (cv->seen_data) 1 41 42
692 1 return 1;
693  
694 41 ▶ if (pan > 0 && pad > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (pan > 0 && pad > 0) { 41 0 41
if (pan > 0 && pad > 0) { 41 0 41
695 41 cv->pan = pan;
696 41 cv->pad = pad;
697 }
698 41 ▶ if (width > 0 && height > 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (width > 0 && height > 0) { 40 1 41
if (width > 0 && height > 0) { 40 0 40
699 40 ▶ if (!dimensions_allowed(width, height, cv->transparent))
Branch: 1 branch
BranchTrueFalseTotal
if (!dimensions_allowed(width, height, cv->transparent)) 1 39 40
700 1 return 0;
701 39 cv->declared_width = width;
702 39 cv->declared_height = height;
703 }
704 40 return 1;
705 }
706  
707 /* ------------------------------------------------------------------ */
708 /* sixel data */
709  
710 typedef enum {
711 DATA_COMPLETE,
712 DATA_TRUNCATED,
713 DATA_FAILED
714 } data_result;
715  
716 /* Reads parameter bytes, returning the first byte that is not one. A
717 * space ends a number, so digits after it start the next parameter.
718 * Control characters, such as line breaks, are ignored.
719 */
720 static int
721 9348 read_params(input *in, params *p) {
722 9348 int in_number = 0;
723 9348 int number_ended = 0;
724  
725 9348 params_reset(p);
726 48869 for (;;) {
727 58217 int c = input_next(in);
728 58217 ▶ if (is_digit(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_digit(c)) { 37439 20778 58217
729 37439 ▶ if (number_ended)
Branch: 1 branch
BranchTrueFalseTotal
if (number_ended) 1 37438 37439
730 1 params_add(p, ';');
731 37439 params_add(p, c);
732 37439 in_number = 1;
733 37439 number_ended = 0;
734 }
735 20778 ▶ else if (c == ';') {
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ';') { 11420 9358 20778
736 11420 params_add(p, c);
737 11420 in_number = 0;
738 11420 number_ended = 0;
739 }
740 9358 ▶ else if (c == ' ') {
Branch: 1 branch
BranchTrueFalseTotal
else if (c == ' ') { 6 9352 9358
741 6 number_ended = in_number;
742 }
743 9352 ▶ else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) {
Branch: 5 branches
BranchTrueFalseTotal
else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) { 9352 0 9352
else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) { 7 9345 9352
else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) { 4 3 7
else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) { 4 0 4
else if (c == CHAR_EOF || c > 0x20 || c == CHAR_ESC || c == CHAR_CAN || c == CHAR_SUB) { 0 4 4
744 9348 return c;
745 }
746 }
747 }
748  
749 static int
750 33013 is_sixel_char(int c) {
751 33013 ▶ return c >= '?' && c <= '~';
Branch: 2 branches
BranchTrueFalseTotal
return c >= '?' && c <= '~'; 22826 10187 33013
return c >= '?' && c <= '~'; 22825 1 22826
752 }
753  
754 static data_result
755 95 read_sixel_data(input *in, canvas *cv) {
756 params p;
757 95 int c = input_next(in);
758  
759 for (;;) {
760 30701 ▶ if (is_sixel_char(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_sixel_char(c)) { 20513 10188 30701
761 20513 ▶ if (!canvas_paint(cv, c - '?', 1))
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_paint(cv, c - '?', 1)) 1 20512 20513
762 1 return DATA_FAILED;
763 20512 c = input_next(in);
764 20512 continue;
765 }
766  
767 10188 switch (c) {
768 4 case CHAR_EOF:
769 4 ▶ return in->failed ? DATA_FAILED : DATA_TRUNCATED;
Branch: 1 branch
BranchTrueFalseTotal
return in->failed ? DATA_FAILED : DATA_TRUNCATED; 0 4 4
770  
771 3 case CHAR_ST:
772 case CHAR_CAN:
773 case CHAR_SUB:
774 3 return DATA_COMPLETE;
775  
776 82 case CHAR_ESC:
777 /* ESC \ is the string terminator; any other escape
778 * sequence ends the control string as well */
779 82 finish_escape(in);
780 82 return DATA_COMPLETE;
781  
782 2312 case '!':
783 2312 c = read_params(in, &p);
784 2312 ▶ if (is_sixel_char(c)) {
Branch: 1 branch
BranchTrueFalseTotal
if (is_sixel_char(c)) { 2312 0 2312
785 2312 ▶ int repeat = p.value[0] ? p.value[0] : 1;
Branch: 1 branch
BranchTrueFalseTotal
int repeat = p.value[0] ? p.value[0] : 1; 2311 1 2312
786 2312 ▶ if (!canvas_paint(cv, c - '?', repeat))
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_paint(cv, c - '?', repeat)) 4 2308 2312
787 4 return DATA_FAILED;
788 2308 c = input_next(in);
789 }
790 2308 continue;
791  
792 6994 case '#':
793 6994 c = read_params(in, &p);
794 6994 ▶ if (!canvas_select_color(cv, &p))
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_select_color(cv, &p)) 0 6994 6994
795 0 return DATA_FAILED;
796 6994 continue;
797  
798 42 case '"':
799 42 c = read_params(in, &p);
800 42 ▶ if (!canvas_raster_attributes(cv, &p))
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_raster_attributes(cv, &p)) 1 41 42
801 1 return DATA_FAILED;
802 41 continue;
803  
804 672 case '$':
805 672 cv->x = 0;
806 672 break;
807  
808 77 case '-':
809 77 cv->x = 0;
810 77 cv->y = advance(cv->y, 6);
811 77 break;
812  
813 2 default:
814 /* anything else is ignored */
815 2 break;
816 }
817 751 c = input_next(in);
818 }
819 }
820  
821 /* ------------------------------------------------------------------ */
822 /* image construction */
823  
824 static void
825 86 set_tags(i_img *img, const canvas *cv, int incomplete) {
826 86 i_tags_set(&img->tags, "i_format", "sixel", -1);
827 86 i_tags_setn(&img->tags, "sixel_pan", cv->pan);
828 86 i_tags_setn(&img->tags, "sixel_pad", cv->pad);
829 86 ▶ if (incomplete)
Branch: 1 branch
BranchTrueFalseTotal
if (incomplete) 2 84 86
830 2 i_tags_setn(&img->tags, "i_incomplete", 1);
831 86 }
832  
833 /* Unpainted pixels show the background: transparency, numbered one
834 * past the last colour, or the final colour of register 0.
835 */
836 static inline size_t
837 1274566 pixel_color(const canvas *cv, uint16_t value) {
838 1274566 ▶ if (value != UNPAINTED)
Branch: 1 branch
BranchTrueFalseTotal
if (value != UNPAINTED) 1274270 296 1274566
839 1274270 return value;
840 296 ▶ return cv->transparent ? cv->color_count : cv->register_color[0];
Branch: 1 branch
BranchTrueFalseTotal
return cv->transparent ? cv->color_count : cv->register_color[0]; 108 188 296
841 }
842  
843 static void
844 5808 color_value(const canvas *cv, size_t color, i_color *out) {
845 5808 ▶ if (color == cv->color_count) {
Branch: 1 branch
BranchTrueFalseTotal
if (color == cv->color_count) { 6 5802 5808
846 6 out->rgba.r = out->rgba.g = out->rgba.b = out->rgba.a = 0;
847 6 return;
848 }
849 5802 out->rgba.r = cv->colors[color].rgb[0];
850 5802 out->rgba.g = cv->colors[color].rgb[1];
851 5802 out->rgba.b = cv->colors[color].rgb[2];
852 5802 out->rgba.a = 255;
853 }
854  
855 static i_img *
856 84 build_paletted(const canvas *cv, i_img_dim width, i_img_dim height, int channels,
857 const int *palette_index) {
858 i_color colors[256];
859 84 int color_count = 0;
860 i_palidx *line;
861 i_img *img;
862 i_img_dim x, y;
863 size_t color;
864  
865 86186 ▶ for (color = 0; color <= cv->color_count; ++color) {
Branch: 1 branch
BranchTrueFalseTotal
for (color = 0; color <= cv->color_count; ++color) { 86102 84 86186
866 86102 ▶ if (palette_index[color] >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (palette_index[color] >= 0) 2208 83894 86102
867 2208 color_value(cv, color, colors + color_count++);
868 }
869  
870 84 line = malloc((size_t)width * sizeof(*line));
871 84 ▶ if (!line) {
Branch: 1 branch
BranchTrueFalseTotal
if (!line) { 0 84 84
872 0 i_push_error(0, "out of memory");
873 0 return NULL;
874 }
875 84 img = i_img_pal_new(width, height, channels, 256);
876 84 ▶ if (!img) {
Branch: 1 branch
BranchTrueFalseTotal
if (!img) { 0 84 84
877 0 free(line);
878 0 return NULL;
879 }
880 84 ▶ i_addcolors(img, colors, color_count);
Branch: 1 branch
BranchTrueFalseTotal
i_addcolors(img, colors, color_count); 84 0 84
881  
882 944 ▶ for (y = 0; y < height; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < height; ++y) { 860 84 944
883 860 const uint16_t *row = cv->pixels + (size_t)y * cv->allocated_width;
884 634543 ▶ for (x = 0; x < width; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) 633683 860 634543
885 633683 line[x] = (i_palidx)palette_index[pixel_color(cv, row[x])];
886 860 ▶ i_ppal(img, 0, width, y, line);
Branch: 1 branch
BranchTrueFalseTotal
i_ppal(img, 0, width, y, line); 860 0 860
887 }
888  
889 84 free(line);
890 84 return img;
891 }
892  
893 static i_img *
894 2 build_direct(const canvas *cv, i_img_dim width, i_img_dim height, int channels) {
895 unsigned char *line;
896 i_img *img;
897 i_img_dim x, y;
898  
899 2 line = malloc((size_t)width * channels);
900 2 ▶ if (!line) {
Branch: 1 branch
BranchTrueFalseTotal
if (!line) { 0 2 2
901 0 i_push_error(0, "out of memory");
902 0 return NULL;
903 }
904 2 img = i_img_8_new(width, height, channels);
905 2 ▶ if (!img) {
Branch: 1 branch
BranchTrueFalseTotal
if (!img) { 0 2 2
906 0 free(line);
907 0 return NULL;
908 }
909  
910 14 ▶ for (y = 0; y < height; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < height; ++y) { 12 2 14
911 12 const uint16_t *row = cv->pixels + (size_t)y * cv->allocated_width;
912 12 unsigned char *out = line;
913 3612 ▶ for (x = 0; x < width; ++x) {
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) { 3600 12 3612
914 i_color color;
915 3600 color_value(cv, pixel_color(cv, row[x]), &color);
916 3600 memcpy(out, color.channel, channels);
917 3600 out += channels;
918 }
919 12 i_psamp(img, 0, width, y, line, NULL, channels);
920 }
921  
922 2 free(line);
923 2 return img;
924 }
925  
926 /* Builds a paletted image when at most 256 colours are needed, else a
927 * direct colour image.
928 */
929 static i_img *
930 87 canvas_to_image(canvas *cv) {
931 87 i_img_dim width = cv->painted_width > cv->declared_width ? cv->painted_width : cv->declared_width;
932 87 i_img_dim height = cv->painted_height > cv->declared_height ? cv->painted_height : cv->declared_height;
933 87 ▶ int channels = cv->transparent ? 4 : 3;
Branch: 1 branch
BranchTrueFalseTotal
int channels = cv->transparent ? 4 : 3; 8 79 87
934 int *palette_index;
935 87 int color_count = 0;
936 i_img_dim x, y;
937 size_t color;
938 i_img *img;
939  
940 87 ▶ if (width == 0 || height == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (width == 0 || height == 0) { 86 1 87
if (width == 0 || height == 0) { 0 86 86
941 1 i_push_error(0, "SIXEL image contains no pixels");
942 1 return NULL;
943 }
944 86 ▶ if (!dimensions_allowed(width, height, cv->transparent) || !canvas_reserve(cv, width, height))
Branch: 2 branches
BranchTrueFalseTotal
if (!dimensions_allowed(width, height, cv->transparent) || !canvas_reserve(cv, width, height)) 86 0 86
if (!dimensions_allowed(width, height, cv->transparent) || !canvas_reserve(cv, width, height)) 0 86 86
945 0 return NULL;
946  
947 /* one more for transparency */
948 86 palette_index = malloc((cv->color_count + 1) * sizeof(int));
949 86 ▶ if (!palette_index) {
Branch: 1 branch
BranchTrueFalseTotal
if (!palette_index) { 0 86 86
950 0 i_push_error(0, "out of memory");
951 0 return NULL;
952 }
953 88537 ▶ for (color = 0; color <= cv->color_count; ++color)
Branch: 1 branch
BranchTrueFalseTotal
for (color = 0; color <= cv->color_count; ++color) 88451 86 88537
954 88451 palette_index[color] = -1;
955 958 ▶ for (y = 0; y < height; ++y) {
Branch: 1 branch
BranchTrueFalseTotal
for (y = 0; y < height; ++y) { 872 86 958
956 872 const uint16_t *row = cv->pixels + (size_t)y * cv->allocated_width;
957 638155 ▶ for (x = 0; x < width; ++x)
Branch: 1 branch
BranchTrueFalseTotal
for (x = 0; x < width; ++x) 637283 872 638155
958 637283 palette_index[pixel_color(cv, row[x])] = 0;
959 }
960 88537 ▶ for (color = 0; color <= cv->color_count; ++color) {
Branch: 1 branch
BranchTrueFalseTotal
for (color = 0; color <= cv->color_count; ++color) { 88451 86 88537
961 88451 ▶ if (palette_index[color] >= 0)
Branch: 1 branch
BranchTrueFalseTotal
if (palette_index[color] >= 0) 2808 85643 88451
962 2808 palette_index[color] = color_count++;
963 }
964  
965 86 img = color_count <= 256
966 84 ? build_paletted(cv, width, height, channels, palette_index)
967 86 ▶ : build_direct(cv, width, height, channels);
Branch: 1 branch
BranchTrueFalseTotal
: build_direct(cv, width, height, channels); 84 2 86
968 86 free(palette_index);
969 86 return img;
970 }
971  
972 /* ------------------------------------------------------------------ */
973 /* public interface */
974  
975 typedef enum {
976 NEXT_IMAGE,
977 NEXT_END,
978 NEXT_FAILED
979 } next_status;
980  
981 /* Decodes the next sixel image of the input. */
982 static i_img *
983 104 read_next_image(input *in, int allow_incomplete, next_status *status) {
984 params header;
985 canvas cv;
986 data_result result;
987 i_img *img;
988  
989 104 ▶ if (find_sixel(in, &header) == FOUND_NOTHING) {
Branch: 1 branch
BranchTrueFalseTotal
if (find_sixel(in, &header) == FOUND_NOTHING) { 9 95 104
990 9 ▶ *status = in->failed ? NEXT_FAILED : NEXT_END;
Branch: 1 branch
BranchTrueFalseTotal
*status = in->failed ? NEXT_FAILED : NEXT_END; 0 9 9
991 9 return NULL;
992 }
993  
994 95 ▶ if (!canvas_init(&cv, &header)) {
Branch: 1 branch
BranchTrueFalseTotal
if (!canvas_init(&cv, &header)) { 0 95 95
995 0 *status = NEXT_FAILED;
996 0 return NULL;
997 }
998 95 result = read_sixel_data(in, &cv);
999 95 ▶ if (result == DATA_TRUNCATED && !allow_incomplete) {
Branch: 2 branches
BranchTrueFalseTotal
if (result == DATA_TRUNCATED && !allow_incomplete) { 4 91 95
if (result == DATA_TRUNCATED && !allow_incomplete) { 2 2 4
1000 2 i_push_error(0, "premature end of SIXEL data");
1001 2 result = DATA_FAILED;
1002 }
1003 95 ▶ img = result == DATA_FAILED ? NULL : canvas_to_image(&cv);
Branch: 1 branch
BranchTrueFalseTotal
img = result == DATA_FAILED ? NULL : canvas_to_image(&cv); 87 8 95
1004 95 canvas_release(&cv);
1005  
1006 95 ▶ if (!img) {
Branch: 1 branch
BranchTrueFalseTotal
if (!img) { 9 86 95
1007 9 *status = NEXT_FAILED;
1008 9 return NULL;
1009 }
1010 86 set_tags(img, &cv, result == DATA_TRUNCATED);
1011 86 *status = NEXT_IMAGE;
1012 86 return img;
1013 }
1014  
1015 i_img *
1016 85 i_readsixel(io_glue *ig, int page, int allow_incomplete) {
1017 input *in;
1018 85 next_status status = NEXT_END;
1019 85 i_img *img = NULL;
1020 int skipped;
1021  
1022 85 i_clear_error();
1023 85 ▶ if (page < 0) {
Branch: 1 branch
BranchTrueFalseTotal
if (page < 0) { 0 85 85
1024 0 i_push_error(0, "page must be a non-negative integer");
1025 0 return NULL;
1026 }
1027 85 in = malloc(sizeof(*in));
1028 85 ▶ if (!in) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { 0 85 85
1029 0 i_push_error(0, "out of memory");
1030 0 return NULL;
1031 }
1032 85 input_init(in, ig);
1033  
1034 89 ▶ for (skipped = 0; skipped < page; ++skipped) {
Branch: 1 branch
BranchTrueFalseTotal
for (skipped = 0; skipped < page; ++skipped) { 4 85 89
1035 params header;
1036 4 ▶ if (find_sixel(in, &header) == FOUND_NOTHING)
Branch: 1 branch
BranchTrueFalseTotal
if (find_sixel(in, &header) == FOUND_NOTHING) 0 4 4
1037 0 break;
1038 4 skip_control_string(in);
1039 }
1040 85 ▶ if (skipped == page)
Branch: 1 branch
BranchTrueFalseTotal
if (skipped == page) 85 0 85
1041 85 img = read_next_image(in, allow_incomplete, &status);
1042 85 ▶ if (!img && status != NEXT_FAILED && !in->failed) {
Branch: 3 branches
BranchTrueFalseTotal
if (!img && status != NEXT_FAILED && !in->failed) { 11 74 85
if (!img && status != NEXT_FAILED && !in->failed) { 3 8 11
if (!img && status != NEXT_FAILED && !in->failed) { 3 0 3
1043 3 ▶ if (page == 0)
Branch: 1 branch
BranchTrueFalseTotal
if (page == 0) 2 1 3
1044 2 i_push_error(0, "no SIXEL image found");
1045 else
1046 1 i_push_errorf(0, "SIXEL page %d not found", page);
1047 }
1048  
1049 85 free(in);
1050 85 return img;
1051 }
1052  
1053 i_img **
1054 7 i_readsixel_multi(io_glue *ig, int *count, int allow_incomplete) {
1055 input *in;
1056 7 i_img **imgs = NULL;
1057 7 int allocated = 0;
1058 next_status status;
1059  
1060 7 i_clear_error();
1061 7 *count = 0;
1062 7 in = malloc(sizeof(*in));
1063 7 ▶ if (!in) {
Branch: 1 branch
BranchTrueFalseTotal
if (!in) { 0 7 7
1064 0 i_push_error(0, "out of memory");
1065 0 return NULL;
1066 }
1067 7 input_init(in, ig);
1068  
1069 12 for (;;) {
1070 19 i_img *img = read_next_image(in, allow_incomplete, &status);
1071 19 ▶ if (!img)
Branch: 1 branch
BranchTrueFalseTotal
if (!img) 7 12 19
1072 7 break;
1073 12 ▶ if (*count == allocated) {
Branch: 1 branch
BranchTrueFalseTotal
if (*count == allocated) { 6 6 12
1074 6 ▶ allocated = allocated ? allocated * 2 : 4;
Branch: 1 branch
BranchTrueFalseTotal
allocated = allocated ? allocated * 2 : 4; 0 6 6
1075 12 ▶ imgs = imgs ? myrealloc(imgs, allocated * sizeof(*imgs))
Branch: 1 branch
BranchTrueFalseTotal
imgs = imgs ? myrealloc(imgs, allocated * sizeof(*imgs)) 0 6 6
1076 6 : mymalloc(allocated * sizeof(*imgs));
1077 }
1078 12 imgs[(*count)++] = img;
1079 }
1080 7 free(in);
1081  
1082 7 ▶ if (status == NEXT_FAILED || *count == 0) {
Branch: 2 branches
BranchTrueFalseTotal
if (status == NEXT_FAILED || *count == 0) { 6 1 7
if (status == NEXT_FAILED || *count == 0) { 1 5 6
1083 int i;
1084 2 ▶ if (status != NEXT_FAILED)
Branch: 1 branch
BranchTrueFalseTotal
if (status != NEXT_FAILED) 1 1 2
1085 1 i_push_error(0, "no SIXEL image found");
1086 3 ▶ for (i = 0; i < *count; ++i)
Branch: 1 branch
BranchTrueFalseTotal
for (i = 0; i < *count; ++i) 1 2 3
1087 1 i_img_destroy(imgs[i]);
1088 2 myfree(imgs);
1089 2 *count = 0;
1090 2 return NULL;
1091 }
1092 5 return imgs;
1093 }
« SIXEL.xs ↑ Summary sixel_write.c »