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

saitoha / libsixel / 19570751760

21 Nov 2025 12:37PM UTC coverage: 41.533% (+0.9%) from 40.618%
19570751760

push

github

saitoha
dequant: drop -d k_undither+ option

9955 of 33992 branches covered (29.29%)

2 of 2 new or added lines in 1 file covered. (100.0%)

4 existing lines in 1 file now uncovered.

13028 of 31368 relevant lines covered (41.53%)

663105.89 hits per line

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

62.32
/src/decoder.c
1
/*
2
 * Copyright (c) 2014-2025 Hayaki Saito
3
 *
4
 * Permission is hereby granted, free of charge, to any person obtaining a copy of
5
 * this software and associated documentation files (the "Software"), to deal in
6
 * the Software without restriction, including without limitation the rights to
7
 * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
8
 * the Software, and to permit persons to whom the Software is furnished to do so,
9
 * subject to the following conditions:
10
 *
11
 * The above copyright notice and this permission notice shall be included in all
12
 * copies or substantial portions of the Software.
13
 *
14
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
16
 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
17
 * COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
18
 * IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
19
 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20
 */
21

22
#include "config.h"
23

24
/* STDC_HEADERS */
25
#include <stdio.h>
26
#include <stdlib.h>
27
#include <string.h>
28

29
#if HAVE_MATH_H
30
# include <math.h>
31
#endif  /* HAVE_MATH_H */
32
#if HAVE_LIMITS_H
33
# include <limits.h>
34
#endif  /* HAVE_LIMITS_H */
35
#if HAVE_UNISTD_H
36
# include <unistd.h>
37
#elif HAVE_SYS_UNISTD_H
38
# include <sys/unistd.h>
39
#endif  /* HAVE_UNISTD_H */
40
#if HAVE_FCNTL_H
41
# include <fcntl.h>
42
#endif  /* HAVE_FCNTL_H */
43
#if HAVE_SYS_STAT_H
44
# include <sys/stat.h>
45
#endif  /* HAVE_SYS_STAT_H */
46
#if HAVE_ERRNO_H
47
# include <errno.h>
48
#endif  /* HAVE_ERRNO_H */
49
#if HAVE_IO_H
50
#include <io.h>
51
#endif  /* HAVE_IO_H */
52

53
#include "decoder.h"
54
#include "decoder-parallel.h"
55
#include "clipboard.h"
56
#include "compat_stub.h"
57
#include "options.h"
58

59
static void
60
decoder_clipboard_select_format(char *dest,
4✔
61
                                size_t dest_size,
62
                                char const *format,
63
                                char const *fallback)
64
{
65
    char const *source;
66
    size_t limit;
67

68
    if (dest == NULL || dest_size == 0u) {
4!
69
        return;
×
70
    }
71

72
    source = fallback;
4✔
73
    if (format != NULL && format[0] != '\0') {
4!
74
        source = format;
×
75
    }
76

77
    limit = dest_size - 1u;
4✔
78
    if (limit == 0u) {
4!
79
        dest[0] = '\0';
×
80
        return;
×
81
    }
82

83
    (void)snprintf(dest, dest_size, "%.*s", (int)limit, source);
4✔
84
}
2✔
85

86

87
static char *
88
decoder_create_temp_template_with_prefix(sixel_allocator_t *allocator,
3✔
89
                                         char const *prefix,
90
                                         size_t *capacity_out)
91
{
92
    char const *tmpdir;
93
    size_t tmpdir_len;
94
    size_t prefix_len;
95
    size_t suffix_len;
96
    size_t template_len;
97
    char *template_path;
98
    int needs_separator;
99
    size_t maximum_tmpdir_len;
100

101
    tmpdir = sixel_compat_getenv("TMPDIR");
3✔
102
#if defined(_WIN32)
103
    if (tmpdir == NULL || tmpdir[0] == '\0') {
104
        tmpdir = sixel_compat_getenv("TEMP");
105
    }
106
    if (tmpdir == NULL || tmpdir[0] == '\0') {
107
        tmpdir = sixel_compat_getenv("TMP");
108
    }
109
#endif
110
    if (tmpdir == NULL || tmpdir[0] == '\0') {
3!
111
#if defined(_WIN32)
112
        tmpdir = ".";
113
#else
114
        tmpdir = "/tmp";
2✔
115
#endif
116
    }
117

118
    tmpdir_len = strlen(tmpdir);
3✔
119
    prefix_len = strlen(prefix);
3✔
120
    suffix_len = prefix_len + strlen("-XXXXXX");
3✔
121
    maximum_tmpdir_len = (size_t)INT_MAX;
3✔
122

123
    if (maximum_tmpdir_len <= suffix_len + 2) {
3!
124
        return NULL;
×
125
    }
126
    if (tmpdir_len > maximum_tmpdir_len - (suffix_len + 2)) {
3!
127
        return NULL;
×
128
    }
129

130
    needs_separator = 1;
3✔
131
    if (tmpdir_len > 0) {
3!
132
        if (tmpdir[tmpdir_len - 1] == '/' || tmpdir[tmpdir_len - 1] == '\\') {
3!
133
            needs_separator = 0;
1✔
134
        }
1✔
135
    }
1✔
136

137
    template_len = tmpdir_len + suffix_len + 2;
3✔
138
    template_path = (char *)sixel_allocator_malloc(allocator, template_len);
3✔
139
    if (template_path == NULL) {
3!
140
        return NULL;
×
141
    }
142

143
    if (needs_separator) {
3!
144
#if defined(_WIN32)
145
        (void)snprintf(template_path, template_len,
146
                       "%s\\%s-XXXXXX", tmpdir, prefix);
147
#else
148
        (void)snprintf(template_path, template_len,
2✔
149
                       "%s/%s-XXXXXX", tmpdir, prefix);
150
#endif
151
    } else {
152
        (void)snprintf(template_path, template_len,
1✔
153
                       "%s%s-XXXXXX", tmpdir, prefix);
154
    }
155

156
    if (capacity_out != NULL) {
3!
157
        *capacity_out = template_len;
3✔
158
    }
1✔
159

160
    return template_path;
3✔
161
}
1✔
162

163

164
static SIXELSTATUS
165
decoder_clipboard_create_spool(sixel_allocator_t *allocator,
3✔
166
                               char const *prefix,
167
                               char **path_out)
168
{
169
    SIXELSTATUS status;
170
    char *template_path;
171
    size_t template_capacity;
172
    int open_flags;
173
    int open_mode;
174
    int fd;
175
    char *tmpname_result;
176

177
    status = SIXEL_FALSE;
3✔
178
    template_path = NULL;
3✔
179
    template_capacity = 0u;
3✔
180
    open_flags = 0;
3✔
181
    open_mode = 0;
3✔
182
    fd = (-1);
3✔
183
    tmpname_result = NULL;
3✔
184

185
    template_path = decoder_create_temp_template_with_prefix(allocator,
4✔
186
                                                             prefix,
1✔
187
                                                             &template_capacity);
188
    if (template_path == NULL) {
3!
189
        sixel_helper_set_additional_message(
×
190
            "clipboard: failed to allocate spool template.");
191
        status = SIXEL_BAD_ALLOCATION;
×
192
        goto end;
×
193
    }
194

195
    if (sixel_compat_mktemp(template_path, template_capacity) != 0) {
3!
196
#if defined(HAVE_TMPNAM)
197

198
# if defined(__clang__)
199
#  if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
200
#   pragma clang diagnostic push
201
#   pragma clang diagnostic ignored "-Wdeprecated-declarations"
202
#  endif
203
# endif
204
        /* Fall back to tmpnam() when mktemp variants are unavailable. */
205
        tmpname_result = tmpnam(template_path);
×
206
# if defined(__clang__)
207
#  if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
208
#   pragma clang diagnostic pop
209
#  endif
210
# endif
211

212
#endif  /* HAVE_TMPNAM */
213
        if (tmpname_result == NULL) {
×
214
            sixel_helper_set_additional_message(
×
215
                "clipboard: failed to reserve spool template.");
216
            status = SIXEL_LIBC_ERROR;
×
217
            goto end;
×
218
        }
219
        template_capacity = strlen(template_path) + 1u;
×
220
    }
221

222
    open_flags = O_RDWR | O_CREAT | O_TRUNC;
3✔
223
#if defined(O_EXCL)
224
    open_flags |= O_EXCL;
3✔
225
#endif
226
    open_mode = S_IRUSR | S_IWUSR;
3✔
227
    fd = sixel_compat_open(template_path, open_flags, open_mode);
3✔
228
    if (fd < 0) {
3!
229
        sixel_helper_set_additional_message(
×
230
            "clipboard: failed to open spool file.");
231
        status = SIXEL_LIBC_ERROR;
×
232
        goto end;
×
233
    }
234

235
    *path_out = template_path;
3✔
236
    if (fd >= 0) {
3!
237
        (void)sixel_compat_close(fd);
3✔
238
        fd = (-1);
3✔
239
    }
1✔
240

241
    template_path = NULL;
3✔
242
    status = SIXEL_OK;
3✔
243

244
end:
2✔
245
    if (fd >= 0) {
3!
246
        (void)sixel_compat_close(fd);
×
247
    }
248
    if (template_path != NULL) {
3!
249
        sixel_allocator_free(allocator, template_path);
×
250
    }
251

252
    return status;
3✔
253
}
254

255

256
static SIXELSTATUS
257
decoder_clipboard_read_file(char const *path,
3✔
258
                            unsigned char **data,
259
                            size_t *size)
260
{
261
    FILE *stream;
262
    long seek_result;
263
    long file_size;
264
    unsigned char *buffer;
265
    size_t read_size;
266

267
    if (data == NULL || size == NULL) {
3!
268
        sixel_helper_set_additional_message(
×
269
            "clipboard: read buffer pointers are null.");
270
        return SIXEL_BAD_ARGUMENT;
×
271
    }
272

273
    *data = NULL;
3✔
274
    *size = 0u;
3✔
275

276
    if (path == NULL) {
3!
277
        sixel_helper_set_additional_message(
×
278
            "clipboard: spool path is null.");
279
        return SIXEL_BAD_ARGUMENT;
×
280
    }
281

282
    stream = sixel_compat_fopen(path, "rb");
3✔
283
    if (stream == NULL) {
3!
284
        sixel_helper_set_additional_message(
×
285
            "clipboard: failed to open spool file for read.");
286
        return SIXEL_LIBC_ERROR;
×
287
    }
288

289
    seek_result = fseek(stream, 0L, SEEK_END);
3✔
290
    if (seek_result != 0) {
3!
291
        (void)fclose(stream);
×
292
        sixel_helper_set_additional_message(
×
293
            "clipboard: failed to seek spool file.");
294
        return SIXEL_LIBC_ERROR;
×
295
    }
296

297
    file_size = ftell(stream);
3✔
298
    if (file_size < 0) {
3!
299
        (void)fclose(stream);
×
300
        sixel_helper_set_additional_message(
×
301
            "clipboard: failed to determine spool size.");
302
        return SIXEL_LIBC_ERROR;
×
303
    }
304

305
    seek_result = fseek(stream, 0L, SEEK_SET);
3✔
306
    if (seek_result != 0) {
3!
307
        (void)fclose(stream);
×
308
        sixel_helper_set_additional_message(
×
309
            "clipboard: failed to rewind spool file.");
310
        return SIXEL_LIBC_ERROR;
×
311
    }
312

313
    if (file_size == 0) {
3!
314
        buffer = NULL;
×
315
        read_size = 0u;
×
316
    } else {
317
        buffer = (unsigned char *)malloc((size_t)file_size);
3✔
318
        if (buffer == NULL) {
3!
319
            (void)fclose(stream);
×
320
            sixel_helper_set_additional_message(
×
321
                "clipboard: malloc() failed for spool payload.");
322
            return SIXEL_BAD_ALLOCATION;
×
323
        }
324
        read_size = fread(buffer, 1u, (size_t)file_size, stream);
3✔
325
        if (read_size != (size_t)file_size) {
3!
326
            free(buffer);
×
327
            (void)fclose(stream);
×
328
            sixel_helper_set_additional_message(
×
329
                "clipboard: failed to read spool payload.");
330
            return SIXEL_LIBC_ERROR;
×
331
        }
332
    }
333

334
    if (fclose(stream) != 0) {
3!
335
        if (buffer != NULL) {
×
336
            free(buffer);
×
337
        }
338
        sixel_helper_set_additional_message(
×
339
            "clipboard: failed to close spool file after read.");
340
        return SIXEL_LIBC_ERROR;
×
341
    }
342

343
    *data = buffer;
3✔
344
    *size = read_size;
3✔
345

346
    return SIXEL_OK;
3✔
347
}
1✔
348

349

350
/* original version of strdup(3) with allocator object */
351
static char *
352
strdup_with_allocator(
232✔
353
    char const          /* in */ *s,          /* source buffer */
354
    sixel_allocator_t   /* in */ *allocator)  /* allocator object for
355
                                                 destination buffer */
356
{
357
    char *p;
358

359
    p = (char *)sixel_allocator_malloc(allocator, (size_t)(strlen(s) + 1));
232✔
360
    if (p) {
232!
361
        (void)sixel_compat_strcpy(p, strlen(s) + 1, s);
232✔
362
    }
80✔
363
    return p;
232✔
364
}
365

366

367
/* create decoder object */
368
SIXELAPI SIXELSTATUS
369
sixel_decoder_new(
94✔
370
    sixel_decoder_t    /* out */ **ppdecoder,  /* decoder object to be created */
371
    sixel_allocator_t  /* in */  *allocator)   /* allocator, null if you use
372
                                                  default allocator */
373
{
374
    SIXELSTATUS status = SIXEL_FALSE;
94✔
375

376
    if (allocator == NULL) {
94!
377
        status = sixel_allocator_new(&allocator, NULL, NULL, NULL, NULL);
94✔
378
        if (SIXEL_FAILED(status)) {
94!
379
            goto end;
×
380
        }
381
    } else {
32✔
382
        sixel_allocator_ref(allocator);
×
383
    }
384

385
    *ppdecoder = sixel_allocator_malloc(allocator, sizeof(sixel_decoder_t));
94✔
386
    if (*ppdecoder == NULL) {
94!
387
        sixel_allocator_unref(allocator);
×
388
        sixel_helper_set_additional_message(
×
389
            "sixel_decoder_new: sixel_allocator_malloc() failed.");
390
        status = SIXEL_BAD_ALLOCATION;
×
391
        goto end;
×
392
    }
393

394
    (*ppdecoder)->ref          = 1;
94✔
395
    (*ppdecoder)->output       = strdup_with_allocator("-", allocator);
94✔
396
    (*ppdecoder)->input        = strdup_with_allocator("-", allocator);
94✔
397
    (*ppdecoder)->allocator    = allocator;
94✔
398
    (*ppdecoder)->dequantize_method = SIXEL_DEQUANTIZE_NONE;
94✔
399
    (*ppdecoder)->dequantize_similarity_bias = 100;
94✔
400
    (*ppdecoder)->dequantize_edge_strength = 0;
94✔
401
    (*ppdecoder)->thumbnail_size = 0;
94✔
402
    (*ppdecoder)->direct_color = 0;
94✔
403
    (*ppdecoder)->clipboard_input_active = 0;
94✔
404
    (*ppdecoder)->clipboard_output_active = 0;
94✔
405
    (*ppdecoder)->clipboard_input_format[0] = '\0';
94✔
406
    (*ppdecoder)->clipboard_output_format[0] = '\0';
94✔
407

408
    if ((*ppdecoder)->output == NULL || (*ppdecoder)->input == NULL) {
94!
409
        sixel_decoder_unref(*ppdecoder);
×
410
        *ppdecoder = NULL;
×
411
        sixel_helper_set_additional_message(
×
412
            "sixel_decoder_new: strdup_with_allocator() failed.");
413
        status = SIXEL_BAD_ALLOCATION;
×
414
        sixel_allocator_unref(allocator);
×
415
        goto end;
×
416
    }
417

418
    status = SIXEL_OK;
94✔
419

420
end:
62✔
421
    return status;
94✔
422
}
423

424

425
/* deprecated version of sixel_decoder_new() */
426
SIXELAPI /* deprecated */ sixel_decoder_t *
427
sixel_decoder_create(void)
×
428
{
429
    SIXELSTATUS status = SIXEL_FALSE;
×
430
    sixel_decoder_t *decoder = NULL;
×
431

432
    status = sixel_decoder_new(&decoder, NULL);
×
433
    if (SIXEL_FAILED(status)) {
×
434
        goto end;
×
435
    }
436

437
end:
438
    return decoder;
×
439
}
440

441

442
/* destroy a decoder object */
443
static void
444
sixel_decoder_destroy(sixel_decoder_t *decoder)
91✔
445
{
446
    sixel_allocator_t *allocator;
447

448
    if (decoder) {
91!
449
        allocator = decoder->allocator;
91✔
450
        sixel_allocator_free(allocator, decoder->input);
91✔
451
        sixel_allocator_free(allocator, decoder->output);
91✔
452
        sixel_allocator_free(allocator, decoder);
91✔
453
        sixel_allocator_unref(allocator);
91✔
454
    }
31✔
455
}
91✔
456

457

458
/* increase reference count of decoder object (thread-unsafe) */
459
SIXELAPI void
460
sixel_decoder_ref(sixel_decoder_t *decoder)
165✔
461
{
462
    /* TODO: be thread safe */
463
    ++decoder->ref;
165✔
464
}
165✔
465

466

467
/* decrease reference count of decoder object (thread-unsafe) */
468
SIXELAPI void
469
sixel_decoder_unref(sixel_decoder_t *decoder)
256✔
470
{
471
    /* TODO: be thread safe */
472
    if (decoder != NULL && --decoder->ref == 0) {
256!
473
        sixel_decoder_destroy(decoder);
91✔
474
    }
31✔
475
}
256✔
476

477

478
typedef struct sixel_similarity {
479
    const unsigned char *palette;
480
    int ncolors;
481
    int stride;
482
    signed char *cache;
483
    int bias;
484
} sixel_similarity_t;
485

486
static SIXELSTATUS
487
sixel_similarity_init(sixel_similarity_t *similarity,
3✔
488
                      const unsigned char *palette,
489
                      int ncolors,
490
                      int bias,
491
                      sixel_allocator_t *allocator)
492
{
493
    size_t cache_size;
494
    int i;
495

496
    if (bias < 1) {
3!
497
        bias = 1;
×
498
    }
499

500
    similarity->palette = palette;
3✔
501
    similarity->ncolors = ncolors;
3✔
502
    similarity->stride = ncolors;
3✔
503
    similarity->bias = bias;
3✔
504

505
    cache_size = (size_t)ncolors * (size_t)ncolors;
3✔
506
    if (cache_size == 0) {
3!
507
        similarity->cache = NULL;
×
508
        return SIXEL_OK;
×
509
    }
510

511
    similarity->cache = (signed char *)sixel_allocator_malloc(
3✔
512
        allocator,
1✔
513
        cache_size);
1✔
514
    if (similarity->cache == NULL) {
3!
515
        sixel_helper_set_additional_message(
×
516
            "sixel_similarity_init: sixel_allocator_malloc() failed.");
517
        return SIXEL_BAD_ALLOCATION;
×
518
    }
519
    memset(similarity->cache, -1, cache_size);
3✔
520
    for (i = 0; i < ncolors; ++i) {
504✔
521
        similarity->cache[i * similarity->stride + i] = 7;
501✔
522
    }
167✔
523

524
    return SIXEL_OK;
3✔
525
}
1✔
526

527
static void
528
sixel_similarity_destroy(sixel_similarity_t *similarity,
3✔
529
                         sixel_allocator_t *allocator)
530
{
531
    if (similarity->cache != NULL) {
3!
532
        sixel_allocator_free(allocator, similarity->cache);
3✔
533
        similarity->cache = NULL;
3✔
534
    }
1✔
535
}
3✔
536

537
static inline unsigned int
538
sixel_similarity_diff(const unsigned char *a, const unsigned char *b)
1,607,046✔
539
{
540
    int dr = (int)a[0] - (int)b[0];
1,607,046✔
541
    int dg = (int)a[1] - (int)b[1];
1,607,046✔
542
    int db = (int)a[2] - (int)b[2];
1,607,046✔
543
    return (unsigned int)(dr * dr + dg * dg + db * db);
1,607,046✔
544
}
545

546
static unsigned int
547
sixel_similarity_compare(sixel_similarity_t *similarity,
6,461,112✔
548
                         int index1,
549
                         int index2,
550
                         int numerator,
551
                         int denominator)
552
{
553
    int min_index;
554
    int max_index;
555
    size_t cache_pos;
556
    signed char cached;
557
    const unsigned char *palette;
558
    const unsigned char *p1;
559
    const unsigned char *p2;
560
    unsigned char avg_color[3];
561
    unsigned int distance;
562
    unsigned int base_distance;
563
    unsigned long long scaled_distance;
564
    int bias;
565
    unsigned int min_diff = UINT_MAX;
6,461,112✔
566
    int i;
567
    unsigned int result;
568
    const unsigned char *pk;
569
    unsigned int diff;
570

571
    if (similarity->cache == NULL) {
6,461,112!
572
        return 0;
×
573
    }
574

575
    if (index1 < 0 || index1 >= similarity->ncolors ||
8,614,816!
576
        index2 < 0 || index2 >= similarity->ncolors) {
6,461,112!
577
        return 0;
×
578
    }
579

580
    if (index1 <= index2) {
6,461,112✔
581
        min_index = index1;
4,514,217✔
582
        max_index = index2;
4,514,217✔
583
    } else {
1,504,739✔
584
        min_index = index2;
1,946,895✔
585
        max_index = index1;
1,946,895✔
586
    }
587

588
    cache_pos = (size_t)min_index * (size_t)similarity->stride
8,614,816✔
589
              + (size_t)max_index;
6,461,112✔
590
    cached = similarity->cache[cache_pos];
6,461,112✔
591
    if (cached >= 0) {
6,461,112✔
592
        return (unsigned int)cached;
6,451,431✔
593
    }
594

595
    palette = similarity->palette;
9,681✔
596
    p1 = palette + index1 * 3;
9,681✔
597
    p2 = palette + index2 * 3;
9,681✔
598

599
#if 1
600
   /*    original: n = (p1 + p2) / 2
601
    */
602
    avg_color[0] = (unsigned char)(((unsigned int)p1[0]
12,908✔
603
                                    + (unsigned int)p2[0]) >> 1);
9,681✔
604
    avg_color[1] = (unsigned char)(((unsigned int)p1[1]
12,908✔
605
                                    + (unsigned int)p2[1]) >> 1);
9,681✔
606
    avg_color[2] = (unsigned char)(((unsigned int)p1[2]
12,908✔
607
                                    + (unsigned int)p2[2]) >> 1);
9,681✔
608
    (void) numerator;
3,227✔
609
    (void) denominator;
3,227✔
610
#else
611
   /*
612
    *    diffuse(pos_a, n1) -> p1
613
    *    diffuse(pos_b, n2) -> p2
614
    *
615
    *    when n1 == n2 == n:
616
    *
617
    *    p2 = n + (n - p1) * numerator / denominator
618
    * => p2 * denominator = n * denominator + (n - p1) * numerator
619
    * => p2 * denominator = n * denominator + n * numerator - p1 * numerator
620
    * => n * (denominator + numerator) = p1 * numerator + p2 * denominator
621
    * => n = (p1 * numerator + p2 * denominator) / (denominator + numerator)
622
    *
623
    */
624
    avg_color[0] = (p1[0] * numerator + p2[0] * denominator + (numerator + denominator + 0.5) / 2)
625
                 / (numerator + denominator);
626
    avg_color[1] = (p1[1] * numerator + p2[1] * denominator + (numerator + denominator + 0.5) / 2)
627
                 / (numerator + denominator);
628
    avg_color[2] = (p1[2] * numerator + p2[2] * denominator + (numerator + denominator + 0.5) / 2)
629
                 / (numerator + denominator);
630
#endif
631

632
    distance = sixel_similarity_diff(avg_color, p1);
9,681✔
633
    bias = similarity->bias;
9,681✔
634
    if (bias < 1) {
9,681!
635
        bias = 1;
×
636
    }
637
    scaled_distance = (unsigned long long)distance
12,908✔
638
                    * (unsigned long long)bias
9,681✔
639
                    + 50ULL;
3,227✔
640
    base_distance = (unsigned int)(scaled_distance / 100ULL);
9,681✔
641
    if (base_distance == 0U) {
9,681!
642
        base_distance = 1U;
×
643
    }
644

645
    for (i = 0; i < similarity->ncolors; ++i) {
1,626,408✔
646
        if (i == index1 || i == index2) {
1,616,727✔
647
            continue;
19,362✔
648
        }
649
        pk = palette + i * 3;
1,597,365✔
650
        diff = sixel_similarity_diff(avg_color, pk);
1,597,365✔
651
        if (diff < min_diff) {
1,597,365✔
652
            min_diff = diff;
75,273✔
653
        }
25,091✔
654
    }
532,455✔
655

656
    if (min_diff == UINT_MAX) {
9,681!
657
        min_diff = base_distance * 2U;
×
658
    }
659

660
    if (min_diff >= base_distance * 2U) {
9,681✔
661
        result = 5U;
516✔
662
    } else if (min_diff >= base_distance) {
9,337✔
663
        result = 8U;
621✔
664
    } else if ((unsigned long long)min_diff * 6ULL
11,599✔
665
               >= (unsigned long long)base_distance * 5ULL) {
8,544✔
666
        result = 7U;
222✔
667
    } else if ((unsigned long long)min_diff * 4ULL
11,170✔
668
               >= (unsigned long long)base_distance * 3ULL) {
8,322✔
669
        result = 7U;
150✔
670
    } else if ((unsigned long long)min_diff * 3ULL
10,946✔
671
               >= (unsigned long long)base_distance * 2ULL) {
8,172✔
672
        result = 5U;
192✔
673
    } else if ((unsigned long long)min_diff * 5ULL
10,704✔
674
               >= (unsigned long long)base_distance * 3ULL) {
7,980✔
675
        result = 7U;
189✔
676
    } else if ((unsigned long long)min_diff * 2ULL
10,451✔
677
               >= (unsigned long long)base_distance * 1ULL) {
7,791✔
678
        result = 4U;
354✔
679
    } else if ((unsigned long long)min_diff * 3ULL
10,034✔
680
               >= (unsigned long long)base_distance * 1ULL) {
7,437✔
681
        result = 2U;
975✔
682
    } else {
325✔
683
        result = 0U;
6,462✔
684
    }
685

686
    similarity->cache[cache_pos] = (signed char)result;
9,681✔
687

688
    return result;
9,681✔
689
}
2,153,704✔
690

691
static inline int
692
sixel_clamp(int value, int min_value, int max_value)
×
693
{
694
    if (value < min_value) {
×
695
        return min_value;
×
696
    }
697
    if (value > max_value) {
×
698
        return max_value;
×
699
    }
700
    return value;
×
701
}
702

703
static inline int
704
sixel_get_gray(const int *gray, int width, int height, int x, int y)
×
705
{
706
    int cx = sixel_clamp(x, 0, width - 1);
×
707
    int cy = sixel_clamp(y, 0, height - 1);
×
708
    return gray[cy * width + cx];
×
709
}
710

711
static unsigned short
712
sixel_prewitt_value(const int *gray, int width, int height, int x, int y)
×
713
{
714
    int top_prev = sixel_get_gray(gray, width, height, x - 1, y - 1);
×
715
    int top_curr = sixel_get_gray(gray, width, height, x, y - 1);
×
716
    int top_next = sixel_get_gray(gray, width, height, x + 1, y - 1);
×
717
    int mid_prev = sixel_get_gray(gray, width, height, x - 1, y);
×
718
    int mid_next = sixel_get_gray(gray, width, height, x + 1, y);
×
719
    int bot_prev = sixel_get_gray(gray, width, height, x - 1, y + 1);
×
720
    int bot_curr = sixel_get_gray(gray, width, height, x, y + 1);
×
721
    int bot_next = sixel_get_gray(gray, width, height, x + 1, y + 1);
×
722
    long gx = (long)top_next - (long)top_prev +
×
723
              (long)mid_next - (long)mid_prev +
×
724
              (long)bot_next - (long)bot_prev;
×
725
    long gy = (long)bot_prev + (long)bot_curr + (long)bot_next -
×
726
              (long)top_prev - (long)top_curr - (long)top_next;
×
727
    unsigned long long magnitude = (unsigned long long)gx
×
728
                                 * (unsigned long long)gx
×
729
                                 + (unsigned long long)gy
×
730
                                 * (unsigned long long)gy;
×
731
    magnitude /= 256ULL;
×
732
    if (magnitude > 65535ULL) {
×
733
        magnitude = 65535ULL;
×
734
    }
735
    return (unsigned short)magnitude;
×
736
}
737

738
static unsigned short
739
sixel_scale_threshold(unsigned short base, int percent)
6✔
740
{
741
    unsigned long long numerator;
742
    unsigned long long scaled;
743

744
    if (percent <= 0) {
6!
745
        percent = 1;
6✔
746
    }
2✔
747

748
    numerator = (unsigned long long)base * 100ULL
8✔
749
              + (unsigned long long)percent / 2ULL;
6✔
750
    scaled = numerator / (unsigned long long)percent;
6✔
751
    if (scaled == 0ULL) {
6!
752
        scaled = 1ULL;
×
753
    }
754
    if (scaled > USHRT_MAX) {
6!
755
        scaled = USHRT_MAX;
×
756
    }
757

758
    return (unsigned short)scaled;
6✔
759
}
760

761
static SIXELSTATUS
762
sixel_dequantize_k_undither(unsigned char *indexed_pixels,
3✔
763
                            int width,
764
                            int height,
765
                            unsigned char *palette,
766
                            int ncolors,
767
                            int similarity_bias,
768
                            int edge_strength,
769
                            sixel_allocator_t *allocator,
770
                            unsigned char **output)
771
{
772
    SIXELSTATUS status = SIXEL_FALSE;
3✔
773
    unsigned char *rgb = NULL;
3✔
774
    int *gray = NULL;
3✔
775
    unsigned short *prewitt = NULL;
3✔
776
    sixel_similarity_t similarity;
777
    size_t num_pixels;
778
    int x;
779
    int y;
780
    unsigned short strong_threshold;
781
    unsigned short detail_threshold;
782
    static const int neighbor_offsets[8][4] = {
783
        {-1, -1,  10, 16}, {0, -1, 16, 16}, {1, -1,   6, 16},
784
        {-1,  0,  11, 16},                  {1,  0,  11, 16},
785
        {-1,  1,   6, 16}, {0,  1, 16, 16}, {1,  1,  10, 16}
786
    };
787
    const unsigned char *color;
788
    size_t out_index;
789
    int palette_index;
790
    unsigned int center_weight;
791
    unsigned int total_weight = 0;
3✔
792
    unsigned int accum_r;
793
    unsigned int accum_g;
794
    unsigned int accum_b;
795
    unsigned short gradient;
796
    int neighbor;
797
    int nx;
798
    int ny;
799
    int numerator;
800
    int denominator;
801
    unsigned int weight;
802
    const unsigned char *neighbor_color;
803
    int neighbor_index;
804

805
    if (width <= 0 || height <= 0 || palette == NULL || ncolors <= 0) {
3!
806
        return SIXEL_BAD_INPUT;
×
807
    }
808

809
    num_pixels = (size_t)width * (size_t)height;
3✔
810

811
    memset(&similarity, 0, sizeof(sixel_similarity_t));
3✔
812

813
    strong_threshold = sixel_scale_threshold(256U, edge_strength);
3✔
814
    detail_threshold = sixel_scale_threshold(160U, edge_strength);
3✔
815
    if (strong_threshold < detail_threshold) {
3!
816
        strong_threshold = detail_threshold;
×
817
    }
818

819
    /*
820
     * Build RGB and luminance buffers so we can reuse the similarity cache
821
     * and gradient analysis across the reconstructed image.
822
     */
823
    rgb = (unsigned char *)sixel_allocator_malloc(
3✔
824
        allocator,
1✔
825
        num_pixels * 3);
1✔
826
    if (rgb == NULL) {
3!
827
        sixel_helper_set_additional_message(
×
828
            "sixel_dequantize_k_undither: "
829
            "sixel_allocator_malloc() failed.");
830
        status = SIXEL_BAD_ALLOCATION;
×
831
        goto end;
×
832
    }
833

834
    gray = (int *)sixel_allocator_malloc(
3✔
835
        allocator,
1✔
836
        num_pixels * sizeof(int));
1✔
837
    if (gray == NULL) {
3!
838
        sixel_helper_set_additional_message(
×
839
            "sixel_dequantize_k_undither: "
840
            "sixel_allocator_malloc() failed.");
841
        status = SIXEL_BAD_ALLOCATION;
×
842
        goto end;
×
843
    }
844

845
    prewitt = (unsigned short *)sixel_allocator_malloc(
3✔
846
        allocator,
1✔
847
        num_pixels * sizeof(unsigned short));
1✔
848
    if (prewitt == NULL) {
3!
849
        sixel_helper_set_additional_message(
×
850
            "sixel_dequantize_k_undither: "
851
            "sixel_allocator_malloc() failed.");
852
        status = SIXEL_BAD_ALLOCATION;
×
853
        goto end;
×
854
    }
855

856
    /*
857
     * Pre-compute palette distance heuristics so each neighbour lookup reuses
858
     * the k_undither-style similarity table.
859
     */
860
    status = sixel_similarity_init(
3✔
861
        &similarity,
862
        palette,
1✔
863
        ncolors,
1✔
864
        similarity_bias,
1✔
865
        allocator);
1✔
866
    if (SIXEL_FAILED(status)) {
3!
867
        goto end;
×
868
    }
869

870
    for (y = 0; y < height; ++y) {
1,353✔
871
        for (x = 0; x < width; ++x) {
811,350✔
872
            palette_index = indexed_pixels[y * width + x];
810,000✔
873
            if (palette_index < 0 || palette_index >= ncolors) {
810,000!
874
                palette_index = 0;
×
875
            }
876

877
            color = palette + palette_index * 3;
810,000✔
878
            out_index = (size_t)(y * width + x) * 3;
810,000✔
879
            rgb[out_index + 0] = color[0];
810,000✔
880
            rgb[out_index + 1] = color[1];
810,000✔
881
            rgb[out_index + 2] = color[2];
810,000✔
882

883
            if (edge_strength > 0) {
810,000!
884
                gray[y * width + x] = (int)color[0]
×
885
                                    + (int)color[1] * 2
×
886
                                    + (int)color[2];
×
887
                /*
888
                 * Edge detection keeps high-frequency content intact while we
889
                 * smooth dithering noise in flatter regions.
890
                 */
891
                prewitt[y * width + x] = sixel_prewitt_value(
×
892
                    gray,
893
                    width,
894
                    height,
895
                    x,
896
                    y);
897

898
                gradient = prewitt[y * width + x];
×
899
                if (gradient > strong_threshold) {
×
900
                    continue;
×
901
                }
902

903
                if (gradient > detail_threshold) {
×
904
                    center_weight = 24U;
×
905
                } else {
906
                    center_weight = 8U;
×
907
                }
908
            } else {
909
                center_weight = 8U;
810,000✔
910
            }
911

912
            out_index = (size_t)(y * width + x) * 3;
810,000✔
913
            accum_r = (unsigned int)rgb[out_index + 0] * center_weight;
810,000✔
914
            accum_g = (unsigned int)rgb[out_index + 1] * center_weight;
810,000✔
915
            accum_b = (unsigned int)rgb[out_index + 2] * center_weight;
810,000✔
916
            total_weight = center_weight;
810,000✔
917

918
            /*
919
             * Blend neighbours that stay within the palette similarity
920
             * threshold so Floyd-Steinberg noise is averaged away without
921
             * bleeding across pronounced edges.
922
             */
923
            for (neighbor = 0; neighbor < 8; ++neighbor) {
7,290,000✔
924
                nx = x + neighbor_offsets[neighbor][0];
6,480,000✔
925
                ny = y + neighbor_offsets[neighbor][1];
6,480,000✔
926
                numerator = neighbor_offsets[neighbor][2];
6,480,000✔
927
                denominator = neighbor_offsets[neighbor][3];
6,480,000✔
928

929
                if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
6,480,000✔
930
                    continue;
18,888✔
931
                }
932

933
                neighbor_index = indexed_pixels[ny * width + nx];
6,461,112✔
934
                if (neighbor_index < 0 || neighbor_index >= ncolors) {
6,461,112!
935
                    continue;
×
936
                }
937

938
                if (numerator) {
6,461,112!
939
                    weight = sixel_similarity_compare(
6,461,112✔
940
                        &similarity,
941
                        palette_index,
2,153,704✔
942
                        neighbor_index,
2,153,704✔
943
                        numerator,
2,153,704✔
944
                        denominator);
2,153,704✔
945
                    if (weight == 0) {
6,461,112✔
946
                        continue;
351,150✔
947
                    }
948

949
                    neighbor_color = palette + neighbor_index * 3;
6,109,962✔
950
                    accum_r += (unsigned int)neighbor_color[0] * weight;
6,109,962✔
951
                    accum_g += (unsigned int)neighbor_color[1] * weight;
6,109,962✔
952
                    accum_b += (unsigned int)neighbor_color[2] * weight;
6,109,962✔
953
                    total_weight += weight;
6,109,962✔
954
                }
2,036,654✔
955
            }
2,036,654✔
956

957
            if (total_weight > 0U) {
810,000!
958
                rgb[out_index + 0] = (unsigned char)(accum_r / total_weight);
810,000✔
959
                rgb[out_index + 1] = (unsigned char)(accum_g / total_weight);
810,000✔
960
                rgb[out_index + 2] = (unsigned char)(accum_b / total_weight);
810,000✔
961
            }
270,000✔
962
        }
270,000✔
963
    }
450✔
964

965

966
    *output = rgb;
3✔
967
    rgb = NULL;
3✔
968
    status = SIXEL_OK;
3✔
969

970
end:
2✔
971
    sixel_similarity_destroy(&similarity, allocator);
3✔
972
    sixel_allocator_free(allocator, rgb);
3✔
973
    sixel_allocator_free(allocator, gray);
3✔
974
    sixel_allocator_free(allocator, prewitt);
3✔
975
    return status;
3✔
976
}
1✔
977
/*
978
 * The dequantizer accepts a method supplied by the shared option helper. The
979
 * decoder keeps a parallel lookup table that translates the matched index
980
 * into the execution constant.
981
 */
982
static int const g_decoder_dequant_methods[] = {
983
    SIXEL_DEQUANTIZE_NONE,
984
    SIXEL_DEQUANTIZE_K_UNDITHER
985
};
986

987
static sixel_option_choice_t const g_decoder_dequant_choices[] = {
988
    { "none", 0 },
989
    { "k_undither", 1 }
990
};
991

992
static void
993
normalise_windows_drive_path(char *path)
9✔
994
{
995
#if defined(_WIN32)
996
    size_t length;
997

998
    length = 0u;
999

1000
    if (path == NULL) {
1001
        return;
1002
    }
1003

1004
    length = strlen(path);
1005
    if (length >= 3u
1006
            && path[0] == '/'
1007
            && ((path[1] >= 'A' && path[1] <= 'Z')
1008
                || (path[1] >= 'a' && path[1] <= 'z'))
1009
            && path[2] == '/') {
1010
        path[0] = path[1];
1011
        path[1] = ':';
1012
    }
1013
#else
1014
    (void)path;
3✔
1015
#endif
1016
}
9✔
1017

1018
/* set an option flag to decoder object */
1019
SIXELAPI SIXELSTATUS
1020
sixel_decoder_setopt(
92✔
1021
    sixel_decoder_t /* in */ *decoder,
1022
    int             /* in */ arg,
1023
    char const      /* in */ *value
1024
)
1025
{
1026
    SIXELSTATUS status = SIXEL_FALSE;
92✔
1027
    unsigned int path_flags;
1028
    int path_check;
1029
    char const *payload = NULL;
92✔
1030
    size_t length;
1031
    sixel_clipboard_spec_t clipboard_spec;
1032
    int match_index;
1033
    sixel_option_choice_result_t match_result;
1034
    char match_detail[128];
1035
    char match_message[256];
1036
    char const *filename = NULL;
92✔
1037
    char *p = NULL;
92✔
1038

1039
    sixel_decoder_ref(decoder);
92✔
1040
    path_flags = 0u;
92✔
1041
    path_check = 0;
92✔
1042

1043
    switch(arg) {
92!
1044
    case SIXEL_OPTFLAG_INPUT:  /* i */
16✔
1045
        path_flags = SIXEL_OPTION_PATH_ALLOW_STDIN |
25✔
1046
            SIXEL_OPTION_PATH_ALLOW_CLIPBOARD |
1047
            SIXEL_OPTION_PATH_ALLOW_REMOTE;
1048
        if (value != NULL) {
25!
1049
            path_check = sixel_option_validate_filesystem_path(
25✔
1050
                value,
9✔
1051
                value,
9✔
1052
                path_flags);
9✔
1053
            if (path_check != 0) {
25✔
1054
                status = SIXEL_BAD_ARGUMENT;
6✔
1055
                goto end;
6✔
1056
            }
1057
        }
7✔
1058
        decoder->clipboard_input_active = 0;
19✔
1059
        decoder->clipboard_input_format[0] = '\0';
19✔
1060
        if (value != NULL) {
19!
1061
            clipboard_spec.is_clipboard = 0;
19✔
1062
            clipboard_spec.format[0] = '\0';
19✔
1063
            if (sixel_clipboard_parse_spec(value, &clipboard_spec)
19!
1064
                    && clipboard_spec.is_clipboard) {
7!
1065
                decoder_clipboard_select_format(
1✔
1066
                    decoder->clipboard_input_format,
1✔
1067
                    sizeof(decoder->clipboard_input_format),
1068
                    clipboard_spec.format,
1✔
1069
                    "sixel");
1070
                decoder->clipboard_input_active = 1;
1✔
1071
            }
1✔
1072
        }
7✔
1073
        free(decoder->input);
19✔
1074
        decoder->input = strdup_with_allocator(value, decoder->allocator);
19✔
1075
        if (decoder->input == NULL) {
19!
1076
            sixel_helper_set_additional_message(
×
1077
                "sixel_decoder_setopt: strdup_with_allocator() failed.");
1078
            status = SIXEL_BAD_ALLOCATION;
×
1079
            goto end;
×
1080
        }
1081
        break;
19✔
1082
    case SIXEL_OPTFLAG_OUTPUT:  /* o */
18✔
1083
        decoder->clipboard_output_active = 0;
28✔
1084
        decoder->clipboard_output_format[0] = '\0';
28✔
1085

1086
        payload = value;
28✔
1087
        if (strncmp(value, "png:", 4) == 0) {
28✔
1088
            payload = value + 4;
12✔
1089
            if (payload[0] == '\0') {
12✔
1090
                sixel_helper_set_additional_message(
3✔
1091
                    "missing target after the \"png:\" prefix.");
1092
                return SIXEL_BAD_ARGUMENT;
3✔
1093
            }
1094
            length = strlen(payload);
9✔
1095
            filename = p = malloc(length + 1U);
9✔
1096
            if (p == NULL) {
9!
1097
                sixel_helper_set_additional_message(
×
1098
                    "sixel_decoder_setopt: malloc() failed for png path filename.");
1099
                return SIXEL_BAD_ALLOCATION;
×
1100
            }
1101
            memcpy(p, payload, length + 1U);
9✔
1102
            normalise_windows_drive_path(p);
9✔
1103
        } else {
3✔
1104
            filename = value;
16✔
1105
        }
1106

1107
        if (filename != NULL) {
25!
1108
            clipboard_spec.is_clipboard = 0;
25✔
1109
            clipboard_spec.format[0] = '\0';
25✔
1110
            if (sixel_clipboard_parse_spec(filename, &clipboard_spec)
25!
1111
                    && clipboard_spec.is_clipboard) {
11!
1112
                decoder_clipboard_select_format(
3✔
1113
                    decoder->clipboard_output_format,
3✔
1114
                    sizeof(decoder->clipboard_output_format),
1115
                    clipboard_spec.format,
1✔
1116
                    "png");
1117
                decoder->clipboard_output_active = 1;
3✔
1118
            }
1✔
1119
        }
9✔
1120
        free(decoder->output);
25✔
1121
        decoder->output = strdup_with_allocator(filename, decoder->allocator);
25✔
1122
        free(p);
25✔
1123
        if (decoder->output == NULL) {
25!
1124
            sixel_helper_set_additional_message(
×
1125
                "sixel_decoder_setopt: strdup_with_allocator() failed.");
1126
            status = SIXEL_BAD_ALLOCATION;
×
1127
            goto end;
×
1128
        }
1129
        break;
25✔
1130
    case SIXEL_OPTFLAG_DEQUANTIZE:  /* d */
6✔
1131
        if (value == NULL) {
9!
1132
            sixel_helper_set_additional_message(
×
1133
                "sixel_decoder_setopt: -d/--dequantize requires an argument.");
1134
            status = SIXEL_BAD_ALLOCATION;
×
1135
            goto end;
×
1136
        }
1137

1138
        match_index = 0;
9✔
1139
        memset(match_detail, 0, sizeof(match_detail));
9✔
1140
        memset(match_message, 0, sizeof(match_message));
9✔
1141

1142
        match_result = sixel_option_match_choice(
9✔
1143
            value,
3✔
1144
            g_decoder_dequant_choices,
1145
            sizeof(g_decoder_dequant_choices) /
1146
            sizeof(g_decoder_dequant_choices[0]),
1147
            &match_index,
1148
            match_detail,
3✔
1149
            sizeof(match_detail));
1150
        if (match_result == SIXEL_OPTION_CHOICE_MATCH) {
9!
1151
            decoder->dequantize_method =
9✔
1152
                g_decoder_dequant_methods[match_index];
9✔
1153
        } else {
3✔
UNCOV
1154
            if (match_result == SIXEL_OPTION_CHOICE_AMBIGUOUS) {
×
UNCOV
1155
                sixel_option_report_ambiguous_prefix(
×
1156
                    value,
1157
                    match_detail,
1158
                    match_message,
1159
                    sizeof(match_message));
1160
            } else {
1161
                sixel_option_report_invalid_choice(
×
1162
                    "unsupported dequantize method.",
1163
                    match_detail,
1164
                    match_message,
1165
                    sizeof(match_message));
1166
            }
UNCOV
1167
            status = SIXEL_BAD_ARGUMENT;
×
UNCOV
1168
            goto end;
×
1169
        }
1170
        break;
9✔
1171

1172
    case SIXEL_OPTFLAG_SIMILARITY:  /* S */
1173
        decoder->dequantize_similarity_bias = atoi(value);
×
1174
        if (decoder->dequantize_similarity_bias < 0 ||
×
1175
            decoder->dequantize_similarity_bias > 1000) {
×
1176
            sixel_helper_set_additional_message(
×
1177
                "similarity must be between 1 and 1000.");
1178
            status = SIXEL_BAD_ARGUMENT;
×
1179
            goto end;
×
1180
        }
1181
        break;
×
1182

1183
    case SIXEL_OPTFLAG_SIZE:  /* s */
1184
        decoder->thumbnail_size = atoi(value);
×
1185
        if (decoder->thumbnail_size <= 0) {
×
1186
            sixel_helper_set_additional_message(
×
1187
                "size must be greater than zero.");
1188
            status = SIXEL_BAD_ARGUMENT;
×
1189
            goto end;
×
1190
        }
1191
        break;
×
1192

1193
    case SIXEL_OPTFLAG_EDGE:  /* e */
1194
        decoder->dequantize_edge_strength = atoi(value);
×
1195
        if (decoder->dequantize_edge_strength < 0 ||
×
1196
            decoder->dequantize_edge_strength > 1000) {
×
1197
            sixel_helper_set_additional_message(
×
1198
                "edge bias must be between 1 and 1000.");
1199
            status = SIXEL_BAD_ARGUMENT;
×
1200
            goto end;
×
1201
        }
1202
        break;
×
1203

1204
    case SIXEL_OPTFLAG_DIRECT:  /* D */
14✔
1205
        decoder->direct_color = 1;
21✔
1206
        break;
21✔
1207

1208
    case SIXEL_OPTFLAG_THREADS:  /* = */
6✔
1209
        status = sixel_decoder_parallel_override_threads(value);
9✔
1210
        if (SIXEL_FAILED(status)) {
9✔
1211
            goto end;
3✔
1212
        }
1213
        break;
6✔
1214

1215
    case '?':
×
1216
    default:
1217
        status = SIXEL_BAD_ARGUMENT;
×
1218
        goto end;
×
1219
    }
1220

1221
    status = SIXEL_OK;
80✔
1222

1223
end:
58✔
1224
    sixel_decoder_unref(decoder);
89✔
1225

1226
    return status;
89✔
1227
}
32✔
1228

1229

1230
/* load source data from stdin or the file specified with
1231
   SIXEL_OPTFLAG_INPUT flag, and decode it */
1232
SIXELAPI SIXELSTATUS
1233
sixel_decoder_decode(
73✔
1234
    sixel_decoder_t /* in */ *decoder)
1235
{
1236
    SIXELSTATUS status = SIXEL_FALSE;
73✔
1237
    unsigned char *raw_data = NULL;
73✔
1238
    int sx;
1239
    int sy;
1240
    int raw_len;
1241
    int max;
1242
    int n;
1243
    FILE *input_fp = NULL;
73✔
1244
    char message[2048];
1245
    unsigned char *indexed_pixels = NULL;
73✔
1246
    unsigned char *palette = NULL;
73✔
1247
    unsigned char *rgb_pixels = NULL;
73✔
1248
    unsigned char *direct_pixels = NULL;
73✔
1249
    unsigned char *output_pixels;
1250
    unsigned char *output_palette;
1251
    int output_pixelformat;
1252
    int ncolors;
1253
    sixel_frame_t *frame;
1254
    int new_width;
1255
    int new_height;
1256
    double scaled_width;
1257
    double scaled_height;
1258
    int max_dimension;
1259
    int thumbnail_size;
1260
    int frame_ncolors;
1261
    unsigned char *clipboard_blob;
1262
    size_t clipboard_blob_size;
1263
    SIXELSTATUS clipboard_status;
1264
    char *clipboard_output_path;
1265
    unsigned char *clipboard_output_data;
1266
    size_t clipboard_output_size;
1267
    SIXELSTATUS clipboard_output_status;
1268

1269
    sixel_decoder_ref(decoder);
73✔
1270

1271
    frame = NULL;
73✔
1272
    new_width = 0;
73✔
1273
    new_height = 0;
73✔
1274
    scaled_width = 0.0;
73✔
1275
    scaled_height = 0.0;
73✔
1276
    max_dimension = 0;
73✔
1277
    thumbnail_size = decoder->thumbnail_size;
73✔
1278
    frame_ncolors = -1;
73✔
1279
    clipboard_blob = NULL;
73✔
1280
    clipboard_blob_size = 0u;
73✔
1281
    clipboard_status = SIXEL_OK;
73✔
1282
    clipboard_output_path = NULL;
73✔
1283
    clipboard_output_data = NULL;
73✔
1284
    clipboard_output_size = 0u;
73✔
1285
    clipboard_output_status = SIXEL_OK;
73✔
1286
    input_fp = NULL;
73✔
1287

1288
    raw_len = 0;
73✔
1289
    max = 0;
73✔
1290
    if (decoder->clipboard_input_active) {
73!
1291
        clipboard_status = sixel_clipboard_read(
1✔
1292
            decoder->clipboard_input_format,
1✔
1293
            &clipboard_blob,
1294
            &clipboard_blob_size,
1295
            decoder->allocator);
1✔
1296
        if (SIXEL_FAILED(clipboard_status)) {
1!
1297
            status = clipboard_status;
×
1298
            goto end;
×
1299
        }
1300
        max = (int)((clipboard_blob_size > 0u)
1!
1301
                    ? clipboard_blob_size
1✔
1302
                    : 1u);
1303
        raw_data = (unsigned char *)sixel_allocator_malloc(
1✔
1304
            decoder->allocator,
1✔
1305
            (size_t)max);
1✔
1306
        if (raw_data == NULL) {
1!
1307
            sixel_helper_set_additional_message(
×
1308
                "sixel_decoder_decode: sixel_allocator_malloc() failed.");
1309
            status = SIXEL_BAD_ALLOCATION;
×
1310
            goto end;
×
1311
        }
1312
        if (clipboard_blob_size > 0u && clipboard_blob != NULL) {
1!
1313
            memcpy(raw_data, clipboard_blob, clipboard_blob_size);
1✔
1314
        }
1✔
1315
        raw_len = (int)clipboard_blob_size;
1✔
1316
        if (clipboard_blob != NULL) {
1!
1317
            free(clipboard_blob);
1✔
1318
            clipboard_blob = NULL;
1✔
1319
        }
1✔
1320
    } else {
1✔
1321
        if (strcmp(decoder->input, "-") == 0) {
72✔
1322
            /* for windows */
1323
#if defined(O_BINARY)
1324
# if HAVE__SETMODE
1325
            _setmode(STDIN_FILENO, O_BINARY);
1326
# elif HAVE_SETMODE
1327
            setmode(STDIN_FILENO, O_BINARY);
1328
# endif  /* HAVE_SETMODE */
1329
#endif  /* defined(O_BINARY) */
1330
            input_fp = stdin;
57✔
1331
        } else {
19✔
1332
            input_fp = sixel_compat_fopen(decoder->input, "rb");
15✔
1333
            if (! input_fp) {
15!
1334
                (void)snprintf(
×
1335
                    message,
1336
                    sizeof(message) - 1,
1337
                    "sixel_decoder_decode: failed to open input file: %s.",
1338
                    decoder->input);
1339
                sixel_helper_set_additional_message(message);
×
1340
                status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
1341
                goto end;
×
1342
            }
1343
        }
1344

1345
        raw_len = 0;
72✔
1346
        max = 64 * 1024;
72✔
1347

1348
        raw_data = (unsigned char *)sixel_allocator_malloc(
72✔
1349
            decoder->allocator,
24✔
1350
            (size_t)max);
24✔
1351
        if (raw_data == NULL) {
72!
1352
            sixel_helper_set_additional_message(
×
1353
                "sixel_decoder_decode: sixel_allocator_malloc() failed.");
1354
            status = SIXEL_BAD_ALLOCATION;
×
1355
            goto end;
×
1356
        }
1357

1358
        for (;;) {
2,452✔
1359
            if ((max - raw_len) < 4096) {
7,356✔
1360
                max *= 2;
156✔
1361
                raw_data = (unsigned char *)sixel_allocator_realloc(
156✔
1362
                    decoder->allocator,
52✔
1363
                    raw_data,
52✔
1364
                    (size_t)max);
52✔
1365
                if (raw_data == NULL) {
156!
1366
                    sixel_helper_set_additional_message(
×
1367
                        "sixel_decoder_decode: sixel_allocator_realloc() failed.");
1368
                    status = SIXEL_BAD_ALLOCATION;
×
1369
                    goto end;
×
1370
                }
1371
            }
52✔
1372
            if ((n = (int)fread(raw_data + raw_len, 1, 4096, input_fp)) <= 0) {
7,356✔
1373
                break;
72✔
1374
            }
1375
            raw_len += n;
7,284✔
1376
        }
1377

1378
        if (input_fp != NULL && input_fp != stdin) {
72!
1379
            fclose(input_fp);
15✔
1380
        }
5✔
1381
    }
1382

1383
    if (decoder->direct_color != 0 &&
73✔
1384
            decoder->dequantize_method != SIXEL_DEQUANTIZE_NONE) {
21✔
1385
        sixel_helper_set_additional_message(
3✔
1386
            "sixel_decoder_decode: direct option "
1387
            "cannot be combined with dequantize option.");
1388
        status = SIXEL_BAD_ARGUMENT;
3✔
1389
        goto end;
3✔
1390
    }
1391

1392
    ncolors = 0;
70✔
1393

1394
    if (decoder->direct_color != 0) {
70✔
1395
        status = sixel_decode_direct(
18✔
1396
            raw_data,
6✔
1397
            raw_len,
6✔
1398
            &direct_pixels,
1399
            &sx,
1400
            &sy,
1401
            decoder->allocator);
6✔
1402
    } else {
6✔
1403
        status = sixel_decode_raw(
52✔
1404
            raw_data,
18✔
1405
            raw_len,
18✔
1406
            &indexed_pixels,
1407
            &sx,
1408
            &sy,
1409
            &palette,
1410
            &ncolors,
1411
            decoder->allocator);
18✔
1412
    }
1413
    if (SIXEL_FAILED(status)) {
70!
1414
        goto end;
×
1415
    }
1416

1417
    if (sx > SIXEL_WIDTH_LIMIT || sy > SIXEL_HEIGHT_LIMIT) {
70!
1418
        status = SIXEL_BAD_INPUT;
×
1419
        goto end;
×
1420
    }
1421

1422
    if (decoder->direct_color != 0) {
70✔
1423
        output_pixels = direct_pixels;
18✔
1424
        output_palette = NULL;
18✔
1425
        output_pixelformat = SIXEL_PIXELFORMAT_RGBA8888;
18✔
1426
        frame_ncolors = 0;
18✔
1427
    } else {
6✔
1428
        output_pixels = indexed_pixels;
52✔
1429
        output_palette = palette;
52✔
1430
        output_pixelformat = SIXEL_PIXELFORMAT_PAL8;
52✔
1431

1432
        if (decoder->dequantize_method == SIXEL_DEQUANTIZE_K_UNDITHER) {
52✔
1433
            status = sixel_dequantize_k_undither(
3✔
1434
                indexed_pixels,
1✔
1435
                sx,
1✔
1436
                sy,
1✔
1437
                palette,
1✔
1438
                ncolors,
1✔
1439
                decoder->dequantize_similarity_bias,
1✔
1440
                decoder->dequantize_edge_strength,
1✔
1441
                decoder->allocator,
1✔
1442
                &rgb_pixels);
1443
            if (SIXEL_FAILED(status)) {
3!
1444
                goto end;
×
1445
            }
1446
            output_pixels = rgb_pixels;
3✔
1447
            output_palette = NULL;
3✔
1448
            output_pixelformat = SIXEL_PIXELFORMAT_RGB888;
3✔
1449
        }
1✔
1450

1451
        if (output_pixelformat == SIXEL_PIXELFORMAT_PAL8) {
52✔
1452
            frame_ncolors = ncolors;
49✔
1453
        } else {
17✔
1454
            frame_ncolors = 0;
3✔
1455
        }
1456
    }
1457

1458
    if (thumbnail_size > 0) {
70!
1459
        /*
1460
         * When the caller requests a thumbnail, compute the new geometry
1461
         * while preserving the original aspect ratio. We only allocate a
1462
         * frame when the dimensions actually change, so the fast path for
1463
         * matching sizes still avoids any additional allocations.
1464
         */
1465
        max_dimension = sx;
×
1466
        if (sy > max_dimension) {
×
1467
            max_dimension = sy;
×
1468
        }
1469
        if (max_dimension > 0) {
×
1470
            if (sx >= sy) {
×
1471
                new_width = thumbnail_size;
×
1472
                scaled_height = (double)sy * (double)thumbnail_size /
×
1473
                    (double)sx;
×
1474
                new_height = (int)(scaled_height + 0.5);
×
1475
            } else {
1476
                new_height = thumbnail_size;
×
1477
                scaled_width = (double)sx * (double)thumbnail_size /
×
1478
                    (double)sy;
×
1479
                new_width = (int)(scaled_width + 0.5);
×
1480
            }
1481
            if (new_width < 1) {
×
1482
                new_width = 1;
×
1483
            }
1484
            if (new_height < 1) {
×
1485
                new_height = 1;
×
1486
            }
1487
            if (new_width != sx || new_height != sy) {
×
1488
                /*
1489
                 * Wrap the decoded pixels in a frame so we can reuse the
1490
                 * central scaling helper. Ownership transfers to the frame,
1491
                 * which keeps the lifetime rules identical on both paths.
1492
                 */
1493
                status = sixel_frame_new(&frame, decoder->allocator);
×
1494
                if (SIXEL_FAILED(status)) {
×
1495
                    goto end;
×
1496
                }
1497
                status = sixel_frame_init(
×
1498
                    frame,
1499
                    output_pixels,
1500
                    sx,
1501
                    sy,
1502
                    output_pixelformat,
1503
                    output_palette,
1504
                    frame_ncolors);
1505
                if (SIXEL_FAILED(status)) {
×
1506
                    goto end;
×
1507
                }
1508
                if (output_pixels == indexed_pixels) {
×
1509
                    indexed_pixels = NULL;
×
1510
                }
1511
                if (output_pixels == rgb_pixels) {
×
1512
                    rgb_pixels = NULL;
×
1513
                }
1514
                if (output_palette == palette) {
×
1515
                    palette = NULL;
×
1516
                }
1517
                status = sixel_frame_resize(
×
1518
                    frame,
1519
                    new_width,
1520
                    new_height,
1521
                    SIXEL_RES_BILINEAR);
1522
                if (SIXEL_FAILED(status)) {
×
1523
                    goto end;
×
1524
                }
1525
                /*
1526
                 * The resized frame already exposes a tightly packed RGB
1527
                 * buffer, so write the updated dimensions and references
1528
                 * back to the main encoder path.
1529
                 */
1530
                sx = sixel_frame_get_width(frame);
×
1531
                sy = sixel_frame_get_height(frame);
×
1532
                output_pixels = sixel_frame_get_pixels(frame);
×
1533
                output_palette = NULL;
×
1534
                output_pixelformat = sixel_frame_get_pixelformat(frame);
×
1535
            }
1536
        }
1537
    }
1538

1539
    if (decoder->clipboard_output_active) {
70✔
1540
        clipboard_output_status = decoder_clipboard_create_spool(
3✔
1541
            decoder->allocator,
1✔
1542
            "clipboard-out",
1543
            &clipboard_output_path);
1544
        if (SIXEL_FAILED(clipboard_output_status)) {
3!
1545
            status = clipboard_output_status;
×
1546
            goto end;
×
1547
        }
1548
    }
1✔
1549

1550
    status = sixel_helper_write_image_file(
70✔
1551
        output_pixels,
24✔
1552
        sx,
24✔
1553
        sy,
24✔
1554
        output_palette,
24✔
1555
        output_pixelformat,
24✔
1556
        decoder->clipboard_output_active
70✔
1557
            ? clipboard_output_path
1✔
1558
            : decoder->output,
23✔
1559
        SIXEL_FORMAT_PNG,
1560
        decoder->allocator);
24✔
1561

1562
    if (SIXEL_FAILED(status)) {
70!
1563
        goto end;
×
1564
    }
1565

1566
    if (decoder->clipboard_output_active) {
71✔
1567
        clipboard_output_status = decoder_clipboard_read_file(
3✔
1568
            clipboard_output_path,
1✔
1569
            &clipboard_output_data,
1570
            &clipboard_output_size);
1571
        if (SIXEL_SUCCEEDED(clipboard_output_status)) {
3!
1572
            clipboard_output_status = sixel_clipboard_write(
3✔
1573
                decoder->clipboard_output_format,
3✔
1574
                clipboard_output_data,
1✔
1575
                clipboard_output_size);
1✔
1576
        }
1✔
1577
        if (clipboard_output_data != NULL) {
3!
1578
            free(clipboard_output_data);
3✔
1579
            clipboard_output_data = NULL;
3✔
1580
        }
1✔
1581
        if (SIXEL_FAILED(clipboard_output_status)) {
3!
1582
            status = clipboard_output_status;
2✔
1583
            goto end;
2✔
1584
        }
1585
    }
1✔
1586

1587
end:
44✔
1588
    sixel_frame_unref(frame);
73✔
1589
    sixel_allocator_free(decoder->allocator, raw_data);
73✔
1590
    sixel_allocator_free(decoder->allocator, indexed_pixels);
73✔
1591
    sixel_allocator_free(decoder->allocator, palette);
73✔
1592
    sixel_allocator_free(decoder->allocator, direct_pixels);
73✔
1593
    sixel_allocator_free(decoder->allocator, rgb_pixels);
73✔
1594
    if (clipboard_blob != NULL) {
73!
1595
        free(clipboard_blob);
×
1596
    }
1597
    if (clipboard_output_path != NULL) {
73✔
1598
        (void)sixel_compat_unlink(clipboard_output_path);
3✔
1599
        sixel_allocator_free(decoder->allocator, clipboard_output_path);
3✔
1600
    }
1✔
1601

1602
    sixel_decoder_unref(decoder);
73✔
1603

1604
    return status;
73✔
1605
}
1606

1607

1608
#if HAVE_TESTS
1609
static int
1610
test1(void)
×
1611
{
1612
    int nret = EXIT_FAILURE;
×
1613
    sixel_decoder_t *decoder = NULL;
×
1614

1615
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
1616
#  pragma GCC diagnostic push
1617
#  pragma GCC diagnostic ignored "-Wdeprecated-declarations"
1618
#endif
1619
    decoder = sixel_decoder_create();
×
1620
#if HAVE_DIAGNOSTIC_DEPRECATED_DECLARATIONS
1621
#  pragma GCC diagnostic pop
1622
#endif
1623
    if (decoder == NULL) {
×
1624
        goto error;
×
1625
    }
1626
    sixel_decoder_ref(decoder);
×
1627
    sixel_decoder_unref(decoder);
×
1628
    nret = EXIT_SUCCESS;
×
1629

1630
error:
1631
    sixel_decoder_unref(decoder);
×
1632
    return nret;
×
1633
}
1634

1635

1636
static int
1637
test2(void)
×
1638
{
1639
    int nret = EXIT_FAILURE;
×
1640
    sixel_decoder_t *decoder = NULL;
×
1641
    SIXELSTATUS status;
1642

1643
    status = sixel_decoder_new(&decoder, NULL);
×
1644
    if (SIXEL_FAILED(status)) {
×
1645
        goto error;
×
1646
    }
1647

1648
    sixel_decoder_ref(decoder);
×
1649
    sixel_decoder_unref(decoder);
×
1650
    nret = EXIT_SUCCESS;
×
1651

1652
error:
1653
    sixel_decoder_unref(decoder);
×
1654
    return nret;
×
1655
}
1656

1657

1658
static int
1659
test3(void)
×
1660
{
1661
    int nret = EXIT_FAILURE;
×
1662
    sixel_decoder_t *decoder = NULL;
×
1663
    sixel_allocator_t *allocator = NULL;
×
1664
    SIXELSTATUS status;
1665

1666
    sixel_debug_malloc_counter = 1;
×
1667

1668
    status = sixel_allocator_new(
×
1669
        &allocator,
1670
        sixel_bad_malloc,
1671
        NULL,
1672
        NULL,
1673
        NULL);
1674
    if (SIXEL_FAILED(status)) {
×
1675
        goto error;
×
1676
    }
1677

1678
    status = sixel_decoder_new(&decoder, allocator);
×
1679
    if (status != SIXEL_BAD_ALLOCATION) {
×
1680
        goto error;
×
1681
    }
1682

1683
    nret = EXIT_SUCCESS;
×
1684

1685
error:
1686
    return nret;
×
1687
}
1688

1689

1690
static int
1691
test4(void)
×
1692
{
1693
    int nret = EXIT_FAILURE;
×
1694
    sixel_decoder_t *decoder = NULL;
×
1695
    sixel_allocator_t *allocator = NULL;
×
1696
    SIXELSTATUS status;
1697

1698
    sixel_debug_malloc_counter = 2;
×
1699

1700
    status = sixel_allocator_new(
×
1701
        &allocator,
1702
        sixel_bad_malloc,
1703
        NULL,
1704
        NULL,
1705
        NULL);
1706
    if (SIXEL_FAILED(status)) {
×
1707
        goto error;
×
1708
    }
1709

1710
    status = sixel_decoder_new(&decoder, allocator);
×
1711
    if (status != SIXEL_BAD_ALLOCATION) {
×
1712
        goto error;
×
1713
    }
1714

1715
    nret = EXIT_SUCCESS;
×
1716

1717
error:
1718
    return nret;
×
1719
}
1720

1721

1722
static int
1723
test5(void)
×
1724
{
1725
    int nret = EXIT_FAILURE;
×
1726
    sixel_decoder_t *decoder = NULL;
×
1727
    sixel_allocator_t *allocator = NULL;
×
1728
    SIXELSTATUS status;
1729

1730
    sixel_debug_malloc_counter = 4;
×
1731

1732
    status = sixel_allocator_new(
×
1733
        &allocator,
1734
        sixel_bad_malloc,
1735
        NULL,
1736
        NULL,
1737
        NULL);
1738
    if (SIXEL_FAILED(status)) {
×
1739
        goto error;
×
1740
    }
1741
    status = sixel_decoder_new(&decoder, allocator);
×
1742
    if (SIXEL_FAILED(status)) {
×
1743
        goto error;
×
1744
    }
1745

1746
    status = sixel_decoder_setopt(
×
1747
        decoder,
1748
        SIXEL_OPTFLAG_INPUT,
1749
        "/");
1750
    if (status != SIXEL_BAD_ALLOCATION) {
×
1751
        goto error;
×
1752
    }
1753

1754
    nret = EXIT_SUCCESS;
×
1755

1756
error:
1757
    return nret;
×
1758
}
1759

1760

1761
static int
1762
test6(void)
×
1763
{
1764
    int nret = EXIT_FAILURE;
×
1765
    sixel_decoder_t *decoder = NULL;
×
1766
    sixel_allocator_t *allocator = NULL;
×
1767
    SIXELSTATUS status;
1768

1769
    sixel_debug_malloc_counter = 4;
×
1770

1771
    status = sixel_allocator_new(
×
1772
        &allocator,
1773
        sixel_bad_malloc,
1774
        NULL,
1775
        NULL,
1776
        NULL);
1777
    if (SIXEL_FAILED(status)) {
×
1778
        goto error;
×
1779
    }
1780

1781
    status = sixel_decoder_new(&decoder, allocator);
×
1782
    if (SIXEL_FAILED(status)) {
×
1783
        goto error;
×
1784
    }
1785

1786
    status = sixel_decoder_setopt(
×
1787
        decoder,
1788
        SIXEL_OPTFLAG_OUTPUT,
1789
        "/");
1790
    if (status != SIXEL_BAD_ALLOCATION) {
×
1791
        goto error;
×
1792
    }
1793

1794
    nret = EXIT_SUCCESS;
×
1795

1796
error:
1797
    return nret;
×
1798
}
1799

1800

1801
static int
1802
test7(void)
×
1803
{
1804
    int nret = EXIT_FAILURE;
×
1805
    sixel_decoder_t *decoder = NULL;
×
1806
    sixel_allocator_t *allocator = NULL;
×
1807
    SIXELSTATUS status;
1808

1809
    status = sixel_allocator_new(
×
1810
        &allocator,
1811
        NULL,
1812
        NULL,
1813
        NULL,
1814
        NULL);
1815
    if (SIXEL_FAILED(status)) {
×
1816
        goto error;
×
1817
    }
1818

1819
    status = sixel_decoder_new(&decoder, allocator);
×
1820
    if (SIXEL_FAILED(status)) {
×
1821
        goto error;
×
1822
    }
1823

1824
    status = sixel_decoder_setopt(
×
1825
        decoder,
1826
        SIXEL_OPTFLAG_INPUT,
1827
        "../images/file");
1828
    if (SIXEL_FAILED(status)) {
×
1829
        goto error;
×
1830
    }
1831

1832
    status = sixel_decoder_decode(decoder);
×
1833
    if ((status >> 8) != (SIXEL_LIBC_ERROR >> 8)) {
×
1834
        goto error;
×
1835
    }
1836

1837
    nret = EXIT_SUCCESS;
×
1838

1839
error:
1840
    return nret;
×
1841
}
1842

1843

1844
static int
1845
test8(void)
×
1846
{
1847
    int nret = EXIT_FAILURE;
×
1848
    sixel_decoder_t *decoder = NULL;
×
1849
    sixel_allocator_t *allocator = NULL;
×
1850
    SIXELSTATUS status;
1851

1852
    sixel_debug_malloc_counter = 5;
×
1853

1854
    status = sixel_allocator_new(
×
1855
        &allocator,
1856
        sixel_bad_malloc,
1857
        NULL,
1858
        NULL,
1859
        NULL);
1860
    if (SIXEL_FAILED(status)) {
×
1861
        goto error;
×
1862
    }
1863

1864
    status = sixel_decoder_new(&decoder, allocator);
×
1865
    if (SIXEL_FAILED(status)) {
×
1866
        goto error;
×
1867
    }
1868

1869
    status = sixel_decoder_setopt(
×
1870
        decoder,
1871
        SIXEL_OPTFLAG_INPUT,
1872
        "../images/map8.six");
1873
    if (SIXEL_FAILED(status)) {
×
1874
        goto error;
×
1875
    }
1876

1877
    status = sixel_decoder_decode(decoder);
×
1878
    if (status != SIXEL_BAD_ALLOCATION) {
×
1879
        goto error;
×
1880
    }
1881

1882
    nret = EXIT_SUCCESS;
×
1883

1884
error:
1885
    return nret;
×
1886
}
1887

1888

1889
SIXELAPI int
1890
sixel_decoder_tests_main(void)
×
1891
{
1892
    int nret = EXIT_FAILURE;
×
1893
    size_t i;
1894
    typedef int (* testcase)(void);
1895

1896
    static testcase const testcases[] = {
1897
        test1,
1898
        test2,
1899
        test3,
1900
        test4,
1901
        test5,
1902
        test6,
1903
        test7,
1904
        test8
1905
    };
1906

1907
    for (i = 0; i < sizeof(testcases) / sizeof(testcase); ++i) {
×
1908
        nret = testcases[i]();
×
1909
        if (nret != EXIT_SUCCESS) {
×
1910
            goto error;
×
1911
        }
1912
    }
1913

1914
    nret = EXIT_SUCCESS;
×
1915

1916
error:
1917
    return nret;
×
1918
}
1919
#endif  /* HAVE_TESTS */
1920

1921
/* emacs Local Variables:      */
1922
/* emacs mode: c               */
1923
/* emacs tab-width: 4          */
1924
/* emacs indent-tabs-mode: nil */
1925
/* emacs c-basic-offset: 4     */
1926
/* emacs End:                  */
1927
/* vim: set expandtab ts=4 sts=4 sw=4 : */
1928
/* 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