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

saitoha / libsixel / 21254372888

22 Jan 2026 03:32PM UTC coverage: 66.513% (+0.6%) from 65.908%
21254372888

push

github

saitoha
tests: change PSNR_Y base-line value on TGA regression tests

26721 of 40174 relevant lines covered (66.51%)

2936029.03 hits per line

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

72.73
/src/decoder.c
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright (c) 2014-2025 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
#if defined(HAVE_CONFIG_H)
25
#include "config.h"
26
#endif
27

28
/* STDC_HEADERS */
29
#include <stdio.h>
30
#include <stdlib.h>
31
#include <string.h>
32

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

57
#include "decoder.h"
58
#include "decoder-parallel.h"
59
#include "clipboard.h"
60
#include "compat_stub.h"
61
#include "options.h"
62
#include "sixel_atomic.h"
63

64
static void
65
decoder_clipboard_select_format(char *dest,
3✔
66
                                size_t dest_size,
67
                                char const *format,
68
                                char const *fallback)
69
{
70
    char const *source;
3✔
71
    size_t limit;
3✔
72

73
    if (dest == NULL || dest_size == 0u) {
3✔
74
        return;
75
    }
76

77
    source = fallback;
3✔
78
    if (format != NULL && format[0] != '\0') {
3✔
79
        source = format;
3✔
80
    }
81

82
    limit = dest_size - 1u;
3✔
83
    if (limit == 0u) {
3✔
84
        dest[0] = '\0';
×
85
        return;
×
86
    }
87

88
    (void)snprintf(dest, dest_size, "%.*s", (int)limit, source);
3✔
89
}
90

91

92
static char *
93
decoder_create_temp_template_with_prefix(sixel_allocator_t *allocator,
2✔
94
                                         char const *prefix,
95
                                         size_t *capacity_out)
96
{
97
    char const *tmpdir;
2✔
98
    size_t tmpdir_len;
2✔
99
    size_t prefix_len;
2✔
100
    size_t suffix_len;
2✔
101
    size_t template_len;
2✔
102
    char *template_path;
2✔
103
    int needs_separator;
2✔
104
    size_t maximum_tmpdir_len;
2✔
105

106
    tmpdir = sixel_compat_getenv("TMPDIR");
2✔
107
#if defined(_WIN32)
108
    if (tmpdir == NULL || tmpdir[0] == '\0') {
1✔
109
        tmpdir = sixel_compat_getenv("TEMP");
1✔
110
    }
111
    if (tmpdir == NULL || tmpdir[0] == '\0') {
1✔
112
        tmpdir = sixel_compat_getenv("TMP");
113
    }
114
#endif
115
    if (tmpdir == NULL || tmpdir[0] == '\0') {
2✔
116
#if defined(_WIN32)
117
        tmpdir = ".";
118
#else
119
        tmpdir = "/tmp";
1✔
120
#endif
121
    }
122

123
    tmpdir_len = strlen(tmpdir);
2✔
124
    prefix_len = strlen(prefix);
2✔
125
    suffix_len = prefix_len + strlen("-XXXXXX");
2✔
126
    maximum_tmpdir_len = (size_t)INT_MAX;
2✔
127

128
    if (maximum_tmpdir_len <= suffix_len + 2) {
2✔
129
        return NULL;
130
    }
131
    if (tmpdir_len > maximum_tmpdir_len - (suffix_len + 2)) {
2✔
132
        return NULL;
133
    }
134

135
    needs_separator = 1;
2✔
136
    if (tmpdir_len > 0) {
2✔
137
        if (tmpdir[tmpdir_len - 1] == '/' || tmpdir[tmpdir_len - 1] == '\\') {
2✔
138
            needs_separator = 0;
2✔
139
        }
140
    }
141

142
    template_len = tmpdir_len + suffix_len + 2;
2✔
143
    template_path = (char *)sixel_allocator_malloc(allocator, template_len);
2✔
144
    if (template_path == NULL) {
2✔
145
        return NULL;
146
    }
147

148
    if (needs_separator) {
2✔
149
#if defined(_WIN32)
150
        (void)snprintf(template_path, template_len,
1✔
151
                       "%s\\%s-XXXXXX", tmpdir, prefix);
152
#else
153
        (void)snprintf(template_path, template_len,
1✔
154
                       "%s/%s-XXXXXX", tmpdir, prefix);
155
#endif
156
    } else {
157
        (void)snprintf(template_path, template_len,
×
158
                       "%s%s-XXXXXX", tmpdir, prefix);
159
    }
160

161
    if (capacity_out != NULL) {
2✔
162
        *capacity_out = template_len;
2✔
163
    }
164

165
    return template_path;
166
}
167

168

169
static SIXELSTATUS
170
decoder_clipboard_create_spool(sixel_allocator_t *allocator,
2✔
171
                               char const *prefix,
172
                               char **path_out)
173
{
174
    SIXELSTATUS status;
2✔
175
    char *template_path;
2✔
176
    size_t template_capacity;
2✔
177
    int open_flags;
2✔
178
    int open_mode;
2✔
179
    int fd;
2✔
180
    char *tmpname_result;
2✔
181

182
    status = SIXEL_FALSE;
2✔
183
    template_path = NULL;
2✔
184
    template_capacity = 0u;
2✔
185
    open_flags = 0;
2✔
186
    open_mode = 0;
2✔
187
    fd = (-1);
2✔
188
    tmpname_result = NULL;
2✔
189

190
    template_path = decoder_create_temp_template_with_prefix(allocator,
2✔
191
                                                             prefix,
192
                                                             &template_capacity);
193
    if (template_path == NULL) {
2✔
194
        sixel_helper_set_additional_message(
×
195
            "clipboard: failed to allocate spool template.");
196
        status = SIXEL_BAD_ALLOCATION;
×
197
        goto end;
×
198
    }
199

200
    if (sixel_compat_mktemp(template_path, template_capacity) != 0) {
2✔
201
        tmpname_result = sixel_compat_tmpnam(template_path,
×
202
                                             template_capacity);
203
        if (tmpname_result == NULL) {
×
204
            sixel_helper_set_additional_message(
×
205
                "clipboard: failed to reserve spool template.");
206
            status = SIXEL_LIBC_ERROR;
×
207
            goto end;
×
208
        }
209
        template_capacity = strlen(template_path) + 1u;
210
    }
211

212
    open_flags = O_RDWR | O_CREAT | O_TRUNC;
2✔
213
#if defined(O_EXCL)
214
    open_flags |= O_EXCL;
2✔
215
#endif
216
    open_mode = S_IRUSR | S_IWUSR;
2✔
217
    fd = sixel_compat_open(template_path, open_flags, open_mode);
2✔
218
    if (fd < 0) {
2✔
219
        sixel_helper_set_additional_message(
×
220
            "clipboard: failed to open spool file.");
221
        status = SIXEL_LIBC_ERROR;
×
222
        goto end;
×
223
    }
224

225
    *path_out = template_path;
2✔
226
    if (fd >= 0) {
2✔
227
        (void)sixel_compat_close(fd);
2✔
228
        fd = (-1);
2✔
229
    }
230

231
    template_path = NULL;
2✔
232
    status = SIXEL_OK;
2✔
233

234
end:
×
235
    if (fd >= 0) {
2✔
236
        (void)sixel_compat_close(fd);
237
    }
238
    if (template_path != NULL) {
2✔
239
        sixel_allocator_free(allocator, template_path);
×
240
    }
241

242
    return status;
2✔
243
}
244

245

246
static SIXELSTATUS
247
decoder_clipboard_read_file(char const *path,
2✔
248
                            unsigned char **data,
249
                            size_t *size)
250
{
251
    FILE *stream;
2✔
252
    long seek_result;
2✔
253
    long file_size;
2✔
254
    unsigned char *buffer;
2✔
255
    size_t read_size;
2✔
256

257
    if (data == NULL || size == NULL) {
2✔
258
        sixel_helper_set_additional_message(
×
259
            "clipboard: read buffer pointers are null.");
260
        return SIXEL_BAD_ARGUMENT;
×
261
    }
262

263
    *data = NULL;
2✔
264
    *size = 0u;
2✔
265

266
    if (path == NULL) {
2✔
267
        sixel_helper_set_additional_message(
×
268
            "clipboard: spool path is null.");
269
        return SIXEL_BAD_ARGUMENT;
×
270
    }
271

272
    stream = sixel_compat_fopen(path, "rb");
2✔
273
    if (stream == NULL) {
2✔
274
        sixel_helper_set_additional_message(
×
275
            "clipboard: failed to open spool file for read.");
276
        return SIXEL_LIBC_ERROR;
×
277
    }
278

279
    seek_result = fseek(stream, 0L, SEEK_END);
2✔
280
    if (seek_result != 0) {
2✔
281
        (void)fclose(stream);
×
282
        sixel_helper_set_additional_message(
×
283
            "clipboard: failed to seek spool file.");
284
        return SIXEL_LIBC_ERROR;
×
285
    }
286

287
    file_size = ftell(stream);
2✔
288
    if (file_size < 0) {
2✔
289
        (void)fclose(stream);
×
290
        sixel_helper_set_additional_message(
×
291
            "clipboard: failed to determine spool size.");
292
        return SIXEL_LIBC_ERROR;
×
293
    }
294

295
    seek_result = fseek(stream, 0L, SEEK_SET);
2✔
296
    if (seek_result != 0) {
2✔
297
        (void)fclose(stream);
×
298
        sixel_helper_set_additional_message(
×
299
            "clipboard: failed to rewind spool file.");
300
        return SIXEL_LIBC_ERROR;
×
301
    }
302

303
    if (file_size == 0) {
2✔
304
        buffer = NULL;
305
        read_size = 0u;
306
    } else {
307
        buffer = (unsigned char *)malloc((size_t)file_size);
2✔
308
        if (buffer == NULL) {
2✔
309
            (void)fclose(stream);
×
310
            sixel_helper_set_additional_message(
×
311
                "clipboard: malloc() failed for spool payload.");
312
            return SIXEL_BAD_ALLOCATION;
×
313
        }
314
        read_size = fread(buffer, 1u, (size_t)file_size, stream);
2✔
315
        if (read_size != (size_t)file_size) {
2✔
316
            free(buffer);
×
317
            (void)fclose(stream);
×
318
            sixel_helper_set_additional_message(
×
319
                "clipboard: failed to read spool payload.");
320
            return SIXEL_LIBC_ERROR;
×
321
        }
322
    }
323

324
    if (fclose(stream) != 0) {
2✔
325
        if (buffer != NULL) {
×
326
            free(buffer);
×
327
        }
328
        sixel_helper_set_additional_message(
×
329
            "clipboard: failed to close spool file after read.");
330
        return SIXEL_LIBC_ERROR;
×
331
    }
332

333
    *data = buffer;
2✔
334
    *size = read_size;
2✔
335

336
    return SIXEL_OK;
2✔
337
}
338

339

340
/* original version of strdup(3) with allocator object */
341
static char *
342
strdup_with_allocator(
481✔
343
    char const          /* in */ *s,          /* source buffer */
344
    sixel_allocator_t   /* in */ *allocator)  /* allocator object for
345
                                                 destination buffer */
346
{
347
    char *p;
481✔
348

349
    p = (char *)sixel_allocator_malloc(allocator, (size_t)(strlen(s) + 1));
481✔
350
    if (p) {
481✔
351
        (void)sixel_compat_strcpy(p, strlen(s) + 1, s);
481✔
352
    }
353
    return p;
481✔
354
}
355

356

357
/* create decoder object */
358
SIXELAPI SIXELSTATUS
359
sixel_decoder_new(
142✔
360
    sixel_decoder_t    /* out */ **ppdecoder,  /* decoder object to be created */
361
    sixel_allocator_t  /* in */  *allocator)   /* allocator, null if you use
362
                                                  default allocator */
363
{
364
    SIXELSTATUS status = SIXEL_FALSE;
142✔
365

366
    if (allocator == NULL) {
142✔
367
        status = sixel_allocator_new(&allocator, NULL, NULL, NULL, NULL);
142✔
368
        if (SIXEL_FAILED(status)) {
142✔
369
            goto end;
×
370
        }
371
    } else {
372
        sixel_allocator_ref(allocator);
×
373
    }
374

375
    *ppdecoder = sixel_allocator_malloc(allocator, sizeof(sixel_decoder_t));
142✔
376
    if (*ppdecoder == NULL) {
142✔
377
        sixel_allocator_unref(allocator);
×
378
        sixel_helper_set_additional_message(
×
379
            "sixel_decoder_new: sixel_allocator_malloc() failed.");
380
        status = SIXEL_BAD_ALLOCATION;
×
381
        goto end;
×
382
    }
383

384
    (*ppdecoder)->ref          = 1U;
142✔
385
    (*ppdecoder)->output       = strdup_with_allocator("-", allocator);
142✔
386
    (*ppdecoder)->input        = strdup_with_allocator("-", allocator);
142✔
387
    (*ppdecoder)->allocator    = allocator;
142✔
388
    (*ppdecoder)->dequantize_method = SIXEL_DEQUANTIZE_NONE;
142✔
389
    (*ppdecoder)->dequantize_similarity_bias = 100;
142✔
390
    (*ppdecoder)->dequantize_edge_strength = 0;
142✔
391
    (*ppdecoder)->thumbnail_size = 0;
142✔
392
    (*ppdecoder)->direct_color = 0;
142✔
393
    (*ppdecoder)->clipboard_input_active = 0;
142✔
394
    (*ppdecoder)->clipboard_output_active = 0;
142✔
395
    (*ppdecoder)->clipboard_input_format[0] = '\0';
142✔
396
    (*ppdecoder)->clipboard_output_format[0] = '\0';
142✔
397

398
    if ((*ppdecoder)->output == NULL || (*ppdecoder)->input == NULL) {
142✔
399
        sixel_decoder_unref(*ppdecoder);
×
400
        *ppdecoder = NULL;
×
401
        sixel_helper_set_additional_message(
×
402
            "sixel_decoder_new: strdup_with_allocator() failed.");
403
        status = SIXEL_BAD_ALLOCATION;
×
404
        sixel_allocator_unref(allocator);
×
405
        goto end;
×
406
    }
407

408
    status = SIXEL_OK;
409

410
end:
142✔
411
    return status;
142✔
412
}
413

414

415
/* deprecated version of sixel_decoder_new() */
416
SIXELAPI /* deprecated */ sixel_decoder_t *
417
sixel_decoder_create(void)
×
418
{
419
    SIXELSTATUS status = SIXEL_FALSE;
×
420
    sixel_decoder_t *decoder = NULL;
×
421

422
    status = sixel_decoder_new(&decoder, NULL);
×
423
    if (SIXEL_FAILED(status)) {
×
424
        goto end;
425
    }
426

427
end:
×
428
    return decoder;
×
429
}
430

431

432
/* destroy a decoder object */
433
static void
434
sixel_decoder_destroy(sixel_decoder_t *decoder)
140✔
435
{
436
    sixel_allocator_t *allocator;
140✔
437

438
    if (decoder) {
140✔
439
        allocator = decoder->allocator;
140✔
440
        sixel_allocator_free(allocator, decoder->input);
140✔
441
        sixel_allocator_free(allocator, decoder->output);
140✔
442
        sixel_allocator_free(allocator, decoder);
140✔
443
        sixel_allocator_unref(allocator);
140✔
444
    }
445
}
140✔
446

447

448
/* increase reference count of decoder object (thread-safe) */
449
SIXELAPI void
450
sixel_decoder_ref(sixel_decoder_t *decoder)
343✔
451
{
452
    if (decoder == NULL) {
343✔
453
        return;
454
    }
455

456
    (void)sixel_atomic_fetch_add_u32(&decoder->ref, 1U);
343✔
457
}
458

459

460
/* decrease reference count of decoder object (thread-safe) */
461
SIXELAPI void
462
sixel_decoder_unref(sixel_decoder_t *decoder)
483✔
463
{
464
    unsigned int previous;
483✔
465

466
    if (decoder == NULL) {
483✔
467
        return;
468
    }
469

470
    previous = sixel_atomic_fetch_sub_u32(&decoder->ref, 1U);
483✔
471
    if (previous == 1U) {
483✔
472
        sixel_decoder_destroy(decoder);
140✔
473
    }
474
}
475

476

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

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

495
    if (bias < 1) {
2✔
496
        bias = 1;
497
    }
498

499
    similarity->palette = palette;
2✔
500
    similarity->ncolors = ncolors;
2✔
501
    similarity->stride = ncolors;
2✔
502
    similarity->bias = bias;
2✔
503

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

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

523
    return SIXEL_OK;
524
}
525

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

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

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

570
    if (similarity->cache == NULL) {
4,307,408✔
571
        return 0;
572
    }
573

574
    if (index1 < 0 || index1 >= similarity->ncolors ||
4,307,408✔
575
        index2 < 0 || index2 >= similarity->ncolors) {
4,307,408✔
576
        return 0;
577
    }
578

579
    if (index1 <= index2) {
4,307,408✔
580
        min_index = index1;
581
        max_index = index2;
582
    } else {
583
        min_index = index2;
1,297,930✔
584
        max_index = index1;
1,297,930✔
585
    }
586

587
    cache_pos = (size_t)min_index * (size_t)similarity->stride
4,307,408✔
588
              + (size_t)max_index;
4,307,408✔
589
    cached = similarity->cache[cache_pos];
4,307,408✔
590
    if (cached >= 0) {
4,307,408✔
591
        return (unsigned int)cached;
4,300,954✔
592
    }
593

594
    palette = similarity->palette;
6,454✔
595
    p1 = palette + index1 * 3;
6,454✔
596
    p2 = palette + index2 * 3;
6,454✔
597

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

631
    distance = sixel_similarity_diff(avg_color, p1);
6,454✔
632
    bias = similarity->bias;
6,454✔
633
    if (bias < 1) {
6,454✔
634
        bias = 1;
635
    }
636
    scaled_distance = (unsigned long long)distance
6,454✔
637
                    * (unsigned long long)bias
6,454✔
638
                    + 50ULL;
639
    base_distance = (unsigned int)(scaled_distance / 100ULL);
6,454✔
640
    if (base_distance == 0U) {
6,454✔
641
        base_distance = 1U;
642
    }
643

644
    for (i = 0; i < similarity->ncolors; ++i) {
1,084,272✔
645
        if (i == index1 || i == index2) {
1,077,818✔
646
            continue;
12,908✔
647
        }
648
        pk = palette + i * 3;
1,064,910✔
649
        diff = sixel_similarity_diff(avg_color, pk);
1,064,910✔
650
        if (diff < min_diff) {
1,064,910✔
651
            min_diff = diff;
1,077,818✔
652
        }
653
    }
654

655
    if (min_diff == UINT_MAX) {
6,454✔
656
        min_diff = base_distance * 2U;
×
657
    }
658

659
    if (min_diff >= base_distance * 2U) {
6,454✔
660
        result = 5U;
661
    } else if (min_diff >= base_distance) {
6,110✔
662
        result = 8U;
663
    } else if ((unsigned long long)min_diff * 6ULL
5,696✔
664
               >= (unsigned long long)base_distance * 5ULL) {
5,696✔
665
        result = 7U;
666
    } else if ((unsigned long long)min_diff * 4ULL
5,548✔
667
               >= (unsigned long long)base_distance * 3ULL) {
5,548✔
668
        result = 7U;
669
    } else if ((unsigned long long)min_diff * 3ULL
5,448✔
670
               >= (unsigned long long)base_distance * 2ULL) {
5,448✔
671
        result = 5U;
672
    } else if ((unsigned long long)min_diff * 5ULL
5,320✔
673
               >= (unsigned long long)base_distance * 3ULL) {
674
        result = 7U;
675
    } else if ((unsigned long long)min_diff * 2ULL
5,194✔
676
               >= (unsigned long long)base_distance * 1ULL) {
677
        result = 4U;
678
    } else if ((unsigned long long)min_diff * 3ULL
4,958✔
679
               >= (unsigned long long)base_distance * 1ULL) {
680
        result = 2U;
681
    } else {
682
        result = 0U;
4,308✔
683
    }
684

685
    similarity->cache[cache_pos] = (signed char)result;
6,454✔
686

687
    return result;
6,454✔
688
}
689

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

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

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

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

743
    if (percent <= 0) {
4✔
744
        percent = 1;
745
    }
746

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

757
    return (unsigned short)scaled;
4✔
758
}
759

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

804
    if (width <= 0 || height <= 0 || palette == NULL || ncolors <= 0) {
2✔
805
        return SIXEL_BAD_INPUT;
806
    }
807

808
    num_pixels = (size_t)width * (size_t)height;
2✔
809

810
    memset(&similarity, 0, sizeof(sixel_similarity_t));
2✔
811

812
    strong_threshold = sixel_scale_threshold(256U, edge_strength);
2✔
813
    detail_threshold = sixel_scale_threshold(160U, edge_strength);
2✔
814
    if (strong_threshold < detail_threshold) {
2✔
815
        strong_threshold = detail_threshold;
816
    }
817

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

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

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

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

869
    for (y = 0; y < height; ++y) {
902✔
870
        for (x = 0; x < width; ++x) {
540,900✔
871
            palette_index = indexed_pixels[y * width + x];
540,000✔
872
            if (palette_index < 0 || palette_index >= ncolors) {
540,000✔
873
                palette_index = 0;
×
874
            }
875

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

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

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

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

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

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

928
                if (nx < 0 || nx >= width || ny < 0 || ny >= height) {
4,320,000✔
929
                    continue;
12,592✔
930
                }
931

932
                neighbor_index = indexed_pixels[ny * width + nx];
4,307,408✔
933
                if (neighbor_index < 0 || neighbor_index >= ncolors) {
4,307,408✔
934
                    continue;
×
935
                }
936

937
                if (numerator) {
4,307,408✔
938
                    weight = sixel_similarity_compare(
4,307,408✔
939
                        &similarity,
940
                        palette_index,
941
                        neighbor_index,
942
                        numerator,
943
                        denominator);
944
                    if (weight == 0) {
4,307,408✔
945
                        continue;
234,100✔
946
                    }
947

948
                    neighbor_color = palette + neighbor_index * 3;
4,073,308✔
949
                    accum_r += (unsigned int)neighbor_color[0] * weight;
4,073,308✔
950
                    accum_g += (unsigned int)neighbor_color[1] * weight;
4,073,308✔
951
                    accum_b += (unsigned int)neighbor_color[2] * weight;
4,073,308✔
952
                    total_weight += weight;
4,073,308✔
953
                }
954
            }
955

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

964

965
    *output = rgb;
2✔
966
    rgb = NULL;
2✔
967
    status = SIXEL_OK;
2✔
968

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

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

991
static void
992
decoder_normalise_windows_drive_path(char *path)
3✔
993
{
994
#if defined(_WIN32)
995
    size_t length;
3✔
996

997
    length = 0u;
3✔
998

999
    if (path == NULL) {
3✔
1000
        return;
1001
    }
1002

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

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

1041
    sixel_decoder_ref(decoder);
221✔
1042
    path_flags = 0u;
221✔
1043
    path_check = 0;
221✔
1044

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

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

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

1140
        match_index = 0;
4✔
1141
        memset(match_detail, 0, sizeof(match_detail));
4✔
1142
        memset(match_message, 0, sizeof(match_message));
4✔
1143

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

1174
    case SIXEL_OPTFLAG_SIMILARITY:  /* S */
2✔
1175
        errno = 0;
2✔
1176
        bias = strtol(value, &endptr, 10);
2✔
1177
        if (endptr == value || endptr[0] != '\0' ||
2✔
1178
            errno == ERANGE || bias < 0 || bias > 1000) {
×
1179
            sixel_helper_set_additional_message(
2✔
1180
                "similarity must be an integer between 0 and 1000.");
1181
            status = SIXEL_BAD_ARGUMENT;
2✔
1182
            goto end;
2✔
1183
        }
1184

1185
        decoder->dequantize_similarity_bias = (int)bias;
×
1186
        break;
×
1187

1188
    case SIXEL_OPTFLAG_SIZE:  /* s */
×
1189
        parsed_value = 0L;
×
1190
        endptr = NULL;
×
1191
        errno = 0;
×
1192
        parsed_value = strtol(value, &endptr, 10);
×
1193
        if (endptr == value || *endptr != '\0' || errno == ERANGE ||
×
1194
            parsed_value < 1L || parsed_value > (long)INT_MAX) {
1195
            sixel_helper_set_additional_message(
×
1196
                "size must be greater than zero.");
1197
            status = SIXEL_BAD_ARGUMENT;
×
1198
            goto end;
×
1199
        }
1200
        decoder->thumbnail_size = (int)parsed_value;
×
1201
        break;
×
1202

1203
    case SIXEL_OPTFLAG_EDGE:  /* e */
×
1204
        parsed_value = 0L;
×
1205
        endptr = NULL;
×
1206
        errno = 0;
×
1207
        parsed_value = strtol(value, &endptr, 10);
×
1208
        if (endptr == value || *endptr != '\0' || errno == ERANGE ||
×
1209
            parsed_value < 0L || parsed_value > 1000L) {
×
1210
            sixel_helper_set_additional_message(
×
1211
                "edge bias must be between 1 and 1000.");
1212
            status = SIXEL_BAD_ARGUMENT;
×
1213
            goto end;
×
1214
        }
1215
        decoder->dequantize_edge_strength = (int)parsed_value;
×
1216
        break;
×
1217

1218
    case SIXEL_OPTFLAG_DIRECT:  /* D */
12✔
1219
        decoder->direct_color = 1;
12✔
1220
        break;
12✔
1221

1222
    case SIXEL_OPTFLAG_THREADS:  /* = */
2✔
1223
        status = sixel_decoder_parallel_override_threads(value);
2✔
1224
        if (SIXEL_FAILED(status)) {
2✔
1225
            goto end;
2✔
1226
        }
1227
        break;
1228

1229
    case '?':
×
1230
    default:
1231
        status = SIXEL_BAD_ARGUMENT;
×
1232
        goto end;
×
1233
    }
1234

1235
    status = SIXEL_OK;
1236

1237
end:
219✔
1238
    sixel_decoder_unref(decoder);
219✔
1239

1240
    return status;
219✔
1241
}
1242

1243

1244
/* load source data from stdin or the file specified with
1245
   SIXEL_OPTFLAG_INPUT flag, and decode it */
1246
SIXELAPI SIXELSTATUS
1247
sixel_decoder_decode(
122✔
1248
    sixel_decoder_t /* in */ *decoder)
1249
{
1250
    SIXELSTATUS status = SIXEL_FALSE;
122✔
1251
    unsigned char *raw_data = NULL;
122✔
1252
    int sx;
122✔
1253
    int sy;
122✔
1254
    int raw_len;
122✔
1255
    int max;
122✔
1256
    int n;
122✔
1257
    FILE *input_fp = NULL;
122✔
1258
    char message[2048];
122✔
1259
    unsigned char *indexed_pixels = NULL;
122✔
1260
    unsigned char *palette = NULL;
122✔
1261
    unsigned char *rgb_pixels = NULL;
122✔
1262
    unsigned char *direct_pixels = NULL;
122✔
1263
    unsigned char *output_pixels;
122✔
1264
    unsigned char *output_palette;
122✔
1265
    int output_pixelformat;
122✔
1266
    int ncolors;
122✔
1267
    sixel_frame_t *frame;
122✔
1268
    int new_width;
122✔
1269
    int new_height;
122✔
1270
    double scaled_width;
122✔
1271
    double scaled_height;
122✔
1272
    int max_dimension;
122✔
1273
    int thumbnail_size;
122✔
1274
    int frame_ncolors;
122✔
1275
    unsigned char *clipboard_blob;
122✔
1276
    size_t clipboard_blob_size;
122✔
1277
    SIXELSTATUS clipboard_status;
122✔
1278
    char *clipboard_output_path;
122✔
1279
    unsigned char *clipboard_output_data;
122✔
1280
    size_t clipboard_output_size;
122✔
1281
    SIXELSTATUS clipboard_output_status;
122✔
1282
    sixel_logger_t logger;
122✔
1283
    int logger_prepared;
122✔
1284

1285
    sixel_decoder_ref(decoder);
122✔
1286

1287
    frame = NULL;
122✔
1288
    new_width = 0;
122✔
1289
    new_height = 0;
122✔
1290
    scaled_width = 0.0;
122✔
1291
    scaled_height = 0.0;
122✔
1292
    max_dimension = 0;
122✔
1293
    thumbnail_size = decoder->thumbnail_size;
122✔
1294
    frame_ncolors = -1;
122✔
1295
    clipboard_blob = NULL;
122✔
1296
    clipboard_blob_size = 0u;
122✔
1297
    clipboard_status = SIXEL_OK;
122✔
1298
    clipboard_output_path = NULL;
122✔
1299
    clipboard_output_data = NULL;
122✔
1300
    clipboard_output_size = 0u;
122✔
1301
    clipboard_output_status = SIXEL_OK;
122✔
1302
    input_fp = NULL;
122✔
1303
    sixel_logger_init(&logger);
122✔
1304
    (void)sixel_logger_prepare_env(&logger);
122✔
1305
    logger_prepared = logger.active;
122✔
1306

1307
    raw_len = 0;
122✔
1308
    max = 0;
122✔
1309
    if (decoder->clipboard_input_active) {
122✔
1310
        clipboard_status = sixel_clipboard_read(
2✔
1311
            decoder->clipboard_input_format,
1✔
1312
            &clipboard_blob,
1313
            &clipboard_blob_size,
1314
            decoder->allocator);
1315
        if (SIXEL_FAILED(clipboard_status)) {
1✔
1316
            status = clipboard_status;
×
1317
            goto end;
×
1318
        }
1319
        max = (int)((clipboard_blob_size > 0u)
1✔
1320
                    ? clipboard_blob_size
1321
                    : 1u);
1322
        raw_data = (unsigned char *)sixel_allocator_malloc(
1✔
1323
            decoder->allocator,
1324
            (size_t)max);
1325
        if (raw_data == NULL) {
1✔
1326
            sixel_helper_set_additional_message(
×
1327
                "sixel_decoder_decode: sixel_allocator_malloc() failed.");
1328
            status = SIXEL_BAD_ALLOCATION;
×
1329
            goto end;
×
1330
        }
1331
        if (clipboard_blob_size > 0u && clipboard_blob != NULL) {
1✔
1332
            memcpy(raw_data, clipboard_blob, clipboard_blob_size);
1✔
1333
        }
1334
        raw_len = (int)clipboard_blob_size;
1✔
1335
        if (clipboard_blob != NULL) {
1✔
1336
            free(clipboard_blob);
1✔
1337
            clipboard_blob = NULL;
1✔
1338
        }
1339
    } else {
1340
        if (strcmp(decoder->input, "-") == 0) {
121✔
1341
            /* for windows */
1342
#if defined(O_BINARY)
1343
            (void)sixel_compat_set_binary(STDIN_FILENO);
28✔
1344
#endif  /* defined(O_BINARY) */
1345
            input_fp = stdin;
28✔
1346
        } else {
1347
            input_fp = sixel_compat_fopen(decoder->input, "rb");
93✔
1348
            if (! input_fp) {
93✔
1349
                (void)snprintf(
×
1350
                    message,
1351
                    sizeof(message) - 1,
1352
                    "sixel_decoder_decode: failed to open input file: %s.",
1353
                    decoder->input);
1354
                sixel_helper_set_additional_message(message);
×
1355
                status = (SIXEL_LIBC_ERROR | (errno & 0xff));
×
1356
                goto end;
×
1357
            }
1358
        }
1359

1360
        raw_len = 0;
121✔
1361
        max = 64 * 1024;
121✔
1362

1363
        raw_data = (unsigned char *)sixel_allocator_malloc(
121✔
1364
            decoder->allocator,
1365
            (size_t)max);
1366
        if (raw_data == NULL) {
121✔
1367
            sixel_helper_set_additional_message(
×
1368
                "sixel_decoder_decode: sixel_allocator_malloc() failed.");
1369
            status = SIXEL_BAD_ALLOCATION;
×
1370
            goto end;
×
1371
        }
1372

1373
        for (;;) {
9,355✔
1374
            if ((max - raw_len) < 4096) {
4,738✔
1375
                max *= 2;
80✔
1376
                raw_data = (unsigned char *)sixel_allocator_realloc(
80✔
1377
                    decoder->allocator,
1378
                    raw_data,
1379
                    (size_t)max);
1380
                if (raw_data == NULL) {
80✔
1381
                    sixel_helper_set_additional_message(
×
1382
                        "sixel_decoder_decode: sixel_allocator_realloc() failed.");
1383
                    status = SIXEL_BAD_ALLOCATION;
×
1384
                    goto end;
×
1385
                }
1386
            }
1387
            if ((n = (int)fread(raw_data + raw_len, 1, 4096, input_fp)) <= 0) {
4,738✔
1388
                break;
1389
            }
1390
            raw_len += n;
4,617✔
1391
        }
1392

1393
        if (input_fp != NULL && input_fp != stdin) {
121✔
1394
            fclose(input_fp);
93✔
1395
        }
1396
    }
1397

1398
    if (decoder->direct_color != 0 &&
122✔
1399
            decoder->dequantize_method != SIXEL_DEQUANTIZE_NONE) {
12✔
1400
        sixel_helper_set_additional_message(
2✔
1401
            "sixel_decoder_decode: direct option "
1402
            "cannot be combined with dequantize option.");
1403
        status = SIXEL_BAD_ARGUMENT;
2✔
1404
        goto end;
2✔
1405
    }
1406

1407
    ncolors = 0;
120✔
1408

1409
    if (decoder->direct_color != 0) {
120✔
1410
        status = sixel_decode_direct(
10✔
1411
            raw_data,
1412
            raw_len,
1413
            &direct_pixels,
1414
            &sx,
1415
            &sy,
1416
            decoder->allocator);
1417
    } else {
1418
        status = sixel_decode_raw(
110✔
1419
            raw_data,
1420
            raw_len,
1421
            &indexed_pixels,
1422
            &sx,
1423
            &sy,
1424
            &palette,
1425
            &ncolors,
1426
            decoder->allocator);
1427
    }
1428
    if (SIXEL_FAILED(status)) {
120✔
1429
        goto end;
×
1430
    }
1431

1432
    if (sx > SIXEL_WIDTH_LIMIT || sy > SIXEL_HEIGHT_LIMIT) {
120✔
1433
        status = SIXEL_BAD_INPUT;
×
1434
        goto end;
×
1435
    }
1436

1437
    if (decoder->direct_color != 0) {
120✔
1438
        output_pixels = direct_pixels;
10✔
1439
        output_palette = NULL;
10✔
1440
        output_pixelformat = SIXEL_PIXELFORMAT_RGBA8888;
10✔
1441
        frame_ncolors = 0;
10✔
1442
    } else {
1443
        output_pixels = indexed_pixels;
110✔
1444
        output_palette = palette;
110✔
1445
        output_pixelformat = SIXEL_PIXELFORMAT_PAL8;
110✔
1446

1447
        if (decoder->dequantize_method == SIXEL_DEQUANTIZE_K_UNDITHER) {
110✔
1448
            if (logger_prepared) {
2✔
1449
                sixel_logger_logf(&logger,
×
1450
                                  "decoder",
1451
                                  "undither",
1452
                                  "start",
1453
                                  0);
1454
            }
1455
            status = sixel_dequantize_k_undither(
2✔
1456
                indexed_pixels,
1457
                sx,
1458
                sy,
1459
                palette,
1460
                ncolors,
1461
                decoder->dequantize_similarity_bias,
1462
                decoder->dequantize_edge_strength,
1463
                decoder->allocator,
1464
                &rgb_pixels);
1465
            if (SIXEL_FAILED(status)) {
2✔
1466
                if (logger_prepared) {
×
1467
                    sixel_logger_logf(
×
1468
                        &logger,
1469
                        "decoder",
1470
                        "undither",
1471
                        "abort",
1472
                        0);
1473
                }
1474
                goto end;
×
1475
            }
1476
            if (logger_prepared) {
2✔
1477
                sixel_logger_logf(&logger,
×
1478
                                  "decoder",
1479
                                  "undither",
1480
                                  "finish",
1481
                                  0);
1482
            }
1483
            output_pixels = rgb_pixels;
2✔
1484
            output_palette = NULL;
2✔
1485
            output_pixelformat = SIXEL_PIXELFORMAT_RGB888;
2✔
1486
        }
1487

1488
        if (output_pixelformat == SIXEL_PIXELFORMAT_PAL8) {
2✔
1489
            frame_ncolors = ncolors;
108✔
1490
        } else {
1491
            frame_ncolors = 0;
1492
        }
1493
    }
1494

1495
    if (thumbnail_size > 0) {
120✔
1496
        /*
1497
         * When the caller requests a thumbnail, compute the new geometry
1498
         * while preserving the original aspect ratio. We only allocate a
1499
         * frame when the dimensions actually change, so the fast path for
1500
         * matching sizes still avoids any additional allocations.
1501
         */
1502
        max_dimension = sx;
×
1503
        if (sy > max_dimension) {
×
1504
            max_dimension = sy;
1505
        }
1506
        if (max_dimension > 0) {
×
1507
            if (sx >= sy) {
×
1508
                new_width = thumbnail_size;
×
1509
                scaled_height = (double)sy * (double)thumbnail_size /
×
1510
                    (double)sx;
×
1511
                new_height = (int)(scaled_height + 0.5);
×
1512
            } else {
1513
                new_height = thumbnail_size;
×
1514
                scaled_width = (double)sx * (double)thumbnail_size /
×
1515
                    (double)sy;
×
1516
                new_width = (int)(scaled_width + 0.5);
×
1517
            }
1518
            if (new_width < 1) {
×
1519
                new_width = 1;
1520
            }
1521
            if (new_height < 1) {
×
1522
                new_height = 1;
1523
            }
1524
            if (new_width != sx || new_height != sy) {
×
1525
                /*
1526
                 * Wrap the decoded pixels in a frame so we can reuse the
1527
                 * central scaling helper. Ownership transfers to the frame,
1528
                 * which keeps the lifetime rules identical on both paths.
1529
                 */
1530
                status = sixel_frame_new(&frame, decoder->allocator);
×
1531
                if (SIXEL_FAILED(status)) {
×
1532
                    goto end;
×
1533
                }
1534
                status = sixel_frame_init(
×
1535
                    frame,
1536
                    output_pixels,
1537
                    sx,
1538
                    sy,
1539
                    output_pixelformat,
1540
                    output_palette,
1541
                    frame_ncolors);
1542
                if (SIXEL_FAILED(status)) {
×
1543
                    goto end;
×
1544
                }
1545
                if (output_pixels == indexed_pixels) {
×
1546
                    indexed_pixels = NULL;
×
1547
                }
1548
                if (output_pixels == rgb_pixels) {
×
1549
                    rgb_pixels = NULL;
×
1550
                }
1551
                if (output_palette == palette) {
×
1552
                    palette = NULL;
×
1553
                }
1554
                status = sixel_frame_resize(
×
1555
                    frame,
1556
                    new_width,
1557
                    new_height,
1558
                    SIXEL_RES_BILINEAR);
1559
                if (SIXEL_FAILED(status)) {
×
1560
                    goto end;
×
1561
                }
1562
                /*
1563
                 * The resized frame already exposes a tightly packed RGB
1564
                 * buffer, so write the updated dimensions and references
1565
                 * back to the main encoder path.
1566
                 */
1567
                sx = sixel_frame_get_width(frame);
×
1568
                sy = sixel_frame_get_height(frame);
×
1569
                output_pixels = sixel_frame_get_pixels(frame);
×
1570
                output_palette = NULL;
×
1571
                output_pixelformat = sixel_frame_get_pixelformat(frame);
×
1572
            }
1573
        }
1574
    }
1575

1576
    if (decoder->clipboard_output_active) {
120✔
1577
        clipboard_output_status = decoder_clipboard_create_spool(
2✔
1578
            decoder->allocator,
1579
            "clipboard-out",
1580
            &clipboard_output_path);
1581
        if (SIXEL_FAILED(clipboard_output_status)) {
2✔
1582
            status = clipboard_output_status;
×
1583
            goto end;
×
1584
        }
1585
    }
1586

1587
    if (logger_prepared) {
120✔
1588
        sixel_logger_logf(&logger,
×
1589
                          "io",
1590
                          "png",
1591
                          "start",
1592
                          0);
1593
    }
1594
    status = sixel_helper_write_image_file(
120✔
1595
        output_pixels,
1596
        sx,
1597
        sy,
1598
        output_palette,
1599
        output_pixelformat,
1600
        decoder->clipboard_output_active
120✔
1601
            ? clipboard_output_path
1602
            : decoder->output,
1603
        SIXEL_FORMAT_PNG,
1604
        decoder->allocator);
1605
    if (SIXEL_FAILED(status)) {
120✔
1606
        if (logger_prepared) {
×
1607
            sixel_logger_logf(&logger,
×
1608
                              "io",
1609
                              "png",
1610
                              "abort",
1611
                              0);
1612
        }
1613
        goto end;
×
1614
    }
1615
    if (logger_prepared) {
120✔
1616
        sixel_logger_logf(&logger,
×
1617
                          "io",
1618
                          "png",
1619
                          "finish",
1620
                          0);
1621
    }
1622

1623
    if (decoder->clipboard_output_active) {
120✔
1624
        clipboard_output_status = decoder_clipboard_read_file(
2✔
1625
            clipboard_output_path,
1626
            &clipboard_output_data,
1627
            &clipboard_output_size);
1628
        if (SIXEL_SUCCEEDED(clipboard_output_status)) {
2✔
1629
            clipboard_output_status = sixel_clipboard_write(
2✔
1630
                decoder->clipboard_output_format,
2✔
1631
                clipboard_output_data,
1632
                clipboard_output_size);
1633
        }
1634
        if (clipboard_output_data != NULL) {
2✔
1635
            free(clipboard_output_data);
2✔
1636
            clipboard_output_data = NULL;
2✔
1637
        }
1638
        if (SIXEL_FAILED(clipboard_output_status)) {
2✔
1639
            status = clipboard_output_status;
1✔
1640
            goto end;
1✔
1641
        }
1642
    }
1643

1644
end:
119✔
1645
    sixel_frame_unref(frame);
122✔
1646
    sixel_allocator_free(decoder->allocator, raw_data);
122✔
1647
    sixel_allocator_free(decoder->allocator, indexed_pixels);
122✔
1648
    sixel_allocator_free(decoder->allocator, palette);
122✔
1649
    sixel_allocator_free(decoder->allocator, direct_pixels);
122✔
1650
    sixel_allocator_free(decoder->allocator, rgb_pixels);
122✔
1651
    if (clipboard_blob != NULL) {
122✔
1652
        free(clipboard_blob);
×
1653
    }
1654
    if (clipboard_output_path != NULL) {
122✔
1655
        (void)sixel_compat_unlink(clipboard_output_path);
2✔
1656
        sixel_allocator_free(decoder->allocator, clipboard_output_path);
2✔
1657
    }
1658

1659
    sixel_decoder_unref(decoder);
122✔
1660
    if (logger_prepared) {
122✔
1661
        sixel_logger_close(&logger);
×
1662
    }
1663

1664
    return status;
122✔
1665
}
1666

1667

1668
/* Exercise legacy constructor and refcounting for the decoder. */
1669

1670
/* emacs Local Variables:      */
1671
/* emacs mode: c               */
1672
/* emacs tab-width: 4          */
1673
/* emacs indent-tabs-mode: nil */
1674
/* emacs c-basic-offset: 4     */
1675
/* emacs End:                  */
1676
/* vim: set expandtab ts=4 sts=4 sw=4 : */
1677
/* 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