-
Notifications
You must be signed in to change notification settings - Fork 4
/
smolscale-private.h
339 lines (277 loc) · 10.8 KB
/
smolscale-private.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
/* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* Copyright © 2019-2023 Hans Petter Jansson. See COPYING for details. */
/* If you're just going to use Smolscale in your project, you don't have to
* worry about anything in here. The public API and documentation, such as
* it is, lives in smolscale.h.
*
* If, on the other hand, you're here to hack on Smolscale itself, this file
* contains all the internal shared declarations. */
#include <stdint.h>
#include "smolscale.h"
#ifndef _SMOLSCALE_PRIVATE_H_
#define _SMOLSCALE_PRIVATE_H_
#ifdef __cplusplus
extern "C" {
#endif
#ifdef SMOL_USE_ALLOCA
# define _SMOL_ALLOC(n) alloca (n)
# define _SMOL_FREE(p)
#else
# define _SMOL_ALLOC(n) malloc (n)
# define _SMOL_FREE(p) free (p)
#endif
/* Enum switches must handle every value */
#ifdef __GNUC__
# pragma GCC diagnostic error "-Wswitch"
#endif
/* Compensate for GCC missing intrinsics */
#ifdef __GNUC__
# if __GNUC__ < 8
# define _mm256_set_m128i(h, l) \
_mm256_insertf128_si256 (_mm256_castsi128_si256 (l), (h), 1)
# endif
#endif
#ifndef FALSE
# define FALSE (0)
#endif
#ifndef TRUE
# define TRUE (!FALSE)
#endif
#ifndef MIN
# define MIN(a, b) ((a) < (b) ? (a) : (b))
#endif
#ifndef MAX
# define MAX(a, b) ((a) > (b) ? (a) : (b))
#endif
typedef unsigned int SmolBool;
#define SMOL_4X2BIT(a, b, c, d) \
(((a) << 6) | ((b) << 4) | ((c) << 2) | (d))
#define SMOL_8X1BIT(a,b,c,d,e,f,g,h) \
(((a) << 7) | ((b) << 6) | ((c) << 5) | ((d) << 4) \
| ((e) << 3) | ((f) << 2) | ((g) << 1) | ((h) << 0))
#define SMOL_UNUSED(x) (void) ((x)=(x))
#define SMOL_RESTRICT __restrict
#define SMOL_INLINE __attribute__((always_inline)) inline
#define SMOL_CONST __attribute__((const))
#define SMOL_PURE __attribute__((pure))
#define SMOL_SMALL_MUL 256U
#define SMOL_BIG_MUL 65536U
#define SMOL_BOXES_MULTIPLIER ((uint64_t) SMOL_BIG_MUL * SMOL_SMALL_MUL)
#define SMOL_BILIN_MULTIPLIER ((uint64_t) SMOL_BIG_MUL * SMOL_BIG_MUL)
#define SMOL_ALIGNMENT 64
#define SMOL_ASSIGN_ALIGNED_TO(x, t, n) (t) __builtin_assume_aligned ((x), (n))
#define SMOL_ASSIGN_ALIGNED(x, t) SMOL_ASSIGN_ALIGNED_TO ((x), t, SMOL_ALIGNMENT)
#define SMOL_ASSUME_ALIGNED_TO(x, t, n) (x) = SMOL_ASSIGN_ALIGNED_TO ((x), t, (n))
#define SMOL_ASSUME_ALIGNED(x, t) SMOL_ASSUME_ALIGNED_TO ((x), t, SMOL_ALIGNMENT)
/* Pointer to beginning of storage is stored in *r. This must be passed to smol_free() later. */
#define smol_alloc_aligned_to(s, a, r) \
({ void *p; *(r) = _SMOL_ALLOC ((s) + (a)); p = (void *) (((uintptr_t) (*(r)) + (a)) & ~((a) - 1)); (p); })
#define smol_alloc_aligned(s, r) smol_alloc_aligned_to ((s), SMOL_ALIGNMENT, (r))
#define smol_free(p) _SMOL_FREE(p)
typedef enum
{
SMOL_STORAGE_24BPP,
SMOL_STORAGE_32BPP,
SMOL_STORAGE_64BPP,
SMOL_STORAGE_128BPP,
SMOL_STORAGE_MAX
}
SmolStorageType;
typedef enum
{
SMOL_FILTER_COPY,
SMOL_FILTER_ONE,
SMOL_FILTER_BILINEAR_0H,
SMOL_FILTER_BILINEAR_1H,
SMOL_FILTER_BILINEAR_2H,
SMOL_FILTER_BILINEAR_3H,
SMOL_FILTER_BILINEAR_4H,
SMOL_FILTER_BILINEAR_5H,
SMOL_FILTER_BILINEAR_6H,
SMOL_FILTER_BOX,
SMOL_FILTER_MAX
}
SmolFilterType;
typedef enum
{
SMOL_REORDER_1234_TO_1234,
SMOL_REORDER_1234_TO_2341,
SMOL_REORDER_1234_TO_3214,
SMOL_REORDER_1234_TO_4123,
SMOL_REORDER_1234_TO_4321,
SMOL_REORDER_1234_TO_123,
SMOL_REORDER_1234_TO_321,
SMOL_REORDER_123_TO_1234,
SMOL_REORDER_1234_TO_1324,
SMOL_REORDER_1234_TO_2314,
SMOL_REORDER_1234_TO_2431,
SMOL_REORDER_1234_TO_4132,
SMOL_REORDER_1234_TO_4231,
SMOL_REORDER_1234_TO_132,
SMOL_REORDER_1234_TO_231,
SMOL_REORDER_123_TO_1324,
SMOL_REORDER_1234_TO_324,
SMOL_REORDER_1234_TO_423,
SMOL_REORDER_1234_TO_1423,
SMOL_REORDER_1234_TO_3241,
SMOL_REORDER_MAX
}
SmolReorderType;
typedef enum
{
SMOL_ALPHA_UNASSOCIATED,
SMOL_ALPHA_PREMUL8,
SMOL_ALPHA_PREMUL16,
SMOL_ALPHA_MAX
}
SmolAlphaType;
typedef enum
{
SMOL_GAMMA_SRGB_COMPRESSED,
SMOL_GAMMA_SRGB_LINEAR,
SMOL_GAMMA_MAX
}
SmolGammaType;
typedef struct
{
unsigned char src [4];
unsigned char dest [4];
}
SmolReorderMeta;
typedef struct
{
unsigned char storage;
unsigned char alpha;
unsigned char order [4];
}
SmolPixelTypeMeta;
/* For reusing rows that have already undergone horizontal scaling */
typedef struct
{
uint32_t in_ofs;
uint64_t *parts_row [4];
uint64_t *row_storage [4];
uint32_t *in_aligned;
uint32_t *in_aligned_storage;
}
SmolVerticalCtx;
typedef void (SmolInitFunc) (SmolScaleCtx *scale_ctx);
typedef void (SmolUnpackRowFunc) (const uint32_t *row_in,
uint64_t *row_out,
uint32_t n_pixels);
typedef void (SmolPackRowFunc) (const uint64_t *row_in,
uint32_t *row_out,
uint32_t n_pixels);
typedef void (SmolRepackRowFunc) (const void *row_in,
void *row_out,
uint32_t n_pixels);
typedef void (SmolHFilterFunc) (const SmolScaleCtx *scale_ctx,
const uint64_t *row_limbs_in,
uint64_t *row_limbs_out);
typedef void (SmolVFilterFunc) (const SmolScaleCtx *scale_ctx,
SmolVerticalCtx *vertical_ctx,
uint32_t outrow_index,
uint32_t *row_out);
#define SMOL_REPACK_SIGNATURE_GET_REORDER(sig) ((sig) >> (2 * (SMOL_GAMMA_BITS + SMOL_ALPHA_BITS + SMOL_STORAGE_BITS)))
#define SMOL_REORDER_BITS 6
#define SMOL_STORAGE_BITS 2
#define SMOL_ALPHA_BITS 2
#define SMOL_GAMMA_BITS 1
#define SMOL_MAKE_REPACK_SIGNATURE_ANY_ORDER(storage_in, alpha_in, gamma_in, \
storage_out, alpha_out, gamma_out) \
(((storage_in) << (SMOL_GAMMA_BITS + SMOL_ALPHA_BITS + SMOL_STORAGE_BITS + SMOL_GAMMA_BITS + SMOL_ALPHA_BITS)) \
| ((alpha_in) << (SMOL_GAMMA_BITS + SMOL_ALPHA_BITS + SMOL_STORAGE_BITS + SMOL_GAMMA_BITS)) \
| ((gamma_in) << (SMOL_GAMMA_BITS + SMOL_ALPHA_BITS + SMOL_STORAGE_BITS)) \
| ((storage_out) << (SMOL_GAMMA_BITS + SMOL_ALPHA_BITS)) \
| ((alpha_out) << (SMOL_GAMMA_BITS)) \
| ((gamma_out) << 0)) \
#define MASK_ITEM(m, n_bits) ((m) ? (1 << (n_bits)) - 1 : 0)
#define SMOL_REPACK_SIGNATURE_ANY_ORDER_MASK(storage_in, alpha_in, gamma_in, \
storage_out, alpha_out, gamma_out) \
SMOL_MAKE_REPACK_SIGNATURE_ANY_ORDER(MASK_ITEM (storage_in, SMOL_STORAGE_BITS), \
MASK_ITEM (alpha_in, SMOL_ALPHA_BITS), \
MASK_ITEM (gamma_in, SMOL_GAMMA_BITS), \
MASK_ITEM (storage_out, SMOL_STORAGE_BITS), \
MASK_ITEM (alpha_out, SMOL_ALPHA_BITS), \
MASK_ITEM (gamma_out, SMOL_GAMMA_BITS))
#define SMOL_REPACK_META(order_in, storage_in, alpha_in, gamma_in, \
order_out, storage_out, alpha_out, gamma_out) \
{ (((SMOL_REORDER_##order_in##_TO_##order_out) << 10) \
| ((SMOL_STORAGE_##storage_in##BPP) << 8) | ((SMOL_ALPHA_##alpha_in) << 6) \
| ((SMOL_GAMMA_SRGB_##gamma_in) << 5) \
| ((SMOL_STORAGE_##storage_out##BPP) << 3) | ((SMOL_ALPHA_##alpha_out) << 1) \
| ((SMOL_GAMMA_SRGB_##gamma_out) << 0)), \
(SmolRepackRowFunc *) repack_row_##order_in##_##storage_in##_##alpha_in##_##gamma_in##_to_##order_out##_##storage_out##_##alpha_out##_##gamma_out }
#define SMOL_REPACK_META_LAST { 0xffff, NULL }
typedef struct
{
uint16_t signature;
SmolRepackRowFunc *repack_row_func;
}
SmolRepackMeta;
#define SMOL_REPACK_ROW_DEF(order_in, storage_in, limb_bits_in, alpha_in, gamma_in, \
order_out, storage_out, limb_bits_out, alpha_out, gamma_out) \
static void repack_row_##order_in##_##storage_in##_##alpha_in##_##gamma_in##_to_##order_out##_##storage_out##_##alpha_out##_##gamma_out \
(const uint##limb_bits_in##_t * SMOL_RESTRICT row_in, \
uint##limb_bits_out##_t * SMOL_RESTRICT row_out, \
uint32_t n_pixels) \
{ \
uint##limb_bits_out##_t *row_out_max = row_out + n_pixels * (storage_out / limb_bits_out); \
SMOL_ASSUME_ALIGNED_TO (row_in, uint##limb_bits_in##_t *, limb_bits_in / 8); \
SMOL_ASSUME_ALIGNED_TO (row_out, uint##limb_bits_out##_t *, limb_bits_out / 8);
#define SMOL_REPACK_ROW_DEF_END }
typedef struct
{
SmolInitFunc *init_h_func;
SmolInitFunc *init_v_func;
SmolHFilterFunc *hfilter_funcs [SMOL_STORAGE_MAX] [SMOL_FILTER_MAX];
SmolVFilterFunc *vfilter_funcs [SMOL_STORAGE_MAX] [SMOL_FILTER_MAX];
const SmolRepackMeta *repack_meta;
}
SmolImplementation;
struct SmolScaleCtx
{
/* <private> */
const char *pixels_in;
char *pixels_out;
uint32_t width_in, height_in, rowstride_in;
uint32_t width_out, height_out, rowstride_out;
SmolPixelType pixel_type_in, pixel_type_out;
SmolFilterType filter_h, filter_v;
SmolStorageType storage_type;
SmolGammaType gamma_type;
SmolRepackRowFunc *unpack_row_func;
SmolRepackRowFunc *pack_row_func;
SmolHFilterFunc *hfilter_func;
SmolVFilterFunc *vfilter_func;
/* User specified, can be NULL */
SmolPostRowFunc *post_row_func;
void *user_data;
/* Each offset is split in two uint16s: { pixel index, fraction }. These
* are relative to the image after halvings have taken place. */
uint16_t *precalc_x, *precalc_y;
uint32_t span_mul_x, span_mul_y; /* For box filter */
void *precalc_x_storage;
uint32_t width_bilin_out, height_bilin_out;
unsigned int width_halvings, height_halvings;
};
#define SRGB_LINEAR_BITS 11
#define SRGB_LINEAR_MAX (1 << (SRGB_LINEAR_BITS))
extern const uint16_t _smol_from_srgb_lut [256];
extern const uint8_t _smol_to_srgb_lut [SRGB_LINEAR_MAX];
#define INVERTED_DIV_SHIFT_P8 (21 - 8)
#define INVERTED_DIV_SHIFT_P8L (21 - SRGB_LINEAR_BITS)
#define INVERTED_DIV_SHIFT_P16 (24 - 8)
#define INVERTED_DIV_SHIFT_P16L (30 - SRGB_LINEAR_BITS)
extern const uint32_t _smol_inv_div_p8_lut [256];
extern const uint32_t _smol_inv_div_p8l_lut [256];
extern const uint32_t _smol_inv_div_p16_lut [256];
extern const uint32_t _smol_inv_div_p16l_lut [256];
const SmolImplementation *_smol_get_generic_implementation (void);
#ifdef SMOL_WITH_AVX2
const SmolImplementation *_smol_get_avx2_implementation (void);
#endif
#ifdef __cplusplus
}
#endif
#endif