• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

saitoha / libsixel / 19001038354

01 Nov 2025 06:40PM UTC coverage: 42.449% (+0.5%) from 41.902%
19001038354

push

github

saitoha
cli: explicitly report "missing argument for ..." when an option argument is absent to clarify previous vague messages

6275 of 22608 branches covered (27.76%)

50 of 79 new or added lines in 1 file covered. (63.29%)

4 existing lines in 1 file now uncovered.

9276 of 21852 relevant lines covered (42.45%)

999406.62 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

43.44
/src/encoder.c
1
/* SPDX-License-Identifier: MIT AND BSD-3-Clause
2
 *
3
 * Copyright (c) 2021-2025 libsixel developers. See `AUTHORS`.
4
 * Copyright (c) 2014-2019 Hayaki Saito
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
7
 * this software and associated documentation files (the "Software"), to deal in
8
 * the Software without restriction, including without limitation the rights to
9
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
10
 * the Software, and to permit persons to whom the Software is furnished to do so,
11
 * subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in all
14
 * copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
18
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
19
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
20
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
21
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
22
 *
23
 * -------------------------------------------------------------------------------
24
 * Portions of this file(sixel_encoder_emit_drcsmmv2_chars) are derived from
25
 * mlterm's drcssixel.c.
26
 *
27
 * Copyright (c) Araki Ken(arakiken@users.sourceforge.net)
28
 *
29
 * Redistribution and use in source and binary forms, with or without
30
 * modification, are permitted provided that the following conditions
31
 * are met:
32
 * 1. Redistributions of source code must retain the above copyright
33
 *    notice, this list of conditions and the following disclaimer.
34
 * 2. Redistributions in binary form must reproduce the above copyright
35
 *    notice, this list of conditions and the following disclaimer in the
36
 *    documentation and/or other materials provided with the distribution.
37
 * 3. The name of any author may not be used to endorse or promote
38
 *    products derived from this software without their specific prior
39
 *    written permission.
40
 *
41
 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
42
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
44
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
45
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
46
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
47
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
49
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
50
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
51
 * SUCH DAMAGE.
52
 *
53
 */
54

55
#include "config.h"
56
#if !defined(_POSIX_C_SOURCE)
57
# define _POSIX_C_SOURCE 200809L
58
#endif
59

60
/* STDC_HEADERS */
61
#include <stdio.h>
62
#include <stdlib.h>
63
#include <stdarg.h>
64

65
# if HAVE_STRING_H
66
#include <string.h>
67
#endif  /* HAVE_STRING_H */
68
#if HAVE_UNISTD_H
69
# include <unistd.h>
70
#elif HAVE_SYS_UNISTD_H
71
# include <sys/unistd.h>
72
#endif  /* HAVE_SYS_UNISTD_H */
73
#if HAVE_SYS_TYPES_H
74
# include <sys/types.h>
75
#endif  /* HAVE_SYS_TYPES_H */
76
#if HAVE_INTTYPES_H
77
# include <inttypes.h>
78
#endif  /* HAVE_INTTYPES_H */
79
#if HAVE_ERRNO_H
80
# include <errno.h>
81
#endif  /* HAVE_ERRNO_H */
82
#if HAVE_SYS_STAT_H
83
# include <sys/stat.h>
84
#endif  /* HAVE_SYS_STAT_H */
85
#if HAVE_SYS_TIME_H
86
# include <sys/time.h>
87
#elif HAVE_TIME_H
88
# include <time.h>
89
#endif  /* HAVE_SYS_TIME_H HAVE_TIME_H */
90
#if HAVE_SYS_IOCTL_H
91
# include <sys/ioctl.h>
92
#endif  /* HAVE_SYS_IOCTL_H */
93
#if HAVE_FCNTL_H
94
# include <fcntl.h>
95
#endif  /* HAVE_FCNTL_H */
96
#if HAVE_ERRNO_H
97
# include <errno.h>
98
#endif  /* HAVE_ERRNO_H */
99
#if HAVE_CTYPE_H
100
# include <ctype.h>
101
#endif  /* HAVE_CTYPE_H */
102
#if HAVE_LIMITS_H
103
# include <limits.h>
104
#endif  /* HAVE_LIMITS_H */
105

106
#include <sixel.h>
107
#include "loader.h"
108
#include "assessment.h"
109
#include "tty.h"
110
#include "encoder.h"
111
#include "output.h"
112
#include "dither.h"
113
#include "frame.h"
114
#include "rgblookup.h"
115
#include "compat_stub.h"
116

117
#if defined(_WIN32)
118

119
# include <windows.h>
120
# if defined(_MSC_VER) || defined(__MINGW32__) || defined(__MINGW64__)
121
#  include <io.h>
122
# endif
123
# if defined(_MSC_VER)
124
#   include <time.h>
125
# endif
126

127
/* for msvc */
128
# ifndef STDIN_FILENO
129
#  define STDIN_FILENO 0
130
# endif
131
# ifndef STDOUT_FILENO
132
#  define STDOUT_FILENO 1
133
# endif
134
# ifndef STDERR_FILENO
135
#  define STDERR_FILENO 2
136
# endif
137
# ifndef S_IRUSR
138
#  define S_IRUSR _S_IREAD
139
# endif
140
# ifndef S_IWUSR
141
#  define S_IWUSR _S_IWRITE
142
# endif
143

144
# if defined(CLOCKS_PER_SEC)
145
#  undef CLOCKS_PER_SEC
146
# endif
147
# define CLOCKS_PER_SEC 1000
148

149
# if !defined(HAVE_NANOSLEEP)
150
# define HAVE_NANOSLEEP_WIN 1
151
static int
152
nanosleep_win(
153
    struct timespec const *req,
154
    struct timespec *rem)
155
{
156
    LONGLONG nanoseconds;
157
    LARGE_INTEGER dueTime;
158
    HANDLE timer;
159

160
    if (req == NULL || req->tv_sec < 0 || req->tv_nsec < 0 ||
161
        req->tv_nsec >= 1000000000L) {
162
        errno = EINVAL;
163
        return (-1);
164
    }
165

166
    /* Convert to 100-nanosecond intervals (Windows FILETIME units) */
167
    nanoseconds = req->tv_sec * 1000000000LL + req->tv_nsec;
168
    dueTime.QuadPart = -(nanoseconds / 100); /* Negative for relative time */
169

170
    timer = CreateWaitableTimer(NULL, TRUE, NULL);
171
    if (timer == NULL) {
172
        errno = EFAULT;  /* Approximate error */
173
        return (-1);
174
    }
175

176
    if (! SetWaitableTimer(timer, &dueTime, 0, NULL, NULL, FALSE)) {
177
        (void) CloseHandle(timer);
178
        errno = EFAULT;
179
        return (-1);
180
    }
181

182
    (void) WaitForSingleObject(timer, INFINITE);
183
    (void) CloseHandle(timer);
184

185
    /* No interruption handling, so rem is unchanged */
186
    if (rem != NULL) {
187
        rem->tv_sec = 0;
188
        rem->tv_nsec = 0;
189
    }
190

191
    return (0);
192
}
193
# endif  /* HAVE_NANOSLEEP */
194

195
# if !defined(HAVE_CLOCK)
196
# define HAVE_CLOCK_WIN 1
197
static sixel_clock_t
198
clock_win(void)
199
{
200
    FILETIME ct, et, kt, ut;
201
    ULARGE_INTEGER u, k;
202

203
    if (! GetProcessTimes(GetCurrentProcess(), &ct, &et, &kt, &ut)) {
204
        return (sixel_clock_t)(-1);
205
    }
206
    u.LowPart = ut.dwLowDateTime; u.HighPart = ut.dwHighDateTime;
207
    k.LowPart = kt.dwLowDateTime; k.HighPart = kt.dwHighDateTime;
208
    /* 100ns -> ms */
209
    return (sixel_clock_t)((u.QuadPart + k.QuadPart) / 10000ULL);
210
}
211
# endif  /* HAVE_CLOCK */
212

213
#endif /* _WIN32 */
214

215

216
/*
217
 * Prefix matcher roadmap:
218
 *
219
 *   +---------+-------------------+
220
 *   | input   | decision           |
221
 *   +---------+-------------------+
222
 *   | "ave"   | average            |
223
 *   | "a"     | ambiguous (auto?)  |
224
 *   +---------+-------------------+
225
 *
226
 * The helper walks the choice table once, collecting prefixes and
227
 * checking whether a unique destination emerges.  Ambiguous prefixes
228
 * bubble up so the caller can craft a friendly diagnostic.
229
 */
230
typedef struct sixel_option_choice {
231
    char const *name;
232
    int value;
233
} sixel_option_choice_t;
234

235
typedef enum sixel_option_choice_result {
236
    SIXEL_OPTION_CHOICE_MATCH = 0,
237
    SIXEL_OPTION_CHOICE_AMBIGUOUS = 1,
238
    SIXEL_OPTION_CHOICE_NONE = 2
239
} sixel_option_choice_result_t;
240

241
static sixel_option_choice_result_t
242
sixel_match_option_choice(
261✔
243
    char const *value,
244
    sixel_option_choice_t const *choices,
245
    size_t choice_count,
246
    int *matched_value,
247
    char *diagnostic,
248
    size_t diagnostic_size)
249
{
250
    size_t index;
251
    size_t value_length;
252
    int candidate_index;
253
    size_t match_count;
254
    int base_value;
255
    int base_value_set;
256
    int ambiguous_values;
257
    size_t diag_length;
258
    size_t copy_length;
259

260
    if (diagnostic != NULL && diagnostic_size > 0u) {
261!
261
        diagnostic[0] = '\0';
261✔
262
    }
87✔
263
    if (value == NULL) {
261!
264
        return SIXEL_OPTION_CHOICE_NONE;
×
265
    }
266

267
    value_length = strlen(value);
261✔
268
    if (value_length == 0u) {
261!
269
        return SIXEL_OPTION_CHOICE_NONE;
×
270
    }
271

272
    index = 0u;
261✔
273
    candidate_index = (-1);
261✔
274
    match_count = 0u;
261✔
275
    base_value = 0;
261✔
276
    base_value_set = 0;
261✔
277
    ambiguous_values = 0;
261✔
278
    diag_length = 0u;
261✔
279

280
    while (index < choice_count) {
1,326✔
281
        if (strncmp(choices[index].name, value, value_length) == 0) {
1,260✔
282
            if (choices[index].name[value_length] == '\0') {
243✔
283
                *matched_value = choices[index].value;
195✔
284
                return SIXEL_OPTION_CHOICE_MATCH;
195✔
285
            }
286
            if (!base_value_set) {
48✔
287
                base_value = choices[index].value;
39✔
288
                base_value_set = 1;
39✔
289
            } else if (choices[index].value != base_value) {
22✔
290
                ambiguous_values = 1;
3✔
291
            }
1✔
292
            if (candidate_index == (-1)) {
48✔
293
                candidate_index = (int)index;
39✔
294
            }
13✔
295
            ++match_count;
48✔
296
            if (diagnostic != NULL && diagnostic_size > 0u) {
48!
297
                if (diag_length > 0u && diag_length + 2u < diagnostic_size) {
48!
298
                    diagnostic[diag_length] = ',';
9✔
299
                    diagnostic[diag_length + 1u] = ' ';
9✔
300
                    diag_length += 2u;
9✔
301
                    diagnostic[diag_length] = '\0';
9✔
302
                }
3✔
303
                copy_length = strlen(choices[index].name);
48✔
304
                if (copy_length > diagnostic_size - diag_length - 1u) {
48!
305
                    copy_length = diagnostic_size - diag_length - 1u;
×
306
                }
307
                memcpy(diagnostic + diag_length,
48✔
308
                       choices[index].name,
32✔
309
                       copy_length);
310
                diag_length += copy_length;
48✔
311
                diagnostic[diag_length] = '\0';
48✔
312
            }
16✔
313
        }
16✔
314
        ++index;
1,065✔
315
    }
316

317
    if (match_count == 0u) {
66✔
318
        return SIXEL_OPTION_CHOICE_NONE;
27✔
319
    }
320
    if (!ambiguous_values) {
39✔
321
        *matched_value = choices[candidate_index].value;
36✔
322
        return SIXEL_OPTION_CHOICE_MATCH;
36✔
323
    }
324

325
    return SIXEL_OPTION_CHOICE_AMBIGUOUS;
3✔
326
}
87✔
327

328
static void
329
sixel_report_ambiguous_prefix(
3✔
330
    char const *option,
331
    char const *value,
332
    char const *candidates,
333
    char *buffer,
334
    size_t buffer_size)
335
{
336
    int written;
337

338
    if (buffer == NULL || buffer_size == 0u) {
3!
339
        return;
×
340
    }
341
    if (candidates != NULL && candidates[0] != '\0') {
3!
342
        written = snprintf(buffer,
3✔
343
                           buffer_size,
344
                           "ambiguous prefix \"%s\" for %s (matches: %s).",
345
                           value,
346
                           option,
347
                           candidates);
348
    } else {
1✔
349
        written = snprintf(buffer,
×
350
                           buffer_size,
351
                           "ambiguous prefix \"%s\" for %s.",
352
                           value,
353
                           option);
354
    }
355
    (void) written;
1✔
356
    sixel_helper_set_additional_message(buffer);
3✔
357
}
1✔
358

359
static sixel_option_choice_t const g_option_choices_builtin_palette[] = {
360
    { "xterm16", SIXEL_BUILTIN_XTERM16 },
361
    { "xterm256", SIXEL_BUILTIN_XTERM256 },
362
    { "vt340mono", SIXEL_BUILTIN_VT340_MONO },
363
    { "vt340color", SIXEL_BUILTIN_VT340_COLOR },
364
    { "gray1", SIXEL_BUILTIN_G1 },
365
    { "gray2", SIXEL_BUILTIN_G2 },
366
    { "gray4", SIXEL_BUILTIN_G4 },
367
    { "gray8", SIXEL_BUILTIN_G8 }
368
};
369

370
static sixel_option_choice_t const g_option_choices_diffusion[] = {
371
    { "auto", SIXEL_DIFFUSE_AUTO },
372
    { "none", SIXEL_DIFFUSE_NONE },
373
    { "fs", SIXEL_DIFFUSE_FS },
374
    { "atkinson", SIXEL_DIFFUSE_ATKINSON },
375
    { "jajuni", SIXEL_DIFFUSE_JAJUNI },
376
    { "stucki", SIXEL_DIFFUSE_STUCKI },
377
    { "burkes", SIXEL_DIFFUSE_BURKES },
378
    { "sierra1", SIXEL_DIFFUSE_SIERRA1 },
379
    { "sierra2", SIXEL_DIFFUSE_SIERRA2 },
380
    { "sierra3", SIXEL_DIFFUSE_SIERRA3 },
381
    { "a_dither", SIXEL_DIFFUSE_A_DITHER },
382
    { "x_dither", SIXEL_DIFFUSE_X_DITHER },
383
    { "lso1", SIXEL_DIFFUSE_LSO1 },
384
    { "lso2", SIXEL_DIFFUSE_LSO2 },
385
    { "lso3", SIXEL_DIFFUSE_LSO3 }
386
};
387

388
static sixel_option_choice_t const g_option_choices_diffusion_scan[] = {
389
    { "auto", SIXEL_SCAN_AUTO },
390
    { "serpentine", SIXEL_SCAN_SERPENTINE },
391
    { "raster", SIXEL_SCAN_RASTER }
392
};
393

394
static sixel_option_choice_t const g_option_choices_diffusion_carry[] = {
395
    { "auto", SIXEL_CARRY_AUTO },
396
    { "direct", SIXEL_CARRY_DISABLE },
397
    { "carry", SIXEL_CARRY_ENABLE }
398
};
399

400
static sixel_option_choice_t const g_option_choices_find_largest[] = {
401
    { "auto", SIXEL_LARGE_AUTO },
402
    { "norm", SIXEL_LARGE_NORM },
403
    { "lum", SIXEL_LARGE_LUM }
404
};
405

406
static sixel_option_choice_t const g_option_choices_select_color[] = {
407
    { "auto", SIXEL_REP_AUTO },
408
    { "center", SIXEL_REP_CENTER_BOX },
409
    { "average", SIXEL_REP_AVERAGE_COLORS },
410
    { "histogram", SIXEL_REP_AVERAGE_PIXELS },
411
    { "histgram", SIXEL_REP_AVERAGE_PIXELS }
412
};
413

414
static sixel_option_choice_t const g_option_choices_resampling[] = {
415
    { "nearest", SIXEL_RES_NEAREST },
416
    { "gaussian", SIXEL_RES_GAUSSIAN },
417
    { "hanning", SIXEL_RES_HANNING },
418
    { "hamming", SIXEL_RES_HAMMING },
419
    { "bilinear", SIXEL_RES_BILINEAR },
420
    { "welsh", SIXEL_RES_WELSH },
421
    { "bicubic", SIXEL_RES_BICUBIC },
422
    { "lanczos2", SIXEL_RES_LANCZOS2 },
423
    { "lanczos3", SIXEL_RES_LANCZOS3 },
424
    { "lanczos4", SIXEL_RES_LANCZOS4 }
425
};
426

427
static sixel_option_choice_t const g_option_choices_quality[] = {
428
    { "auto", SIXEL_QUALITY_AUTO },
429
    { "high", SIXEL_QUALITY_HIGH },
430
    { "low", SIXEL_QUALITY_LOW },
431
    { "full", SIXEL_QUALITY_FULL }
432
};
433

434
static sixel_option_choice_t const g_option_choices_loopmode[] = {
435
    { "auto", SIXEL_LOOP_AUTO },
436
    { "force", SIXEL_LOOP_FORCE },
437
    { "disable", SIXEL_LOOP_DISABLE }
438
};
439

440
static sixel_option_choice_t const g_option_choices_palette_type[] = {
441
    { "auto", SIXEL_PALETTETYPE_AUTO },
442
    { "hls", SIXEL_PALETTETYPE_HLS },
443
    { "rgb", SIXEL_PALETTETYPE_RGB }
444
};
445

446
static sixel_option_choice_t const g_option_choices_encode_policy[] = {
447
    { "auto", SIXEL_ENCODEPOLICY_AUTO },
448
    { "fast", SIXEL_ENCODEPOLICY_FAST },
449
    { "size", SIXEL_ENCODEPOLICY_SIZE }
450
};
451

452
static sixel_option_choice_t const g_option_choices_lut_policy[] = {
453
    { "auto", SIXEL_LUT_POLICY_AUTO },
454
    { "5bit", SIXEL_LUT_POLICY_5BIT },
455
    { "6bit", SIXEL_LUT_POLICY_6BIT },
456
    { "robinhood", SIXEL_LUT_POLICY_ROBINHOOD },
457
    { "hopscotch", SIXEL_LUT_POLICY_HOPSCOTCH }
458
};
459

460
static sixel_option_choice_t const g_option_choices_working_colorspace[] = {
461
    { "gamma", SIXEL_COLORSPACE_GAMMA },
462
    { "linear", SIXEL_COLORSPACE_LINEAR },
463
    { "oklab", SIXEL_COLORSPACE_OKLAB }
464
};
465

466
static sixel_option_choice_t const g_option_choices_output_colorspace[] = {
467
    { "gamma", SIXEL_COLORSPACE_GAMMA },
468
    { "linear", SIXEL_COLORSPACE_LINEAR },
469
    { "smpte-c", SIXEL_COLORSPACE_SMPTEC },
470
    { "smptec", SIXEL_COLORSPACE_SMPTEC }
471
};
472

473

474
static char *
475
arg_strdup(
60✔
476
    char const          /* in */ *s,          /* source buffer */
477
    sixel_allocator_t   /* in */ *allocator)  /* allocator object for
478
                                                 destination buffer */
479
{
480
    char *p;
481
    size_t len;
482

483
    len = strlen(s);
60✔
484

485
    p = (char *)sixel_allocator_malloc(allocator, len + 1);
60✔
486
    if (p) {
60!
487
        (void)sixel_compat_strcpy(p, len + 1, s);
60✔
488
    }
20✔
489
    return p;
60✔
490
}
491

492

493
/* An clone function of XColorSpec() of xlib */
494
static SIXELSTATUS
495
sixel_parse_x_colorspec(
45✔
496
    unsigned char       /* out */ **bgcolor,     /* destination buffer */
497
    char const          /* in */  *s,            /* source buffer */
498
    sixel_allocator_t   /* in */  *allocator)    /* allocator object for
499
                                                    destination buffer */
500
{
501
    SIXELSTATUS status = SIXEL_FALSE;
45✔
502
    char *p;
503
    unsigned char components[3];
504
    int component_index = 0;
45✔
505
    unsigned long v;
506
    char *endptr;
507
    char *buf = NULL;
45✔
508
    struct color const *pcolor;
509

510
    /* from rgb_lookup.h generated by gpref */
511
    pcolor = lookup_rgb(s, strlen(s));
45✔
512
    if (pcolor) {
45✔
513
        *bgcolor = (unsigned char *)sixel_allocator_malloc(allocator, 3);
3✔
514
        if (*bgcolor == NULL) {
3!
515
            sixel_helper_set_additional_message(
×
516
                "sixel_parse_x_colorspec: sixel_allocator_malloc() failed.");
517
            status = SIXEL_BAD_ALLOCATION;
×
518
            goto end;
×
519
        }
520
        (*bgcolor)[0] = pcolor->r;
3✔
521
        (*bgcolor)[1] = pcolor->g;
3✔
522
        (*bgcolor)[2] = pcolor->b;
3✔
523
    } else if (s[0] == 'r' && s[1] == 'g' && s[2] == 'b' && s[3] == ':') {
43!
524
        p = buf = arg_strdup(s + 4, allocator);
6✔
525
        if (buf == NULL) {
6!
526
            sixel_helper_set_additional_message(
×
527
                "sixel_parse_x_colorspec: sixel_allocator_malloc() failed.");
528
            status = SIXEL_BAD_ALLOCATION;
×
529
            goto end;
×
530
        }
531
        while (*p) {
15!
532
            v = 0;
15✔
533
            for (endptr = p; endptr - p <= 12; ++endptr) {
36!
534
                if (*endptr >= '0' && *endptr <= '9') {
36✔
535
                    v = (v << 4) | (unsigned long)(*endptr - '0');
15✔
536
                } else if (*endptr >= 'a' && *endptr <= 'f') {
26!
537
                    v = (v << 4) | (unsigned long)(*endptr - 'a' + 10);
3✔
538
                } else if (*endptr >= 'A' && *endptr <= 'F') {
19!
539
                    v = (v << 4) | (unsigned long)(*endptr - 'A' + 10);
3✔
540
                } else {
1✔
541
                    break;
5✔
542
                }
543
            }
7✔
544
            if (endptr - p == 0) {
15!
545
                break;
×
546
            }
547
            if (endptr - p > 4) {
15!
548
                break;
×
549
            }
550
            v = v << ((4 - (endptr - p)) * 4) >> 8;
15✔
551
            components[component_index++] = (unsigned char)v;
15✔
552
            p = endptr;
15✔
553
            if (component_index == 3) {
15✔
554
                break;
3✔
555
            }
556
            if (*p == '\0') {
12✔
557
                break;
3✔
558
            }
559
            if (*p != '/') {
9!
560
                break;
×
561
            }
562
            ++p;
9✔
563
        }
564
        if (component_index != 3 || *p != '\0' || *p == '/') {
6!
565
            status = SIXEL_BAD_ARGUMENT;
3✔
566
            goto end;
3✔
567
        }
568
        *bgcolor = (unsigned char *)sixel_allocator_malloc(allocator, 3);
3✔
569
        if (*bgcolor == NULL) {
3!
570
            sixel_helper_set_additional_message(
×
571
                "sixel_parse_x_colorspec: sixel_allocator_malloc() failed.");
572
            status = SIXEL_BAD_ALLOCATION;
×
573
            goto end;
×
574
        }
575
        (*bgcolor)[0] = components[0];
3✔
576
        (*bgcolor)[1] = components[1];
3✔
577
        (*bgcolor)[2] = components[2];
3✔
578
    } else if (*s == '#') {
37✔
579
        buf = arg_strdup(s + 1, allocator);
27✔
580
        if (buf == NULL) {
27!
581
            sixel_helper_set_additional_message(
×
582
                "sixel_parse_x_colorspec: sixel_allocator_malloc() failed.");
583
            status = SIXEL_BAD_ALLOCATION;
×
584
            goto end;
×
585
        }
586
        for (p = endptr = buf; endptr - p <= 12; ++endptr) {
192✔
587
            if (*endptr >= '0' && *endptr <= '9') {
189✔
588
                *endptr -= '0';
99✔
589
            } else if (*endptr >= 'a' && *endptr <= 'f') {
123!
590
                *endptr -= 'a' - 10;
57✔
591
            } else if (*endptr >= 'A' && *endptr <= 'F') {
52✔
592
                *endptr -= 'A' - 10;
9✔
593
            } else if (*endptr == '\0') {
27✔
594
                break;
21✔
595
            } else {
596
                status = SIXEL_BAD_ARGUMENT;
3✔
597
                goto end;
3✔
598
            }
599
        }
55✔
600
        if (endptr - p > 12) {
24✔
601
            status = SIXEL_BAD_ARGUMENT;
3✔
602
            goto end;
3✔
603
        }
604
        *bgcolor = (unsigned char *)sixel_allocator_malloc(allocator, 3);
21✔
605
        if (*bgcolor == NULL) {
21!
606
            sixel_helper_set_additional_message(
×
607
                "sixel_parse_x_colorspec: sixel_allocator_malloc() failed.");
608
            status = SIXEL_BAD_ALLOCATION;
×
609
            goto end;
×
610
        }
611
        switch (endptr - p) {
21✔
612
        case 3:
6✔
613
            (*bgcolor)[0] = (unsigned char)(p[0] << 4);
9✔
614
            (*bgcolor)[1] = (unsigned char)(p[1] << 4);
9✔
615
            (*bgcolor)[2] = (unsigned char)(p[2] << 4);
9✔
616
            break;
9✔
617
        case 6:
2✔
618
            (*bgcolor)[0] = (unsigned char)(p[0] << 4 | p[1]);
3✔
619
            (*bgcolor)[1] = (unsigned char)(p[2] << 4 | p[3]);
3✔
620
            (*bgcolor)[2] = (unsigned char)(p[4] << 4 | p[4]);
3✔
621
            break;
3✔
622
        case 9:
2✔
623
            (*bgcolor)[0] = (unsigned char)(p[0] << 4 | p[1]);
3✔
624
            (*bgcolor)[1] = (unsigned char)(p[3] << 4 | p[4]);
3✔
625
            (*bgcolor)[2] = (unsigned char)(p[6] << 4 | p[7]);
3✔
626
            break;
3✔
627
        case 12:
2✔
628
            (*bgcolor)[0] = (unsigned char)(p[0] << 4 | p[1]);
3✔
629
            (*bgcolor)[1] = (unsigned char)(p[4] << 4 | p[5]);
3✔
630
            (*bgcolor)[2] = (unsigned char)(p[8] << 4 | p[9]);
3✔
631
            break;
3✔
632
        default:
2✔
633
            status = SIXEL_BAD_ARGUMENT;
3✔
634
            goto end;
3✔
635
        }
636
    } else {
6✔
637
        status = SIXEL_BAD_ARGUMENT;
9✔
638
        goto end;
9✔
639
    }
640

641
    status = SIXEL_OK;
24✔
642

643
end:
30✔
644
    sixel_allocator_free(allocator, buf);
45✔
645

646
    return status;
45✔
647
}
648

649

650
/* generic writer function for passing to sixel_output_new() */
651
static int
652
sixel_write_callback(char *data, int size, void *priv)
4,361✔
653
{
654
    int result;
655

656
    result = (int)sixel_compat_write(*(int *)priv,
5,822✔
657
                                     data,
1,461✔
658
                                     (size_t)size);
1,461✔
659

660
    return result;
4,361✔
661
}
662

663

664
/* the writer function with hex-encoding for passing to sixel_output_new() */
665
static int
666
sixel_hex_write_callback(
73✔
667
    char    /* in */ *data,
668
    int     /* in */ size,
669
    void    /* in */ *priv)
670
{
671
    char hex[SIXEL_OUTPUT_PACKET_SIZE * 2];
672
    int i;
673
    int j;
674
    int result;
675

676
    for (i = j = 0; i < size; ++i, ++j) {
635,609✔
677
        hex[j] = (data[i] >> 4) & 0xf;
635,536✔
678
        hex[j] += (hex[j] < 10 ? '0': ('a' - 10));
635,536!
679
        hex[++j] = data[i] & 0xf;
635,536✔
680
        hex[j] += (hex[j] < 10 ? '0': ('a' - 10));
635,536✔
681
    }
168,020✔
682

683
    result = (int)sixel_compat_write(*(int *)priv,
146✔
684
                                     hex,
25✔
685
                                     (size_t)(size * 2));
73✔
686

687
    return result;
73✔
688
}
689

690
typedef struct sixel_encoder_output_probe {
691
    sixel_encoder_t *encoder;
692
    sixel_write_function base_write;
693
    void *base_priv;
694
} sixel_encoder_output_probe_t;
695

696
static int
697
sixel_write_with_probe(char *data, int size, void *priv)
×
698
{
699
    sixel_encoder_output_probe_t *probe;
700
    int written;
701
    double started_at;
702
    double finished_at;
703
    double duration;
704

705
    probe = (sixel_encoder_output_probe_t *)priv;
×
706
    if (probe == NULL || probe->base_write == NULL) {
×
707
        return 0;
×
708
    }
709
    started_at = 0.0;
×
710
    finished_at = 0.0;
×
711
    duration = 0.0;
×
712
    if (probe->encoder != NULL &&
×
713
            probe->encoder->assessment_observer != NULL) {
×
714
        started_at = sixel_assessment_timer_now();
×
715
    }
716
    written = probe->base_write(data, size, probe->base_priv);
×
717
    if (probe->encoder != NULL &&
×
718
            probe->encoder->assessment_observer != NULL) {
×
719
        finished_at = sixel_assessment_timer_now();
×
720
        duration = finished_at - started_at;
×
721
        if (duration < 0.0) {
×
722
            duration = 0.0;
×
723
        }
724
    }
725
    if (written > 0 && probe->encoder != NULL &&
×
726
            probe->encoder->assessment_observer != NULL) {
×
727
        sixel_assessment_record_output_write(
×
728
            probe->encoder->assessment_observer,
×
729
            (size_t)written,
730
            duration);
731
    }
732
    return written;
×
733
}
734

735
/*
736
 * Reuse the fn_write probe for raw escape writes so that every
737
 * assessment bucket receives the same accounting.
738
 *
739
 *     encoder        probe wrapper       write(2)
740
 *     +------+    +----------------+    +---------+
741
 *     | data | -> | sixel_write_*  | -> | target  |
742
 *     +------+    +----------------+    +---------+
743
 */
744
static int
745
sixel_encoder_probe_fd_write(sixel_encoder_t *encoder,
×
746
                             char *data,
747
                             int size,
748
                             int fd)
749
{
750
    sixel_encoder_output_probe_t probe;
751
    int written;
752

753
    probe.encoder = encoder;
×
754
    probe.base_write = sixel_write_callback;
×
755
    probe.base_priv = &fd;
×
756
    written = sixel_write_with_probe(data, size, &probe);
×
757

758
    return written;
×
759
}
760

761
static SIXELSTATUS
762
sixel_encoder_ensure_cell_size(sixel_encoder_t *encoder)
×
763
{
764
#if defined(TIOCGWINSZ)
765
    struct winsize ws;
766
    int result;
767
    int fd = 0;
×
768

769
    if (encoder->cell_width > 0 && encoder->cell_height > 0) {
×
770
        return SIXEL_OK;
×
771
    }
772

773
    fd = sixel_compat_open("/dev/tty", O_RDONLY);
×
774
    if (fd >= 0) {
×
775
        result = ioctl(fd, TIOCGWINSZ, &ws);
×
776
        (void)sixel_compat_close(fd);
×
777
    } else {
778
        sixel_helper_set_additional_message(
×
779
            "failed to open /dev/tty");
780
        return (SIXEL_LIBC_ERROR | (errno & 0xff));
×
781
    }
782
    if (result != 0) {
×
783
        sixel_helper_set_additional_message(
×
784
            "failed to query terminal geometry with ioctl().");
785
        return (SIXEL_LIBC_ERROR | (errno & 0xff));
×
786
    }
787

788
    if (ws.ws_col <= 0 || ws.ws_row <= 0 ||
×
789
        ws.ws_xpixel <= ws.ws_col || ws.ws_ypixel <= ws.ws_row) {
×
790
        sixel_helper_set_additional_message(
×
791
            "terminal does not report pixel cell size for drcs option.");
792
        return SIXEL_BAD_ARGUMENT;
×
793
    }
794

795
    encoder->cell_width = ws.ws_xpixel / ws.ws_col;
×
796
    encoder->cell_height = ws.ws_ypixel / ws.ws_row;
×
797
    if (encoder->cell_width <= 0 || encoder->cell_height <= 0) {
×
798
        sixel_helper_set_additional_message(
×
799
            "terminal cell size reported zero via ioctl().");
800
        return SIXEL_BAD_ARGUMENT;
×
801
    }
802

803
    return SIXEL_OK;
×
804
#else
805
    (void) encoder;
806
    sixel_helper_set_additional_message(
807
        "drcs option is not supported on this platform.");
808
    return SIXEL_NOT_IMPLEMENTED;
809
#endif
810
}
811

812

813
/* returns monochrome dithering context object */
814
static SIXELSTATUS
815
sixel_prepare_monochrome_palette(
12✔
816
    sixel_dither_t  /* out */ **dither,
817
     int            /* in */  finvert)
818
{
819
    SIXELSTATUS status = SIXEL_FALSE;
12✔
820

821
    if (finvert) {
12✔
822
        *dither = sixel_dither_get(SIXEL_BUILTIN_MONO_LIGHT);
3✔
823
    } else {
1✔
824
        *dither = sixel_dither_get(SIXEL_BUILTIN_MONO_DARK);
9✔
825
    }
826
    if (*dither == NULL) {
12!
827
        sixel_helper_set_additional_message(
×
828
            "sixel_prepare_monochrome_palette: sixel_dither_get() failed.");
829
        status = SIXEL_RUNTIME_ERROR;
×
830
        goto end;
×
831
    }
832

833
    status = SIXEL_OK;
12✔
834

835
end:
8✔
836
    return status;
12✔
837
}
838

839

840
/* returns dithering context object with specified builtin palette */
841
typedef struct palette_conversion {
842
    unsigned char *original;
843
    unsigned char *copy;
844
    size_t size;
845
    int convert_inplace;
846
    int converted;
847
    int frame_colorspace;
848
} palette_conversion_t;
849

850
static SIXELSTATUS
851
sixel_encoder_convert_palette(sixel_encoder_t *encoder,
516✔
852
                              sixel_output_t *output,
853
                              sixel_dither_t *dither,
854
                              int frame_colorspace,
855
                              int pixelformat,
856
                              palette_conversion_t *ctx)
857
{
858
    SIXELSTATUS status = SIXEL_OK;
516✔
859
    unsigned char *palette;
860
    int palette_colors;
861

862
    ctx->original = NULL;
516✔
863
    ctx->copy = NULL;
516✔
864
    ctx->size = 0;
516✔
865
    ctx->convert_inplace = 0;
516✔
866
    ctx->converted = 0;
516✔
867
    ctx->frame_colorspace = frame_colorspace;
516✔
868

869
    palette = sixel_dither_get_palette(dither);
516✔
870
    palette_colors = sixel_dither_get_num_of_palette_colors(dither);
516✔
871
    ctx->original = palette;
516✔
872

873
    if (palette == NULL || palette_colors <= 0 ||
516!
874
            frame_colorspace == output->colorspace) {
516!
875
        return SIXEL_OK;
516✔
876
    }
877

878
    ctx->size = (size_t)palette_colors * 3;
×
879

880
    output->pixelformat = SIXEL_PIXELFORMAT_RGB888;
×
881
    output->source_colorspace = frame_colorspace;
×
882

883
    if (palette != (unsigned char *)(dither + 1)) {
×
884
        ctx->copy = (unsigned char *)sixel_allocator_malloc(
×
885
            encoder->allocator, ctx->size);
886
        if (ctx->copy == NULL) {
×
887
            sixel_helper_set_additional_message(
×
888
                "sixel_encoder_convert_palette: "
889
                "sixel_allocator_malloc() failed.");
890
            status = SIXEL_BAD_ALLOCATION;
×
891
            goto end;
×
892
        }
893
        memcpy(ctx->copy, palette, ctx->size);
×
894
        dither->palette = ctx->copy;
×
895
    } else {
896
        ctx->convert_inplace = 1;
×
897
    }
898

899
    status = sixel_output_convert_colorspace(output,
×
900
                                             dither->palette,
901
                                             ctx->size);
902
    if (SIXEL_FAILED(status)) {
×
903
        goto end;
×
904
    }
905
    ctx->converted = 1;
×
906

907
end:
908
    output->pixelformat = pixelformat;
×
909
    output->source_colorspace = frame_colorspace;
×
910

911
    return status;
×
912
}
178✔
913

914
static void
915
sixel_encoder_restore_palette(sixel_encoder_t *encoder,
516✔
916
                              sixel_dither_t *dither,
917
                              palette_conversion_t *ctx)
918
{
919
    if (ctx->copy) {
516!
920
        dither->palette = ctx->original;
×
921
        sixel_allocator_free(encoder->allocator, ctx->copy);
×
922
        ctx->copy = NULL;
×
923
    } else if (ctx->convert_inplace && ctx->converted &&
516!
924
               ctx->original && ctx->size > 0) {
×
925
        (void)sixel_helper_convert_colorspace(ctx->original,
×
926
                                              ctx->size,
927
                                              SIXEL_PIXELFORMAT_RGB888,
928
                                              SIXEL_COLORSPACE_GAMMA,
929
                                              ctx->frame_colorspace);
930
    }
931
}
516✔
932

933
static SIXELSTATUS
934
sixel_encoder_capture_quantized(sixel_encoder_t *encoder,
×
935
                                sixel_dither_t *dither,
936
                                unsigned char const *pixels,
937
                                size_t size,
938
                                int width,
939
                                int height,
940
                                int pixelformat,
941
                                int colorspace)
942
{
943
    SIXELSTATUS status;
944
    unsigned char *palette;
945
    int ncolors;
946
    size_t palette_bytes;
947
    unsigned char *new_pixels;
948
    unsigned char *new_palette;
949
    size_t capture_bytes;
950
    unsigned char const *capture_source;
951
    sixel_index_t *paletted_pixels;
952
    size_t quantized_pixels;
953
    sixel_allocator_t *dither_allocator;
954
    int saved_pixelformat;
955
    int restore_pixelformat;
956

957
    /*
958
     * Preserve the quantized frame for assessment observers.
959
     *
960
     *     +-----------------+     +---------------------+
961
     *     | quantized bytes | --> | encoder->capture_*  |
962
     *     +-----------------+     +---------------------+
963
     */
964

965
    status = SIXEL_OK;
×
966
    palette = NULL;
×
967
    ncolors = 0;
×
968
    palette_bytes = 0;
×
969
    new_pixels = NULL;
×
970
    new_palette = NULL;
×
971
    capture_bytes = size;
×
972
    capture_source = pixels;
×
973
    paletted_pixels = NULL;
×
974
    quantized_pixels = 0;
×
975
    dither_allocator = NULL;
×
976

977
    if (encoder == NULL || pixels == NULL ||
×
978
            (dither == NULL && size == 0)) {
×
979
        sixel_helper_set_additional_message(
×
980
            "sixel_encoder_capture_quantized: invalid capture request.");
981
        return SIXEL_BAD_ARGUMENT;
×
982
    }
983

984
    if (!encoder->capture_quantized) {
×
985
        return SIXEL_OK;
×
986
    }
987

988
    saved_pixelformat = SIXEL_PIXELFORMAT_RGB888;
×
989
    restore_pixelformat = 0;
×
990
    if (dither != NULL) {
×
991
        dither_allocator = dither->allocator;
×
992
        saved_pixelformat = dither->pixelformat;
×
993
        restore_pixelformat = 1;
×
994
        if (width <= 0 || height <= 0) {
×
995
            sixel_helper_set_additional_message(
×
996
                "sixel_encoder_capture_quantized: invalid dimensions.");
997
            status = SIXEL_BAD_ARGUMENT;
×
998
            goto cleanup;
×
999
        }
1000
        quantized_pixels = (size_t)width * (size_t)height;
×
1001
        if (height != 0 &&
×
1002
                quantized_pixels / (size_t)height != (size_t)width) {
×
1003
            sixel_helper_set_additional_message(
×
1004
                "sixel_encoder_capture_quantized: image too large.");
1005
            status = SIXEL_RUNTIME_ERROR;
×
1006
            goto cleanup;
×
1007
        }
1008
        paletted_pixels = sixel_dither_apply_palette(
×
1009
            dither, (unsigned char *)pixels, width, height);
1010
        if (paletted_pixels == NULL) {
×
1011
            sixel_helper_set_additional_message(
×
1012
                "sixel_encoder_capture_quantized: palette conversion failed.");
1013
            status = SIXEL_RUNTIME_ERROR;
×
1014
            goto cleanup;
×
1015
        }
1016
        capture_source = (unsigned char const *)paletted_pixels;
×
1017
        capture_bytes = quantized_pixels;
×
1018
    }
1019

1020
    if (capture_bytes > 0) {
×
1021
        if (encoder->capture_pixels == NULL ||
×
1022
                encoder->capture_pixels_size < capture_bytes) {
×
1023
            new_pixels = (unsigned char *)sixel_allocator_malloc(
×
1024
                encoder->allocator, capture_bytes);
1025
            if (new_pixels == NULL) {
×
1026
                sixel_helper_set_additional_message(
×
1027
                    "sixel_encoder_capture_quantized: "
1028
                    "sixel_allocator_malloc() failed.");
1029
                status = SIXEL_BAD_ALLOCATION;
×
1030
                goto cleanup;
×
1031
            }
1032
            sixel_allocator_free(encoder->allocator, encoder->capture_pixels);
×
1033
            encoder->capture_pixels = new_pixels;
×
1034
            encoder->capture_pixels_size = capture_bytes;
×
1035
        }
1036
        memcpy(encoder->capture_pixels, capture_source, capture_bytes);
×
1037
    }
1038
    encoder->capture_pixel_bytes = capture_bytes;
×
1039

1040
    palette = NULL;
×
1041
    ncolors = 0;
×
1042
    palette_bytes = 0;
×
1043
    if (dither != NULL) {
×
1044
        palette = sixel_dither_get_palette(dither);
×
1045
        ncolors = sixel_dither_get_num_of_palette_colors(dither);
×
1046
    }
1047
    if (palette != NULL && ncolors > 0) {
×
1048
        palette_bytes = (size_t)ncolors * 3;
×
1049
        if (encoder->capture_palette == NULL ||
×
1050
                encoder->capture_palette_size < palette_bytes) {
×
1051
            new_palette = (unsigned char *)sixel_allocator_malloc(
×
1052
                encoder->allocator, palette_bytes);
1053
            if (new_palette == NULL) {
×
1054
                sixel_helper_set_additional_message(
×
1055
                    "sixel_encoder_capture_quantized: "
1056
                    "sixel_allocator_malloc() failed.");
1057
                status = SIXEL_BAD_ALLOCATION;
×
1058
                goto cleanup;
×
1059
            }
1060
            sixel_allocator_free(encoder->allocator,
×
1061
                                 encoder->capture_palette);
×
1062
            encoder->capture_palette = new_palette;
×
1063
            encoder->capture_palette_size = palette_bytes;
×
1064
        }
1065
        memcpy(encoder->capture_palette, palette, palette_bytes);
×
1066
    }
1067

1068
    encoder->capture_width = width;
×
1069
    encoder->capture_height = height;
×
1070
    if (dither != NULL) {
×
1071
        encoder->capture_pixelformat = SIXEL_PIXELFORMAT_PAL8;
×
1072
    } else {
1073
        encoder->capture_pixelformat = pixelformat;
×
1074
    }
1075
    encoder->capture_colorspace = colorspace;
×
1076
    encoder->capture_palette_size = palette_bytes;
×
1077
    encoder->capture_ncolors = ncolors;
×
1078
    encoder->capture_valid = 1;
×
1079

1080
cleanup:
1081
    if (restore_pixelformat && dither != NULL) {
×
1082
        /*
1083
         * Undo the normalization performed by sixel_dither_apply_palette().
1084
         *
1085
         *     RGBA8888 --capture--> RGB888 (temporary)
1086
         *          \______________________________/
1087
         *                          |
1088
         *                 restore original state for
1089
         *                 the real encoder execution.
1090
         */
1091
        sixel_dither_set_pixelformat(dither, saved_pixelformat);
×
1092
    }
1093
    if (paletted_pixels != NULL && dither_allocator != NULL) {
×
1094
        sixel_allocator_free(dither_allocator, paletted_pixels);
×
1095
    }
1096

1097
    return status;
×
1098
}
1099

1100
static SIXELSTATUS
1101
sixel_prepare_builtin_palette(
27✔
1102
    sixel_dither_t /* out */ **dither,
1103
    int            /* in */  builtin_palette)
1104
{
1105
    SIXELSTATUS status = SIXEL_FALSE;
27✔
1106

1107
    *dither = sixel_dither_get(builtin_palette);
27✔
1108
    if (*dither == NULL) {
27!
1109
        sixel_helper_set_additional_message(
×
1110
            "sixel_prepare_builtin_palette: sixel_dither_get() failed.");
1111
        status = SIXEL_RUNTIME_ERROR;
×
1112
        goto end;
×
1113
    }
1114

1115
    status = SIXEL_OK;
27✔
1116

1117
end:
18✔
1118
    return status;
27✔
1119
}
1120

1121
static int
1122
sixel_encoder_thumbnail_hint(sixel_encoder_t *encoder)
450✔
1123
{
1124
    int width_hint;
1125
    int height_hint;
1126
    long base;
1127
    long size;
1128

1129
    width_hint = 0;
450✔
1130
    height_hint = 0;
450✔
1131
    base = 0;
450✔
1132
    size = 0;
450✔
1133

1134
    if (encoder == NULL) {
450!
1135
        return 0;
×
1136
    }
1137

1138
    width_hint = encoder->pixelwidth;
450✔
1139
    height_hint = encoder->pixelheight;
450✔
1140

1141
    /* Request extra resolution for downscaling to preserve detail. */
1142
    if (width_hint > 0 && height_hint > 0) {
450✔
1143
        /* Follow the CLI rule: double the larger axis before doubling
1144
         * again for the final request size. */
1145
        if (width_hint >= height_hint) {
9!
1146
            base = (long)width_hint;
9✔
1147
        } else {
3✔
1148
            base = (long)height_hint;
×
1149
        }
1150
        base *= 2L;
9✔
1151
    } else if (width_hint > 0) {
444✔
1152
        base = (long)width_hint;
48✔
1153
    } else if (height_hint > 0) {
409✔
1154
        base = (long)height_hint;
36✔
1155
    } else {
12✔
1156
        return 0;
357✔
1157
    }
1158

1159
    size = base * 2L;
93✔
1160
    if (size > (long)INT_MAX) {
93!
1161
        size = (long)INT_MAX;
×
1162
    }
1163
    if (size < 1L) {
93!
1164
        size = 1L;
×
1165
    }
1166

1167
    return (int)size;
93✔
1168
}
150✔
1169

1170

1171
typedef struct sixel_callback_context_for_mapfile {
1172
    int reqcolors;
1173
    sixel_dither_t *dither;
1174
    sixel_allocator_t *allocator;
1175
    int working_colorspace;
1176
    int lut_policy;
1177
} sixel_callback_context_for_mapfile_t;
1178

1179

1180
/* callback function for sixel_helper_load_image_file() */
1181
static SIXELSTATUS
1182
load_image_callback_for_palette(
21✔
1183
    sixel_frame_t   /* in */    *frame, /* frame object from image loader */
1184
    void            /* in */    *data)  /* private data */
1185
{
1186
    SIXELSTATUS status = SIXEL_FALSE;
21✔
1187
    sixel_callback_context_for_mapfile_t *callback_context;
1188

1189
    /* get callback context object from the private data */
1190
    callback_context = (sixel_callback_context_for_mapfile_t *)data;
21✔
1191

1192
    status = sixel_frame_ensure_colorspace(frame,
28✔
1193
                                           callback_context->working_colorspace);
7✔
1194
    if (SIXEL_FAILED(status)) {
21!
1195
        goto end;
×
1196
    }
1197

1198
    switch (sixel_frame_get_pixelformat(frame)) {
21!
1199
    case SIXEL_PIXELFORMAT_PAL1:
2✔
1200
    case SIXEL_PIXELFORMAT_PAL2:
1201
    case SIXEL_PIXELFORMAT_PAL4:
1202
    case SIXEL_PIXELFORMAT_PAL8:
1203
        if (sixel_frame_get_palette(frame) == NULL) {
3!
1204
            status = SIXEL_LOGIC_ERROR;
×
1205
            goto end;
×
1206
        }
1207
        /* create new dither object */
1208
        status = sixel_dither_new(
3✔
1209
            &callback_context->dither,
1✔
1210
            sixel_frame_get_ncolors(frame),
1✔
1211
            callback_context->allocator);
1✔
1212
        if (SIXEL_FAILED(status)) {
3!
1213
            goto end;
×
1214
        }
1215

1216
        sixel_dither_set_lut_policy(callback_context->dither,
4✔
1217
                                    callback_context->lut_policy);
1✔
1218

1219
        /* use palette which is extracted from the image */
1220
        sixel_dither_set_palette(callback_context->dither,
4✔
1221
                                 sixel_frame_get_palette(frame));
1✔
1222
        /* success */
1223
        status = SIXEL_OK;
3✔
1224
        break;
3✔
1225
    case SIXEL_PIXELFORMAT_G1:
1226
        /* use 1bpp grayscale builtin palette */
1227
        callback_context->dither = sixel_dither_get(SIXEL_BUILTIN_G1);
×
1228
        /* success */
1229
        status = SIXEL_OK;
×
1230
        break;
×
1231
    case SIXEL_PIXELFORMAT_G2:
1232
        /* use 2bpp grayscale builtin palette */
1233
        callback_context->dither = sixel_dither_get(SIXEL_BUILTIN_G1);
×
1234
        callback_context->dither = sixel_dither_get(SIXEL_BUILTIN_G2);
×
1235
        /* success */
1236
        status = SIXEL_OK;
×
1237
        break;
×
1238
    case SIXEL_PIXELFORMAT_G4:
1239
        /* use 4bpp grayscale builtin palette */
1240
        callback_context->dither = sixel_dither_get(SIXEL_BUILTIN_G4);
×
1241
        /* success */
1242
        status = SIXEL_OK;
×
1243
        break;
×
1244
    case SIXEL_PIXELFORMAT_G8:
1245
        /* use 8bpp grayscale builtin palette */
1246
        callback_context->dither = sixel_dither_get(SIXEL_BUILTIN_G8);
×
1247
        /* success */
1248
        status = SIXEL_OK;
×
1249
        break;
×
1250
    default:
12✔
1251
        /* create new dither object */
1252
        status = sixel_dither_new(
18✔
1253
            &callback_context->dither,
6✔
1254
            callback_context->reqcolors,
6✔
1255
            callback_context->allocator);
6✔
1256
        if (SIXEL_FAILED(status)) {
18!
1257
            goto end;
×
1258
        }
1259

1260
        sixel_dither_set_lut_policy(callback_context->dither,
24✔
1261
                                    callback_context->lut_policy);
6✔
1262

1263
        /* create adaptive palette from given frame object */
1264
        status = sixel_dither_initialize(callback_context->dither,
24✔
1265
                                         sixel_frame_get_pixels(frame),
6✔
1266
                                         sixel_frame_get_width(frame),
6✔
1267
                                         sixel_frame_get_height(frame),
6✔
1268
                                         sixel_frame_get_pixelformat(frame),
6✔
1269
                                         SIXEL_LARGE_NORM,
1270
                                         SIXEL_REP_CENTER_BOX,
1271
                                         SIXEL_QUALITY_HIGH);
1272
        if (SIXEL_FAILED(status)) {
18!
1273
            sixel_dither_unref(callback_context->dither);
×
1274
            goto end;
×
1275
        }
1276

1277
        /* success */
1278
        status = SIXEL_OK;
18✔
1279

1280
        break;
18✔
1281
    }
7✔
1282

1283
end:
14✔
1284
    return status;
21✔
1285
}
1286

1287

1288
static SIXELSTATUS
1289
sixel_encoder_emit_palette_output(sixel_encoder_t *encoder);
1290

1291

1292
static int
1293
sixel_path_has_extension(char const *path, char const *extension)
78✔
1294
{
1295
    size_t path_len;
1296
    size_t ext_len;
1297
    size_t index;
1298

1299
    path_len = 0u;
78✔
1300
    ext_len = 0u;
78✔
1301
    index = 0u;
78✔
1302

1303
    if (path == NULL || extension == NULL) {
78!
1304
        return 0;
×
1305
    }
1306

1307
    path_len = strlen(path);
78✔
1308
    ext_len = strlen(extension);
78✔
1309
    if (ext_len == 0u || path_len < ext_len) {
78!
1310
        return 0;
×
1311
    }
1312

1313
    for (index = 0u; index < ext_len; ++index) {
159!
1314
        unsigned char path_ch;
1315
        unsigned char ext_ch;
1316

1317
        path_ch = (unsigned char)path[path_len - ext_len + index];
159✔
1318
        ext_ch = (unsigned char)extension[index];
159✔
1319
        if (tolower(path_ch) != tolower(ext_ch)) {
159✔
1320
            return 0;
78✔
1321
        }
1322
    }
27✔
1323

1324
    return 1;
×
1325
}
30✔
1326

1327
typedef enum sixel_palette_format {
1328
    SIXEL_PALETTE_FORMAT_NONE = 0,
1329
    SIXEL_PALETTE_FORMAT_ACT,
1330
    SIXEL_PALETTE_FORMAT_PAL_JASC,
1331
    SIXEL_PALETTE_FORMAT_PAL_RIFF,
1332
    SIXEL_PALETTE_FORMAT_PAL_AUTO,
1333
    SIXEL_PALETTE_FORMAT_GPL
1334
} sixel_palette_format_t;
1335

1336
/*
1337
 * Palette specification parser
1338
 *
1339
 *   TYPE:PATH  -> explicit format prefix
1340
 *   PATH       -> rely on extension or heuristics
1341
 *
1342
 * The ASCII diagram below shows how the prefix is peeled:
1343
 *
1344
 *   [type] : [path]
1345
 *    ^-- left part selects decoder/encoder when present.
1346
 */
1347
static char const *
1348
sixel_palette_strip_prefix(char const *spec,
26✔
1349
                           sixel_palette_format_t *format_hint)
1350
{
1351
    char const *colon;
1352
    size_t type_len;
1353
    size_t index;
1354
    char lowered[16];
1355

1356
    colon = NULL;
26✔
1357
    type_len = 0u;
26✔
1358
    index = 0u;
26✔
1359

1360
    if (format_hint != NULL) {
26!
1361
        *format_hint = SIXEL_PALETTE_FORMAT_NONE;
26✔
1362
    }
10✔
1363
    if (spec == NULL) {
26!
1364
        return NULL;
×
1365
    }
1366

1367
    colon = strchr(spec, ':');
26✔
1368
    if (colon == NULL) {
26!
1369
        return spec;
26✔
1370
    }
1371

1372
    type_len = (size_t)(colon - spec);
×
1373
    if (type_len == 0u || type_len >= sizeof(lowered)) {
×
1374
        return spec;
×
1375
    }
1376

1377
    for (index = 0u; index < type_len; ++index) {
×
1378
        lowered[index] = (char)tolower((unsigned char)spec[index]);
×
1379
    }
1380
    lowered[type_len] = '\0';
×
1381

1382
    if (strcmp(lowered, "act") == 0) {
×
1383
        if (format_hint != NULL) {
×
1384
            *format_hint = SIXEL_PALETTE_FORMAT_ACT;
×
1385
        }
1386
        return colon + 1;
×
1387
    }
1388
    if (strcmp(lowered, "pal") == 0) {
×
1389
        if (format_hint != NULL) {
×
1390
            *format_hint = SIXEL_PALETTE_FORMAT_PAL_AUTO;
×
1391
        }
1392
        return colon + 1;
×
1393
    }
1394
    if (strcmp(lowered, "pal-jasc") == 0) {
×
1395
        if (format_hint != NULL) {
×
1396
            *format_hint = SIXEL_PALETTE_FORMAT_PAL_JASC;
×
1397
        }
1398
        return colon + 1;
×
1399
    }
1400
    if (strcmp(lowered, "pal-riff") == 0) {
×
1401
        if (format_hint != NULL) {
×
1402
            *format_hint = SIXEL_PALETTE_FORMAT_PAL_RIFF;
×
1403
        }
1404
        return colon + 1;
×
1405
    }
1406
    if (strcmp(lowered, "gpl") == 0) {
×
1407
        if (format_hint != NULL) {
×
1408
            *format_hint = SIXEL_PALETTE_FORMAT_GPL;
×
1409
        }
1410
        return colon + 1;
×
1411
    }
1412

1413
    return spec;
×
1414
}
10✔
1415

1416
static sixel_palette_format_t
1417
sixel_palette_format_from_extension(char const *path)
26✔
1418
{
1419
    if (path == NULL) {
26!
1420
        return SIXEL_PALETTE_FORMAT_NONE;
×
1421
    }
1422

1423
    if (sixel_path_has_extension(path, ".act")) {
26!
1424
        return SIXEL_PALETTE_FORMAT_ACT;
×
1425
    }
1426
    if (sixel_path_has_extension(path, ".pal")) {
26!
1427
        return SIXEL_PALETTE_FORMAT_PAL_AUTO;
×
1428
    }
1429
    if (sixel_path_has_extension(path, ".gpl")) {
26!
1430
        return SIXEL_PALETTE_FORMAT_GPL;
×
1431
    }
1432

1433
    return SIXEL_PALETTE_FORMAT_NONE;
26✔
1434
}
10✔
1435

1436
static int
1437
sixel_path_has_any_extension(char const *path)
26✔
1438
{
1439
    char const *slash_forward;
1440
#if defined(_WIN32)
1441
    char const *slash_backward;
1442
#endif
1443
    char const *start;
1444
    char const *dot;
1445

1446
    slash_forward = NULL;
26✔
1447
#if defined(_WIN32)
1448
    slash_backward = NULL;
1449
#endif
1450
    start = path;
26✔
1451
    dot = NULL;
26✔
1452

1453
    if (path == NULL) {
26!
1454
        return 0;
×
1455
    }
1456

1457
    slash_forward = strrchr(path, '/');
26✔
1458
#if defined(_WIN32)
1459
    slash_backward = strrchr(path, '\\');
1460
    if (slash_backward != NULL &&
1461
            (slash_forward == NULL || slash_backward > slash_forward)) {
1462
        slash_forward = slash_backward;
1463
    }
1464
#endif
1465
    if (slash_forward == NULL) {
26!
1466
        start = path;
×
1467
    } else {
1468
        start = slash_forward + 1;
26✔
1469
    }
1470

1471
    dot = strrchr(start, '.');
26✔
1472
    if (dot == NULL) {
26✔
1473
        return 0;
5✔
1474
    }
1475

1476
    if (dot[1] == '\0') {
21!
1477
        return 0;
×
1478
    }
1479

1480
    return 1;
21✔
1481
}
10✔
1482

1483
static int
1484
sixel_palette_has_utf8_bom(unsigned char const *data, size_t size)
×
1485
{
1486
    if (data == NULL || size < 3u) {
×
1487
        return 0;
×
1488
    }
1489
    if (data[0] == 0xefu && data[1] == 0xbbu && data[2] == 0xbfu) {
×
1490
        return 1;
×
1491
    }
1492
    return 0;
×
1493
}
1494

1495

1496
/*
1497
 * Materialize palette bytes from a stream.
1498
 *
1499
 * The flow looks like:
1500
 *
1501
 *   stream --> [scratch buffer] --> [resizable heap buffer]
1502
 *                  ^ looped read        ^ returned payload
1503
 */
1504
static SIXELSTATUS
1505
sixel_palette_read_stream(FILE *stream,
×
1506
                          sixel_allocator_t *allocator,
1507
                          unsigned char **pdata,
1508
                          size_t *psize)
1509
{
1510
    SIXELSTATUS status;
1511
    unsigned char *buffer;
1512
    unsigned char *grown;
1513
    size_t capacity;
1514
    size_t used;
1515
    size_t read_bytes;
1516
    size_t needed;
1517
    size_t new_capacity;
1518
    unsigned char scratch[4096];
1519

1520
    status = SIXEL_FALSE;
×
1521
    buffer = NULL;
×
1522
    grown = NULL;
×
1523
    capacity = 0u;
×
1524
    used = 0u;
×
1525
    read_bytes = 0u;
×
1526
    needed = 0u;
×
1527
    new_capacity = 0u;
×
1528

1529
    if (pdata == NULL || psize == NULL || stream == NULL || allocator == NULL) {
×
1530
        sixel_helper_set_additional_message(
×
1531
            "sixel_palette_read_stream: invalid argument.");
1532
        return SIXEL_BAD_ARGUMENT;
×
1533
    }
1534

1535
    *pdata = NULL;
×
1536
    *psize = 0u;
×
1537

1538
    while (1) {
1539
        read_bytes = fread(scratch, 1, sizeof(scratch), stream);
×
1540
        if (read_bytes == 0u) {
×
1541
            if (ferror(stream)) {
×
1542
                sixel_helper_set_additional_message(
×
1543
                    "sixel_palette_read_stream: fread() failed.");
1544
                status = SIXEL_LIBC_ERROR;
×
1545
                goto cleanup;
×
1546
            }
1547
            break;
×
1548
        }
1549

1550
        if (used > SIZE_MAX - read_bytes) {
×
1551
            sixel_helper_set_additional_message(
×
1552
                "sixel_palette_read_stream: size overflow.");
1553
            status = SIXEL_BAD_ALLOCATION;
×
1554
            goto cleanup;
×
1555
        }
1556
        needed = used + read_bytes;
×
1557

1558
        if (needed > capacity) {
×
1559
            new_capacity = capacity;
×
1560
            if (new_capacity == 0u) {
×
1561
                new_capacity = 4096u;
×
1562
            }
1563
            while (needed > new_capacity) {
×
1564
                if (new_capacity > SIZE_MAX / 2u) {
×
1565
                    sixel_helper_set_additional_message(
×
1566
                        "sixel_palette_read_stream: size overflow.");
1567
                    status = SIXEL_BAD_ALLOCATION;
×
1568
                    goto cleanup;
×
1569
                }
1570
                new_capacity *= 2u;
×
1571
            }
1572

1573
            grown = (unsigned char *)sixel_allocator_malloc(allocator,
×
1574
                                                             new_capacity);
1575
            if (grown == NULL) {
×
1576
                sixel_helper_set_additional_message(
×
1577
                    "sixel_palette_read_stream: allocation failed.");
1578
                status = SIXEL_BAD_ALLOCATION;
×
1579
                goto cleanup;
×
1580
            }
1581

1582
            if (buffer != NULL) {
×
1583
                memcpy(grown, buffer, used);
×
1584
                sixel_allocator_free(allocator, buffer);
×
1585
            }
1586

1587
            buffer = grown;
×
1588
            grown = NULL;
×
1589
            capacity = new_capacity;
×
1590
        }
1591

1592
        memcpy(buffer + used, scratch, read_bytes);
×
1593
        used += read_bytes;
×
1594
    }
1595

1596
    *pdata = buffer;
×
1597
    *psize = used;
×
1598
    status = SIXEL_OK;
×
1599
    return status;
×
1600

1601
cleanup:
1602
    if (grown != NULL) {
×
1603
        sixel_allocator_free(allocator, grown);
×
1604
    }
1605
    if (buffer != NULL) {
×
1606
        sixel_allocator_free(allocator, buffer);
×
1607
    }
1608
    return status;
×
1609
}
1610

1611

1612
static SIXELSTATUS
1613
sixel_palette_open_read(char const *path, FILE **pstream, int *pclose)
5✔
1614
{
1615
    if (pstream == NULL || pclose == NULL || path == NULL) {
5!
1616
        sixel_helper_set_additional_message(
×
1617
            "sixel_palette_open_read: invalid argument.");
1618
        return SIXEL_BAD_ARGUMENT;
×
1619
    }
1620

1621
    if (strcmp(path, "-") == 0) {
5!
1622
        *pstream = stdin;
×
1623
        *pclose = 0;
×
1624
        return SIXEL_OK;
×
1625
    }
1626

1627
    *pstream = fopen(path, "rb");
5✔
1628
    if (*pstream == NULL) {
5!
1629
        sixel_helper_set_additional_message(
5✔
1630
            "sixel_palette_open_read: failed to open file.");
1631
        return SIXEL_LIBC_ERROR;
5✔
1632
    }
1633

1634
    *pclose = 1;
×
1635
    return SIXEL_OK;
×
1636
}
3✔
1637

1638

1639
static void
1640
sixel_palette_close_stream(FILE *stream, int close_stream)
×
1641
{
1642
    if (close_stream && stream != NULL) {
×
1643
        (void) fclose(stream);
×
1644
    }
1645
}
×
1646

1647

1648
static sixel_palette_format_t
1649
sixel_palette_guess_format(unsigned char const *data, size_t size)
×
1650
{
1651
    size_t offset;
1652
    size_t data_size;
1653

1654
    offset = 0u;
×
1655
    data_size = size;
×
1656

1657
    if (data == NULL || size == 0u) {
×
1658
        return SIXEL_PALETTE_FORMAT_NONE;
×
1659
    }
1660

1661
    if (size == 256u * 3u || size == 256u * 3u + 4u) {
×
1662
        return SIXEL_PALETTE_FORMAT_ACT;
×
1663
    }
1664

1665
    if (size >= 12u && memcmp(data, "RIFF", 4) == 0
×
1666
            && memcmp(data + 8, "PAL ", 4) == 0) {
×
1667
        return SIXEL_PALETTE_FORMAT_PAL_RIFF;
×
1668
    }
1669

1670
    if (sixel_palette_has_utf8_bom(data, size)) {
×
1671
        offset = 3u;
×
1672
        data_size = size - 3u;
×
1673
    }
1674

1675
    if (data_size >= 8u && memcmp(data + offset, "JASC-PAL", 8) == 0) {
×
1676
        return SIXEL_PALETTE_FORMAT_PAL_JASC;
×
1677
    }
1678
    if (data_size >= 12u && memcmp(data + offset, "GIMP Palette", 12) == 0) {
×
1679
        return SIXEL_PALETTE_FORMAT_GPL;
×
1680
    }
1681

1682
    return SIXEL_PALETTE_FORMAT_NONE;
×
1683
}
1684

1685

1686
static unsigned int
1687
sixel_palette_read_le16(unsigned char const *ptr)
×
1688
{
1689
    if (ptr == NULL) {
×
1690
        return 0u;
×
1691
    }
1692
    return (unsigned int)ptr[0] | ((unsigned int)ptr[1] << 8);
×
1693
}
1694

1695

1696
static unsigned int
1697
sixel_palette_read_le32(unsigned char const *ptr)
×
1698
{
1699
    if (ptr == NULL) {
×
1700
        return 0u;
×
1701
    }
1702
    return ((unsigned int)ptr[0])
×
1703
        | ((unsigned int)ptr[1] << 8)
×
1704
        | ((unsigned int)ptr[2] << 16)
×
1705
        | ((unsigned int)ptr[3] << 24);
×
1706
}
1707

1708

1709
/*
1710
 * Adobe Color Table (*.act) reader
1711
 *
1712
 *   +-----------+---------------------------+
1713
 *   | section   | bytes                     |
1714
 *   +-----------+---------------------------+
1715
 *   | palette   | 256 entries * 3 RGB bytes |
1716
 *   | trailer   | optional count/start pair |
1717
 *   +-----------+---------------------------+
1718
 */
1719
static SIXELSTATUS
1720
sixel_palette_parse_act(unsigned char const *data,
×
1721
                        size_t size,
1722
                        sixel_encoder_t *encoder,
1723
                        sixel_dither_t **dither)
1724
{
1725
    SIXELSTATUS status;
1726
    sixel_dither_t *local;
1727
    unsigned char const *palette_start;
1728
    unsigned char const *trailer;
1729
    unsigned char *target;
1730
    size_t copy_bytes;
1731
    int exported_colors;
1732
    int start_index;
1733

1734
    status = SIXEL_FALSE;
×
1735
    local = NULL;
×
1736
    palette_start = data;
×
1737
    trailer = NULL;
×
1738
    target = NULL;
×
1739
    copy_bytes = 0u;
×
1740
    exported_colors = 0;
×
1741
    start_index = 0;
×
1742

1743
    if (encoder == NULL || dither == NULL) {
×
1744
        sixel_helper_set_additional_message(
×
1745
            "sixel_palette_parse_act: invalid argument.");
1746
        return SIXEL_BAD_ARGUMENT;
×
1747
    }
1748
    if (data == NULL || size < 256u * 3u) {
×
1749
        sixel_helper_set_additional_message(
×
1750
            "sixel_palette_parse_act: truncated ACT palette.");
1751
        return SIXEL_BAD_INPUT;
×
1752
    }
1753

1754
    if (size == 256u * 3u) {
×
1755
        exported_colors = 256;
×
1756
        start_index = 0;
×
1757
    } else if (size == 256u * 3u + 4u) {
×
1758
        trailer = data + 256u * 3u;
×
1759
        exported_colors = (int)(((unsigned int)trailer[0] << 8)
×
1760
                                | (unsigned int)trailer[1]);
×
1761
        start_index = (int)(((unsigned int)trailer[2] << 8)
×
1762
                            | (unsigned int)trailer[3]);
×
1763
    } else {
1764
        sixel_helper_set_additional_message(
×
1765
            "sixel_palette_parse_act: invalid ACT length.");
1766
        return SIXEL_BAD_INPUT;
×
1767
    }
1768

1769
    if (start_index < 0 || start_index >= 256) {
×
1770
        sixel_helper_set_additional_message(
×
1771
            "sixel_palette_parse_act: ACT start index out of range.");
1772
        return SIXEL_BAD_INPUT;
×
1773
    }
1774
    if (exported_colors <= 0 || exported_colors > 256) {
×
1775
        exported_colors = 256;
×
1776
    }
1777
    if (start_index + exported_colors > 256) {
×
1778
        sixel_helper_set_additional_message(
×
1779
            "sixel_palette_parse_act: ACT palette exceeds 256 slots.");
1780
        return SIXEL_BAD_INPUT;
×
1781
    }
1782

1783
    status = sixel_dither_new(&local, exported_colors, encoder->allocator);
×
1784
    if (SIXEL_FAILED(status)) {
×
1785
        return status;
×
1786
    }
1787

1788
    sixel_dither_set_lut_policy(local, encoder->lut_policy);
×
1789

1790
    target = sixel_dither_get_palette(local);
×
1791
    copy_bytes = (size_t)exported_colors * 3u;
×
1792
    memcpy(target, palette_start + (size_t)start_index * 3u, copy_bytes);
×
1793

1794
    *dither = local;
×
1795
    return SIXEL_OK;
×
1796
}
1797

1798

1799
static SIXELSTATUS
1800
sixel_palette_parse_pal_jasc(unsigned char const *data,
×
1801
                             size_t size,
1802
                             sixel_encoder_t *encoder,
1803
                             sixel_dither_t **dither)
1804
{
1805
    SIXELSTATUS status;
1806
    char *text;
1807
    size_t index;
1808
    size_t offset;
1809
    char *cursor;
1810
    char *line;
1811
    char *line_end;
1812
    int stage;
1813
    int exported_colors;
1814
    int parsed_colors;
1815
    sixel_dither_t *local;
1816
    unsigned char *target;
1817
    long component;
1818
    char *parse_end;
1819
    int value_index;
1820
    int values[3];
1821
    char tail;
1822

1823
    status = SIXEL_FALSE;
×
1824
    text = NULL;
×
1825
    index = 0u;
×
1826
    offset = 0u;
×
1827
    cursor = NULL;
×
1828
    line = NULL;
×
1829
    line_end = NULL;
×
1830
    stage = 0;
×
1831
    exported_colors = 0;
×
1832
    parsed_colors = 0;
×
1833
    local = NULL;
×
1834
    target = NULL;
×
1835
    component = 0;
×
1836
    parse_end = NULL;
×
1837
    value_index = 0;
×
1838
    values[0] = 0;
×
1839
    values[1] = 0;
×
1840
    values[2] = 0;
×
1841

1842
    if (encoder == NULL || dither == NULL) {
×
1843
        sixel_helper_set_additional_message(
×
1844
            "sixel_palette_parse_pal_jasc: invalid argument.");
1845
        return SIXEL_BAD_ARGUMENT;
×
1846
    }
1847
    if (data == NULL || size == 0u) {
×
1848
        sixel_helper_set_additional_message(
×
1849
            "sixel_palette_parse_pal_jasc: empty palette.");
1850
        return SIXEL_BAD_INPUT;
×
1851
    }
1852

1853
    text = (char *)sixel_allocator_malloc(encoder->allocator, size + 1u);
×
1854
    if (text == NULL) {
×
1855
        sixel_helper_set_additional_message(
×
1856
            "sixel_palette_parse_pal_jasc: allocation failed.");
1857
        return SIXEL_BAD_ALLOCATION;
×
1858
    }
1859
    memcpy(text, data, size);
×
1860
    text[size] = '\0';
×
1861

1862
    if (sixel_palette_has_utf8_bom((unsigned char const *)text, size)) {
×
1863
        offset = 3u;
×
1864
    }
1865
    cursor = text + offset;
×
1866

1867
    while (*cursor != '\0') {
×
1868
        line = cursor;
×
1869
        line_end = cursor;
×
1870
        while (*line_end != '\0' && *line_end != '\n' && *line_end != '\r') {
×
1871
            ++line_end;
×
1872
        }
1873
        if (*line_end != '\0') {
×
1874
            *line_end = '\0';
×
1875
            cursor = line_end + 1;
×
1876
        } else {
1877
            cursor = line_end;
×
1878
        }
1879
        while (*cursor == '\n' || *cursor == '\r') {
×
1880
            ++cursor;
×
1881
        }
1882

1883
        while (*line == ' ' || *line == '\t') {
×
1884
            ++line;
×
1885
        }
1886
        index = strlen(line);
×
1887
        while (index > 0u) {
×
1888
            tail = line[index - 1];
×
1889
            if (tail != ' ' && tail != '\t') {
×
1890
                break;
×
1891
            }
1892
            line[index - 1] = '\0';
×
1893
            --index;
×
1894
        }
1895
        if (*line == '\0') {
×
1896
            continue;
×
1897
        }
1898
        if (*line == '#') {
×
1899
            continue;
×
1900
        }
1901

1902
        if (stage == 0) {
×
1903
            if (strcmp(line, "JASC-PAL") != 0) {
×
1904
                sixel_helper_set_additional_message(
×
1905
                    "sixel_palette_parse_pal_jasc: missing header.");
1906
                status = SIXEL_BAD_INPUT;
×
1907
                goto cleanup;
×
1908
            }
1909
            stage = 1;
×
1910
            continue;
×
1911
        }
1912
        if (stage == 1) {
×
1913
            stage = 2;
×
1914
            continue;
×
1915
        }
1916
        if (stage == 2) {
×
1917
            component = strtol(line, &parse_end, 10);
×
1918
            if (parse_end == line || component <= 0L || component > 256L) {
×
1919
                sixel_helper_set_additional_message(
×
1920
                    "sixel_palette_parse_pal_jasc: invalid color count.");
1921
                status = SIXEL_BAD_INPUT;
×
1922
                goto cleanup;
×
1923
            }
1924
            exported_colors = (int)component;
×
1925
            status = sixel_dither_new(&local, exported_colors,
×
1926
                                      encoder->allocator);
1927
            if (SIXEL_FAILED(status)) {
×
1928
                goto cleanup;
×
1929
            }
1930
            sixel_dither_set_lut_policy(local, encoder->lut_policy);
×
1931
            target = sixel_dither_get_palette(local);
×
1932
            stage = 3;
×
1933
            continue;
×
1934
        }
1935

1936
        value_index = 0;
×
1937
        while (value_index < 3) {
×
1938
            component = strtol(line, &parse_end, 10);
×
1939
            if (parse_end == line || component < 0L || component > 255L) {
×
1940
                sixel_helper_set_additional_message(
×
1941
                    "sixel_palette_parse_pal_jasc: invalid component.");
1942
                status = SIXEL_BAD_INPUT;
×
1943
                goto cleanup;
×
1944
            }
1945
            values[value_index] = (int)component;
×
1946
            ++value_index;
×
1947
            line = parse_end;
×
1948
            while (*line == ' ' || *line == '\t') {
×
1949
                ++line;
×
1950
            }
1951
        }
1952

1953
        if (parsed_colors >= exported_colors) {
×
1954
            sixel_helper_set_additional_message(
×
1955
                "sixel_palette_parse_pal_jasc: excess entries.");
1956
            status = SIXEL_BAD_INPUT;
×
1957
            goto cleanup;
×
1958
        }
1959

1960
        target[parsed_colors * 3 + 0] =
×
1961
            (unsigned char)values[0];
×
1962
        target[parsed_colors * 3 + 1] =
×
1963
            (unsigned char)values[1];
×
1964
        target[parsed_colors * 3 + 2] =
×
1965
            (unsigned char)values[2];
×
1966
        ++parsed_colors;
×
1967
    }
1968

1969
    if (stage < 3) {
×
1970
        sixel_helper_set_additional_message(
×
1971
            "sixel_palette_parse_pal_jasc: incomplete header.");
1972
        status = SIXEL_BAD_INPUT;
×
1973
        goto cleanup;
×
1974
    }
1975
    if (parsed_colors != exported_colors) {
×
1976
        sixel_helper_set_additional_message(
×
1977
            "sixel_palette_parse_pal_jasc: color count mismatch.");
1978
        status = SIXEL_BAD_INPUT;
×
1979
        goto cleanup;
×
1980
    }
1981

1982
    *dither = local;
×
1983
    status = SIXEL_OK;
×
1984

1985
cleanup:
1986
    if (SIXEL_FAILED(status) && local != NULL) {
×
1987
        sixel_dither_unref(local);
×
1988
    }
1989
    if (text != NULL) {
×
1990
        sixel_allocator_free(encoder->allocator, text);
×
1991
    }
1992
    return status;
×
1993
}
1994

1995

1996
static SIXELSTATUS
1997
sixel_palette_parse_pal_riff(unsigned char const *data,
×
1998
                             size_t size,
1999
                             sixel_encoder_t *encoder,
2000
                             sixel_dither_t **dither)
2001
{
2002
    SIXELSTATUS status;
2003
    size_t offset;
2004
    size_t chunk_size;
2005
    sixel_dither_t *local;
2006
    unsigned char const *chunk;
2007
    unsigned char *target;
2008
    unsigned int entry_count;
2009
    unsigned int version;
2010
    unsigned int index;
2011
    size_t palette_offset;
2012

2013
    status = SIXEL_FALSE;
×
2014
    offset = 0u;
×
2015
    chunk_size = 0u;
×
2016
    local = NULL;
×
2017
    chunk = NULL;
×
2018
    target = NULL;
×
2019
    entry_count = 0u;
×
2020
    version = 0u;
×
2021
    index = 0u;
×
2022
    palette_offset = 0u;
×
2023

2024
    if (encoder == NULL || dither == NULL) {
×
2025
        sixel_helper_set_additional_message(
×
2026
            "sixel_palette_parse_pal_riff: invalid argument.");
2027
        return SIXEL_BAD_ARGUMENT;
×
2028
    }
2029
    if (data == NULL || size < 12u) {
×
2030
        sixel_helper_set_additional_message(
×
2031
            "sixel_palette_parse_pal_riff: truncated palette.");
2032
        return SIXEL_BAD_INPUT;
×
2033
    }
2034
    if (memcmp(data, "RIFF", 4) != 0 || memcmp(data + 8, "PAL ", 4) != 0) {
×
2035
        sixel_helper_set_additional_message(
×
2036
            "sixel_palette_parse_pal_riff: missing RIFF header.");
2037
        return SIXEL_BAD_INPUT;
×
2038
    }
2039

2040
    offset = 12u;
×
2041
    while (offset + 8u <= size) {
×
2042
        chunk = data + offset;
×
2043
        chunk_size = (size_t)sixel_palette_read_le32(chunk + 4);
×
2044
        if (offset + 8u + chunk_size > size) {
×
2045
            sixel_helper_set_additional_message(
×
2046
                "sixel_palette_parse_pal_riff: chunk extends past end.");
2047
            return SIXEL_BAD_INPUT;
×
2048
        }
2049
        if (memcmp(chunk, "data", 4) == 0) {
×
2050
            break;
×
2051
        }
2052
        offset += 8u + ((chunk_size + 1u) & ~1u);
×
2053
    }
2054

2055
    if (offset + 8u > size || memcmp(chunk, "data", 4) != 0) {
×
2056
        sixel_helper_set_additional_message(
×
2057
            "sixel_palette_parse_pal_riff: missing data chunk.");
2058
        return SIXEL_BAD_INPUT;
×
2059
    }
2060

2061
    if (chunk_size < 4u) {
×
2062
        sixel_helper_set_additional_message(
×
2063
            "sixel_palette_parse_pal_riff: data chunk too small.");
2064
        return SIXEL_BAD_INPUT;
×
2065
    }
2066
    version = sixel_palette_read_le16(chunk + 8);
×
2067
    (void)version;
2068
    entry_count = sixel_palette_read_le16(chunk + 10);
×
2069
    if (entry_count == 0u || entry_count > 256u) {
×
2070
        sixel_helper_set_additional_message(
×
2071
            "sixel_palette_parse_pal_riff: invalid entry count.");
2072
        return SIXEL_BAD_INPUT;
×
2073
    }
2074
    if (chunk_size != 4u + (size_t)entry_count * 4u) {
×
2075
        sixel_helper_set_additional_message(
×
2076
            "sixel_palette_parse_pal_riff: unexpected chunk size.");
2077
        return SIXEL_BAD_INPUT;
×
2078
    }
2079

2080
    status = sixel_dither_new(&local, (int)entry_count, encoder->allocator);
×
2081
    if (SIXEL_FAILED(status)) {
×
2082
        return status;
×
2083
    }
2084
    sixel_dither_set_lut_policy(local, encoder->lut_policy);
×
2085
    target = sixel_dither_get_palette(local);
×
2086
    palette_offset = 12u;
×
2087
    for (index = 0u; index < entry_count; ++index) {
×
2088
        target[index * 3u + 0u] =
×
2089
            chunk[palette_offset + index * 4u + 0u];
×
2090
        target[index * 3u + 1u] =
×
2091
            chunk[palette_offset + index * 4u + 1u];
×
2092
        target[index * 3u + 2u] =
×
2093
            chunk[palette_offset + index * 4u + 2u];
×
2094
    }
2095

2096
    *dither = local;
×
2097
    return SIXEL_OK;
×
2098
}
2099

2100

2101
static SIXELSTATUS
2102
sixel_palette_parse_gpl(unsigned char const *data,
×
2103
                        size_t size,
2104
                        sixel_encoder_t *encoder,
2105
                        sixel_dither_t **dither)
2106
{
2107
    SIXELSTATUS status;
2108
    char *text;
2109
    size_t offset;
2110
    char *cursor;
2111
    char *line;
2112
    char *line_end;
2113
    size_t index;
2114
    int header_seen;
2115
    int parsed_colors;
2116
    unsigned char palette_bytes[256 * 3];
2117
    long component;
2118
    char *parse_end;
2119
    int value_index;
2120
    int values[3];
2121
    sixel_dither_t *local;
2122
    unsigned char *target;
2123
    char tail;
2124

2125
    status = SIXEL_FALSE;
×
2126
    text = NULL;
×
2127
    offset = 0u;
×
2128
    cursor = NULL;
×
2129
    line = NULL;
×
2130
    line_end = NULL;
×
2131
    index = 0u;
×
2132
    header_seen = 0;
×
2133
    parsed_colors = 0;
×
2134
    component = 0;
×
2135
    parse_end = NULL;
×
2136
    value_index = 0;
×
2137
    values[0] = 0;
×
2138
    values[1] = 0;
×
2139
    values[2] = 0;
×
2140
    local = NULL;
×
2141
    target = NULL;
×
2142

2143
    if (encoder == NULL || dither == NULL) {
×
2144
        sixel_helper_set_additional_message(
×
2145
            "sixel_palette_parse_gpl: invalid argument.");
2146
        return SIXEL_BAD_ARGUMENT;
×
2147
    }
2148
    if (data == NULL || size == 0u) {
×
2149
        sixel_helper_set_additional_message(
×
2150
            "sixel_palette_parse_gpl: empty palette.");
2151
        return SIXEL_BAD_INPUT;
×
2152
    }
2153

2154
    text = (char *)sixel_allocator_malloc(encoder->allocator, size + 1u);
×
2155
    if (text == NULL) {
×
2156
        sixel_helper_set_additional_message(
×
2157
            "sixel_palette_parse_gpl: allocation failed.");
2158
        return SIXEL_BAD_ALLOCATION;
×
2159
    }
2160
    memcpy(text, data, size);
×
2161
    text[size] = '\0';
×
2162

2163
    if (sixel_palette_has_utf8_bom((unsigned char const *)text, size)) {
×
2164
        offset = 3u;
×
2165
    }
2166
    cursor = text + offset;
×
2167

2168
    while (*cursor != '\0') {
×
2169
        line = cursor;
×
2170
        line_end = cursor;
×
2171
        while (*line_end != '\0' && *line_end != '\n' && *line_end != '\r') {
×
2172
            ++line_end;
×
2173
        }
2174
        if (*line_end != '\0') {
×
2175
            *line_end = '\0';
×
2176
            cursor = line_end + 1;
×
2177
        } else {
2178
            cursor = line_end;
×
2179
        }
2180
        while (*cursor == '\n' || *cursor == '\r') {
×
2181
            ++cursor;
×
2182
        }
2183

2184
        while (*line == ' ' || *line == '\t') {
×
2185
            ++line;
×
2186
        }
2187
        index = strlen(line);
×
2188
        while (index > 0u) {
×
2189
            tail = line[index - 1];
×
2190
            if (tail != ' ' && tail != '\t') {
×
2191
                break;
×
2192
            }
2193
            line[index - 1] = '\0';
×
2194
            --index;
×
2195
        }
2196
        if (*line == '\0') {
×
2197
            continue;
×
2198
        }
2199
        if (*line == '#') {
×
2200
            continue;
×
2201
        }
2202
        if (strncmp(line, "Name:", 5) == 0) {
×
2203
            continue;
×
2204
        }
2205
        if (strncmp(line, "Columns:", 8) == 0) {
×
2206
            continue;
×
2207
        }
2208

2209
        if (!header_seen) {
×
2210
            if (strcmp(line, "GIMP Palette") != 0) {
×
2211
                sixel_helper_set_additional_message(
×
2212
                    "sixel_palette_parse_gpl: missing header.");
2213
                status = SIXEL_BAD_INPUT;
×
2214
                goto cleanup;
×
2215
            }
2216
            header_seen = 1;
×
2217
            continue;
×
2218
        }
2219

2220
        if (parsed_colors >= 256) {
×
2221
            sixel_helper_set_additional_message(
×
2222
                "sixel_palette_parse_gpl: too many colors.");
2223
            status = SIXEL_BAD_INPUT;
×
2224
            goto cleanup;
×
2225
        }
2226

2227
        value_index = 0;
×
2228
        while (value_index < 3) {
×
2229
            component = strtol(line, &parse_end, 10);
×
2230
            if (parse_end == line || component < 0L || component > 255L) {
×
2231
                sixel_helper_set_additional_message(
×
2232
                    "sixel_palette_parse_gpl: invalid component.");
2233
                status = SIXEL_BAD_INPUT;
×
2234
                goto cleanup;
×
2235
            }
2236
            values[value_index] = (int)component;
×
2237
            ++value_index;
×
2238
            line = parse_end;
×
2239
            while (*line == ' ' || *line == '\t') {
×
2240
                ++line;
×
2241
            }
2242
        }
2243

2244
        palette_bytes[parsed_colors * 3 + 0] =
×
2245
            (unsigned char)values[0];
×
2246
        palette_bytes[parsed_colors * 3 + 1] =
×
2247
            (unsigned char)values[1];
×
2248
        palette_bytes[parsed_colors * 3 + 2] =
×
2249
            (unsigned char)values[2];
×
2250
        ++parsed_colors;
×
2251
    }
2252

2253
    if (!header_seen) {
×
2254
        sixel_helper_set_additional_message(
×
2255
            "sixel_palette_parse_gpl: header missing.");
2256
        status = SIXEL_BAD_INPUT;
×
2257
        goto cleanup;
×
2258
    }
2259
    if (parsed_colors <= 0) {
×
2260
        sixel_helper_set_additional_message(
×
2261
            "sixel_palette_parse_gpl: no colors parsed.");
2262
        status = SIXEL_BAD_INPUT;
×
2263
        goto cleanup;
×
2264
    }
2265

2266
    status = sixel_dither_new(&local, parsed_colors, encoder->allocator);
×
2267
    if (SIXEL_FAILED(status)) {
×
2268
        goto cleanup;
×
2269
    }
2270
    sixel_dither_set_lut_policy(local, encoder->lut_policy);
×
2271
    target = sixel_dither_get_palette(local);
×
2272
    memcpy(target, palette_bytes, (size_t)parsed_colors * 3u);
×
2273

2274
    *dither = local;
×
2275
    status = SIXEL_OK;
×
2276

2277
cleanup:
2278
    if (SIXEL_FAILED(status) && local != NULL) {
×
2279
        sixel_dither_unref(local);
×
2280
    }
2281
    if (text != NULL) {
×
2282
        sixel_allocator_free(encoder->allocator, text);
×
2283
    }
2284
    return status;
×
2285
}
2286

2287

2288
/*
2289
 * Palette exporters
2290
 *
2291
 *   +----------+-------------------------+
2292
 *   | format   | emission strategy       |
2293
 *   +----------+-------------------------+
2294
 *   | ACT      | fixed 256 entries + EOF |
2295
 *   | PAL JASC | textual lines           |
2296
 *   | PAL RIFF | RIFF container          |
2297
 *   | GPL      | textual lines           |
2298
 *   +----------+-------------------------+
2299
 */
2300
static SIXELSTATUS
2301
sixel_palette_write_act(FILE *stream,
×
2302
                        unsigned char const *palette,
2303
                        int exported_colors)
2304
{
2305
    SIXELSTATUS status;
2306
    unsigned char act_table[256 * 3];
2307
    unsigned char trailer[4];
2308
    size_t exported_bytes;
2309

2310
    status = SIXEL_FALSE;
×
2311
    exported_bytes = 0u;
×
2312

2313
    if (stream == NULL || palette == NULL || exported_colors <= 0) {
×
2314
        return SIXEL_BAD_ARGUMENT;
×
2315
    }
2316
    if (exported_colors > 256) {
×
2317
        exported_colors = 256;
×
2318
    }
2319

2320
    memset(act_table, 0, sizeof(act_table));
×
2321
    exported_bytes = (size_t)exported_colors * 3u;
×
2322
    memcpy(act_table, palette, exported_bytes);
×
2323

2324
    trailer[0] = (unsigned char)(((unsigned int)exported_colors >> 8)
×
2325
                                 & 0xffu);
2326
    trailer[1] = (unsigned char)((unsigned int)exported_colors & 0xffu);
×
2327
    trailer[2] = 0u;
×
2328
    trailer[3] = 0u;
×
2329

2330
    if (fwrite(act_table, 1, sizeof(act_table), stream)
×
2331
            != sizeof(act_table)) {
2332
        status = SIXEL_LIBC_ERROR;
×
2333
        return status;
×
2334
    }
2335
    if (fwrite(trailer, 1, sizeof(trailer), stream)
×
2336
            != sizeof(trailer)) {
2337
        status = SIXEL_LIBC_ERROR;
×
2338
        return status;
×
2339
    }
2340

2341
    return SIXEL_OK;
×
2342
}
2343

2344

2345
static SIXELSTATUS
2346
sixel_palette_write_pal_jasc(FILE *stream,
×
2347
                             unsigned char const *palette,
2348
                             int exported_colors)
2349
{
2350
    int index;
2351

2352
    if (stream == NULL || palette == NULL || exported_colors <= 0) {
×
2353
        return SIXEL_BAD_ARGUMENT;
×
2354
    }
2355
    if (fprintf(stream, "JASC-PAL\n0100\n%d\n", exported_colors) < 0) {
×
2356
        return SIXEL_LIBC_ERROR;
×
2357
    }
2358
    for (index = 0; index < exported_colors; ++index) {
×
2359
        if (fprintf(stream, "%d %d %d\n",
×
2360
                    (int)palette[index * 3 + 0],
×
2361
                    (int)palette[index * 3 + 1],
×
2362
                    (int)palette[index * 3 + 2]) < 0) {
×
2363
            return SIXEL_LIBC_ERROR;
×
2364
        }
2365
    }
2366
    return SIXEL_OK;
×
2367
}
2368

2369

2370
static SIXELSTATUS
2371
sixel_palette_write_pal_riff(FILE *stream,
×
2372
                             unsigned char const *palette,
2373
                             int exported_colors)
2374
{
2375
    unsigned char header[12];
2376
    unsigned char chunk[8];
2377
    unsigned char log_palette[4 + 256 * 4];
2378
    unsigned int data_size;
2379
    unsigned int riff_size;
2380
    int index;
2381

2382
    if (stream == NULL || palette == NULL || exported_colors <= 0) {
×
2383
        return SIXEL_BAD_ARGUMENT;
×
2384
    }
2385
    if (exported_colors > 256) {
×
2386
        exported_colors = 256;
×
2387
    }
2388

2389
    data_size = 4u + (unsigned int)exported_colors * 4u;
×
2390
    riff_size = 4u + 8u + data_size;
×
2391

2392
    memcpy(header, "RIFF", 4);
×
2393
    header[4] = (unsigned char)(riff_size & 0xffu);
×
2394
    header[5] = (unsigned char)((riff_size >> 8) & 0xffu);
×
2395
    header[6] = (unsigned char)((riff_size >> 16) & 0xffu);
×
2396
    header[7] = (unsigned char)((riff_size >> 24) & 0xffu);
×
2397
    memcpy(header + 8, "PAL ", 4);
×
2398

2399
    memcpy(chunk, "data", 4);
×
2400
    chunk[4] = (unsigned char)(data_size & 0xffu);
×
2401
    chunk[5] = (unsigned char)((data_size >> 8) & 0xffu);
×
2402
    chunk[6] = (unsigned char)((data_size >> 16) & 0xffu);
×
2403
    chunk[7] = (unsigned char)((data_size >> 24) & 0xffu);
×
2404

2405
    memset(log_palette, 0, sizeof(log_palette));
×
2406
    log_palette[0] = 0x00;
×
2407
    log_palette[1] = 0x03;
×
2408
    log_palette[2] = (unsigned char)(exported_colors & 0xff);
×
2409
    log_palette[3] = (unsigned char)((exported_colors >> 8) & 0xff);
×
2410
    for (index = 0; index < exported_colors; ++index) {
×
2411
        log_palette[4 + index * 4 + 0] = palette[index * 3 + 0];
×
2412
        log_palette[4 + index * 4 + 1] = palette[index * 3 + 1];
×
2413
        log_palette[4 + index * 4 + 2] = palette[index * 3 + 2];
×
2414
        log_palette[4 + index * 4 + 3] = 0u;
×
2415
    }
2416

2417
    if (fwrite(header, 1, sizeof(header), stream)
×
2418
            != sizeof(header)) {
2419
        return SIXEL_LIBC_ERROR;
×
2420
    }
2421
    if (fwrite(chunk, 1, sizeof(chunk), stream) != sizeof(chunk)) {
×
2422
        return SIXEL_LIBC_ERROR;
×
2423
    }
2424
    if (fwrite(log_palette, 1, (size_t)data_size, stream)
×
2425
            != (size_t)data_size) {
×
2426
        return SIXEL_LIBC_ERROR;
×
2427
    }
2428
    return SIXEL_OK;
×
2429
}
2430

2431

2432
static SIXELSTATUS
2433
sixel_palette_write_gpl(FILE *stream,
×
2434
                        unsigned char const *palette,
2435
                        int exported_colors)
2436
{
2437
    int index;
2438

2439
    if (stream == NULL || palette == NULL || exported_colors <= 0) {
×
2440
        return SIXEL_BAD_ARGUMENT;
×
2441
    }
2442
    if (fprintf(stream, "GIMP Palette\n") < 0) {
×
2443
        return SIXEL_LIBC_ERROR;
×
2444
    }
2445
    if (fprintf(stream, "Name: libsixel export\n") < 0) {
×
2446
        return SIXEL_LIBC_ERROR;
×
2447
    }
2448
    if (fprintf(stream, "Columns: 16\n") < 0) {
×
2449
        return SIXEL_LIBC_ERROR;
×
2450
    }
2451
    if (fprintf(stream, "# Exported by libsixel\n") < 0) {
×
2452
        return SIXEL_LIBC_ERROR;
×
2453
    }
2454
    for (index = 0; index < exported_colors; ++index) {
×
2455
        if (fprintf(stream, "%3d %3d %3d\tIndex %d\n",
×
2456
                    (int)palette[index * 3 + 0],
×
2457
                    (int)palette[index * 3 + 1],
×
2458
                    (int)palette[index * 3 + 2],
×
2459
                    index) < 0) {
2460
            return SIXEL_LIBC_ERROR;
×
2461
        }
2462
    }
2463
    return SIXEL_OK;
×
2464
}
2465

2466

2467
/* create palette from specified map file */
2468
static SIXELSTATUS
2469
sixel_prepare_specified_palette(
26✔
2470
    sixel_dither_t  /* out */   **dither,
2471
    sixel_encoder_t /* in */    *encoder)
2472
{
2473
    SIXELSTATUS status;
2474
    sixel_callback_context_for_mapfile_t callback_context;
2475
    sixel_loader_t *loader;
2476
    int fstatic;
2477
    int fuse_palette;
2478
    int reqcolors;
2479
    int loop_override;
2480
    char const *path;
2481
    sixel_palette_format_t format_hint;
2482
    sixel_palette_format_t format_ext;
2483
    sixel_palette_format_t format_final;
2484
    sixel_palette_format_t format_detected;
2485
    FILE *stream;
2486
    int close_stream;
2487
    unsigned char *buffer;
2488
    size_t buffer_size;
2489
    int palette_request;
2490
    int need_detection;
2491
    int treat_as_image;
2492
    int path_has_extension;
2493

2494
    status = SIXEL_FALSE;
26✔
2495
    loader = NULL;
26✔
2496
    fstatic = 1;
26✔
2497
    fuse_palette = 1;
26✔
2498
    reqcolors = SIXEL_PALETTE_MAX;
26✔
2499
    loop_override = SIXEL_LOOP_DISABLE;
26✔
2500
    path = NULL;
26✔
2501
    format_hint = SIXEL_PALETTE_FORMAT_NONE;
26✔
2502
    format_ext = SIXEL_PALETTE_FORMAT_NONE;
26✔
2503
    format_final = SIXEL_PALETTE_FORMAT_NONE;
26✔
2504
    format_detected = SIXEL_PALETTE_FORMAT_NONE;
26✔
2505
    stream = NULL;
26✔
2506
    close_stream = 0;
26✔
2507
    buffer = NULL;
26✔
2508
    buffer_size = 0u;
26✔
2509
    palette_request = 0;
26✔
2510
    need_detection = 0;
26✔
2511
    treat_as_image = 0;
26✔
2512
    path_has_extension = 0;
26✔
2513

2514
    if (dither == NULL || encoder == NULL || encoder->mapfile == NULL) {
26!
2515
        sixel_helper_set_additional_message(
×
2516
            "sixel_prepare_specified_palette: invalid mapfile path.");
2517
        return SIXEL_BAD_ARGUMENT;
×
2518
    }
2519

2520
    path = sixel_palette_strip_prefix(encoder->mapfile, &format_hint);
26✔
2521
    if (path == NULL || *path == '\0') {
26!
2522
        sixel_helper_set_additional_message(
×
2523
            "sixel_prepare_specified_palette: empty mapfile path.");
2524
        return SIXEL_BAD_ARGUMENT;
×
2525
    }
2526

2527
    format_ext = sixel_palette_format_from_extension(path);
26✔
2528
    path_has_extension = sixel_path_has_any_extension(path);
26✔
2529

2530
    if (format_hint != SIXEL_PALETTE_FORMAT_NONE) {
26!
2531
        palette_request = 1;
×
2532
        format_final = format_hint;
×
2533
    } else if (format_ext != SIXEL_PALETTE_FORMAT_NONE) {
26!
2534
        palette_request = 1;
×
2535
        format_final = format_ext;
×
2536
    } else if (!path_has_extension) {
26✔
2537
        palette_request = 1;
5✔
2538
        need_detection = 1;
5✔
2539
    } else {
3✔
2540
        treat_as_image = 1;
21✔
2541
    }
2542

2543
    if (palette_request) {
26✔
2544
        status = sixel_palette_open_read(path, &stream, &close_stream);
5✔
2545
        if (SIXEL_FAILED(status)) {
5!
2546
            goto palette_cleanup;
5✔
2547
        }
2548
        status = sixel_palette_read_stream(stream,
×
2549
                                           encoder->allocator,
2550
                                           &buffer,
2551
                                           &buffer_size);
2552
        if (close_stream) {
×
2553
            sixel_palette_close_stream(stream, close_stream);
×
2554
            stream = NULL;
×
2555
            close_stream = 0;
×
2556
        }
2557
        if (SIXEL_FAILED(status)) {
×
2558
            goto palette_cleanup;
×
2559
        }
2560
        if (buffer_size == 0u) {
×
2561
            sixel_helper_set_additional_message(
×
2562
                "sixel_prepare_specified_palette: mapfile is empty.");
2563
            status = SIXEL_BAD_INPUT;
×
2564
            goto palette_cleanup;
×
2565
        }
2566

2567
        if (format_final == SIXEL_PALETTE_FORMAT_NONE) {
×
2568
            format_detected = sixel_palette_guess_format(buffer,
×
2569
                                                         buffer_size);
2570
            if (format_detected == SIXEL_PALETTE_FORMAT_NONE) {
×
2571
                sixel_helper_set_additional_message(
×
2572
                    "sixel_prepare_specified_palette: "
2573
                    "unable to detect palette format.");
2574
                status = SIXEL_BAD_INPUT;
×
2575
                goto palette_cleanup;
×
2576
            }
2577
            format_final = format_detected;
×
2578
        } else if (format_final == SIXEL_PALETTE_FORMAT_PAL_AUTO) {
×
2579
            format_detected = sixel_palette_guess_format(buffer,
×
2580
                                                         buffer_size);
2581
            if (format_detected == SIXEL_PALETTE_FORMAT_PAL_JASC ||
×
2582
                    format_detected == SIXEL_PALETTE_FORMAT_PAL_RIFF) {
2583
                format_final = format_detected;
×
2584
            } else {
2585
                sixel_helper_set_additional_message(
×
2586
                    "sixel_prepare_specified_palette: "
2587
                    "ambiguous .pal content.");
2588
                status = SIXEL_BAD_INPUT;
×
2589
                goto palette_cleanup;
×
2590
            }
2591
        } else if (need_detection) {
×
2592
            format_detected = sixel_palette_guess_format(buffer,
×
2593
                                                         buffer_size);
2594
            if (format_detected == SIXEL_PALETTE_FORMAT_NONE) {
×
2595
                sixel_helper_set_additional_message(
×
2596
                    "sixel_prepare_specified_palette: "
2597
                    "unable to detect palette format.");
2598
                status = SIXEL_BAD_INPUT;
×
2599
                goto palette_cleanup;
×
2600
            }
2601
            format_final = format_detected;
×
2602
        }
2603

2604
        switch (format_final) {
×
2605
        case SIXEL_PALETTE_FORMAT_ACT:
2606
            status = sixel_palette_parse_act(buffer,
×
2607
                                             buffer_size,
2608
                                             encoder,
2609
                                             dither);
2610
            break;
×
2611
        case SIXEL_PALETTE_FORMAT_PAL_JASC:
2612
            status = sixel_palette_parse_pal_jasc(buffer,
×
2613
                                                  buffer_size,
2614
                                                  encoder,
2615
                                                  dither);
2616
            break;
×
2617
        case SIXEL_PALETTE_FORMAT_PAL_RIFF:
2618
            status = sixel_palette_parse_pal_riff(buffer,
×
2619
                                                  buffer_size,
2620
                                                  encoder,
2621
                                                  dither);
2622
            break;
×
2623
        case SIXEL_PALETTE_FORMAT_GPL:
2624
            status = sixel_palette_parse_gpl(buffer,
×
2625
                                             buffer_size,
2626
                                             encoder,
2627
                                             dither);
2628
            break;
×
2629
        default:
2630
            sixel_helper_set_additional_message(
×
2631
                "sixel_prepare_specified_palette: "
2632
                "unsupported palette format.");
2633
            status = SIXEL_BAD_INPUT;
×
2634
            break;
×
2635
        }
2636

2637
palette_cleanup:
2✔
2638
        if (buffer != NULL) {
5!
2639
            sixel_allocator_free(encoder->allocator, buffer);
×
2640
            buffer = NULL;
×
2641
        }
2642
        if (stream != NULL) {
5!
2643
            sixel_palette_close_stream(stream, close_stream);
×
2644
            stream = NULL;
×
2645
        }
2646
        if (SIXEL_SUCCEEDED(status)) {
5!
2647
            return status;
×
2648
        }
2649
        if (!treat_as_image) {
5!
2650
            return status;
5✔
2651
        }
2652
    }
2653

2654
    callback_context.reqcolors = encoder->reqcolors;
21✔
2655
    callback_context.dither = NULL;
21✔
2656
    callback_context.allocator = encoder->allocator;
21✔
2657
    callback_context.working_colorspace = encoder->working_colorspace;
21✔
2658
    callback_context.lut_policy = encoder->lut_policy;
21✔
2659

2660
    sixel_helper_set_loader_trace(encoder->verbose);
21✔
2661
    sixel_helper_set_thumbnail_size_hint(
21✔
2662
        sixel_encoder_thumbnail_hint(encoder));
7✔
2663
    status = sixel_loader_new(&loader, encoder->allocator);
21✔
2664
    if (SIXEL_FAILED(status)) {
21!
2665
        goto end_loader;
×
2666
    }
2667

2668
    status = sixel_loader_setopt(loader,
21✔
2669
                                 SIXEL_LOADER_OPTION_REQUIRE_STATIC,
2670
                                 &fstatic);
2671
    if (SIXEL_FAILED(status)) {
21!
2672
        goto end_loader;
×
2673
    }
2674

2675
    status = sixel_loader_setopt(loader,
21✔
2676
                                 SIXEL_LOADER_OPTION_USE_PALETTE,
2677
                                 &fuse_palette);
2678
    if (SIXEL_FAILED(status)) {
21!
2679
        goto end_loader;
×
2680
    }
2681

2682
    status = sixel_loader_setopt(loader,
21✔
2683
                                 SIXEL_LOADER_OPTION_REQCOLORS,
2684
                                 &reqcolors);
2685
    if (SIXEL_FAILED(status)) {
21!
2686
        goto end_loader;
×
2687
    }
2688

2689
    status = sixel_loader_setopt(loader,
28✔
2690
                                 SIXEL_LOADER_OPTION_BGCOLOR,
2691
                                 encoder->bgcolor);
21✔
2692
    if (SIXEL_FAILED(status)) {
21!
2693
        goto end_loader;
×
2694
    }
2695

2696
    status = sixel_loader_setopt(loader,
21✔
2697
                                 SIXEL_LOADER_OPTION_LOOP_CONTROL,
2698
                                 &loop_override);
2699
    if (SIXEL_FAILED(status)) {
21!
2700
        goto end_loader;
×
2701
    }
2702

2703
    status = sixel_loader_setopt(loader,
28✔
2704
                                 SIXEL_LOADER_OPTION_INSECURE,
2705
                                 &encoder->finsecure);
21✔
2706
    if (SIXEL_FAILED(status)) {
21!
2707
        goto end_loader;
×
2708
    }
2709

2710
    status = sixel_loader_setopt(loader,
28✔
2711
                                 SIXEL_LOADER_OPTION_CANCEL_FLAG,
2712
                                 encoder->cancel_flag);
21✔
2713
    if (SIXEL_FAILED(status)) {
21!
2714
        goto end_loader;
×
2715
    }
2716

2717
    status = sixel_loader_setopt(loader,
28✔
2718
                                 SIXEL_LOADER_OPTION_LOADER_ORDER,
2719
                                 encoder->loader_order);
21✔
2720
    if (SIXEL_FAILED(status)) {
21!
2721
        goto end_loader;
×
2722
    }
2723

2724
    status = sixel_loader_setopt(loader,
21✔
2725
                                 SIXEL_LOADER_OPTION_CONTEXT,
2726
                                 &callback_context);
2727
    if (SIXEL_FAILED(status)) {
21!
2728
        goto end_loader;
×
2729
    }
2730

2731
    status = sixel_loader_load_file(loader,
28✔
2732
                                    encoder->mapfile,
21✔
2733
                                    load_image_callback_for_palette);
2734
    if (status != SIXEL_OK) {
21!
2735
        goto end_loader;
×
2736
    }
2737

2738
end_loader:
14✔
2739
    sixel_loader_unref(loader);
21✔
2740

2741
    if (status != SIXEL_OK) {
21!
2742
        return status;
×
2743
    }
2744

2745
    if (! callback_context.dither) {
21!
2746
        sixel_helper_set_additional_message(
×
2747
            "sixel_prepare_specified_palette() failed.\n"
2748
            "reason: mapfile is empty.");
2749
        return SIXEL_BAD_INPUT;
×
2750
    }
2751

2752
    *dither = callback_context.dither;
21✔
2753

2754
    return status;
21✔
2755
}
10✔
2756

2757

2758
/* create dither object from a frame */
2759
static SIXELSTATUS
2760
sixel_encoder_prepare_palette(
521✔
2761
    sixel_encoder_t *encoder,  /* encoder object */
2762
    sixel_frame_t   *frame,    /* input frame object */
2763
    sixel_dither_t  **dither)  /* dither object to be created from the frame */
2764
{
2765
    SIXELSTATUS status = SIXEL_FALSE;
521✔
2766
    int histogram_colors;
2767
    sixel_assessment_t *assessment;
2768
    int promoted_stage;
2769

2770
    assessment = NULL;
521✔
2771
    promoted_stage = 0;
521✔
2772
    if (encoder != NULL) {
521!
2773
        assessment = encoder->assessment_observer;
521✔
2774
    }
181✔
2775

2776
    switch (encoder->color_option) {
521!
2777
    case SIXEL_COLOR_OPTION_HIGHCOLOR:
24✔
2778
        if (encoder->dither_cache) {
36!
2779
            *dither = encoder->dither_cache;
×
2780
            status = SIXEL_OK;
×
2781
        } else {
2782
            status = sixel_dither_new(dither, (-1), encoder->allocator);
36✔
2783
            sixel_dither_set_pixelformat(*dither, sixel_frame_get_pixelformat(frame));
36✔
2784
        }
2785
        goto end;
36✔
2786
    case SIXEL_COLOR_OPTION_MONOCHROME:
8✔
2787
        if (encoder->dither_cache) {
12!
2788
            *dither = encoder->dither_cache;
×
2789
            status = SIXEL_OK;
×
2790
        } else {
2791
            status = sixel_prepare_monochrome_palette(dither, encoder->finvert);
12✔
2792
        }
2793
        goto end;
12✔
2794
    case SIXEL_COLOR_OPTION_MAPFILE:
16✔
2795
        if (encoder->dither_cache) {
26!
2796
            *dither = encoder->dither_cache;
×
2797
            status = SIXEL_OK;
×
2798
        } else {
2799
            status = sixel_prepare_specified_palette(dither, encoder);
26✔
2800
        }
2801
        goto end;
26✔
2802
    case SIXEL_COLOR_OPTION_BUILTIN:
18✔
2803
        if (encoder->dither_cache) {
27!
2804
            *dither = encoder->dither_cache;
×
2805
            status = SIXEL_OK;
×
2806
        } else {
2807
            status = sixel_prepare_builtin_palette(dither, encoder->builtin_palette);
27✔
2808
        }
2809
        goto end;
27✔
2810
    case SIXEL_COLOR_OPTION_DEFAULT:
420✔
2811
    default:
2812
        break;
420✔
2813
    }
2814

2815
    if (sixel_frame_get_pixelformat(frame) & SIXEL_FORMATTYPE_PALETTE) {
420✔
2816
        if (!sixel_frame_get_palette(frame)) {
191!
2817
            status = SIXEL_LOGIC_ERROR;
×
2818
            goto end;
×
2819
        }
2820
        status = sixel_dither_new(dither, sixel_frame_get_ncolors(frame),
230✔
2821
                                  encoder->allocator);
39✔
2822
        if (SIXEL_FAILED(status)) {
191!
2823
            goto end;
×
2824
        }
2825
        sixel_dither_set_palette(*dither, sixel_frame_get_palette(frame));
191✔
2826
        sixel_dither_set_pixelformat(*dither, sixel_frame_get_pixelformat(frame));
191✔
2827
        if (sixel_frame_get_transparent(frame) != (-1)) {
191!
2828
            sixel_dither_set_transparent(*dither, sixel_frame_get_transparent(frame));
×
2829
        }
2830
        if (*dither && encoder->dither_cache) {
191!
2831
            sixel_dither_unref(encoder->dither_cache);
×
2832
        }
2833
        goto end;
191✔
2834
    }
2835

2836
    if (sixel_frame_get_pixelformat(frame) & SIXEL_FORMATTYPE_GRAYSCALE) {
229!
2837
        switch (sixel_frame_get_pixelformat(frame)) {
×
2838
        case SIXEL_PIXELFORMAT_G1:
2839
            *dither = sixel_dither_get(SIXEL_BUILTIN_G1);
×
2840
            break;
×
2841
        case SIXEL_PIXELFORMAT_G2:
2842
            *dither = sixel_dither_get(SIXEL_BUILTIN_G2);
×
2843
            break;
×
2844
        case SIXEL_PIXELFORMAT_G4:
2845
            *dither = sixel_dither_get(SIXEL_BUILTIN_G4);
×
2846
            break;
×
2847
        case SIXEL_PIXELFORMAT_G8:
2848
            *dither = sixel_dither_get(SIXEL_BUILTIN_G8);
×
2849
            break;
×
2850
        default:
2851
            *dither = NULL;
×
2852
            status = SIXEL_LOGIC_ERROR;
×
2853
            goto end;
×
2854
        }
2855
        if (*dither && encoder->dither_cache) {
×
2856
            sixel_dither_unref(encoder->dither_cache);
×
2857
        }
2858
        sixel_dither_set_pixelformat(*dither, sixel_frame_get_pixelformat(frame));
×
2859
        status = SIXEL_OK;
×
2860
        goto end;
×
2861
    }
2862

2863
    if (encoder->dither_cache) {
229!
2864
        sixel_dither_unref(encoder->dither_cache);
×
2865
    }
2866
    status = sixel_dither_new(dither, encoder->reqcolors, encoder->allocator);
229✔
2867
    if (SIXEL_FAILED(status)) {
229!
2868
        goto end;
×
2869
    }
2870

2871
    sixel_dither_set_lut_policy(*dither, encoder->lut_policy);
229✔
2872

2873
    status = sixel_dither_initialize(*dither,
336✔
2874
                                     sixel_frame_get_pixels(frame),
107✔
2875
                                     sixel_frame_get_width(frame),
107✔
2876
                                     sixel_frame_get_height(frame),
107✔
2877
                                     sixel_frame_get_pixelformat(frame),
107✔
2878
                                     encoder->method_for_largest,
107✔
2879
                                     encoder->method_for_rep,
107✔
2880
                                     encoder->quality_mode);
107✔
2881
    if (SIXEL_FAILED(status)) {
229!
2882
        sixel_dither_unref(*dither);
×
2883
        goto end;
×
2884
    }
2885

2886
    if (assessment != NULL && promoted_stage == 0) {
229!
2887
        sixel_assessment_stage_transition(
×
2888
            assessment,
2889
            SIXEL_ASSESSMENT_STAGE_PALETTE_SOLVE);
2890
        promoted_stage = 1;
×
2891
    }
2892

2893
    histogram_colors = sixel_dither_get_num_of_histogram_colors(*dither);
229✔
2894
    if (histogram_colors <= encoder->reqcolors) {
229✔
2895
        encoder->method_for_diffuse = SIXEL_DIFFUSE_NONE;
172✔
2896
    }
88✔
2897
    sixel_dither_set_pixelformat(*dither, sixel_frame_get_pixelformat(frame));
229✔
2898

2899
    status = SIXEL_OK;
229✔
2900

2901
end:
340✔
2902
    if (assessment != NULL && promoted_stage == 0) {
521!
2903
        sixel_assessment_stage_transition(
×
2904
            assessment,
2905
            SIXEL_ASSESSMENT_STAGE_PALETTE_SOLVE);
2906
        promoted_stage = 1;
×
2907
    }
2908
    if (SIXEL_SUCCEEDED(status) && dither != NULL && *dither != NULL) {
521!
2909
        sixel_dither_set_lut_policy(*dither, encoder->lut_policy);
516✔
2910
        /* pass down the user's demand for an exact palette size */
2911
        (*dither)->force_palette = encoder->force_palette;
516✔
2912
    }
178✔
2913
    return status;
521✔
2914
}
2915

2916

2917
/* resize a frame with settings of specified encoder object */
2918
static SIXELSTATUS
2919
sixel_encoder_do_resize(
528✔
2920
    sixel_encoder_t /* in */    *encoder,   /* encoder object */
2921
    sixel_frame_t   /* in */    *frame)     /* frame object to be resized */
2922
{
2923
    SIXELSTATUS status = SIXEL_FALSE;
528✔
2924
    int src_width;
2925
    int src_height;
2926
    int dst_width;
2927
    int dst_height;
2928

2929
    /* get frame width and height */
2930
    src_width = sixel_frame_get_width(frame);
528✔
2931
    src_height = sixel_frame_get_height(frame);
528✔
2932

2933
    if (src_width < 1) {
528✔
2934
         sixel_helper_set_additional_message(
6✔
2935
             "sixel_encoder_do_resize: "
2936
             "detected a frame with a non-positive width.");
2937
        return SIXEL_BAD_ARGUMENT;
6✔
2938
    }
2939

2940
    if (src_height < 1) {
522!
2941
         sixel_helper_set_additional_message(
×
2942
             "sixel_encoder_do_resize: "
2943
             "detected a frame with a non-positive height.");
2944
        return SIXEL_BAD_ARGUMENT;
×
2945
    }
2946

2947
    /* settings around scaling */
2948
    dst_width = encoder->pixelwidth;    /* may be -1 (default) */
522✔
2949
    dst_height = encoder->pixelheight;  /* may be -1 (default) */
522✔
2950

2951
    /* if the encoder has percentwidth or percentheight property,
2952
       convert them to pixelwidth / pixelheight */
2953
    if (encoder->percentwidth > 0) {
522✔
2954
        dst_width = src_width * encoder->percentwidth / 100;
12✔
2955
    }
4✔
2956
    if (encoder->percentheight > 0) {
522✔
2957
        dst_height = src_height * encoder->percentheight / 100;
9✔
2958
    }
3✔
2959

2960
    /* if only either width or height is set, set also the other
2961
       to retain frame aspect ratio */
2962
    if (dst_width > 0 && dst_height <= 0) {
522✔
2963
        dst_height = src_height * dst_width / src_width;
42✔
2964
    }
14✔
2965
    if (dst_height > 0 && dst_width <= 0) {
522✔
2966
        dst_width = src_width * dst_height / src_height;
33✔
2967
    }
11✔
2968

2969
    /* do resize */
2970
    if (dst_width > 0 && dst_height > 0) {
522!
2971
        status = sixel_frame_resize(frame, dst_width, dst_height,
124✔
2972
                                    encoder->method_for_resampling);
31✔
2973
        if (SIXEL_FAILED(status)) {
93!
2974
            goto end;
×
2975
        }
2976
    }
31✔
2977

2978
    /* success */
2979
    status = SIXEL_OK;
522✔
2980

2981
end:
340✔
2982
    return status;
522✔
2983
}
184✔
2984

2985

2986
/* clip a frame with settings of specified encoder object */
2987
static SIXELSTATUS
2988
sixel_encoder_do_clip(
524✔
2989
    sixel_encoder_t /* in */    *encoder,   /* encoder object */
2990
    sixel_frame_t   /* in */    *frame)     /* frame object to be resized */
2991
{
2992
    SIXELSTATUS status = SIXEL_FALSE;
524✔
2993
    int src_width;
2994
    int src_height;
2995
    int clip_x;
2996
    int clip_y;
2997
    int clip_w;
2998
    int clip_h;
2999

3000
    /* get frame width and height */
3001
    src_width = sixel_frame_get_width(frame);
524✔
3002
    src_height = sixel_frame_get_height(frame);
524✔
3003

3004
    /* settings around clipping */
3005
    clip_x = encoder->clipx;
524✔
3006
    clip_y = encoder->clipy;
524✔
3007
    clip_w = encoder->clipwidth;
524✔
3008
    clip_h = encoder->clipheight;
524✔
3009

3010
    /* adjust clipping width with comparing it to frame width */
3011
    if (clip_w + clip_x > src_width) {
524✔
3012
        if (clip_x > src_width) {
7✔
3013
            clip_w = 0;
3✔
3014
        } else {
1✔
3015
            clip_w = src_width - clip_x;
4✔
3016
        }
3017
    }
3✔
3018

3019
    /* adjust clipping height with comparing it to frame height */
3020
    if (clip_h + clip_y > src_height) {
524✔
3021
        if (clip_y > src_height) {
6✔
3022
            clip_h = 0;
3✔
3023
        } else {
1✔
3024
            clip_h = src_height - clip_y;
3✔
3025
        }
3026
    }
2✔
3027

3028
    /* do clipping */
3029
    if (clip_w > 0 && clip_h > 0) {
524!
3030
        status = sixel_frame_clip(frame, clip_x, clip_y, clip_w, clip_h);
15✔
3031
        if (SIXEL_FAILED(status)) {
15!
3032
            goto end;
3✔
3033
        }
3034
    }
4✔
3035

3036
    /* success */
3037
    status = SIXEL_OK;
521✔
3038

3039
end:
340✔
3040
    return status;
524✔
3041
}
3042

3043

3044
static void
3045
sixel_debug_print_palette(
3✔
3046
    sixel_dither_t /* in */ *dither /* dithering object */
3047
)
3048
{
3049
    unsigned char *palette;
3050
    int i;
3051

3052
    palette = sixel_dither_get_palette(dither);
3✔
3053
    fprintf(stderr, "palette:\n");
3✔
3054
    for (i = 0; i < sixel_dither_get_num_of_palette_colors(dither); ++i) {
51✔
3055
        fprintf(stderr, "%d: #%02x%02x%02x\n", i,
64✔
3056
                palette[i * 3 + 0],
48✔
3057
                palette[i * 3 + 1],
48✔
3058
                palette[i * 3 + 2]);
48✔
3059
    }
16✔
3060
}
3✔
3061

3062

3063
static SIXELSTATUS
3064
sixel_encoder_output_without_macro(
423✔
3065
    sixel_frame_t       /* in */ *frame,
3066
    sixel_dither_t      /* in */ *dither,
3067
    sixel_output_t      /* in */ *output,
3068
    sixel_encoder_t     /* in */ *encoder)
3069
{
3070
    SIXELSTATUS status = SIXEL_OK;
423✔
3071
    static unsigned char *p;
3072
    int depth;
3073
    enum { message_buffer_size = 2048 };
3074
    char message[message_buffer_size];
3075
    int nwrite;
3076
#if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3077
    int dulation;
3078
    int delay;
3079
    struct timespec tv;
3080
#endif
3081
    unsigned char *pixbuf;
3082
    int width;
3083
    int height;
3084
    int pixelformat = 0;
423✔
3085
    size_t size;
3086
    int frame_colorspace = SIXEL_COLORSPACE_GAMMA;
423✔
3087
    palette_conversion_t palette_ctx;
3088

3089
    memset(&palette_ctx, 0, sizeof(palette_ctx));
423✔
3090
#if defined(HAVE_CLOCK) || defined(HAVE_CLOCK_WIN)
3091
    sixel_clock_t last_clock;
3092
#endif
3093

3094
    if (encoder == NULL) {
423!
3095
        sixel_helper_set_additional_message(
×
3096
            "sixel_encoder_output_without_macro: encoder object is null.");
3097
        status = SIXEL_BAD_ARGUMENT;
×
3098
        goto end;
×
3099
    }
3100

3101
    if (encoder->assessment_observer != NULL) {
423!
3102
        sixel_assessment_stage_transition(
×
3103
            encoder->assessment_observer,
×
3104
            SIXEL_ASSESSMENT_STAGE_PALETTE_APPLY);
3105
    }
3106

3107
    if (encoder->color_option == SIXEL_COLOR_OPTION_DEFAULT) {
423✔
3108
        if (encoder->force_palette) {
327!
3109
            /* keep every slot when the user forced the palette size */
3110
            sixel_dither_set_optimize_palette(dither, 0);
×
3111
        } else {
3112
            sixel_dither_set_optimize_palette(dither, 1);
327✔
3113
        }
3114
    }
109✔
3115

3116
    pixelformat = sixel_frame_get_pixelformat(frame);
423✔
3117
    frame_colorspace = sixel_frame_get_colorspace(frame);
423✔
3118
    output->pixelformat = pixelformat;
423✔
3119
    output->source_colorspace = frame_colorspace;
423✔
3120
    output->colorspace = encoder->output_colorspace;
423✔
3121
    sixel_dither_set_pixelformat(dither, pixelformat);
423✔
3122
    depth = sixel_helper_compute_depth(pixelformat);
423✔
3123
    if (depth < 0) {
423!
3124
        status = SIXEL_LOGIC_ERROR;
×
3125
        nwrite = sixel_compat_snprintf(
×
3126
            message,
3127
            sizeof(message),
3128
            "sixel_encoder_output_without_macro: "
3129
            "sixel_helper_compute_depth(%08x) failed.",
3130
            pixelformat);
3131
        if (nwrite > 0) {
×
3132
            sixel_helper_set_additional_message(message);
×
3133
        }
3134
        goto end;
×
3135
    }
3136

3137
    width = sixel_frame_get_width(frame);
423✔
3138
    height = sixel_frame_get_height(frame);
423✔
3139
    size = (size_t)(width * height * depth);
423✔
3140
    p = (unsigned char *)sixel_allocator_malloc(encoder->allocator, size);
423✔
3141
    if (p == NULL) {
423!
3142
        sixel_helper_set_additional_message(
×
3143
            "sixel_encoder_output_without_macro: sixel_allocator_malloc() failed.");
3144
        status = SIXEL_BAD_ALLOCATION;
×
3145
        goto end;
×
3146
    }
3147
#if defined(HAVE_CLOCK)
3148
    if (output->last_clock == 0) {
423!
3149
        output->last_clock = clock();
423✔
3150
    }
141✔
3151
#elif defined(HAVE_CLOCK_WIN)
3152
    if (output->last_clock == 0) {
3153
        output->last_clock = clock_win();
3154
    }
3155
#endif
3156
#if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3157
    delay = sixel_frame_get_delay(frame);
423✔
3158
    if (delay > 0 && !encoder->fignore_delay) {
423✔
3159
# if defined(HAVE_CLOCK)
3160
        last_clock = clock();
80✔
3161
/* https://stackoverflow.com/questions/16697005/clock-and-clocks-per-sec-on-osx-10-7 */
3162
#  if defined(__APPLE__)
3163
        dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
156✔
3164
                          / 100000);
78✔
3165
#  else
3166
        dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
2✔
3167
                          / CLOCKS_PER_SEC);
3168
#  endif
3169
        output->last_clock = last_clock;
80✔
3170
# elif defined(HAVE_CLOCK_WIN)
3171
        last_clock = clock_win();
3172
        dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
3173
                          / CLOCKS_PER_SEC);
3174
        output->last_clock = last_clock;
3175
# else
3176
        dulation = 0;
3177
# endif
3178
        if (dulation < 1000 * 10 * delay) {
80!
3179
# if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3180
            tv.tv_sec = 0;
80✔
3181
            tv.tv_nsec = (long)((1000 * 10 * delay - dulation) * 1000);
80✔
3182
#  if defined(HAVE_NANOSLEEP)
3183
            nanosleep(&tv, NULL);
80✔
3184
#  else
3185
            nanosleep_win(&tv, NULL);
3186
#  endif
3187
# endif
3188
        }
78✔
3189
    }
78✔
3190
#endif
3191

3192
    pixbuf = sixel_frame_get_pixels(frame);
423✔
3193
    memcpy(p, pixbuf, (size_t)(width * height * depth));
423✔
3194

3195
    status = sixel_output_convert_colorspace(output, p, size);
423✔
3196
    if (SIXEL_FAILED(status)) {
423!
3197
        goto end;
×
3198
    }
3199

3200
    if (encoder->cancel_flag && *encoder->cancel_flag) {
423!
3201
        goto end;
×
3202
    }
3203

3204
    status = sixel_encoder_convert_palette(encoder,
564✔
3205
                                           output,
141✔
3206
                                           dither,
141✔
3207
                                           frame_colorspace,
141✔
3208
                                           pixelformat,
141✔
3209
                                           &palette_ctx);
3210
    if (SIXEL_FAILED(status)) {
423!
3211
        goto end;
×
3212
    }
3213

3214
    if (encoder->capture_quantized) {
423!
3215
        status = sixel_encoder_capture_quantized(encoder,
×
3216
                                                 dither,
3217
                                                 p,
3218
                                                 size,
3219
                                                 width,
3220
                                                 height,
3221
                                                 pixelformat,
3222
                                                 output->colorspace);
3223
        if (SIXEL_FAILED(status)) {
×
3224
            goto end;
×
3225
        }
3226
    }
3227

3228
    if (encoder->assessment_observer != NULL) {
423!
3229
        sixel_assessment_stage_transition(
×
3230
            encoder->assessment_observer,
×
3231
            SIXEL_ASSESSMENT_STAGE_ENCODE);
3232
    }
3233
    status = sixel_encode(p, width, height, depth, dither, output);
423✔
3234
    if (encoder->assessment_observer != NULL) {
423!
3235
        sixel_assessment_stage_finish(encoder->assessment_observer);
×
3236
    }
3237
    if (status != SIXEL_OK) {
423!
3238
        goto end;
×
3239
    }
3240

3241
end:
282✔
3242
    sixel_encoder_restore_palette(encoder, dither, &palette_ctx);
423✔
3243
    output->pixelformat = pixelformat;
423✔
3244
    output->source_colorspace = frame_colorspace;
423✔
3245
    sixel_allocator_free(encoder->allocator, p);
423✔
3246

3247
    return status;
423✔
3248
}
3249

3250

3251
static SIXELSTATUS
3252
sixel_encoder_output_with_macro(
93✔
3253
    sixel_frame_t   /* in */ *frame,
3254
    sixel_dither_t  /* in */ *dither,
3255
    sixel_output_t  /* in */ *output,
3256
    sixel_encoder_t /* in */ *encoder)
3257
{
3258
    SIXELSTATUS status = SIXEL_OK;
93✔
3259
    enum { message_buffer_size = 256 };
3260
    char buffer[message_buffer_size];
3261
    int nwrite;
3262
#if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3263
    int dulation;
3264
    struct timespec tv;
3265
#endif
3266
    int width;
3267
    int height;
3268
    int pixelformat;
3269
    int depth;
3270
    size_t size = 0;
93✔
3271
    int frame_colorspace = SIXEL_COLORSPACE_GAMMA;
93✔
3272
    unsigned char *converted = NULL;
93✔
3273
    palette_conversion_t palette_ctx;
3274
#if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3275
    int delay;
3276
#endif
3277
#if defined(HAVE_CLOCK) || defined(HAVE_CLOCK_WIN)
3278
    sixel_clock_t last_clock;
3279
#endif
3280
    double write_started_at;
3281
    double write_finished_at;
3282
    double write_duration;
3283

3284
    memset(&palette_ctx, 0, sizeof(palette_ctx));
93✔
3285

3286
    if (encoder != NULL && encoder->assessment_observer != NULL) {
93!
3287
        sixel_assessment_stage_transition(
×
3288
            encoder->assessment_observer,
×
3289
            SIXEL_ASSESSMENT_STAGE_PALETTE_APPLY);
3290
    }
3291

3292
#if defined(HAVE_CLOCK)
3293
    if (output->last_clock == 0) {
93!
3294
        output->last_clock = clock();
93✔
3295
    }
37✔
3296
#elif defined(HAVE_CLOCK_WIN)
3297
    if (output->last_clock == 0) {
3298
        output->last_clock = clock_win();
3299
    }
3300
#endif
3301

3302
    width = sixel_frame_get_width(frame);
93✔
3303
    height = sixel_frame_get_height(frame);
93✔
3304
    pixelformat = sixel_frame_get_pixelformat(frame);
93✔
3305
    depth = sixel_helper_compute_depth(pixelformat);
93✔
3306
    if (depth < 0) {
93!
3307
        status = SIXEL_LOGIC_ERROR;
×
3308
        sixel_helper_set_additional_message(
×
3309
            "sixel_encoder_output_with_macro: "
3310
            "sixel_helper_compute_depth() failed.");
3311
        goto end;
×
3312
    }
3313

3314
    frame_colorspace = sixel_frame_get_colorspace(frame);
93✔
3315
    size = (size_t)width * (size_t)height * (size_t)depth;
93✔
3316
    converted = (unsigned char *)sixel_allocator_malloc(
93✔
3317
        encoder->allocator, size);
37✔
3318
    if (converted == NULL) {
93!
3319
        sixel_helper_set_additional_message(
×
3320
            "sixel_encoder_output_with_macro: "
3321
            "sixel_allocator_malloc() failed.");
3322
        status = SIXEL_BAD_ALLOCATION;
×
3323
        goto end;
×
3324
    }
3325

3326
    memcpy(converted, sixel_frame_get_pixels(frame), size);
93✔
3327
    output->pixelformat = pixelformat;
93✔
3328
    output->source_colorspace = frame_colorspace;
93✔
3329
    output->colorspace = encoder->output_colorspace;
93✔
3330
    status = sixel_output_convert_colorspace(output, converted, size);
93✔
3331
    if (SIXEL_FAILED(status)) {
93!
3332
        goto end;
×
3333
    }
3334

3335
    status = sixel_encoder_convert_palette(encoder,
130✔
3336
                                           output,
37✔
3337
                                           dither,
37✔
3338
                                           frame_colorspace,
37✔
3339
                                           pixelformat,
37✔
3340
                                           &palette_ctx);
3341
    if (SIXEL_FAILED(status)) {
93!
3342
        goto end;
×
3343
    }
3344

3345
    if (sixel_frame_get_loop_no(frame) == 0) {
93✔
3346
        if (encoder->macro_number >= 0) {
55!
3347
            nwrite = sixel_compat_snprintf(
1✔
3348
                buffer,
1✔
3349
                sizeof(buffer),
3350
                "\033P%d;0;1!z",
3351
                encoder->macro_number);
1✔
3352
        } else {
1✔
3353
            nwrite = sixel_compat_snprintf(
54✔
3354
                buffer,
18✔
3355
                sizeof(buffer),
3356
                "\033P%d;0;1!z",
3357
                sixel_frame_get_frame_no(frame));
18✔
3358
        }
3359
        if (nwrite < 0) {
55!
3360
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3361
            sixel_helper_set_additional_message(
×
3362
                "sixel_encoder_output_with_macro: command format failed.");
3363
            goto end;
×
3364
        }
3365
        write_started_at = 0.0;
55✔
3366
        write_finished_at = 0.0;
55✔
3367
        write_duration = 0.0;
55✔
3368
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3369
            write_started_at = sixel_assessment_timer_now();
×
3370
        }
3371
        nwrite = sixel_write_callback(buffer,
74✔
3372
                                      (int)strlen(buffer),
55✔
3373
                                      &encoder->outfd);
55✔
3374
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3375
            write_finished_at = sixel_assessment_timer_now();
×
3376
            write_duration = write_finished_at - write_started_at;
×
3377
            if (write_duration < 0.0) {
×
3378
                write_duration = 0.0;
×
3379
            }
3380
        }
3381
        if (nwrite < 0) {
55!
3382
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3383
            sixel_helper_set_additional_message(
×
3384
                "sixel_encoder_output_with_macro: "
3385
                "sixel_write_callback() failed.");
3386
            goto end;
×
3387
        }
3388
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3389
            sixel_assessment_record_output_write(
×
3390
                encoder->assessment_observer,
×
3391
                (size_t)nwrite,
3392
                write_duration);
3393
        }
3394

3395
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3396
            sixel_assessment_stage_transition(
×
3397
                encoder->assessment_observer,
×
3398
                SIXEL_ASSESSMENT_STAGE_ENCODE);
3399
        }
3400
        status = sixel_encode(converted,
74✔
3401
                              width,
19✔
3402
                              height,
19✔
3403
                              depth,
19✔
3404
                              dither,
19✔
3405
                              output);
19✔
3406
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3407
            sixel_assessment_stage_finish(
×
3408
                encoder->assessment_observer);
×
3409
        }
3410
        if (SIXEL_FAILED(status)) {
55!
3411
            goto end;
×
3412
        }
3413

3414
        write_started_at = 0.0;
55✔
3415
        write_finished_at = 0.0;
55✔
3416
        write_duration = 0.0;
55✔
3417
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3418
            write_started_at = sixel_assessment_timer_now();
×
3419
        }
3420
        nwrite = sixel_write_callback("\033\\", 2, &encoder->outfd);
55✔
3421
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3422
            write_finished_at = sixel_assessment_timer_now();
×
3423
            write_duration = write_finished_at - write_started_at;
×
3424
            if (write_duration < 0.0) {
×
3425
                write_duration = 0.0;
×
3426
            }
3427
        }
3428
        if (nwrite < 0) {
55!
3429
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3430
            sixel_helper_set_additional_message(
×
3431
                "sixel_encoder_output_with_macro: "
3432
                "sixel_write_callback() failed.");
3433
            goto end;
×
3434
        }
3435
        if (encoder != NULL && encoder->assessment_observer != NULL) {
55!
3436
            sixel_assessment_record_output_write(
×
3437
                encoder->assessment_observer,
×
3438
                (size_t)nwrite,
3439
                write_duration);
3440
        }
3441
    }
19✔
3442
    if (encoder->macro_number < 0) {
129✔
3443
        nwrite = sixel_compat_snprintf(
90✔
3444
            buffer,
36✔
3445
            sizeof(buffer),
3446
            "\033[%d*z",
3447
            sixel_frame_get_frame_no(frame));
36✔
3448
        if (nwrite < 0) {
90!
3449
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3450
            sixel_helper_set_additional_message(
×
3451
                "sixel_encoder_output_with_macro: command format failed.");
3452
        }
3453
        write_started_at = 0.0;
90✔
3454
        write_finished_at = 0.0;
90✔
3455
        write_duration = 0.0;
90✔
3456
        if (encoder != NULL && encoder->assessment_observer != NULL) {
90!
3457
            write_started_at = sixel_assessment_timer_now();
×
3458
        }
3459
        nwrite = sixel_write_callback(buffer,
126✔
3460
                                      (int)strlen(buffer),
90✔
3461
                                      &encoder->outfd);
90✔
3462
        if (encoder != NULL && encoder->assessment_observer != NULL) {
90!
3463
            write_finished_at = sixel_assessment_timer_now();
×
3464
            write_duration = write_finished_at - write_started_at;
×
3465
            if (write_duration < 0.0) {
×
3466
                write_duration = 0.0;
×
3467
            }
3468
        }
3469
        if (nwrite < 0) {
90!
3470
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3471
            sixel_helper_set_additional_message(
×
3472
                "sixel_encoder_output_with_macro: "
3473
                "sixel_write_callback() failed.");
3474
            goto end;
×
3475
        }
3476
        if (encoder != NULL && encoder->assessment_observer != NULL) {
90!
3477
            sixel_assessment_record_output_write(
×
3478
                encoder->assessment_observer,
×
3479
                (size_t)nwrite,
3480
                write_duration);
3481
        }
3482
#if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3483
        delay = sixel_frame_get_delay(frame);
90✔
3484
        if (delay > 0 && !encoder->fignore_delay) {
90!
3485
# if defined(HAVE_CLOCK)
3486
            last_clock = clock();
63✔
3487
/* https://stackoverflow.com/questions/16697005/clock-and-clocks-per-sec-on-osx-10-7 */
3488
#  if defined(__APPLE__)
3489
            dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
54✔
3490
                             / 100000);
27✔
3491
#  else
3492
            dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
36✔
3493
                             / CLOCKS_PER_SEC);
3494
#  endif
3495
            output->last_clock = last_clock;
63✔
3496
# elif defined(HAVE_CLOCK_WIN)
3497
            last_clock = clock_win();
3498
            dulation = (int)((last_clock - output->last_clock) * 1000 * 1000
3499
                             / CLOCKS_PER_SEC);
3500
            output->last_clock = last_clock;
3501
# else
3502
            dulation = 0;
3503
# endif
3504
            if (dulation < 1000 * 10 * delay) {
63!
3505
# if defined(HAVE_NANOSLEEP) || defined(HAVE_NANOSLEEP_WIN)
3506
                tv.tv_sec = 0;
60✔
3507
                tv.tv_nsec = (long)((1000 * 10 * delay - dulation) * 1000);
60✔
3508
#  if defined(HAVE_NANOSLEEP)
3509
                nanosleep(&tv, NULL);
60✔
3510
#  else
3511
                nanosleep_win(&tv, NULL);
3512
#  endif
3513
# endif
3514
            }
24✔
3515
        }
27✔
3516
#endif
3517
    }
36✔
3518

3519
end:
20✔
3520
    sixel_encoder_restore_palette(encoder, dither, &palette_ctx);
93✔
3521
    output->pixelformat = pixelformat;
93✔
3522
    output->source_colorspace = frame_colorspace;
93✔
3523
    sixel_allocator_free(encoder->allocator, converted);
93✔
3524

3525
    return status;
93✔
3526
}
3527

3528

3529
static SIXELSTATUS
3530
sixel_encoder_emit_iso2022_chars(
×
3531
    sixel_encoder_t *encoder,
3532
    sixel_frame_t *frame
3533
)
3534
{
3535
    char *buf_p, *buf;
3536
    int col, row;
3537
    int charset;
3538
    int is_96cs;
3539
    unsigned int charset_no;
3540
    unsigned int code;
3541
    int num_cols, num_rows;
3542
    SIXELSTATUS status;
3543
    size_t alloc_size;
3544
    int nwrite;
3545
    int target_fd;
3546
    int chunk_size;
3547

3548
    charset_no = encoder->drcs_charset_no;
×
3549
    if (charset_no == 0u) {
×
3550
        charset_no = 1u;
×
3551
    }
3552
    if (encoder->drcs_mmv == 0) {
×
3553
        is_96cs = (charset_no > 63u) ? 1 : 0;
×
3554
        charset = (int)(((charset_no - 1u) % 63u) + 0x40u);
×
3555
    } else if (encoder->drcs_mmv == 1) {
×
3556
        is_96cs = 0;
×
3557
        charset = (int)(charset_no + 0x3fu);
×
3558
    } else {
3559
        is_96cs = (charset_no > 79u) ? 1 : 0;
×
3560
        charset = (int)(((charset_no - 1u) % 79u) + 0x30u);
×
3561
    }
3562
    code = 0x100020 + (is_96cs ? 0x80 : 0) + charset * 0x100;
×
3563
    num_cols = (sixel_frame_get_width(frame) + encoder->cell_width - 1)
×
3564
             / encoder->cell_width;
×
3565
    num_rows = (sixel_frame_get_height(frame) + encoder->cell_height - 1)
×
3566
             / encoder->cell_height;
×
3567

3568
    /* cols x rows + designation(4 chars) + SI + SO + LFs */
3569
    alloc_size = num_cols * num_rows + (num_cols * num_rows / 96 + 1) * 4 + 2 + num_rows;
×
3570
    buf_p = buf = sixel_allocator_malloc(encoder->allocator, alloc_size);
×
3571
    if (buf == NULL) {
×
3572
        sixel_helper_set_additional_message(
×
3573
            "sixel_encoder_emit_iso2022_chars: sixel_allocator_malloc() failed.");
3574
        status = SIXEL_BAD_ALLOCATION;
×
3575
        goto end;
×
3576
    }
3577

3578
    code = 0x20;
×
3579
    *(buf_p++) = '\016';  /* SI */
×
3580
    *(buf_p++) = '\033';
×
3581
    *(buf_p++) = ')';
×
3582
    *(buf_p++) = ' ';
×
3583
    *(buf_p++) = charset;
×
3584
    for(row = 0; row < num_rows; row++) {
×
3585
        for(col = 0; col < num_cols; col++) {
×
3586
            if ((code & 0x7f) == 0x0) {
×
3587
                if (charset == 0x7e) {
×
3588
                    is_96cs = 1 - is_96cs;
×
3589
                    charset = '0';
×
3590
                } else {
3591
                    charset++;
×
3592
                }
3593
                code = 0x20;
×
3594
                *(buf_p++) = '\033';
×
3595
                *(buf_p++) = is_96cs ? '-': ')';
×
3596
                *(buf_p++) = ' ';
×
3597
                *(buf_p++) = charset;
×
3598
            }
3599
            *(buf_p++) = code++;
×
3600
        }
3601
        *(buf_p++) = '\n';
×
3602
    }
3603
    *(buf_p++) = '\017';  /* SO */
×
3604

3605
    if (encoder->tile_outfd >= 0) {
×
3606
        target_fd = encoder->tile_outfd;
×
3607
    } else {
3608
        target_fd = encoder->outfd;
×
3609
    }
3610

3611
    chunk_size = (int)(buf_p - buf);
×
3612
    nwrite = sixel_encoder_probe_fd_write(encoder,
×
3613
                                          buf,
3614
                                          chunk_size,
3615
                                          target_fd);
3616
    if (nwrite != chunk_size) {
×
3617
        sixel_helper_set_additional_message(
×
3618
            "sixel_encoder_emit_iso2022_chars: write() failed.");
3619
        status = SIXEL_RUNTIME_ERROR;
×
3620
        goto end;
×
3621
    }
3622

3623
    sixel_allocator_free(encoder->allocator, buf);
×
3624

3625
    status = SIXEL_OK;
×
3626

3627
end:
3628
    return status;
×
3629
}
3630

3631

3632
/*
3633
 * This routine is derived from mlterm's drcssixel.c
3634
 * (https://raw.githubusercontent.com/arakiken/mlterm/master/drcssixel/drcssixel.c).
3635
 * The original implementation is credited to Araki Ken.
3636
 * Adjusted here to integrate with libsixel's encoder pipeline.
3637
 */
3638
static SIXELSTATUS
3639
sixel_encoder_emit_drcsmmv2_chars(
×
3640
    sixel_encoder_t *encoder,
3641
    sixel_frame_t *frame
3642
)
3643
{
3644
    char *buf_p, *buf;
3645
    int col, row;
3646
    int charset;
3647
    int is_96cs;
3648
    unsigned int charset_no;
3649
    unsigned int code;
3650
    int num_cols, num_rows;
3651
    SIXELSTATUS status;
3652
    size_t alloc_size;
3653
    int nwrite;
3654
    int target_fd;
3655
    int chunk_size;
3656

3657
    charset_no = encoder->drcs_charset_no;
×
3658
    if (charset_no == 0u) {
×
3659
        charset_no = 1u;
×
3660
    }
3661
    if (encoder->drcs_mmv == 0) {
×
3662
        is_96cs = (charset_no > 63u) ? 1 : 0;
×
3663
        charset = (int)(((charset_no - 1u) % 63u) + 0x40u);
×
3664
    } else if (encoder->drcs_mmv == 1) {
×
3665
        is_96cs = 0;
×
3666
        charset = (int)(charset_no + 0x3fu);
×
3667
    } else {
3668
        is_96cs = (charset_no > 79u) ? 1 : 0;
×
3669
        charset = (int)(((charset_no - 1u) % 79u) + 0x30u);
×
3670
    }
3671
    code = 0x100020 + (is_96cs ? 0x80 : 0) + charset * 0x100;
×
3672
    num_cols = (sixel_frame_get_width(frame) + encoder->cell_width - 1)
×
3673
             / encoder->cell_width;
×
3674
    num_rows = (sixel_frame_get_height(frame) + encoder->cell_height - 1)
×
3675
             / encoder->cell_height;
×
3676

3677
    /* cols x rows x 4(out of BMP) + rows(LFs) */
3678
    alloc_size = num_cols * num_rows * 4 + num_rows;
×
3679
    buf_p = buf = sixel_allocator_malloc(encoder->allocator, alloc_size);
×
3680
    if (buf == NULL) {
×
3681
        sixel_helper_set_additional_message(
×
3682
            "sixel_encoder_emit_drcsmmv2_chars: sixel_allocator_malloc() failed.");
3683
        status = SIXEL_BAD_ALLOCATION;
×
3684
        goto end;
×
3685
    }
3686

3687
    for(row = 0; row < num_rows; row++) {
×
3688
        for(col = 0; col < num_cols; col++) {
×
3689
            *(buf_p++) = ((code >> 18) & 0x07) | 0xf0;
×
3690
            *(buf_p++) = ((code >> 12) & 0x3f) | 0x80;
×
3691
            *(buf_p++) = ((code >> 6) & 0x3f) | 0x80;
×
3692
            *(buf_p++) = (code & 0x3f) | 0x80;
×
3693
            code++;
×
3694
            if ((code & 0x7f) == 0x0) {
×
3695
                if (charset == 0x7e) {
×
3696
                    is_96cs = 1 - is_96cs;
×
3697
                    charset = '0';
×
3698
                } else {
3699
                    charset++;
×
3700
                }
3701
                code = 0x100020 + (is_96cs ? 0x80 : 0) + charset * 0x100;
×
3702
            }
3703
        }
3704
        *(buf_p++) = '\n';
×
3705
    }
3706

3707
    if (charset == 0x7e) {
×
3708
        is_96cs = 1 - is_96cs;
×
3709
    } else {
3710
        charset = '0';
×
3711
        charset++;
×
3712
    }
3713
    if (encoder->tile_outfd >= 0) {
×
3714
        target_fd = encoder->tile_outfd;
×
3715
    } else {
3716
        target_fd = encoder->outfd;
×
3717
    }
3718

3719
    chunk_size = (int)(buf_p - buf);
×
3720
    nwrite = sixel_encoder_probe_fd_write(encoder,
×
3721
                                          buf,
3722
                                          chunk_size,
3723
                                          target_fd);
3724
    if (nwrite != chunk_size) {
×
3725
        sixel_helper_set_additional_message(
×
3726
            "sixel_encoder_emit_drcsmmv2_chars: write() failed.");
3727
        status = SIXEL_RUNTIME_ERROR;
×
3728
        goto end;
×
3729
    }
3730

3731
    sixel_allocator_free(encoder->allocator, buf);
×
3732

3733
    status = SIXEL_OK;
×
3734

3735
end:
3736
    return status;
×
3737
}
3738

3739
static SIXELSTATUS
3740
sixel_encoder_encode_frame(
530✔
3741
    sixel_encoder_t *encoder,
3742
    sixel_frame_t   *frame,
3743
    sixel_output_t  *output)
3744
{
3745
    SIXELSTATUS status = SIXEL_FALSE;
530✔
3746
    sixel_dither_t *dither = NULL;
530✔
3747
    int height;
3748
    int is_animation = 0;
530✔
3749
    int nwrite;
3750
    int drcs_is_96cs_param;
3751
    int drcs_designate_char;
3752
    char buf[256];
3753
    sixel_write_function fn_write;
3754
    sixel_write_function write_callback;
3755
    sixel_write_function scroll_callback;
3756
    void *write_priv;
3757
    void *scroll_priv;
3758
    sixel_encoder_output_probe_t probe;
3759
    sixel_encoder_output_probe_t scroll_probe;
3760
    sixel_assessment_t *assessment;
3761

3762
    fn_write = sixel_write_callback;
530✔
3763
    write_callback = sixel_write_callback;
530✔
3764
    scroll_callback = sixel_write_callback;
530✔
3765
    write_priv = &encoder->outfd;
530✔
3766
    scroll_priv = &encoder->outfd;
530✔
3767
    probe.encoder = NULL;
530✔
3768
    probe.base_write = NULL;
530✔
3769
    probe.base_priv = NULL;
530✔
3770
    scroll_probe.encoder = NULL;
530✔
3771
    scroll_probe.base_write = NULL;
530✔
3772
    scroll_probe.base_priv = NULL;
530✔
3773
    assessment = NULL;
530✔
3774
    if (encoder != NULL) {
530!
3775
        assessment = encoder->assessment_observer;
530✔
3776
    }
186✔
3777
    if (assessment != NULL) {
530!
3778
        if (encoder->clipfirst) {
×
3779
            sixel_assessment_stage_transition(
×
3780
                assessment,
3781
                SIXEL_ASSESSMENT_STAGE_CROP);
3782
        } else {
3783
            sixel_assessment_stage_transition(
×
3784
                assessment,
3785
                SIXEL_ASSESSMENT_STAGE_SCALE);
3786
        }
3787
    }
3788

3789
    /*
3790
     *  Geometry timeline:
3791
     *
3792
     *      +-------+    +------+    +---------------+
3793
     *      | scale | -> | crop | -> | color convert |
3794
     *      +-------+    +------+    +---------------+
3795
     *
3796
     *  The order of the first two blocks mirrors `-c`, so we hop between
3797
     *  SCALE and CROP depending on `clipfirst`.
3798
     */
3799

3800
    if (encoder->clipfirst) {
530✔
3801
        status = sixel_encoder_do_clip(encoder, frame);
8✔
3802
        if (SIXEL_FAILED(status)) {
8!
3803
            goto end;
2✔
3804
        }
3805
        if (assessment != NULL) {
6!
3806
            sixel_assessment_stage_transition(
×
3807
                assessment,
3808
                SIXEL_ASSESSMENT_STAGE_SCALE);
3809
        }
3810
        status = sixel_encoder_do_resize(encoder, frame);
6✔
3811
        if (SIXEL_FAILED(status)) {
6!
3812
            goto end;
×
3813
        }
3814
    } else {
2✔
3815
        status = sixel_encoder_do_resize(encoder, frame);
522✔
3816
        if (SIXEL_FAILED(status)) {
522✔
3817
            goto end;
6✔
3818
        }
3819
        if (assessment != NULL) {
516!
3820
            sixel_assessment_stage_transition(
×
3821
                assessment,
3822
                SIXEL_ASSESSMENT_STAGE_CROP);
3823
        }
3824
        status = sixel_encoder_do_clip(encoder, frame);
516✔
3825
        if (SIXEL_FAILED(status)) {
516!
3826
            goto end;
1✔
3827
        }
3828
    }
3829

3830
    if (assessment != NULL) {
521!
3831
        sixel_assessment_stage_transition(
×
3832
            assessment,
3833
            SIXEL_ASSESSMENT_STAGE_COLORSPACE);
3834
    }
3835

3836
    status = sixel_frame_ensure_colorspace(frame,
702✔
3837
                                           encoder->working_colorspace);
181✔
3838
    if (SIXEL_FAILED(status)) {
521!
3839
        goto end;
×
3840
    }
3841

3842
    if (assessment != NULL) {
521!
3843
        sixel_assessment_stage_transition(
×
3844
            assessment,
3845
            SIXEL_ASSESSMENT_STAGE_PALETTE_HISTOGRAM);
3846
    }
3847

3848
    /* prepare dither context */
3849
    status = sixel_encoder_prepare_palette(encoder, frame, &dither);
521✔
3850
    if (status != SIXEL_OK) {
521✔
3851
        dither = NULL;
5✔
3852
        goto end;
5✔
3853
    }
3854

3855
    if (encoder->dither_cache != NULL) {
516!
3856
        encoder->dither_cache = dither;
×
3857
        sixel_dither_ref(dither);
×
3858
    }
3859

3860
    if (encoder->fdrcs) {
516!
3861
        status = sixel_encoder_ensure_cell_size(encoder);
×
3862
        if (SIXEL_FAILED(status)) {
×
3863
            goto end;
×
3864
        }
3865
        if (encoder->fuse_macro || encoder->macro_number >= 0) {
×
3866
            sixel_helper_set_additional_message(
×
3867
                "drcs option cannot be used together with macro output.");
3868
            status = SIXEL_BAD_ARGUMENT;
×
3869
            goto end;
×
3870
        }
3871
    }
3872

3873
    /* evaluate -v option: print palette */
3874
    if (encoder->verbose) {
516✔
3875
        if ((sixel_frame_get_pixelformat(frame) & SIXEL_FORMATTYPE_PALETTE)) {
9✔
3876
            sixel_debug_print_palette(dither);
3✔
3877
        }
1✔
3878
    }
3✔
3879

3880
    /* evaluate -d option: set method for diffusion */
3881
    sixel_dither_set_diffusion_type(dither, encoder->method_for_diffuse);
516✔
3882
    sixel_dither_set_diffusion_scan(dither, encoder->method_for_scan);
516✔
3883
    sixel_dither_set_diffusion_carry(dither, encoder->method_for_carry);
516✔
3884

3885
    /* evaluate -C option: set complexion score */
3886
    if (encoder->complexion > 1) {
516✔
3887
        sixel_dither_set_complexion_score(dither, encoder->complexion);
6✔
3888
    }
2✔
3889

3890
    if (output) {
516!
3891
        sixel_output_ref(output);
×
3892
        if (encoder->assessment_observer != NULL) {
×
3893
            probe.encoder = encoder;
×
3894
            probe.base_write = fn_write;
×
3895
            probe.base_priv = &encoder->outfd;
×
3896
            write_callback = sixel_write_with_probe;
×
3897
            write_priv = &probe;
×
3898
        }
3899
    } else {
3900
        /* create output context */
3901
        if (encoder->fuse_macro || encoder->macro_number >= 0) {
516✔
3902
            /* -u or -n option */
3903
            fn_write = sixel_hex_write_callback;
93✔
3904
        } else {
37✔
3905
            fn_write = sixel_write_callback;
423✔
3906
        }
3907
        write_callback = fn_write;
516✔
3908
        write_priv = &encoder->outfd;
516✔
3909
        if (encoder->assessment_observer != NULL) {
516!
3910
            probe.encoder = encoder;
×
3911
            probe.base_write = fn_write;
×
3912
            probe.base_priv = &encoder->outfd;
×
3913
            write_callback = sixel_write_with_probe;
×
3914
            write_priv = &probe;
×
3915
        }
3916
        status = sixel_output_new(&output,
516✔
3917
                                  write_callback,
178✔
3918
                                  write_priv,
178✔
3919
                                  encoder->allocator);
178✔
3920
        if (SIXEL_FAILED(status)) {
516!
3921
            goto end;
×
3922
        }
3923
    }
3924

3925
    if (encoder->fdrcs) {
516!
3926
        sixel_output_set_skip_dcs_envelope(output, 1);
×
3927
        sixel_output_set_skip_header(output, 1);
×
3928
    }
3929

3930
    sixel_output_set_8bit_availability(output, encoder->f8bit);
516✔
3931
    sixel_output_set_gri_arg_limit(output, encoder->has_gri_arg_limit);
516✔
3932
    sixel_output_set_palette_type(output, encoder->palette_type);
516✔
3933
    sixel_output_set_penetrate_multiplexer(
516✔
3934
        output, encoder->penetrate_multiplexer);
178✔
3935
    sixel_output_set_encode_policy(output, encoder->encode_policy);
516✔
3936
    sixel_output_set_ormode(output, encoder->ormode);
516✔
3937

3938
    if (sixel_frame_get_multiframe(frame) && !encoder->fstatic) {
516!
3939
        if (sixel_frame_get_loop_no(frame) != 0 || sixel_frame_get_frame_no(frame) != 0) {
117✔
3940
            is_animation = 1;
108✔
3941
        }
42✔
3942
        height = sixel_frame_get_height(frame);
117✔
3943
        if (encoder->assessment_observer != NULL) {
117!
3944
            scroll_probe.encoder = encoder;
×
3945
            scroll_probe.base_write = sixel_write_callback;
×
3946
            scroll_probe.base_priv = &encoder->outfd;
×
3947
            scroll_callback = sixel_write_with_probe;
×
3948
            scroll_priv = &scroll_probe;
×
3949
        } else {
3950
            scroll_callback = sixel_write_callback;
117✔
3951
            scroll_priv = &encoder->outfd;
117✔
3952
        }
3953
        (void) sixel_tty_scroll(scroll_callback,
162✔
3954
                                scroll_priv,
45✔
3955
                                encoder->outfd,
45✔
3956
                                height,
45✔
3957
                                is_animation);
45✔
3958
    }
45✔
3959

3960
    if (encoder->cancel_flag && *encoder->cancel_flag) {
516!
3961
        status = SIXEL_INTERRUPTED;
×
3962
        goto end;
×
3963
    }
3964

3965
    if (encoder->fdrcs) {  /* -@ option */
516!
3966
        if (encoder->drcs_mmv == 0) {
×
3967
            drcs_is_96cs_param =
×
3968
                (encoder->drcs_charset_no > 63u) ? 1 : 0;
×
3969
            drcs_designate_char =
×
3970
                (int)(((encoder->drcs_charset_no - 1u) % 63u) + 0x40u);
×
3971
        } else if (encoder->drcs_mmv == 1) {
×
3972
            drcs_is_96cs_param = 0;
×
3973
            drcs_designate_char =
×
3974
                (int)(encoder->drcs_charset_no + 0x3fu);
×
3975
        } else {
3976
            drcs_is_96cs_param =
×
3977
                (encoder->drcs_charset_no > 79u) ? 1 : 0;
×
3978
            drcs_designate_char =
×
3979
                (int)(((encoder->drcs_charset_no - 1u) % 79u) + 0x30u);
×
3980
        }
3981
        nwrite = sprintf(buf,
×
3982
                         "%s%s1;0;0;%d;1;3;%d;%d{ %c",
3983
                         (encoder->drcs_mmv > 0) ? (
×
3984
                             encoder->f8bit ? "\233?8800h": "\033[?8800h"
×
3985
                         ): "",
3986
                         encoder->f8bit ? "\220": "\033P",
×
3987
                         encoder->cell_width,
3988
                         encoder->cell_height,
3989
                         drcs_is_96cs_param,
3990
                         drcs_designate_char);
3991
        if (nwrite < 0) {
×
3992
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
3993
            sixel_helper_set_additional_message(
×
3994
                "sixel_encoder_encode_frame: command format failed.");
3995
            goto end;
×
3996
        }
3997
        nwrite = write_callback(buf, nwrite, write_priv);
×
3998
        if (nwrite < 0) {
×
3999
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
4000
            sixel_helper_set_additional_message(
×
4001
                "sixel_encoder_encode_frame: write() failed.");
4002
            goto end;
×
4003
        }
4004
    }
4005

4006
    /* output sixel: junction of multi-frame processing strategy */
4007
    if (encoder->fuse_macro) {  /* -u option */
516✔
4008
        /* use macro */
4009
        status = sixel_encoder_output_with_macro(frame, dither, output, encoder);
90✔
4010
    } else if (encoder->macro_number >= 0) { /* -n option */
462✔
4011
        /* use macro */
4012
        status = sixel_encoder_output_with_macro(frame, dither, output, encoder);
3✔
4013
    } else {
1✔
4014
        /* do not use macro */
4015
        status = sixel_encoder_output_without_macro(frame, dither, output, encoder);
423✔
4016
    }
4017
    if (SIXEL_FAILED(status)) {
516!
4018
        goto end;
×
4019
    }
4020

4021
    if (encoder->cancel_flag && *encoder->cancel_flag) {
516!
4022
        nwrite = write_callback("\x18\033\\", 3, write_priv);
×
4023
        if (nwrite < 0) {
×
4024
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
4025
            sixel_helper_set_additional_message(
×
4026
                "sixel_encoder_encode_frame: write_callback() failed.");
4027
            goto end;
×
4028
        }
4029
        status = SIXEL_INTERRUPTED;
×
4030
    }
4031
    if (SIXEL_FAILED(status)) {
516!
4032
        goto end;
×
4033
    }
4034

4035
    if (encoder->fdrcs) {  /* -@ option */
516!
4036
        if (encoder->f8bit) {
×
4037
            nwrite = write_callback("\234", 1, write_priv);
×
4038
        } else {
4039
            nwrite = write_callback("\033\\", 2, write_priv);
×
4040
        }
4041
        if (nwrite < 0) {
×
4042
            status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
4043
            sixel_helper_set_additional_message(
×
4044
                "sixel_encoder_encode_frame: write_callback() failed.");
4045
            goto end;
×
4046
        }
4047

4048
        if (encoder->tile_outfd >= 0) {
×
4049
            if (encoder->drcs_mmv == 0) {
×
4050
                status = sixel_encoder_emit_iso2022_chars(encoder, frame);
×
4051
                if (SIXEL_FAILED(status)) {
×
4052
                    goto end;
×
4053
                }
4054
            } else {
4055
                status = sixel_encoder_emit_drcsmmv2_chars(encoder, frame);
×
4056
                if (SIXEL_FAILED(status)) {
×
4057
                    goto end;
×
4058
                }
4059
            }
4060
        }
4061
    }
4062

4063

4064
end:
338✔
4065
    if (output) {
530✔
4066
        sixel_output_unref(output);
516✔
4067
    }
178✔
4068
    if (dither) {
530✔
4069
        sixel_dither_unref(dither);
516✔
4070
    }
178✔
4071

4072
    return status;
530✔
4073
}
4074

4075

4076
/* create encoder object */
4077
SIXELAPI SIXELSTATUS
4078
sixel_encoder_new(
513✔
4079
    sixel_encoder_t     /* out */ **ppencoder, /* encoder object to be created */
4080
    sixel_allocator_t   /* in */  *allocator)  /* allocator, null if you use
4081
                                                  default allocator */
4082
{
4083
    SIXELSTATUS status = SIXEL_FALSE;
513✔
4084
    char const *env_default_bgcolor = NULL;
513✔
4085
    char const *env_default_ncolors = NULL;
513✔
4086
    int ncolors;
4087
#if HAVE__DUPENV_S
4088
    errno_t e;
4089
    size_t len;
4090
#endif  /* HAVE__DUPENV_S */
4091

4092
    if (allocator == NULL) {
513!
4093
        status = sixel_allocator_new(&allocator, NULL, NULL, NULL, NULL);
513✔
4094
        if (SIXEL_FAILED(status)) {
513!
4095
            goto end;
×
4096
        }
4097
    } else {
171✔
4098
        sixel_allocator_ref(allocator);
×
4099
    }
4100

4101
    *ppencoder
171✔
4102
        = (sixel_encoder_t *)sixel_allocator_malloc(allocator,
684✔
4103
                                                    sizeof(sixel_encoder_t));
4104
    if (*ppencoder == NULL) {
513!
4105
        sixel_helper_set_additional_message(
×
4106
            "sixel_encoder_new: sixel_allocator_malloc() failed.");
4107
        status = SIXEL_BAD_ALLOCATION;
×
4108
        sixel_allocator_unref(allocator);
×
4109
        goto end;
×
4110
    }
4111

4112
    (*ppencoder)->ref                   = 1;
513✔
4113
    (*ppencoder)->reqcolors             = (-1);
513✔
4114
    (*ppencoder)->force_palette         = 0;
513✔
4115
    (*ppencoder)->mapfile               = NULL;
513✔
4116
    (*ppencoder)->palette_output        = NULL;
513✔
4117
    (*ppencoder)->loader_order          = NULL;
513✔
4118
    (*ppencoder)->color_option          = SIXEL_COLOR_OPTION_DEFAULT;
513✔
4119
    (*ppencoder)->builtin_palette       = 0;
513✔
4120
    (*ppencoder)->method_for_diffuse    = SIXEL_DIFFUSE_AUTO;
513✔
4121
    (*ppencoder)->method_for_scan       = SIXEL_SCAN_AUTO;
513✔
4122
    (*ppencoder)->method_for_carry      = SIXEL_CARRY_AUTO;
513✔
4123
    (*ppencoder)->method_for_largest    = SIXEL_LARGE_AUTO;
513✔
4124
    (*ppencoder)->method_for_rep        = SIXEL_REP_AUTO;
513✔
4125
    (*ppencoder)->quality_mode          = SIXEL_QUALITY_AUTO;
513✔
4126
    (*ppencoder)->lut_policy            = SIXEL_LUT_POLICY_AUTO;
513✔
4127
    (*ppencoder)->method_for_resampling = SIXEL_RES_BILINEAR;
513✔
4128
    (*ppencoder)->loop_mode             = SIXEL_LOOP_AUTO;
513✔
4129
    (*ppencoder)->palette_type          = SIXEL_PALETTETYPE_AUTO;
513✔
4130
    (*ppencoder)->f8bit                 = 0;
513✔
4131
    (*ppencoder)->has_gri_arg_limit     = 0;
513✔
4132
    (*ppencoder)->finvert               = 0;
513✔
4133
    (*ppencoder)->fuse_macro            = 0;
513✔
4134
    (*ppencoder)->fdrcs                 = 0;
513✔
4135
    (*ppencoder)->fignore_delay         = 0;
513✔
4136
    (*ppencoder)->complexion            = 1;
513✔
4137
    (*ppencoder)->fstatic               = 0;
513✔
4138
    (*ppencoder)->cell_width            = 0;
513✔
4139
    (*ppencoder)->cell_height           = 0;
513✔
4140
    (*ppencoder)->pixelwidth            = (-1);
513✔
4141
    (*ppencoder)->pixelheight           = (-1);
513✔
4142
    (*ppencoder)->percentwidth          = (-1);
513✔
4143
    (*ppencoder)->percentheight         = (-1);
513✔
4144
    (*ppencoder)->clipx                 = 0;
513✔
4145
    (*ppencoder)->clipy                 = 0;
513✔
4146
    (*ppencoder)->clipwidth             = 0;
513✔
4147
    (*ppencoder)->clipheight            = 0;
513✔
4148
    (*ppencoder)->clipfirst             = 0;
513✔
4149
    (*ppencoder)->macro_number          = (-1);
513✔
4150
    (*ppencoder)->verbose               = 0;
513✔
4151
    (*ppencoder)->penetrate_multiplexer = 0;
513✔
4152
    (*ppencoder)->encode_policy         = SIXEL_ENCODEPOLICY_AUTO;
513✔
4153
    (*ppencoder)->working_colorspace    = SIXEL_COLORSPACE_GAMMA;
513✔
4154
    (*ppencoder)->output_colorspace     = SIXEL_COLORSPACE_GAMMA;
513✔
4155
    (*ppencoder)->ormode                = 0;
513✔
4156
    (*ppencoder)->pipe_mode             = 0;
513✔
4157
    (*ppencoder)->bgcolor               = NULL;
513✔
4158
    (*ppencoder)->outfd                 = STDOUT_FILENO;
513✔
4159
    (*ppencoder)->tile_outfd            = (-1);
513✔
4160
    (*ppencoder)->finsecure             = 0;
513✔
4161
    (*ppencoder)->cancel_flag           = NULL;
513✔
4162
    (*ppencoder)->dither_cache          = NULL;
513✔
4163
    (*ppencoder)->drcs_charset_no       = 1u;
513✔
4164
    (*ppencoder)->drcs_mmv              = 2;
513✔
4165
    (*ppencoder)->capture_quantized     = 0;
513✔
4166
    (*ppencoder)->capture_source        = 0;
513✔
4167
    (*ppencoder)->capture_pixels        = NULL;
513✔
4168
    (*ppencoder)->capture_pixels_size   = 0;
513✔
4169
    (*ppencoder)->capture_palette       = NULL;
513✔
4170
    (*ppencoder)->capture_palette_size  = 0;
513✔
4171
    (*ppencoder)->capture_pixel_bytes   = 0;
513✔
4172
    (*ppencoder)->capture_width         = 0;
513✔
4173
    (*ppencoder)->capture_height        = 0;
513✔
4174
    (*ppencoder)->capture_pixelformat   = SIXEL_PIXELFORMAT_RGB888;
513✔
4175
    (*ppencoder)->capture_colorspace    = SIXEL_COLORSPACE_GAMMA;
513✔
4176
    (*ppencoder)->capture_ncolors       = 0;
513✔
4177
    (*ppencoder)->capture_valid         = 0;
513✔
4178
    (*ppencoder)->capture_source_frame  = NULL;
513✔
4179
    (*ppencoder)->assessment_observer   = NULL;
513✔
4180
    (*ppencoder)->last_loader_name[0]   = '\0';
513✔
4181
    (*ppencoder)->last_source_path[0]   = '\0';
513✔
4182
    (*ppencoder)->last_input_bytes      = 0u;
513✔
4183
    (*ppencoder)->allocator             = allocator;
513✔
4184

4185
    /* evaluate environment variable ${SIXEL_BGCOLOR} */
4186
#if HAVE__DUPENV_S
4187
    e = _dupenv_s(&env_default_bgcolor, &len, "SIXEL_BGCOLOR");
4188
    if (e != (0)) {
4189
        sixel_helper_set_additional_message(
4190
            "failed to get environment variable $SIXEL_BGCOLOR.");
4191
        return (SIXEL_LIBC_ERROR | (e & 0xff));
4192
    }
4193
#else
4194
    env_default_bgcolor = sixel_compat_getenv("SIXEL_BGCOLOR");
513✔
4195
#endif  /* HAVE__DUPENV_S */
4196
    if (env_default_bgcolor != NULL) {
513!
4197
        status = sixel_parse_x_colorspec(&(*ppencoder)->bgcolor,
×
4198
                                         env_default_bgcolor,
4199
                                         allocator);
4200
        if (SIXEL_FAILED(status)) {
×
4201
            goto error;
×
4202
        }
4203
    }
4204

4205
    /* evaluate environment variable ${SIXEL_COLORS} */
4206
#if HAVE__DUPENV_S
4207
    e = _dupenv_s(&env_default_bgcolor, &len, "SIXEL_COLORS");
4208
    if (e != (0)) {
4209
        sixel_helper_set_additional_message(
4210
            "failed to get environment variable $SIXEL_COLORS.");
4211
        return (SIXEL_LIBC_ERROR | (e & 0xff));
4212
    }
4213
#else
4214
    env_default_ncolors = sixel_compat_getenv("SIXEL_COLORS");
513✔
4215
#endif  /* HAVE__DUPENV_S */
4216
    if (env_default_ncolors) {
513!
4217
        ncolors = atoi(env_default_ncolors); /* may overflow */
×
4218
        if (ncolors > 1 && ncolors <= SIXEL_PALETTE_MAX) {
×
4219
            (*ppencoder)->reqcolors = ncolors;
×
4220
        }
4221
    }
4222

4223
    /* success */
4224
    status = SIXEL_OK;
513✔
4225

4226
    goto end;
513✔
4227

4228
error:
4229
    sixel_allocator_free(allocator, *ppencoder);
×
4230
    sixel_allocator_unref(allocator);
×
4231
    *ppencoder = NULL;
×
4232

4233
end:
342✔
4234
#if HAVE__DUPENV_S
4235
    free(env_default_bgcolor);
4236
    free(env_default_ncolors);
4237
#endif  /* HAVE__DUPENV_S */
4238
    return status;
513✔
4239
}
4240

4241

4242
/* create encoder object (deprecated version) */
4243
SIXELAPI /* deprecated */ sixel_encoder_t *
4244
sixel_encoder_create(void)
×
4245
{
4246
    SIXELSTATUS status = SIXEL_FALSE;
×
4247
    sixel_encoder_t *encoder = NULL;
×
4248

4249
    status = sixel_encoder_new(&encoder, NULL);
×
4250
    if (SIXEL_FAILED(status)) {
×
4251
        return NULL;
×
4252
    }
4253

4254
    return encoder;
×
4255
}
4256

4257

4258
/* destroy encoder object */
4259
static void
4260
sixel_encoder_destroy(sixel_encoder_t *encoder)
513✔
4261
{
4262
    sixel_allocator_t *allocator;
4263

4264
    if (encoder) {
513!
4265
        allocator = encoder->allocator;
513✔
4266
        sixel_allocator_free(allocator, encoder->mapfile);
513✔
4267
        sixel_allocator_free(allocator, encoder->palette_output);
513✔
4268
        sixel_allocator_free(allocator, encoder->loader_order);
513✔
4269
        sixel_allocator_free(allocator, encoder->bgcolor);
513✔
4270
        sixel_dither_unref(encoder->dither_cache);
513✔
4271
        if (encoder->outfd
522!
4272
            && encoder->outfd != STDOUT_FILENO
513!
4273
            && encoder->outfd != STDERR_FILENO) {
189!
4274
            (void)sixel_compat_close(encoder->outfd);
27✔
4275
        }
9✔
4276
        if (encoder->tile_outfd >= 0
513!
4277
            && encoder->tile_outfd != encoder->outfd
171!
4278
            && encoder->tile_outfd != STDOUT_FILENO
×
4279
            && encoder->tile_outfd != STDERR_FILENO) {
×
4280
            (void)sixel_compat_close(encoder->tile_outfd);
×
4281
        }
4282
        if (encoder->capture_source_frame != NULL) {
513!
4283
            sixel_frame_unref(encoder->capture_source_frame);
×
4284
        }
4285
        sixel_allocator_free(allocator, encoder->capture_pixels);
513✔
4286
        sixel_allocator_free(allocator, encoder->capture_palette);
513✔
4287
        sixel_allocator_free(allocator, encoder);
513✔
4288
        sixel_allocator_unref(allocator);
513✔
4289
    }
171✔
4290
}
513✔
4291

4292

4293
/* increase reference count of encoder object (thread-unsafe) */
4294
SIXELAPI void
4295
sixel_encoder_ref(sixel_encoder_t *encoder)
1,113✔
4296
{
4297
    /* TODO: be thread safe */
4298
    ++encoder->ref;
1,113✔
4299
}
1,113✔
4300

4301

4302
/* decrease reference count of encoder object (thread-unsafe) */
4303
SIXELAPI void
4304
sixel_encoder_unref(sixel_encoder_t *encoder)
1,626✔
4305
{
4306
    /* TODO: be thread safe */
4307
    if (encoder != NULL && --encoder->ref == 0) {
1,626!
4308
        sixel_encoder_destroy(encoder);
513✔
4309
    }
171✔
4310
}
1,626✔
4311

4312

4313
/* set cancel state flag to encoder object */
4314
SIXELAPI SIXELSTATUS
4315
sixel_encoder_set_cancel_flag(
429✔
4316
    sixel_encoder_t /* in */ *encoder,
4317
    int             /* in */ *cancel_flag
4318
)
4319
{
4320
    SIXELSTATUS status = SIXEL_OK;
429✔
4321

4322
    encoder->cancel_flag = cancel_flag;
429✔
4323

4324
    return status;
429✔
4325
}
4326

4327

4328
/* set an option flag to encoder object */
4329
SIXELAPI SIXELSTATUS
4330
sixel_encoder_setopt(
684✔
4331
    sixel_encoder_t /* in */ *encoder,
4332
    int             /* in */ arg,
4333
    char const      /* in */ *value)
4334
{
4335
    SIXELSTATUS status = SIXEL_FALSE;
684✔
4336
    int number;
4337
    int parsed;
4338
    char unit[32];
4339
    char lowered[16];
4340
    size_t len;
4341
    size_t i;
4342
    long parsed_reqcolors;
4343
    char *endptr;
4344
    int forced_palette;
4345
    char *opt_copy;
4346
    char const *drcs_arg_delim;
4347
    char const *drcs_arg_charset;
4348
    char const *drcs_arg_second_delim;
4349
    char const *drcs_arg_path;
4350
    size_t drcs_arg_path_length;
4351
    size_t drcs_segment_length;
4352
    char drcs_segment[32];
4353
    int drcs_mmv_value;
4354
    long drcs_charset_value;
4355
    unsigned int drcs_charset_limit;
4356
    sixel_option_choice_result_t match_result;
4357
    int match_value;
4358
    char match_detail[128];
4359
    char match_message[256];
4360

4361
    sixel_encoder_ref(encoder);
684✔
4362
    opt_copy = NULL;
684✔
4363

4364
    switch(arg) {
684!
4365
    case SIXEL_OPTFLAG_OUTFILE:  /* o */
18✔
4366
        if (*value == '\0') {
27!
4367
            sixel_helper_set_additional_message(
×
4368
                "no file name specified.");
4369
            status = SIXEL_BAD_ARGUMENT;
×
4370
            goto end;
×
4371
        }
4372
        if (strcmp(value, "-") != 0) {
27!
4373
            if (encoder->outfd && encoder->outfd != STDOUT_FILENO) {
27!
4374
                (void)sixel_compat_close(encoder->outfd);
×
4375
            }
4376
            encoder->outfd = sixel_compat_open(value,
27✔
4377
                                               O_RDWR | O_CREAT | O_TRUNC,
4378
                                               S_IRUSR | S_IWUSR);
4379
        }
9✔
4380
        break;
27✔
4381
    case SIXEL_OPTFLAG_7BIT_MODE:  /* 7 */
10✔
4382
        encoder->f8bit = 0;
15✔
4383
        break;
15✔
4384
    case SIXEL_OPTFLAG_8BIT_MODE:  /* 8 */
12✔
4385
        encoder->f8bit = 1;
18✔
4386
        break;
18✔
4387
    case SIXEL_OPTFLAG_HAS_GRI_ARG_LIMIT:  /* R */
4388
        encoder->has_gri_arg_limit = 1;
×
4389
        break;
×
4390
    case SIXEL_OPTFLAG_COLORS:  /* p */
18✔
4391
        forced_palette = 0;
27✔
4392
        errno = 0;
27✔
4393
        endptr = NULL;
27✔
4394
        if (*value == '!' && value[1] == '\0') {
27!
4395
            /*
4396
             * Force the default palette size even when the median cut
4397
             * finished early.
4398
             *
4399
             *   requested colors
4400
             *          |
4401
             *          v
4402
             *        [ 256 ]  <--- "-p!" triggers this shortcut
4403
             */
4404
            parsed_reqcolors = SIXEL_PALETTE_MAX;
×
4405
            forced_palette = 1;
×
4406
        } else {
4407
            parsed_reqcolors = strtol(value, &endptr, 10);
27✔
4408
            if (endptr != NULL && *endptr == '!') {
27!
4409
                forced_palette = 1;
×
4410
                ++endptr;
×
4411
            }
4412
            if (errno == ERANGE || endptr == value) {
27!
4413
                sixel_helper_set_additional_message(
×
4414
                    "cannot parse -p/--colors option.");
4415
                status = SIXEL_BAD_ARGUMENT;
×
4416
                goto end;
×
4417
            }
4418
            if (endptr != NULL && *endptr != '\0') {
27!
4419
                sixel_helper_set_additional_message(
×
4420
                    "cannot parse -p/--colors option.");
4421
                status = SIXEL_BAD_ARGUMENT;
×
4422
                goto end;
×
4423
            }
4424
        }
4425
        if (parsed_reqcolors < 1) {
27!
4426
            sixel_helper_set_additional_message(
×
4427
                "-p/--colors parameter must be 1 or more.");
4428
            status = SIXEL_BAD_ARGUMENT;
×
4429
            goto end;
×
4430
        }
4431
        if (parsed_reqcolors > SIXEL_PALETTE_MAX) {
27!
4432
            sixel_helper_set_additional_message(
×
4433
                "-p/--colors parameter must be less then or equal to 256.");
4434
            status = SIXEL_BAD_ARGUMENT;
×
4435
            goto end;
×
4436
        }
4437
        encoder->reqcolors = (int)parsed_reqcolors;
27✔
4438
        encoder->force_palette = forced_palette;
27✔
4439
        break;
27✔
4440
    case SIXEL_OPTFLAG_MAPFILE:  /* m */
18✔
4441
        if (encoder->mapfile) {
27✔
4442
            sixel_allocator_free(encoder->allocator, encoder->mapfile);
3✔
4443
        }
1✔
4444
        encoder->mapfile = arg_strdup(value, encoder->allocator);
27✔
4445
        if (encoder->mapfile == NULL) {
27!
4446
            sixel_helper_set_additional_message(
×
4447
                "sixel_encoder_setopt: sixel_allocator_malloc() failed.");
4448
            status = SIXEL_BAD_ALLOCATION;
×
4449
            goto end;
×
4450
        }
4451
        encoder->color_option = SIXEL_COLOR_OPTION_MAPFILE;
27✔
4452
        break;
27✔
4453
    case SIXEL_OPTFLAG_MAPFILE_OUTPUT:  /* M */
4454
        if (value == NULL || *value == '\0') {
×
4455
            sixel_helper_set_additional_message(
×
4456
                "sixel_encoder_setopt: mapfile-output path is empty.");
4457
            status = SIXEL_BAD_ARGUMENT;
×
4458
            goto end;
×
4459
        }
4460
        opt_copy = arg_strdup(value, encoder->allocator);
×
4461
        if (opt_copy == NULL) {
×
4462
            sixel_helper_set_additional_message(
×
4463
                "sixel_encoder_setopt: sixel_allocator_malloc() failed.");
4464
            status = SIXEL_BAD_ALLOCATION;
×
4465
            goto end;
×
4466
        }
4467
        status = sixel_encoder_enable_quantized_capture(encoder, 1);
×
4468
        if (SIXEL_FAILED(status)) {
×
4469
            sixel_allocator_free(encoder->allocator, opt_copy);
×
4470
            goto end;
×
4471
        }
4472
        sixel_allocator_free(encoder->allocator, encoder->palette_output);
×
4473
        encoder->palette_output = opt_copy;
×
4474
        opt_copy = NULL;
×
4475
        break;
×
4476
    case SIXEL_OPTFLAG_MONOCHROME:  /* e */
10✔
4477
        encoder->color_option = SIXEL_COLOR_OPTION_MONOCHROME;
15✔
4478
        break;
15✔
4479
    case SIXEL_OPTFLAG_HIGH_COLOR:  /* I */
28✔
4480
        encoder->color_option = SIXEL_COLOR_OPTION_HIGHCOLOR;
42✔
4481
        break;
42✔
4482
    case SIXEL_OPTFLAG_BUILTIN_PALETTE:  /* b */
22✔
4483
        match_result = sixel_match_option_choice(
33✔
4484
            value,
11✔
4485
            g_option_choices_builtin_palette,
4486
            sizeof(g_option_choices_builtin_palette) /
4487
            sizeof(g_option_choices_builtin_palette[0]),
4488
            &match_value,
4489
            match_detail,
11✔
4490
            sizeof(match_detail));
4491
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
33✔
4492
            encoder->builtin_palette = match_value;
30✔
4493
        } else {
10✔
4494
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4495
                sixel_report_ambiguous_prefix("--builtin-palette",
×
4496
                                              value,
4497
                                              match_detail,
4498
                                              match_message,
4499
                                              sizeof(match_message));
4500
            } else {
4501
                sixel_helper_set_additional_message(
3✔
4502
                    "cannot parse builtin palette option.");
4503
            }
4504
            status = SIXEL_BAD_ARGUMENT;
3✔
4505
            goto end;
3✔
4506
        }
4507
        encoder->color_option = SIXEL_COLOR_OPTION_BUILTIN;
30✔
4508
        break;
30✔
4509
    case SIXEL_OPTFLAG_DIFFUSION:  /* d */
46✔
4510
        match_result = sixel_match_option_choice(
69✔
4511
            value,
23✔
4512
            g_option_choices_diffusion,
4513
            sizeof(g_option_choices_diffusion) /
4514
            sizeof(g_option_choices_diffusion[0]),
4515
            &match_value,
4516
            match_detail,
23✔
4517
            sizeof(match_detail));
4518
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
69✔
4519
            encoder->method_for_diffuse = match_value;
66✔
4520
        } else {
22✔
4521
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4522
                sixel_report_ambiguous_prefix("--diffusion",
×
4523
                                              value,
4524
                                              match_detail,
4525
                                              match_message,
4526
                                              sizeof(match_message));
4527
            } else {
4528
                sixel_helper_set_additional_message(
3✔
4529
                    "specified diffusion method is not supported.");
4530
            }
4531
            status = SIXEL_BAD_ARGUMENT;
3✔
4532
            goto end;
3✔
4533
        }
4534
        break;
66✔
4535
    case SIXEL_OPTFLAG_DIFFUSION_SCAN:  /* y */
4536
        match_result = sixel_match_option_choice(
×
4537
            value,
4538
            g_option_choices_diffusion_scan,
4539
            sizeof(g_option_choices_diffusion_scan) /
4540
            sizeof(g_option_choices_diffusion_scan[0]),
4541
            &match_value,
4542
            match_detail,
4543
            sizeof(match_detail));
4544
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
×
4545
            encoder->method_for_scan = match_value;
×
4546
        } else {
4547
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
4548
                sixel_report_ambiguous_prefix("--diffusion-scan",
×
4549
                                              value,
4550
                                              match_detail,
4551
                                              match_message,
4552
                                              sizeof(match_message));
4553
            } else {
4554
                sixel_helper_set_additional_message(
×
4555
                    "specified diffusion scan is not supported.");
4556
            }
4557
            status = SIXEL_BAD_ARGUMENT;
×
4558
            goto end;
×
4559
        }
4560
        break;
×
4561
    case SIXEL_OPTFLAG_DIFFUSION_CARRY:  /* Y */
4562
        match_result = sixel_match_option_choice(
×
4563
            value,
4564
            g_option_choices_diffusion_carry,
4565
            sizeof(g_option_choices_diffusion_carry) /
4566
            sizeof(g_option_choices_diffusion_carry[0]),
4567
            &match_value,
4568
            match_detail,
4569
            sizeof(match_detail));
4570
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
×
4571
            encoder->method_for_carry = match_value;
×
4572
        } else {
4573
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
4574
                sixel_report_ambiguous_prefix("--diffusion-carry",
×
4575
                                              value,
4576
                                              match_detail,
4577
                                              match_message,
4578
                                              sizeof(match_message));
4579
            } else {
4580
                sixel_helper_set_additional_message(
×
4581
                    "specified diffusion carry mode is not supported.");
4582
            }
4583
            status = SIXEL_BAD_ARGUMENT;
×
4584
            goto end;
×
4585
        }
4586
        break;
×
4587
    case SIXEL_OPTFLAG_FIND_LARGEST:  /* f */
10✔
4588
        if (value != NULL) {
15!
4589
            match_result = sixel_match_option_choice(
15✔
4590
                value,
5✔
4591
                g_option_choices_find_largest,
4592
                sizeof(g_option_choices_find_largest) /
4593
                sizeof(g_option_choices_find_largest[0]),
4594
                &match_value,
4595
                match_detail,
5✔
4596
                sizeof(match_detail));
4597
            if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
15✔
4598
                encoder->method_for_largest = match_value;
12✔
4599
            } else {
4✔
4600
                if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4601
                    sixel_report_ambiguous_prefix("--find-largest",
×
4602
                                                  value,
4603
                                                  match_detail,
4604
                                                  match_message,
4605
                                                  sizeof(match_message));
4606
                } else {
4607
                    sixel_helper_set_additional_message(
3✔
4608
                        "specified finding method is not supported.");
4609
                }
4610
                status = SIXEL_BAD_ARGUMENT;
3✔
4611
                goto end;
3✔
4612
            }
4613
        }
4✔
4614
        break;
12✔
4615
    case SIXEL_OPTFLAG_SELECT_COLOR:  /* s */
14✔
4616
        match_result = sixel_match_option_choice(
21✔
4617
            value,
7✔
4618
            g_option_choices_select_color,
4619
            sizeof(g_option_choices_select_color) /
4620
            sizeof(g_option_choices_select_color[0]),
4621
            &match_value,
4622
            match_detail,
7✔
4623
            sizeof(match_detail));
4624
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
21✔
4625
            encoder->method_for_rep = match_value;
15✔
4626
        } else {
5✔
4627
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
6✔
4628
                sixel_report_ambiguous_prefix("--select-color",
3✔
4629
                                              value,
1✔
4630
                                              match_detail,
1✔
4631
                                              match_message,
1✔
4632
                                              sizeof(match_message));
4633
            } else {
1✔
4634
                sixel_helper_set_additional_message(
3✔
4635
                    "specified finding method is not supported.");
4636
            }
4637
            status = SIXEL_BAD_ARGUMENT;
6✔
4638
            goto end;
6✔
4639
        }
4640
        break;
15✔
4641
    case SIXEL_OPTFLAG_CROP:  /* c */
10✔
4642
#if HAVE_SSCANF_S
4643
        number = sscanf_s(value, "%dx%d+%d+%d",
4644
                          &encoder->clipwidth, &encoder->clipheight,
4645
                          &encoder->clipx, &encoder->clipy);
4646
#else
4647
        number = sscanf(value, "%dx%d+%d+%d",
20✔
4648
                        &encoder->clipwidth, &encoder->clipheight,
5✔
4649
                        &encoder->clipx, &encoder->clipy);
5✔
4650
#endif  /* HAVE_SSCANF_S */
4651
        if (number != 4) {
15!
4652
            status = SIXEL_BAD_ARGUMENT;
×
4653
            goto end;
×
4654
        }
4655
        if (encoder->clipwidth <= 0 || encoder->clipheight <= 0) {
15!
4656
            status = SIXEL_BAD_ARGUMENT;
×
4657
            goto end;
×
4658
        }
4659
        if (encoder->clipx < 0 || encoder->clipy < 0) {
15!
4660
            status = SIXEL_BAD_ARGUMENT;
×
4661
            goto end;
×
4662
        }
4663
        encoder->clipfirst = 0;
15✔
4664
        break;
15✔
4665
    case SIXEL_OPTFLAG_WIDTH:  /* w */
50✔
4666
#if HAVE_SSCANF_S
4667
        parsed = sscanf_s(value, "%d%2s", &number, unit, sizeof(unit) - 1);
4668
#else
4669
        parsed = sscanf(value, "%d%2s", &number, unit);
75✔
4670
#endif  /* HAVE_SSCANF_S */
4671
        if (parsed == 2 && strcmp(unit, "%") == 0) {
75!
4672
            encoder->pixelwidth = (-1);
12✔
4673
            encoder->percentwidth = number;
12✔
4674
        } else if (parsed == 1 || (parsed == 2 && strcmp(unit, "px") == 0)) {
67!
4675
            encoder->pixelwidth = number;
51✔
4676
            encoder->percentwidth = (-1);
51✔
4677
        } else if (strcmp(value, "auto") == 0) {
29✔
4678
            encoder->pixelwidth = (-1);
9✔
4679
            encoder->percentwidth = (-1);
9✔
4680
        } else {
3✔
4681
            sixel_helper_set_additional_message(
3✔
4682
                "cannot parse -w/--width option.");
4683
            status = SIXEL_BAD_ARGUMENT;
3✔
4684
            goto end;
3✔
4685
        }
4686
        if (encoder->clipwidth) {
72✔
4687
            encoder->clipfirst = 1;
6✔
4688
        }
2✔
4689
        break;
72✔
4690
    case SIXEL_OPTFLAG_HEIGHT:  /* h */
44✔
4691
#if HAVE_SSCANF_S
4692
        parsed = sscanf_s(value, "%d%2s", &number, unit, sizeof(unit) - 1);
4693
#else
4694
        parsed = sscanf(value, "%d%2s", &number, unit);
66✔
4695
#endif  /* HAVE_SSCANF_S */
4696
        if (parsed == 2 && strcmp(unit, "%") == 0) {
66!
4697
            encoder->pixelheight = (-1);
9✔
4698
            encoder->percentheight = number;
9✔
4699
        } else if (parsed == 1 || (parsed == 2 && strcmp(unit, "px") == 0)) {
60!
4700
            encoder->pixelheight = number;
45✔
4701
            encoder->percentheight = (-1);
45✔
4702
        } else if (strcmp(value, "auto") == 0) {
27✔
4703
            encoder->pixelheight = (-1);
9✔
4704
            encoder->percentheight = (-1);
9✔
4705
        } else {
3✔
4706
            sixel_helper_set_additional_message(
3✔
4707
                "cannot parse -h/--height option.");
4708
            status = SIXEL_BAD_ARGUMENT;
3✔
4709
            goto end;
3✔
4710
        }
4711
        if (encoder->clipheight) {
63✔
4712
            encoder->clipfirst = 1;
3✔
4713
        }
1✔
4714
        break;
63✔
4715
    case SIXEL_OPTFLAG_RESAMPLING:  /* r */
34✔
4716
        match_result = sixel_match_option_choice(
51✔
4717
            value,
17✔
4718
            g_option_choices_resampling,
4719
            sizeof(g_option_choices_resampling) /
4720
            sizeof(g_option_choices_resampling[0]),
4721
            &match_value,
4722
            match_detail,
17✔
4723
            sizeof(match_detail));
4724
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
51✔
4725
            encoder->method_for_resampling = match_value;
48✔
4726
        } else {
16✔
4727
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4728
                sixel_report_ambiguous_prefix("--resampling",
×
4729
                                              value,
4730
                                              match_detail,
4731
                                              match_message,
4732
                                              sizeof(match_message));
4733
            } else {
4734
                sixel_helper_set_additional_message(
3✔
4735
                    "specified desampling method is not supported.");
4736
            }
4737
            status = SIXEL_BAD_ARGUMENT;
3✔
4738
            goto end;
3✔
4739
        }
4740
        break;
48✔
4741
    case SIXEL_OPTFLAG_QUALITY:  /* q */
12✔
4742
        match_result = sixel_match_option_choice(
18✔
4743
            value,
6✔
4744
            g_option_choices_quality,
4745
            sizeof(g_option_choices_quality) /
4746
            sizeof(g_option_choices_quality[0]),
4747
            &match_value,
4748
            match_detail,
6✔
4749
            sizeof(match_detail));
4750
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
18✔
4751
            encoder->quality_mode = match_value;
15✔
4752
        } else {
5✔
4753
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4754
                sixel_report_ambiguous_prefix("--quality",
×
4755
                                              value,
4756
                                              match_detail,
4757
                                              match_message,
4758
                                              sizeof(match_message));
4759
            } else {
4760
                sixel_helper_set_additional_message(
3✔
4761
                    "cannot parse quality option.");
4762
            }
4763
            status = SIXEL_BAD_ARGUMENT;
3✔
4764
            goto end;
3✔
4765
        }
4766
        break;
15✔
4767
    case SIXEL_OPTFLAG_LOOPMODE:  /* l */
10✔
4768
        match_result = sixel_match_option_choice(
15✔
4769
            value,
5✔
4770
            g_option_choices_loopmode,
4771
            sizeof(g_option_choices_loopmode) /
4772
            sizeof(g_option_choices_loopmode[0]),
4773
            &match_value,
4774
            match_detail,
5✔
4775
            sizeof(match_detail));
4776
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
15✔
4777
            encoder->loop_mode = match_value;
12✔
4778
        } else {
4✔
4779
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4780
                sixel_report_ambiguous_prefix("--loop-control",
×
4781
                                              value,
4782
                                              match_detail,
4783
                                              match_message,
4784
                                              sizeof(match_message));
4785
            } else {
4786
                sixel_helper_set_additional_message(
3✔
4787
                    "cannot parse loop-control option.");
4788
            }
4789
            status = SIXEL_BAD_ARGUMENT;
3✔
4790
            goto end;
3✔
4791
        }
4792
        break;
12✔
4793
    case SIXEL_OPTFLAG_PALETTE_TYPE:  /* t */
18✔
4794
        match_result = sixel_match_option_choice(
27✔
4795
            value,
9✔
4796
            g_option_choices_palette_type,
4797
            sizeof(g_option_choices_palette_type) /
4798
            sizeof(g_option_choices_palette_type[0]),
4799
            &match_value,
4800
            match_detail,
9✔
4801
            sizeof(match_detail));
4802
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
27✔
4803
            encoder->palette_type = match_value;
24✔
4804
        } else {
8✔
4805
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
4806
                sixel_report_ambiguous_prefix("--palette-type",
×
4807
                                              value,
4808
                                              match_detail,
4809
                                              match_message,
4810
                                              sizeof(match_message));
4811
            } else {
4812
                sixel_helper_set_additional_message(
3✔
4813
                    "cannot parse palette type option.");
4814
            }
4815
            status = SIXEL_BAD_ARGUMENT;
3✔
4816
            goto end;
3✔
4817
        }
4818
        break;
24✔
4819
    case SIXEL_OPTFLAG_BGCOLOR:  /* B */
30✔
4820
        /* parse --bgcolor option */
4821
        if (encoder->bgcolor) {
45✔
4822
            sixel_allocator_free(encoder->allocator, encoder->bgcolor);
6✔
4823
            encoder->bgcolor = NULL;
6✔
4824
        }
2✔
4825
        status = sixel_parse_x_colorspec(&encoder->bgcolor,
60✔
4826
                                         value,
15✔
4827
                                         encoder->allocator);
15✔
4828
        if (SIXEL_FAILED(status)) {
45✔
4829
            sixel_helper_set_additional_message(
21✔
4830
                "cannot parse bgcolor option.");
4831
            status = SIXEL_BAD_ARGUMENT;
21✔
4832
            goto end;
21✔
4833
        }
4834
        break;
24✔
4835
    case SIXEL_OPTFLAG_INSECURE:  /* k */
4836
        encoder->finsecure = 1;
×
4837
        break;
×
4838
    case SIXEL_OPTFLAG_INVERT:  /* i */
4✔
4839
        encoder->finvert = 1;
6✔
4840
        break;
6✔
4841
    case SIXEL_OPTFLAG_USE_MACRO:  /* u */
4✔
4842
        encoder->fuse_macro = 1;
6✔
4843
        break;
6✔
4844
    case SIXEL_OPTFLAG_MACRO_NUMBER:  /* n */
2✔
4845
        encoder->macro_number = atoi(value);
3✔
4846
        if (encoder->macro_number < 0) {
3!
4847
            status = SIXEL_BAD_ARGUMENT;
×
4848
            goto end;
×
4849
        }
4850
        break;
3✔
4851
    case SIXEL_OPTFLAG_IGNORE_DELAY:  /* g */
4✔
4852
        encoder->fignore_delay = 1;
6✔
4853
        break;
6✔
4854
    case SIXEL_OPTFLAG_VERBOSE:  /* v */
6✔
4855
        encoder->verbose = 1;
9✔
4856
        sixel_helper_set_loader_trace(1);
9✔
4857
        break;
9✔
4858
    case SIXEL_OPTFLAG_LOADERS:  /* J */
4859
        if (encoder->loader_order != NULL) {
×
4860
            sixel_allocator_free(encoder->allocator,
×
4861
                                 encoder->loader_order);
×
4862
            encoder->loader_order = NULL;
×
4863
        }
4864
        if (value != NULL && *value != '\0') {
×
4865
            encoder->loader_order = arg_strdup(value,
×
4866
                                               encoder->allocator);
4867
            if (encoder->loader_order == NULL) {
×
4868
                sixel_helper_set_additional_message(
×
4869
                    "sixel_encoder_setopt: "
4870
                    "sixel_allocator_malloc() failed.");
4871
                status = SIXEL_BAD_ALLOCATION;
×
4872
                goto end;
×
4873
            }
4874
        }
4875
        break;
×
4876
    case SIXEL_OPTFLAG_STATIC:  /* S */
2✔
4877
        encoder->fstatic = 1;
3✔
4878
        break;
3✔
4879
    case SIXEL_OPTFLAG_DRCS:  /* @ */
4880
        encoder->fdrcs = 1;
×
4881
        drcs_arg_delim = NULL;
×
4882
        drcs_arg_charset = NULL;
×
4883
        drcs_arg_second_delim = NULL;
×
4884
        drcs_arg_path = NULL;
×
4885
        drcs_arg_path_length = 0u;
×
4886
        drcs_segment_length = 0u;
×
4887
        drcs_mmv_value = 2;
×
4888
        drcs_charset_value = 1L;
×
4889
        drcs_charset_limit = 0u;
×
4890
        if (value != NULL && *value != '\0') {
×
4891
            drcs_arg_delim = strchr(value, ':');
×
4892
            if (drcs_arg_delim == NULL) {
×
4893
                drcs_segment_length = strlen(value);
×
4894
                if (drcs_segment_length >= sizeof(drcs_segment)) {
×
4895
                    sixel_helper_set_additional_message(
×
4896
                        "DRCS mapping revision is too long.");
4897
                    status = SIXEL_BAD_ARGUMENT;
×
4898
                    goto end;
×
4899
                }
4900
                memcpy(drcs_segment, value, drcs_segment_length);
×
4901
                drcs_segment[drcs_segment_length] = '\0';
×
4902
                errno = 0;
×
4903
                endptr = NULL;
×
4904
                drcs_mmv_value = (int)strtol(drcs_segment, &endptr, 10);
×
4905
                if (errno != 0 || endptr == drcs_segment || *endptr != '\0') {
×
4906
                    sixel_helper_set_additional_message(
×
4907
                        "cannot parse DRCS option.");
4908
                    status = SIXEL_BAD_ARGUMENT;
×
4909
                    goto end;
×
4910
                }
4911
            } else {
4912
                if (drcs_arg_delim != value) {
×
4913
                    drcs_segment_length =
×
4914
                        (size_t)(drcs_arg_delim - value);
×
4915
                    if (drcs_segment_length >= sizeof(drcs_segment)) {
×
4916
                        sixel_helper_set_additional_message(
×
4917
                            "DRCS mapping revision is too long.");
4918
                        status = SIXEL_BAD_ARGUMENT;
×
4919
                        goto end;
×
4920
                    }
4921
                    memcpy(drcs_segment, value, drcs_segment_length);
×
4922
                    drcs_segment[drcs_segment_length] = '\0';
×
4923
                    errno = 0;
×
4924
                    endptr = NULL;
×
4925
                    drcs_mmv_value = (int)strtol(drcs_segment, &endptr, 10);
×
4926
                    if (errno != 0 || endptr == drcs_segment || *endptr != '\0') {
×
4927
                        sixel_helper_set_additional_message(
×
4928
                            "cannot parse DRCS option.");
4929
                        status = SIXEL_BAD_ARGUMENT;
×
4930
                        goto end;
×
4931
                    }
4932
                }
4933
                drcs_arg_charset = drcs_arg_delim + 1;
×
4934
                drcs_arg_second_delim = strchr(drcs_arg_charset, ':');
×
4935
                if (drcs_arg_second_delim != NULL) {
×
4936
                    if (drcs_arg_second_delim != drcs_arg_charset) {
×
4937
                        drcs_segment_length =
×
4938
                            (size_t)(drcs_arg_second_delim - drcs_arg_charset);
×
4939
                        if (drcs_segment_length >= sizeof(drcs_segment)) {
×
4940
                            sixel_helper_set_additional_message(
×
4941
                                "DRCS charset number is too long.");
4942
                            status = SIXEL_BAD_ARGUMENT;
×
4943
                            goto end;
×
4944
                        }
4945
                        memcpy(drcs_segment,
×
4946
                               drcs_arg_charset,
4947
                               drcs_segment_length);
4948
                        drcs_segment[drcs_segment_length] = '\0';
×
4949
                        errno = 0;
×
4950
                        endptr = NULL;
×
4951
                        drcs_charset_value = strtol(drcs_segment,
×
4952
                                                    &endptr,
4953
                                                    10);
4954
                        if (errno != 0 || endptr == drcs_segment ||
×
4955
                                *endptr != '\0') {
×
4956
                            sixel_helper_set_additional_message(
×
4957
                                "cannot parse DRCS charset number.");
4958
                            status = SIXEL_BAD_ARGUMENT;
×
4959
                            goto end;
×
4960
                        }
4961
                    }
4962
                    drcs_arg_path = drcs_arg_second_delim + 1;
×
4963
                    drcs_arg_path_length = strlen(drcs_arg_path);
×
4964
                    if (drcs_arg_path_length == 0u) {
×
4965
                        drcs_arg_path = NULL;
×
4966
                    }
4967
                } else if (*drcs_arg_charset != '\0') {
×
4968
                    drcs_segment_length = strlen(drcs_arg_charset);
×
4969
                    if (drcs_segment_length >= sizeof(drcs_segment)) {
×
4970
                        sixel_helper_set_additional_message(
×
4971
                            "DRCS charset number is too long.");
4972
                        status = SIXEL_BAD_ARGUMENT;
×
4973
                        goto end;
×
4974
                    }
4975
                    memcpy(drcs_segment,
×
4976
                           drcs_arg_charset,
4977
                           drcs_segment_length);
4978
                    drcs_segment[drcs_segment_length] = '\0';
×
4979
                    errno = 0;
×
4980
                    endptr = NULL;
×
4981
                    drcs_charset_value = strtol(drcs_segment,
×
4982
                                                &endptr,
4983
                                                10);
4984
                    if (errno != 0 || endptr == drcs_segment ||
×
4985
                            *endptr != '\0') {
×
4986
                        sixel_helper_set_additional_message(
×
4987
                            "cannot parse DRCS charset number.");
4988
                        status = SIXEL_BAD_ARGUMENT;
×
4989
                        goto end;
×
4990
                    }
4991
                }
4992
            }
4993
        }
4994
        /*
4995
         * Layout of the DRCS option value:
4996
         *
4997
         *    value = <mmv>:<charset_no>:<path>
4998
         *          ^        ^                ^
4999
         *          |        |                |
5000
         *          |        |                +-- optional path that may reuse
5001
         *          |        |                    STDOUT when set to "-" or drop
5002
         *          |        |                    tiles when left blank
5003
         *          |        +-- charset number (defaults to 1 when omitted)
5004
         *          +-- mapping revision (defaults to 2 when omitted)
5005
         */
5006
        if (drcs_mmv_value < 0 || drcs_mmv_value > 2) {
×
5007
            sixel_helper_set_additional_message(
×
5008
                "unknown DRCS unicode mapping version.");
5009
            status = SIXEL_BAD_ARGUMENT;
×
5010
            goto end;
×
5011
        }
5012
        if (drcs_mmv_value == 0) {
×
5013
            drcs_charset_limit = 126u;
×
5014
        } else if (drcs_mmv_value == 1) {
×
5015
            drcs_charset_limit = 63u;
×
5016
        } else {
5017
            drcs_charset_limit = 158u;
×
5018
        }
5019
        if (drcs_charset_value < 1 ||
×
5020
            (unsigned long)drcs_charset_value > drcs_charset_limit) {
×
5021
            sixel_helper_set_additional_message(
×
5022
                "DRCS charset number is out of range.");
5023
            status = SIXEL_BAD_ARGUMENT;
×
5024
            goto end;
×
5025
        }
5026
        encoder->drcs_mmv = drcs_mmv_value;
×
5027
        encoder->drcs_charset_no = (unsigned short)drcs_charset_value;
×
5028
        if (encoder->tile_outfd >= 0
×
5029
            && encoder->tile_outfd != encoder->outfd
×
5030
            && encoder->tile_outfd != STDOUT_FILENO
×
5031
            && encoder->tile_outfd != STDERR_FILENO) {
×
5032
#if HAVE__CLOSE
5033
            (void) _close(encoder->tile_outfd);
5034
#else
5035
            (void) close(encoder->tile_outfd);
×
5036
#endif  /* HAVE__CLOSE */
5037
        }
5038
        encoder->tile_outfd = (-1);
×
5039
        if (drcs_arg_path != NULL) {
×
5040
            if (strcmp(drcs_arg_path, "-") == 0) {
×
5041
                encoder->tile_outfd = STDOUT_FILENO;
×
5042
            } else {
5043
#if HAVE__OPEN
5044
                encoder->tile_outfd = _open(drcs_arg_path,
5045
                                            O_RDWR|O_CREAT|O_TRUNC,
5046
                                            S_IRUSR|S_IWUSR);
5047
#else
5048
                encoder->tile_outfd = open(drcs_arg_path,
×
5049
                                           O_RDWR|O_CREAT|O_TRUNC,
5050
                                           S_IRUSR|S_IWUSR);
5051
#endif  /* HAVE__OPEN */
5052
                if (encoder->tile_outfd < 0) {
×
5053
                    sixel_helper_set_additional_message(
×
5054
                        "sixel_encoder_setopt: failed to open tile"
5055
                        " output path.");
5056
                    status = SIXEL_RUNTIME_ERROR;
×
5057
                    goto end;
×
5058
                }
5059
            }
5060
        }
5061
        break;
×
5062
    case SIXEL_OPTFLAG_PENETRATE:  /* P */
6✔
5063
        encoder->penetrate_multiplexer = 1;
9✔
5064
        break;
9✔
5065
    case SIXEL_OPTFLAG_ENCODE_POLICY:  /* E */
8✔
5066
        match_result = sixel_match_option_choice(
12✔
5067
            value,
4✔
5068
            g_option_choices_encode_policy,
5069
            sizeof(g_option_choices_encode_policy) /
5070
            sizeof(g_option_choices_encode_policy[0]),
5071
            &match_value,
5072
            match_detail,
4✔
5073
            sizeof(match_detail));
5074
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
12✔
5075
            encoder->encode_policy = match_value;
9✔
5076
        } else {
3✔
5077
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
3!
5078
                sixel_report_ambiguous_prefix("--encode-policy",
×
5079
                                              value,
5080
                                              match_detail,
5081
                                              match_message,
5082
                                              sizeof(match_message));
5083
            } else {
5084
                sixel_helper_set_additional_message(
3✔
5085
                    "cannot parse encode policy option.");
5086
            }
5087
            status = SIXEL_BAD_ARGUMENT;
3✔
5088
            goto end;
3✔
5089
        }
5090
        break;
9✔
5091
    case SIXEL_OPTFLAG_LUT_POLICY:  /* L */
5092
        match_result = sixel_match_option_choice(
×
5093
            value,
5094
            g_option_choices_lut_policy,
5095
            sizeof(g_option_choices_lut_policy) /
5096
            sizeof(g_option_choices_lut_policy[0]),
5097
            &match_value,
5098
            match_detail,
5099
            sizeof(match_detail));
5100
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
×
5101
            encoder->lut_policy = match_value;
×
5102
        } else {
5103
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
5104
                sixel_report_ambiguous_prefix("--lut-policy",
×
5105
                                              value,
5106
                                              match_detail,
5107
                                              match_message,
5108
                                              sizeof(match_message));
5109
            } else {
5110
                sixel_helper_set_additional_message(
×
5111
                    "cannot parse lut policy option.");
5112
            }
5113
            status = SIXEL_BAD_ARGUMENT;
×
5114
            goto end;
×
5115
        }
5116
        if (encoder->dither_cache != NULL) {
×
5117
            sixel_dither_set_lut_policy(encoder->dither_cache,
×
5118
                                        encoder->lut_policy);
5119
        }
5120
        break;
×
5121
    case SIXEL_OPTFLAG_WORKING_COLORSPACE:  /* W */
5122
        if (value == NULL) {
×
5123
            sixel_helper_set_additional_message(
×
5124
                "working-colorspace requires an argument.");
5125
            status = SIXEL_BAD_ARGUMENT;
×
5126
            goto end;
×
5127
        } else {
5128
            len = strlen(value);
×
5129

5130
            if (len >= sizeof(lowered)) {
×
5131
                sixel_helper_set_additional_message(
×
5132
                    "specified working colorspace name is too long.");
5133
                status = SIXEL_BAD_ARGUMENT;
×
5134
                goto end;
×
5135
            }
5136
            for (i = 0; i < len; ++i) {
×
5137
                lowered[i] = (char)tolower((unsigned char)value[i]);
×
5138
            }
5139
            lowered[len] = '\0';
×
5140

5141
            match_result = sixel_match_option_choice(
×
5142
                lowered,
5143
                g_option_choices_working_colorspace,
5144
                sizeof(g_option_choices_working_colorspace) /
5145
                sizeof(g_option_choices_working_colorspace[0]),
5146
                &match_value,
5147
                match_detail,
5148
                sizeof(match_detail));
5149
            if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
×
5150
                encoder->working_colorspace = match_value;
×
5151
            } else {
5152
                if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
5153
                    sixel_report_ambiguous_prefix(
×
5154
                        "--working-colorspace",
5155
                        value,
5156
                        match_detail,
5157
                        match_message,
5158
                        sizeof(match_message));
5159
                } else {
5160
                    sixel_helper_set_additional_message(
×
5161
                        "unsupported working colorspace specified.");
5162
                }
5163
                status = SIXEL_BAD_ARGUMENT;
×
5164
                goto end;
×
5165
            }
5166
        }
5167
        break;
×
5168
    case SIXEL_OPTFLAG_OUTPUT_COLORSPACE:  /* U */
5169
        if (value == NULL) {
×
5170
            sixel_helper_set_additional_message(
×
5171
                "output-colorspace requires an argument.");
5172
            status = SIXEL_BAD_ARGUMENT;
×
5173
            goto end;
×
5174
        } else {
5175
            len = strlen(value);
×
5176

5177
            if (len >= sizeof(lowered)) {
×
5178
                sixel_helper_set_additional_message(
×
5179
                    "specified output colorspace name is too long.");
5180
                status = SIXEL_BAD_ARGUMENT;
×
5181
                goto end;
×
5182
            }
5183
            for (i = 0; i < len; ++i) {
×
5184
                lowered[i] = (char)tolower((unsigned char)value[i]);
×
5185
            }
5186
            lowered[len] = '\0';
×
5187

5188
            match_result = sixel_match_option_choice(
×
5189
                lowered,
5190
                g_option_choices_output_colorspace,
5191
                sizeof(g_option_choices_output_colorspace) /
5192
                sizeof(g_option_choices_output_colorspace[0]),
5193
                &match_value,
5194
                match_detail,
5195
                sizeof(match_detail));
5196
            if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
×
5197
                encoder->output_colorspace = match_value;
×
5198
            } else {
5199
                if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
5200
                    sixel_report_ambiguous_prefix(
×
5201
                        "--output-colorspace",
5202
                        value,
5203
                        match_detail,
5204
                        match_message,
5205
                        sizeof(match_message));
5206
                } else {
5207
                    sixel_helper_set_additional_message(
×
5208
                        "unsupported output colorspace specified.");
5209
                }
5210
                status = SIXEL_BAD_ARGUMENT;
×
5211
                goto end;
×
5212
            }
5213
        }
5214
        break;
×
5215
    case SIXEL_OPTFLAG_ORMODE:  /* O */
5216
        encoder->ormode = 1;
×
5217
        break;
×
5218
    case SIXEL_OPTFLAG_COMPLEXION_SCORE:  /* C */
6✔
5219
        encoder->complexion = atoi(value);
9✔
5220
        if (encoder->complexion < 1) {
9✔
5221
            sixel_helper_set_additional_message(
3✔
5222
                "complexion parameter must be 1 or more.");
5223
            status = SIXEL_BAD_ARGUMENT;
3✔
5224
            goto end;
3✔
5225
        }
5226
        break;
6✔
5227
    case SIXEL_OPTFLAG_PIPE_MODE:  /* D */
5228
        encoder->pipe_mode = 1;
×
5229
        break;
×
UNCOV
5230
    case '?':  /* unknown option */
×
5231
    default:
5232
        /* exit if unknown options are specified */
UNCOV
5233
        sixel_helper_set_additional_message(
×
5234
            "unknown option is specified.");
UNCOV
5235
        status = SIXEL_BAD_ARGUMENT;
×
UNCOV
5236
        goto end;
×
5237
    }
5238

5239
    /* detects arguments conflictions */
5240
    if (encoder->reqcolors != (-1)) {
624✔
5241
        switch (encoder->color_option) {
99!
5242
        case SIXEL_COLOR_OPTION_MAPFILE:
5243
            sixel_helper_set_additional_message(
×
5244
                "option -p, --colors conflicts with -m, --mapfile.");
5245
            status = SIXEL_BAD_ARGUMENT;
×
5246
            goto end;
×
5247
        case SIXEL_COLOR_OPTION_MONOCHROME:
2✔
5248
            sixel_helper_set_additional_message(
3✔
5249
                "option -e, --monochrome conflicts with -p, --colors.");
5250
            status = SIXEL_BAD_ARGUMENT;
3✔
5251
            goto end;
3✔
5252
        case SIXEL_COLOR_OPTION_HIGHCOLOR:
2✔
5253
            sixel_helper_set_additional_message(
3✔
5254
                "option -p, --colors conflicts with -I, --high-color.");
5255
            status = SIXEL_BAD_ARGUMENT;
3✔
5256
            goto end;
3✔
5257
        case SIXEL_COLOR_OPTION_BUILTIN:
2✔
5258
            sixel_helper_set_additional_message(
3✔
5259
                "option -p, --colors conflicts with -b, --builtin-palette.");
5260
            status = SIXEL_BAD_ARGUMENT;
3✔
5261
            goto end;
3✔
5262
        default:
60✔
5263
            break;
90✔
5264
        }
5265
    }
30✔
5266

5267
    /* 8bit output option(-8) conflicts width GNU Screen integration(-P) */
5268
    if (encoder->f8bit && encoder->penetrate_multiplexer) {
615✔
5269
        sixel_helper_set_additional_message(
3✔
5270
            "option -8 --8bit-mode conflicts"
5271
            " with -P, --penetrate.");
5272
        status = SIXEL_BAD_ARGUMENT;
3✔
5273
        goto end;
3✔
5274
    }
5275

5276
    status = SIXEL_OK;
612✔
5277

5278
end:
456✔
5279
    if (opt_copy != NULL) {
684!
5280
        sixel_allocator_free(encoder->allocator, opt_copy);
×
5281
    }
5282
    sixel_encoder_unref(encoder);
684✔
5283

5284
    return status;
684✔
5285
}
5286

5287

5288
/* called when image loader component load a image frame */
5289
static SIXELSTATUS
5290
load_image_callback(sixel_frame_t *frame, void *data)
530✔
5291
{
5292
    sixel_encoder_t *encoder;
5293

5294
    encoder = (sixel_encoder_t *)data;
530✔
5295
    if (encoder->capture_source && encoder->capture_source_frame == NULL) {
530!
5296
        sixel_frame_ref(frame);
×
5297
        encoder->capture_source_frame = frame;
×
5298
    }
5299

5300
    return sixel_encoder_encode_frame(encoder, frame, NULL);
530✔
5301
}
5302

5303

5304
/* load source data from specified file and encode it to SIXEL format
5305
 * output to encoder->outfd */
5306
SIXELAPI SIXELSTATUS
5307
sixel_encoder_encode(
429✔
5308
    sixel_encoder_t *encoder,   /* encoder object */
5309
    char const      *filename)  /* input filename */
5310
{
5311
    SIXELSTATUS status = SIXEL_FALSE;
429✔
5312
    SIXELSTATUS palette_status = SIXEL_OK;
429✔
5313
    int fuse_palette = 1;
429✔
5314
    sixel_loader_t *loader;
5315

5316
    if (encoder == NULL) {
429!
5317
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5318
#  pragma GCC diagnostic push
5319
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
5320
#endif
5321
        encoder = sixel_encoder_create();
×
5322
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5323
#  pragma GCC diagnostic pop
5324
#endif
5325
        if (encoder == NULL) {
×
5326
            sixel_helper_set_additional_message(
×
5327
                "sixel_encoder_encode: sixel_encoder_create() failed.");
5328
            status = SIXEL_BAD_ALLOCATION;
×
5329
            goto end;
×
5330
        }
5331
    } else {
5332
        sixel_encoder_ref(encoder);
429✔
5333
    }
5334

5335
    if (encoder->assessment_observer != NULL) {
429!
5336
        sixel_assessment_stage_transition(
×
5337
            encoder->assessment_observer,
×
5338
            SIXEL_ASSESSMENT_STAGE_IMAGE_CHUNK);
5339
    }
5340
    encoder->last_loader_name[0] = '\0';
429✔
5341
    encoder->last_source_path[0] = '\0';
429✔
5342
    encoder->last_input_bytes = 0u;
429✔
5343

5344
    /* if required color is not set, set the max value */
5345
    if (encoder->reqcolors == (-1)) {
429✔
5346
        encoder->reqcolors = SIXEL_PALETTE_MAX;
411✔
5347
    }
137✔
5348

5349
    if (encoder->capture_source && encoder->capture_source_frame != NULL) {
429!
5350
        sixel_frame_unref(encoder->capture_source_frame);
×
5351
        encoder->capture_source_frame = NULL;
×
5352
    }
5353

5354
    /* if required color is less then 2, set the min value */
5355
    if (encoder->reqcolors < 2) {
429✔
5356
        encoder->reqcolors = SIXEL_PALETTE_MIN;
3✔
5357
    }
1✔
5358

5359
    /* if color space option is not set, choose RGB color space */
5360
    if (encoder->palette_type == SIXEL_PALETTETYPE_AUTO) {
429✔
5361
        encoder->palette_type = SIXEL_PALETTETYPE_RGB;
411✔
5362
    }
137✔
5363

5364
    /* if color option is not default value, prohibit to read
5365
       the file as a paletted image */
5366
    if (encoder->color_option != SIXEL_COLOR_OPTION_DEFAULT) {
429✔
5367
        fuse_palette = 0;
99✔
5368
    }
33✔
5369

5370
    /* if scaling options are set, prohibit to read the file as
5371
       a paletted image */
5372
    if (encoder->percentwidth > 0 ||
549✔
5373
        encoder->percentheight > 0 ||
417✔
5374
        encoder->pixelwidth > 0 ||
411✔
5375
        encoder->pixelheight > 0) {
393✔
5376
        fuse_palette = 0;
99✔
5377
    }
33✔
5378

5379
reload:
286✔
5380
    loader = NULL;
429✔
5381

5382
    sixel_helper_set_loader_trace(encoder->verbose);
429✔
5383
    sixel_helper_set_thumbnail_size_hint(
429✔
5384
        sixel_encoder_thumbnail_hint(encoder));
143✔
5385

5386
    status = sixel_loader_new(&loader, encoder->allocator);
429✔
5387
    if (SIXEL_FAILED(status)) {
429!
5388
        goto load_end;
×
5389
    }
5390

5391
    status = sixel_loader_setopt(loader,
572✔
5392
                                 SIXEL_LOADER_OPTION_REQUIRE_STATIC,
5393
                                 &encoder->fstatic);
429✔
5394
    if (SIXEL_FAILED(status)) {
429!
5395
        goto load_end;
×
5396
    }
5397

5398
    status = sixel_loader_setopt(loader,
429✔
5399
                                 SIXEL_LOADER_OPTION_USE_PALETTE,
5400
                                 &fuse_palette);
5401
    if (SIXEL_FAILED(status)) {
429!
5402
        goto load_end;
×
5403
    }
5404

5405
    status = sixel_loader_setopt(loader,
572✔
5406
                                 SIXEL_LOADER_OPTION_REQCOLORS,
5407
                                 &encoder->reqcolors);
429✔
5408
    if (SIXEL_FAILED(status)) {
429!
5409
        goto load_end;
×
5410
    }
5411

5412
    status = sixel_loader_setopt(loader,
572✔
5413
                                 SIXEL_LOADER_OPTION_BGCOLOR,
5414
                                 encoder->bgcolor);
429✔
5415
    if (SIXEL_FAILED(status)) {
429!
5416
        goto load_end;
×
5417
    }
5418

5419
    status = sixel_loader_setopt(loader,
572✔
5420
                                 SIXEL_LOADER_OPTION_LOOP_CONTROL,
5421
                                 &encoder->loop_mode);
429✔
5422
    if (SIXEL_FAILED(status)) {
429!
5423
        goto load_end;
×
5424
    }
5425

5426
    status = sixel_loader_setopt(loader,
572✔
5427
                                 SIXEL_LOADER_OPTION_INSECURE,
5428
                                 &encoder->finsecure);
429✔
5429
    if (SIXEL_FAILED(status)) {
429!
5430
        goto load_end;
×
5431
    }
5432

5433
    status = sixel_loader_setopt(loader,
572✔
5434
                                 SIXEL_LOADER_OPTION_CANCEL_FLAG,
5435
                                 encoder->cancel_flag);
429✔
5436
    if (SIXEL_FAILED(status)) {
429!
5437
        goto load_end;
×
5438
    }
5439

5440
    status = sixel_loader_setopt(loader,
572✔
5441
                                 SIXEL_LOADER_OPTION_LOADER_ORDER,
5442
                                 encoder->loader_order);
429✔
5443
    if (SIXEL_FAILED(status)) {
429!
5444
        goto load_end;
×
5445
    }
5446

5447
    status = sixel_loader_setopt(loader,
572✔
5448
                                 SIXEL_LOADER_OPTION_CONTEXT,
5449
                                 encoder);
143✔
5450
    if (SIXEL_FAILED(status)) {
429!
5451
        goto load_end;
×
5452
    }
5453

5454
    /*
5455
     * Wire the optional assessment observer into the loader.
5456
     *
5457
     * The observer travels separately from the callback context so mapfile
5458
     * palette probes and other callbacks can keep using arbitrary structs.
5459
     */
5460
    status = sixel_loader_setopt(loader,
572✔
5461
                                 SIXEL_LOADER_OPTION_ASSESSMENT,
5462
                                 encoder->assessment_observer);
429✔
5463
    if (SIXEL_FAILED(status)) {
429!
5464
        goto load_end;
×
5465
    }
5466

5467
    status = sixel_loader_load_file(loader,
572✔
5468
                                    filename,
143✔
5469
                                    load_image_callback);
5470
    if (status != SIXEL_OK) {
429✔
5471
        goto load_end;
21✔
5472
    }
5473
    encoder->last_input_bytes = sixel_loader_get_last_input_bytes(loader);
408✔
5474
    if (sixel_loader_get_last_success_name(loader) != NULL) {
408!
5475
        (void)snprintf(encoder->last_loader_name,
408✔
5476
                       sizeof(encoder->last_loader_name),
5477
                       "%s",
5478
                       sixel_loader_get_last_success_name(loader));
5479
    } else {
136✔
5480
        encoder->last_loader_name[0] = '\0';
×
5481
    }
5482
    if (sixel_loader_get_last_source_path(loader) != NULL) {
408✔
5483
        (void)snprintf(encoder->last_source_path,
270✔
5484
                       sizeof(encoder->last_source_path),
5485
                       "%s",
5486
                       sixel_loader_get_last_source_path(loader));
5487
    } else {
90✔
5488
        encoder->last_source_path[0] = '\0';
138✔
5489
    }
5490
    if (encoder->assessment_observer != NULL) {
408!
5491
        sixel_assessment_record_loader(encoder->assessment_observer,
×
5492
                                       encoder->last_source_path,
×
5493
                                       encoder->last_loader_name,
×
5494
                                       encoder->last_input_bytes);
5495
    }
5496

5497
load_end:
272✔
5498
    sixel_loader_unref(loader);
429✔
5499
    loader = NULL;
429✔
5500

5501
    if (status != SIXEL_OK) {
429✔
5502
        goto end;
21✔
5503
    }
5504

5505
    palette_status = sixel_encoder_emit_palette_output(encoder);
408✔
5506
    if (SIXEL_FAILED(palette_status)) {
408!
5507
        status = palette_status;
×
5508
        goto end;
×
5509
    }
5510

5511
    if (encoder->pipe_mode) {
408!
5512
#if HAVE_CLEARERR
5513
        clearerr(stdin);
×
5514
#endif  /* HAVE_FSEEK */
5515
        while (encoder->cancel_flag && !*encoder->cancel_flag) {
×
5516
            status = sixel_tty_wait_stdin(1000000);
×
5517
            if (SIXEL_FAILED(status)) {
×
5518
                goto end;
×
5519
            }
5520
            if (status != SIXEL_OK) {
×
5521
                break;
×
5522
            }
5523
        }
5524
        if (!encoder->cancel_flag || !*encoder->cancel_flag) {
×
5525
            goto reload;
×
5526
        }
5527
    }
5528

5529
    /* the status may not be SIXEL_OK */
5530

5531
end:
272✔
5532
    sixel_encoder_unref(encoder);
429✔
5533

5534
    return status;
429✔
5535
}
5536

5537

5538
/* encode specified pixel data to SIXEL format
5539
 * output to encoder->outfd */
5540
SIXELAPI SIXELSTATUS
5541
sixel_encoder_encode_bytes(
×
5542
    sixel_encoder_t     /* in */    *encoder,
5543
    unsigned char       /* in */    *bytes,
5544
    int                 /* in */    width,
5545
    int                 /* in */    height,
5546
    int                 /* in */    pixelformat,
5547
    unsigned char       /* in */    *palette,
5548
    int                 /* in */    ncolors)
5549
{
5550
    SIXELSTATUS status = SIXEL_FALSE;
×
5551
    sixel_frame_t *frame = NULL;
×
5552

5553
    if (encoder == NULL || bytes == NULL) {
×
5554
        status = SIXEL_BAD_ARGUMENT;
×
5555
        goto end;
×
5556
    }
5557

5558
    status = sixel_frame_new(&frame, encoder->allocator);
×
5559
    if (SIXEL_FAILED(status)) {
×
5560
        goto end;
×
5561
    }
5562

5563
    status = sixel_frame_init(frame, bytes, width, height,
×
5564
                              pixelformat, palette, ncolors);
5565
    if (SIXEL_FAILED(status)) {
×
5566
        goto end;
×
5567
    }
5568

5569
    status = sixel_encoder_encode_frame(encoder, frame, NULL);
×
5570
    if (SIXEL_FAILED(status)) {
×
5571
        goto end;
×
5572
    }
5573

5574
    status = SIXEL_OK;
×
5575

5576
end:
5577
    /* we need to free the frame before exiting, but we can't use the
5578
       sixel_frame_destroy function, because that will also attempt to
5579
       free the pixels and palette, which we don't own */
5580
    if (frame != NULL && encoder->allocator != NULL) {
×
5581
        sixel_allocator_free(encoder->allocator, frame);
×
5582
        sixel_allocator_unref(encoder->allocator);
×
5583
    }
5584
    return status;
×
5585
}
5586

5587

5588
/*
5589
 * Toggle source-frame capture for assessment consumers.
5590
 */
5591
SIXELAPI SIXELSTATUS
5592
sixel_encoder_enable_source_capture(
×
5593
    sixel_encoder_t *encoder,
5594
    int enable)
5595
{
5596
    if (encoder == NULL) {
×
5597
        sixel_helper_set_additional_message(
×
5598
            "sixel_encoder_enable_source_capture: encoder is null.");
5599
        return SIXEL_BAD_ARGUMENT;
×
5600
    }
5601

5602
    encoder->capture_source = enable ? 1 : 0;
×
5603
    if (!encoder->capture_source && encoder->capture_source_frame != NULL) {
×
5604
        sixel_frame_unref(encoder->capture_source_frame);
×
5605
        encoder->capture_source_frame = NULL;
×
5606
    }
5607

5608
    return SIXEL_OK;
×
5609
}
5610

5611

5612
/*
5613
 * Enable or disable the quantized-frame capture facility.
5614
 *
5615
 *     capture on --> encoder keeps the latest palette-quantized frame.
5616
 *     capture off --> encoder forgets previously stored frames.
5617
 */
5618
SIXELAPI SIXELSTATUS
5619
sixel_encoder_enable_quantized_capture(
×
5620
    sixel_encoder_t *encoder,
5621
    int enable)
5622
{
5623
    if (encoder == NULL) {
×
5624
        sixel_helper_set_additional_message(
×
5625
            "sixel_encoder_enable_quantized_capture: encoder is null.");
5626
        return SIXEL_BAD_ARGUMENT;
×
5627
    }
5628

5629
    encoder->capture_quantized = enable ? 1 : 0;
×
5630
    if (!encoder->capture_quantized) {
×
5631
        encoder->capture_valid = 0;
×
5632
    }
5633

5634
    return SIXEL_OK;
×
5635
}
5636

5637

5638
/*
5639
 * Materialize the captured quantized frame as a heap-allocated
5640
 * sixel_frame_t instance for assessment consumers.
5641
 */
5642
SIXELAPI SIXELSTATUS
5643
sixel_encoder_copy_quantized_frame(
×
5644
    sixel_encoder_t   *encoder,
5645
    sixel_allocator_t *allocator,
5646
    sixel_frame_t     **ppframe)
5647
{
5648
    SIXELSTATUS status = SIXEL_FALSE;
×
5649
    sixel_frame_t *frame;
5650
    unsigned char *pixels;
5651
    unsigned char *palette;
5652
    size_t palette_bytes;
5653

5654
    if (encoder == NULL || allocator == NULL || ppframe == NULL) {
×
5655
        sixel_helper_set_additional_message(
×
5656
            "sixel_encoder_copy_quantized_frame: invalid argument.");
5657
        return SIXEL_BAD_ARGUMENT;
×
5658
    }
5659

5660
    if (!encoder->capture_quantized || !encoder->capture_valid) {
×
5661
        sixel_helper_set_additional_message(
×
5662
            "sixel_encoder_copy_quantized_frame: no frame captured.");
5663
        return SIXEL_RUNTIME_ERROR;
×
5664
    }
5665

5666
    *ppframe = NULL;
×
5667
    frame = NULL;
×
5668
    pixels = NULL;
×
5669
    palette = NULL;
×
5670

5671
    status = sixel_frame_new(&frame, allocator);
×
5672
    if (SIXEL_FAILED(status)) {
×
5673
        return status;
×
5674
    }
5675

5676
    if (encoder->capture_pixel_bytes > 0) {
×
5677
        pixels = (unsigned char *)sixel_allocator_malloc(
×
5678
            allocator, encoder->capture_pixel_bytes);
5679
        if (pixels == NULL) {
×
5680
            sixel_helper_set_additional_message(
×
5681
                "sixel_encoder_copy_quantized_frame: "
5682
                "sixel_allocator_malloc() failed.");
5683
            status = SIXEL_BAD_ALLOCATION;
×
5684
            goto cleanup;
×
5685
        }
5686
        memcpy(pixels,
×
5687
               encoder->capture_pixels,
5688
               encoder->capture_pixel_bytes);
5689
    }
5690

5691
    palette_bytes = encoder->capture_palette_size;
×
5692
    if (palette_bytes > 0) {
×
5693
        palette = (unsigned char *)sixel_allocator_malloc(allocator,
×
5694
                                                          palette_bytes);
5695
        if (palette == NULL) {
×
5696
            sixel_helper_set_additional_message(
×
5697
                "sixel_encoder_copy_quantized_frame: "
5698
                "sixel_allocator_malloc() failed.");
5699
            status = SIXEL_BAD_ALLOCATION;
×
5700
            goto cleanup;
×
5701
        }
5702
        memcpy(palette,
×
5703
               encoder->capture_palette,
5704
               palette_bytes);
5705
    }
5706

5707
    status = sixel_frame_init(frame,
×
5708
                              pixels,
5709
                              encoder->capture_width,
5710
                              encoder->capture_height,
5711
                              encoder->capture_pixelformat,
5712
                              palette,
5713
                              encoder->capture_ncolors);
5714
    if (SIXEL_FAILED(status)) {
×
5715
        goto cleanup;
×
5716
    }
5717

5718
    pixels = NULL;
×
5719
    palette = NULL;
×
5720
    frame->colorspace = encoder->capture_colorspace;
×
5721
    *ppframe = frame;
×
5722
    return SIXEL_OK;
×
5723

5724
cleanup:
5725
    if (palette != NULL) {
×
5726
        sixel_allocator_free(allocator, palette);
×
5727
    }
5728
    if (pixels != NULL) {
×
5729
        sixel_allocator_free(allocator, pixels);
×
5730
    }
5731
    if (frame != NULL) {
×
5732
        sixel_frame_unref(frame);
×
5733
    }
5734
    return status;
×
5735
}
5736

5737

5738
/*
5739
 * Emit the captured palette in the requested format.
5740
 *
5741
 *   palette_output == NULL  -> skip
5742
 *   palette_output != NULL  -> materialize captured palette
5743
 */
5744
static SIXELSTATUS
5745
sixel_encoder_emit_palette_output(sixel_encoder_t *encoder)
408✔
5746
{
5747
    SIXELSTATUS status;
5748
    sixel_frame_t *frame;
5749
    unsigned char const *palette;
5750
    int exported_colors;
5751
    FILE *stream;
5752
    int close_stream;
5753
    char const *path;
5754
    sixel_palette_format_t format_hint;
5755
    sixel_palette_format_t format_ext;
5756
    sixel_palette_format_t format_final;
5757
    char const *mode;
5758

5759
    status = SIXEL_OK;
408✔
5760
    frame = NULL;
408✔
5761
    palette = NULL;
408✔
5762
    exported_colors = 0;
408✔
5763
    stream = NULL;
408✔
5764
    close_stream = 0;
408✔
5765
    path = NULL;
408✔
5766
    format_hint = SIXEL_PALETTE_FORMAT_NONE;
408✔
5767
    format_ext = SIXEL_PALETTE_FORMAT_NONE;
408✔
5768
    format_final = SIXEL_PALETTE_FORMAT_NONE;
408✔
5769
    mode = "wb";
408✔
5770

5771
    if (encoder == NULL || encoder->palette_output == NULL) {
408!
5772
        return SIXEL_OK;
408✔
5773
    }
5774

5775
    status = sixel_encoder_copy_quantized_frame(encoder,
×
5776
                                                encoder->allocator,
5777
                                                &frame);
5778
    if (SIXEL_FAILED(status)) {
×
5779
        return status;
×
5780
    }
5781

5782
    palette = (unsigned char const *)sixel_frame_get_palette(frame);
×
5783
    exported_colors = sixel_frame_get_ncolors(frame);
×
5784
    if (palette == NULL || exported_colors <= 0) {
×
5785
        sixel_helper_set_additional_message(
×
5786
            "sixel_encoder_emit_palette_output: palette unavailable.");
5787
        status = SIXEL_BAD_INPUT;
×
5788
        goto cleanup;
×
5789
    }
5790
    if (exported_colors > 256) {
×
5791
        exported_colors = 256;
×
5792
    }
5793

5794
    path = sixel_palette_strip_prefix(encoder->palette_output, &format_hint);
×
5795
    if (path == NULL || *path == '\0') {
×
5796
        sixel_helper_set_additional_message(
×
5797
            "sixel_encoder_emit_palette_output: invalid path.");
5798
        status = SIXEL_BAD_ARGUMENT;
×
5799
        goto cleanup;
×
5800
    }
5801

5802
    format_ext = sixel_palette_format_from_extension(path);
×
5803
    format_final = format_hint;
×
5804
    if (format_final == SIXEL_PALETTE_FORMAT_NONE) {
×
5805
        if (format_ext == SIXEL_PALETTE_FORMAT_NONE) {
×
5806
            if (strcmp(path, "-") == 0) {
×
5807
                sixel_helper_set_additional_message(
×
5808
                    "sixel_encoder_emit_palette_output: "
5809
                    "format required for '-'.");
5810
                status = SIXEL_BAD_ARGUMENT;
×
5811
                goto cleanup;
×
5812
            }
5813
            sixel_helper_set_additional_message(
×
5814
                "sixel_encoder_emit_palette_output: "
5815
                "unknown palette file extension.");
5816
            status = SIXEL_BAD_ARGUMENT;
×
5817
            goto cleanup;
×
5818
        }
5819
        format_final = format_ext;
×
5820
    }
5821
    if (format_final == SIXEL_PALETTE_FORMAT_PAL_AUTO) {
×
5822
        format_final = SIXEL_PALETTE_FORMAT_PAL_JASC;
×
5823
    }
5824

5825
    if (strcmp(path, "-") == 0) {
×
5826
        stream = stdout;
×
5827
    } else {
5828
        if (format_final == SIXEL_PALETTE_FORMAT_PAL_JASC ||
×
5829
                format_final == SIXEL_PALETTE_FORMAT_GPL) {
5830
            mode = "w";
×
5831
        } else {
5832
            mode = "wb";
×
5833
        }
5834
        stream = fopen(path, mode);
×
5835
        if (stream == NULL) {
×
5836
            sixel_helper_set_additional_message(
×
5837
                "sixel_encoder_emit_palette_output: failed to open file.");
5838
            status = SIXEL_LIBC_ERROR;
×
5839
            goto cleanup;
×
5840
        }
5841
        close_stream = 1;
×
5842
    }
5843

5844
    switch (format_final) {
×
5845
    case SIXEL_PALETTE_FORMAT_ACT:
5846
        status = sixel_palette_write_act(stream, palette, exported_colors);
×
5847
        if (SIXEL_FAILED(status)) {
×
5848
            sixel_helper_set_additional_message(
×
5849
                "sixel_encoder_emit_palette_output: failed to write ACT.");
5850
        }
5851
        break;
×
5852
    case SIXEL_PALETTE_FORMAT_PAL_JASC:
5853
        status = sixel_palette_write_pal_jasc(stream,
×
5854
                                              palette,
5855
                                              exported_colors);
5856
        if (SIXEL_FAILED(status)) {
×
5857
            sixel_helper_set_additional_message(
×
5858
                "sixel_encoder_emit_palette_output: failed to write JASC.");
5859
        }
5860
        break;
×
5861
    case SIXEL_PALETTE_FORMAT_PAL_RIFF:
5862
        status = sixel_palette_write_pal_riff(stream,
×
5863
                                              palette,
5864
                                              exported_colors);
5865
        if (SIXEL_FAILED(status)) {
×
5866
            sixel_helper_set_additional_message(
×
5867
                "sixel_encoder_emit_palette_output: failed to write RIFF.");
5868
        }
5869
        break;
×
5870
    case SIXEL_PALETTE_FORMAT_GPL:
5871
        status = sixel_palette_write_gpl(stream,
×
5872
                                         palette,
5873
                                         exported_colors);
5874
        if (SIXEL_FAILED(status)) {
×
5875
            sixel_helper_set_additional_message(
×
5876
                "sixel_encoder_emit_palette_output: failed to write GPL.");
5877
        }
5878
        break;
×
5879
    default:
5880
        sixel_helper_set_additional_message(
×
5881
            "sixel_encoder_emit_palette_output: unsupported format.");
5882
        status = SIXEL_BAD_ARGUMENT;
×
5883
        break;
×
5884
    }
5885
    if (SIXEL_FAILED(status)) {
×
5886
        goto cleanup;
×
5887
    }
5888

5889
    if (close_stream) {
×
5890
        if (fclose(stream) != 0) {
×
5891
            sixel_helper_set_additional_message(
×
5892
                "sixel_encoder_emit_palette_output: fclose() failed.");
5893
            status = SIXEL_LIBC_ERROR;
×
5894
            stream = NULL;
×
5895
            goto cleanup;
×
5896
        }
5897
        stream = NULL;
×
5898
    } else {
5899
        if (fflush(stream) != 0) {
×
5900
            sixel_helper_set_additional_message(
×
5901
                "sixel_encoder_emit_palette_output: fflush() failed.");
5902
            status = SIXEL_LIBC_ERROR;
×
5903
            goto cleanup;
×
5904
        }
5905
    }
5906

5907
cleanup:
5908
    if (close_stream && stream != NULL) {
×
5909
        (void) fclose(stream);
×
5910
    }
5911
    if (frame != NULL) {
×
5912
        sixel_frame_unref(frame);
×
5913
    }
5914

5915
    return status;
×
5916
}
136✔
5917

5918

5919
/*
5920
 * Share the captured source frame with assessment consumers.
5921
 */
5922
SIXELAPI SIXELSTATUS
5923
sixel_encoder_copy_source_frame(
×
5924
    sixel_encoder_t *encoder,
5925
    sixel_frame_t  **ppframe)
5926
{
5927
    if (encoder == NULL || ppframe == NULL) {
×
5928
        sixel_helper_set_additional_message(
×
5929
            "sixel_encoder_copy_source_frame: invalid argument.");
5930
        return SIXEL_BAD_ARGUMENT;
×
5931
    }
5932

5933
    if (!encoder->capture_source || encoder->capture_source_frame == NULL) {
×
5934
        sixel_helper_set_additional_message(
×
5935
            "sixel_encoder_copy_source_frame: no frame captured.");
5936
        return SIXEL_RUNTIME_ERROR;
×
5937
    }
5938

5939
    sixel_frame_ref(encoder->capture_source_frame);
×
5940
    *ppframe = encoder->capture_source_frame;
×
5941

5942
    return SIXEL_OK;
×
5943
}
5944

5945

5946
#if HAVE_TESTS
5947
static int
5948
test1(void)
×
5949
{
5950
    int nret = EXIT_FAILURE;
×
5951
    sixel_encoder_t *encoder = NULL;
×
5952

5953
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5954
#  pragma GCC diagnostic push
5955
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
5956
#endif
5957
    encoder = sixel_encoder_create();
×
5958
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5959
#  pragma GCC diagnostic pop
5960
#endif
5961
    if (encoder == NULL) {
×
5962
        goto error;
×
5963
    }
5964
    sixel_encoder_ref(encoder);
×
5965
    sixel_encoder_unref(encoder);
×
5966
    nret = EXIT_SUCCESS;
×
5967

5968
error:
5969
    sixel_encoder_unref(encoder);
×
5970
    return nret;
×
5971
}
5972

5973

5974
static int
5975
test2(void)
×
5976
{
5977
    int nret = EXIT_FAILURE;
×
5978
    SIXELSTATUS status;
5979
    sixel_encoder_t *encoder = NULL;
×
5980
    sixel_frame_t *frame = NULL;
×
5981
    unsigned char *buffer;
5982
    int height = 0;
×
5983
    int is_animation = 0;
×
5984

5985
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5986
#  pragma GCC diagnostic push
5987
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
5988
#endif
5989
    encoder = sixel_encoder_create();
×
5990
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5991
#  pragma GCC diagnostic pop
5992
#endif
5993
    if (encoder == NULL) {
×
5994
        goto error;
×
5995
    }
5996

5997
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
5998
#  pragma GCC diagnostic push
5999
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
6000
#endif
6001
    frame = sixel_frame_create();
×
6002
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
6003
#  pragma GCC diagnostic pop
6004
#endif
6005
    if (encoder == NULL) {
×
6006
        goto error;
×
6007
    }
6008

6009
    buffer = (unsigned char *)sixel_allocator_malloc(encoder->allocator, 3);
×
6010
    if (buffer == NULL) {
×
6011
        goto error;
×
6012
    }
6013
    status = sixel_frame_init(frame, buffer, 1, 1,
×
6014
                              SIXEL_PIXELFORMAT_RGB888,
6015
                              NULL, 0);
6016
    if (SIXEL_FAILED(status)) {
×
6017
        goto error;
×
6018
    }
6019

6020
    if (sixel_frame_get_loop_no(frame) != 0 || sixel_frame_get_frame_no(frame) != 0) {
×
6021
        is_animation = 1;
×
6022
    }
6023

6024
    height = sixel_frame_get_height(frame);
×
6025

6026
    status = sixel_tty_scroll(sixel_write_callback,
×
6027
                              &encoder->outfd,
×
6028
                              encoder->outfd,
6029
                              height,
6030
                              is_animation);
6031
    if (SIXEL_FAILED(status)) {
×
6032
        goto error;
×
6033
    }
6034

6035
    nret = EXIT_SUCCESS;
×
6036

6037
error:
6038
    sixel_encoder_unref(encoder);
×
6039
    sixel_frame_unref(frame);
×
6040
    return nret;
×
6041
}
6042

6043

6044
static int
6045
test3(void)
×
6046
{
6047
    int nret = EXIT_FAILURE;
×
6048
    int result;
6049

6050
    result = sixel_tty_wait_stdin(1000);
×
6051
    if (result != 0) {
×
6052
        goto error;
×
6053
    }
6054

6055
    nret = EXIT_SUCCESS;
×
6056

6057
error:
6058
    return nret;
×
6059
}
6060

6061

6062
static int
6063
test4(void)
×
6064
{
6065
    int nret = EXIT_FAILURE;
×
6066
    sixel_encoder_t *encoder = NULL;
×
6067
    SIXELSTATUS status;
6068

6069
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
6070
# pragma GCC diagnostic push
6071
# pragma GCC diagnostic ignored "-Wdeprecated-declarations"
6072
#endif
6073
    encoder = sixel_encoder_create();
×
6074
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
6075
# pragma GCC diagnostic pop
6076
#endif
6077
    if (encoder == NULL) {
×
6078
        goto error;
×
6079
    }
6080

6081
    status = sixel_encoder_setopt(encoder,
×
6082
                                  SIXEL_OPTFLAG_LOOPMODE,
6083
                                  "force");
6084
    if (SIXEL_FAILED(status)) {
×
6085
        goto error;
×
6086
    }
6087

6088
    status = sixel_encoder_setopt(encoder,
×
6089
                                  SIXEL_OPTFLAG_PIPE_MODE,
6090
                                  "force");
6091
    if (SIXEL_FAILED(status)) {
×
6092
        goto error;
×
6093
    }
6094

6095
    nret = EXIT_SUCCESS;
×
6096

6097
error:
6098
    sixel_encoder_unref(encoder);
×
6099
    return nret;
×
6100
}
6101

6102

6103
static int
6104
test5(void)
×
6105
{
6106
    int nret = EXIT_FAILURE;
×
6107
    sixel_encoder_t *encoder = NULL;
×
6108
    sixel_allocator_t *allocator = NULL;
×
6109
    SIXELSTATUS status;
6110

6111
    status = sixel_allocator_new(&allocator, NULL, NULL, NULL, NULL);
×
6112
    if (SIXEL_FAILED(status)) {
×
6113
        goto error;
×
6114
    }
6115

6116
    status = sixel_encoder_new(&encoder, allocator);
×
6117
    if (SIXEL_FAILED(status)) {
×
6118
        goto error;
×
6119
    }
6120

6121
    sixel_encoder_ref(encoder);
×
6122
    sixel_encoder_unref(encoder);
×
6123
    nret = EXIT_SUCCESS;
×
6124

6125
error:
6126
    sixel_encoder_unref(encoder);
×
6127
    return nret;
×
6128
}
6129

6130

6131
SIXELAPI int
6132
sixel_encoder_tests_main(void)
×
6133
{
6134
    int nret = EXIT_FAILURE;
×
6135
    size_t i;
6136
    typedef int (* testcase)(void);
6137

6138
    static testcase const testcases[] = {
6139
        test1,
6140
        test2,
6141
        test3,
6142
        test4,
6143
        test5
6144
    };
6145

6146
    for (i = 0; i < sizeof(testcases) / sizeof(testcase); ++i) {
×
6147
        nret = testcases[i]();
×
6148
        if (nret != EXIT_SUCCESS) {
×
6149
            goto error;
×
6150
        }
6151
    }
6152

6153
    nret = EXIT_SUCCESS;
×
6154

6155
error:
6156
    return nret;
×
6157
}
6158
#endif  /* HAVE_TESTS */
6159

6160

6161
/* emacs Local Variables:      */
6162
/* emacs mode: c               */
6163
/* emacs tab-width: 4          */
6164
/* emacs indent-tabs-mode: nil */
6165
/* emacs c-basic-offset: 4     */
6166
/* emacs End:                  */
6167
/* vim: set expandtab ts=4 : */
6168
/* EOF */
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc