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

saitoha / libsixel / 20466639304

23 Dec 2025 04:53PM UTC coverage: 51.46% (-6.3%) from 57.773%
20466639304

push

github

saitoha
build: fix windows find path in images meson build

14511 of 44933 branches covered (32.29%)

21089 of 40981 relevant lines covered (51.46%)

3915123.44 hits per line

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

57.83
/src/loader-common.c
1
/*
2
 * SPDX-License-Identifier: MIT
3
 *
4
 * Copyright (c) 2021-2025 libsixel developers. See `AUTHORS`.
5
 * Copyright (c) 2014-2019 Hayaki Saito
6
 *
7
 * Permission is hereby granted, free of charge, to any person obtaining a copy
8
 * of this software and associated documentation files (the "Software"), to
9
 * deal in the Software without restriction, including without limitation the
10
 * rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
11
 * sell copies of the Software, and to permit persons to whom the Software is
12
 * furnished to do so, subject to the following conditions:
13
 *
14
 * The above copyright notice and this permission notice shall be included in
15
 * all copies or substantial portions of the Software.
16
 *
17
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF, OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
23
 * DEALINGS IN THE SOFTWARE.
24
 *
25
 * Shared loader helpers used across backend implementations.  This module
26
 * centralizes trace logging, thumbnail size hints, and small detection
27
 * helpers so backend files stay narrow and platform headers remain isolated.
28
 */
29

30
#include "config.h"
31

32
#include <stdio.h>
33
#include <stdlib.h>
34

35
#if HAVE_STRING_H
36
# include <string.h>
37
#endif
38
#if HAVE_STDARG_H
39
# include <stdarg.h>
40
#endif
41
#if HAVE_LIMITS_H
42
# include <limits.h>
43
#endif
44
#if HAVE_ERRNO_H
45
# include <errno.h>
46
#endif
47

48
#include <sixel.h>
49

50
#include "compat_stub.h"
51
#include "loader-common.h"
52
#include "logger.h"
53

54
static int loader_trace_enabled;
55
static int thumbnailer_default_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
56
static int thumbnailer_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
57
static int thumbnailer_size_hint_initialized;
58

59
void
60
loader_thumbnailer_initialize_size_hint(void)
656✔
61
{
62
    char const *env_value;
656✔
63
    char *endptr;
656✔
64
    long parsed;
656✔
65

66
    if (thumbnailer_size_hint_initialized) {
656✔
67
        return;
656✔
68
    }
69

70
    thumbnailer_size_hint_initialized = 1;
628✔
71
    thumbnailer_default_size_hint = SIXEL_THUMBNAILER_DEFAULT_SIZE;
628✔
72
    thumbnailer_size_hint = thumbnailer_default_size_hint;
628✔
73

74
    env_value = sixel_compat_getenv("SIXEL_THUMBNAILER_HINT_SIZE");
628✔
75
    if (env_value == NULL || env_value[0] == '\0') {
628!
76
        return;
77
    }
78

79
    errno = 0;
×
80
    parsed = strtol(env_value, &endptr, 10);
×
81
    if (errno != 0) {
×
82
        return;
83
    }
84
    if (endptr == env_value || *endptr != '\0') {
×
85
        return;
86
    }
87
    if (parsed <= 0) {
×
88
        return;
89
    }
90
    if (parsed > (long)INT_MAX) {
×
91
        parsed = (long)INT_MAX;
92
    }
93

94
    thumbnailer_default_size_hint = (int)parsed;
×
95
    thumbnailer_size_hint = thumbnailer_default_size_hint;
×
96
}
1!
97

98
int
99
loader_thumbnailer_get_size_hint(void)
×
100
{
101
    loader_thumbnailer_initialize_size_hint();
×
102

103
    return thumbnailer_size_hint;
×
104
}
105

106
int
107
loader_thumbnailer_get_default_size_hint(void)
×
108
{
109
    loader_thumbnailer_initialize_size_hint();
×
110

111
    return thumbnailer_default_size_hint;
×
112
}
113

114
void
115
sixel_helper_set_loader_trace(int enable)
680✔
116
{
117
    loader_trace_enabled = enable ? 1 : 0;
680✔
118
}
680✔
119

120
void
121
sixel_helper_set_thumbnail_size_hint(int size)
656✔
122
{
123
    loader_thumbnailer_initialize_size_hint();
656✔
124

125
    if (size > 0) {
656✔
126
        thumbnailer_size_hint = size;
136✔
127
    } else {
128
        thumbnailer_size_hint = thumbnailer_default_size_hint;
520✔
129
    }
130
}
656✔
131

132
void
133
loader_trace_message(char const *format, ...)
138✔
134
{
135
    va_list args;
138✔
136

137
    if (!loader_trace_enabled) {
138✔
138
        return;
110✔
139
    }
140

141
    fprintf(stderr, "libsixel: ");
28✔
142

143
    va_start(args, format);
28✔
144
    sixel_compat_vfprintf(stderr, format, args);
28✔
145
    va_end(args);
28✔
146

147
    fprintf(stderr, "\n");
28✔
148
}
1!
149

150
void
151
loader_trace_try(char const *name)
806✔
152
{
153
    if (loader_trace_enabled) {
806✔
154
        fprintf(stderr, "libsixel: trying %s loader\n", name);
24✔
155
    }
156
}
806✔
157

158
void
159
loader_trace_result(char const *name, SIXELSTATUS status)
806✔
160
{
161
    if (!loader_trace_enabled) {
806✔
162
        return;
163
    }
164
    if (SIXEL_SUCCEEDED(status)) {
24!
165
        fprintf(stderr, "libsixel: loader %s succeeded\n", name);
24✔
166
    } else {
167
        fprintf(stderr, "libsixel: loader %s failed (%s)\n",
×
168
                name, sixel_helper_format_error(status));
169
    }
170
}
171

172
int
173
loader_trace_is_enabled(void)
772✔
174
{
175
    return loader_trace_enabled;
772✔
176
}
177

178
int
179
chunk_is_png(sixel_chunk_t const *chunk)
×
180
{
181
    if (chunk == NULL || chunk->size < 8) {
×
182
        return 0;
183
    }
184

185
    /*
186
     * PNG streams begin with an 8-byte signature.  Checking the fixed magic
187
     * sequence keeps the detection fast and avoids depending on libpng
188
     * helpers when only the signature is needed.
189
     */
190
    if (chunk->buffer[0] == (unsigned char)0x89 &&
×
191
        chunk->buffer[1] == 'P' &&
×
192
        chunk->buffer[2] == 'N' &&
×
193
        chunk->buffer[3] == 'G' &&
×
194
        chunk->buffer[4] == (unsigned char)0x0d &&
×
195
        chunk->buffer[5] == (unsigned char)0x0a &&
×
196
        chunk->buffer[6] == (unsigned char)0x1a &&
×
197
        chunk->buffer[7] == (unsigned char)0x0a) {
×
198
        return 1;
×
199
    }
200

201
    return 0;
202
}
203

204
int
205
chunk_is_jpeg(sixel_chunk_t const *chunk)
×
206
{
207
    if (chunk == NULL || chunk->size < 2) {
×
208
        return 0;
209
    }
210

211
    /*
212
     * JPEG files start with SOI (Start of Image) marker 0xFF 0xD8.  The GD
213
     * loader uses this to decide whether libgd should attempt JPEG decoding.
214
     */
215
    if (chunk->buffer[0] == (unsigned char)0xff &&
×
216
        chunk->buffer[1] == (unsigned char)0xd8) {
×
217
        return 1;
×
218
    }
219

220
    return 0;
221
}
222

223
int
224
chunk_is_bmp(sixel_chunk_t const *chunk)
×
225
{
226
    if (chunk == NULL || chunk->size < 2) {
×
227
        return 0;
228
    }
229

230
    /* BMP headers begin with the literal characters 'B' 'M'. */
231
    if (chunk->buffer[0] == 'B' && chunk->buffer[1] == 'M') {
×
232
        return 1;
×
233
    }
234

235
    return 0;
236
}
237

238
int
239
chunk_is_gif(sixel_chunk_t const *chunk)
542✔
240
{
241
    if (chunk->size < 6) {
542✔
242
        return 0;
243
    }
244
    if (chunk->buffer[0] == 'G' &&
534!
245
        chunk->buffer[1] == 'I' &&
32!
246
        chunk->buffer[2] == 'F' &&
32!
247
        chunk->buffer[3] == '8' &&
32!
248
        (chunk->buffer[4] == '7' || chunk->buffer[4] == '9') &&
32!
249
        chunk->buffer[5] == 'a') {
32!
250
        return 1;
32✔
251
    }
252
    return 0;
253
}
254

255
/* emacs Local Variables:      */
256
/* emacs mode: c               */
257
/* emacs tab-width: 4          */
258
/* emacs indent-tabs-mode: nil */
259
/* emacs c-basic-offset: 4     */
260
/* emacs End:                  */
261
/* vim: set expandtab ts=4 sts=4 sw=4 : */
262
/* 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