-
Notifications
You must be signed in to change notification settings - Fork 3
/
j40.h
8505 lines (7470 loc) · 311 KB
/
j40.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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// J40: Independent, self-contained JPEG XL decoder
// Kang Seonghoon, version 2270 (2022-09), Public Domain
// https://github.com/lifthrasiir/j40
//
// This is a decoder for JPEG XL (ISO/IEC 18181) image format. It intends to be a fully compatible
// reimplementation to the reference implementation, libjxl, and also serves as a verification that
// the specification allows for an independent implementation besides from libjxl.
//
// The following is a simple but complete converter from JPEG XL to Portable Arbitrary Map format:
//
/* -------------------------------------------------------------------------------- //
#define J40_IMPLEMENTATION // only a SINGLE file should have this
#include "j40.h" // you also need to define a macro for experimental versions; follow the error.
#include <stdio.h>
#include <stdarg.h> // for va_*
static int oops(const char *fmt, ...) {
va_list args;
va_start(args, fmt);
vfprintf(stderr, fmt, args);
va_end(args);
return 1;
}
int main(int argc, char **argv) {
if (argc < 3) return oops("Usage: %s input.jxl output.pam\n", argv[0]);
FILE *out = fopen(argv[2], "wb");
if (!out) return oops("Error: Cannot open an output file.\n");
j40_image image;
j40_from_file(&image, argv[1]); // or: j40_from_memory(&image, buf, bufsize, freefunc);
j40_output_format(&image, J40_RGBA, J40_U8X4);
// JPEG XL supports animation, so `j40_next_frame` calls can be called multiple times
if (j40_next_frame(&image)) {
j40_frame frame = j40_current_frame(&image);
j40_pixels_u8x4 pixels = j40_frame_pixels_u8x4(&frame, J40_RGBA);
fprintf(out,
"P7\n"
"WIDTH %d\n"
"HEIGHT %d\n"
"DEPTH 4\n"
"MAXVAL 255\n"
"TUPLTYPE RGB_ALPHA\n"
"ENDHDR\n",
pixels.width, pixels.height);
for (int y = 0; y < height; ++y) {
fwrite(j40_row_u8x4(pixels, y), 4, pixels.width, out);
}
}
// J40 stops once the first error is encountered; its error can be checked at the very end
if (j40_error(&image)) return oops("Error: %s\n", j40_error_string(&image));
if (ferror(out)) return oops("Error: Cannot fully write to the output file.\n");
j40_free(&image); // also frees all memory associated to j40_frame etc.
fclose(out);
return 0;
}
// -------------------------------------------------------------------------------- */
////////////////////////////////////////////////////////////////////////////////
// preamble (only reachable via the user `#include`)
// controls whether each `#if`-`#endif` section in this file should be included or not.
// there are multiple purposes of this macro:
// - `J40__RECURSING` is always defined after the first ever `#include`, so that:
// - the preamble will precede every other code in the typical usage, and
// - the preamble won't be included twice.
// - `J40__RECURSING` is either 0 (public) or -1 (internal) depending on the logical visibility,
// so that the preamble can choose whether to include the internal code or not.
// - larger values (>= 100) are used to repeat a specific section of code with
// slightly different parameters, i.e. templated code.
// - one value (currently 9999) is reserved and used to ignore subsequent top-level `#include`s.
#ifndef J40__RECURSING
#define J40_VERSION 2270 // (fractional gregorian year - 2000) * 100, with a liberal rounding
#ifndef J40_CONFIRM_THAT_THIS_IS_EXPERIMENTAL_AND_POTENTIALLY_UNSAFE
#error "Please #define J40_CONFIRM_THAT_THIS_IS_EXPERIMENTAL_AND_POTENTIALLY_UNSAFE to use J40. Proceed at your own risk."
#endif
//#define J40_DEBUG
#ifndef J40_FILENAME // should be provided if this file has a different name than `j40.h`
#define J40_FILENAME "j40.h"
#endif
#include <stddef.h>
#include <stdint.h>
#include <stdlib.h>
#ifdef J40_IMPLEMENTATION
#define J40__IMPLEMENTATION_INCLUDED
#include <string.h>
#include <math.h>
#include <limits.h>
#include <errno.h>
#include <stdio.h>
#ifdef J40_DEBUG
#include <assert.h>
#endif
#ifndef J40__EXPOSE_INTERNALS
#define J40__EXPOSE_INTERNALS
#endif
#endif
#ifdef J40__EXPOSE_INTERNALS
#define J40__RECURSING (-1)
#else
#define J40__RECURSING 0
#endif
// we don't care about secure CRT, which is only marginally safe and not even compatible with C11
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable: 4996)
#endif
#ifdef __cplusplus
extern "C" {
#endif
#endif // !defined J40__RECURSING
#if J40__RECURSING == 9999 // enabled only when the header file is included the second time or more
#if !defined J40__IMPLEMENTATION_INCLUDED && defined J40_IMPLEMENTATION
#error "J40 is included with J40_IMPLEMENTATION defined, but it was already included without it so it would have been ignored!"
#endif
#endif
////////////////////////////////////////////////////////////////////////////////
// public platform macros
#if J40__RECURSING <= 0
// just in case:
#if CHAR_BIT != 8 // in fact, pretty much every file processing wouldn't make sense if CHAR_BIT > 8
#error "J40 requires CHAR_BIT == 8"
#endif
#ifndef J40_STATIC_ASSERT
#if __STDC_VERSION__ >= 199901L
#define J40_STATIC_ASSERT(cond, msg) _Static_assert(cond, #msg)
#else
#define J40_STATIC_ASSERT(cond, msg) typedef char j40__##msg[(cond) ? 1 : -1]
#endif
#endif // !defined J40_STATIC_ASSERT
// just in case again, because it is still possible for them to have padding bits (that we needn't):
J40_STATIC_ASSERT(sizeof(uint8_t) == 1, uint8_t_should_have_no_padding_bits);
J40_STATIC_ASSERT(sizeof(uint16_t) == 2, uint16_t_should_have_no_padding_bits);
J40_STATIC_ASSERT(sizeof(uint32_t) == 4, uint32_t_should_have_no_padding_bits);
J40_STATIC_ASSERT(sizeof(uint64_t) == 8, uint64_t_should_have_no_padding_bits);
#ifndef J40_API
#define J40_API // TODO
#endif
#endif // J40__RECURSING <= 0
////////////////////////////////////////////////////////////////////////////////
// public API
#if J40__RECURSING <= 0
// an internal error type. non-zero indicates a different error condition.
// user callbacks can also emit error codes, which should not exceed `J40_MIN_RESERVED_ERR`.
// it can be interpreted as a four-letter code, but such encoding is not guaranteed.
typedef uint32_t j40_err;
#define J40_MIN_RESERVED_ERR (j40_err) (1 << 24) // anything below this can be used freely
typedef struct {
// either J40__IMAGE_MAGIC, (J40__IMAGE_ERR_MAGIC ^ origin) or (J40__IMAGE_OPEN_ERR_MAGIC ^ origin)
uint32_t magic;
union {
struct j40__inner *inner; // if magic == J40__IMAGE_MAGIC
j40_err err; // if magic == J40__IMAGE_ERR_MAGIC
int saved_errno; // if magic == J40__IMAGE_OPEN_ERR_MAGIC (err is assumed to be `open`)
} u;
} j40_image;
typedef struct {
uint32_t magic; // should be J40__FRAME_MAGIC or J40__FRAME_ERR_MAGIC
uint32_t reserved;
struct j40__inner *inner;
} j40_frame;
typedef void (*j40_memory_free_func)(void *data);
// pixel formats
//rsvd: J40_U8 0x0f0f
//rsvd: J40_U16 0x0f17
//rsvd: J40_U32 0x0f1b
//rsvd: J40_U64 0x0f1d
//rsvd: J40_F32 0x0f1e
//rsvd: J40_U8X3 0x0f27
//rsvd: J40_U16X3 0x0f2b
//rsvd: J40_U32X3 0x0f2d
//rsvd: J40_F32X3 0x0f2e
#define J40_U8X4 0x0f33
//rsvd: J40_U16X4 0x0f35
//rsvd: J40_U32X4 0x0f36
//rsvd: J40_F32X4 0x0f39
// color types
//rsvd: J40_RED 0x170f
//rsvd: J40_GREEN 0x1717
//rsvd: J40_BLUE 0x171b
//rsvd: J40_LUMI 0x171d
//rsvd: J40_ALPHA 0x171e
//rsvd: J40_CYAN 0x1727
//rsvd: J40_YELLOW 0x172b
//rsvd: J40_MAGENTA 0x172d
//rsvd: J40_BLACK 0x172e
//rsvd: J40_JPEG_Y 0x1733
//rsvd: J40_JPEG_CB 0x1735
//rsvd: J40_JPEG_CR 0x1736
//rsvd: J40_OPSIN_X 0x1739
//rsvd: J40_OPSIN_Y 0x173a
//rsvd: J40_OPSIN_B 0x173c
//rsvd: J40_RED_BEFORE_CT 0x1747
//rsvd: J40_GREEN_BEFORE_CT 0x174b
//rsvd: J40_BLUE_BEFORE_CT 0x174d
//rsvd: J40_RGB 0x174e
//rsvd: J40_BGR 0x1753
#define J40_RGBA 0x1755
//rsvd: J40_ARGB 0x1756
//rsvd: J40_BGRA 0x1759
//rsvd: J40_ABGR 0x175a
J40_API j40_err j40_error(const j40_image *image);
J40_API const char *j40_error_string(const j40_image *image);
J40_API j40_err j40_from_memory(j40_image *image, void *buf, size_t size, j40_memory_free_func freefunc);
J40_API j40_err j40_from_file(j40_image *image, const char *path);
J40_API j40_err j40_output_format(j40_image *image, int32_t channel, int32_t format);
J40_API int j40_next_frame(j40_image *image);
J40_API j40_frame j40_current_frame(j40_image *image);
#define J40__DEFINE_PIXELS(type, suffix) \
typedef struct { \
int32_t width, height; \
int32_t stride_bytes; \
const void *data; \
} j40_pixels_##suffix; \
J40_API j40_pixels_##suffix j40_frame_pixels_##suffix(const j40_frame *frame, int32_t channel); \
J40_API const type *j40_row_##suffix(j40_pixels_##suffix pixels, int32_t y)
typedef uint8_t /*j40_u8x3[3],*/ j40_u8x4[4];
//typedef uint16_t j40_u16x3[3], j40_u16x4[4];
//typedef uint32_t j40_u32x3[3], j40_u32x4[4];
typedef float /*j40_f32x3[3],*/ j40_f32x4[4]; // TODO temporary, API will be available later
//J40__DEFINE_PIXELS(uint8_t, u8); // j40_pixels_u8, j40_frame_pixels_u8, j40_row_u8
//J40__DEFINE_PIXELS(uint16_t, u16); // j40_pixels_u16, j40_frame_pixels_u16, j40_row_u16
//J40__DEFINE_PIXELS(uint32_t, u32); // j40_pixels_u32, j40_frame_pixels_u32, j40_row_u32
//J40__DEFINE_PIXELS(uint64_t, u64); // j40_pixels_u64, j40_frame_pixels_u64, j40_row_u64
//J40__DEFINE_PIXELS(float, f32); // j40_pixels_f32, j40_frame_pixels_f32, j40_row_f32
//J40__DEFINE_PIXELS(j40_u8x3, u8x3); // j40_pixels_u8x3, j40_frame_pixels_u8x3, j40_row_u8x3
//J40__DEFINE_PIXELS(j40_u16x3, u16x3); // j40_pixels_u16x3, j40_frame_pixels_u16x3, j40_row_u16x3
//J40__DEFINE_PIXELS(j40_u32x3, u32x3); // j40_pixels_u32x3, j40_frame_pixels_u32x3, j40_row_u32x3
//J40__DEFINE_PIXELS(j40_f32x3, f32x3); // j40_pixels_f32x3, j40_frame_pixels_f32x3, j40_row_f32x3
J40__DEFINE_PIXELS(j40_u8x4, u8x4); // j40_pixels_u8x4, j40_frame_pixels_u8x4, j40_row_u8x4
//J40__DEFINE_PIXELS(j40_u16x4, u16x4); // j40_pixels_u16x4, j40_frame_pixels_u16x4, j40_row_u16x4
//J40__DEFINE_PIXELS(j40_u32x4, u32x4); // j40_pixels_u32x4, j40_frame_pixels_u32x4, j40_row_u32x4
//J40__DEFINE_PIXELS(j40_f32x4, f32x4); // j40_pixels_f32x4, j40_frame_pixels_f32x4, j40_row_f32x4
J40_API void j40_free(j40_image *image);
#endif // J40__RECURSING <= 0
////////////////////////////////////////////////////////////////////////////////
//////////////////////// internal code starts from here ////////////////////////
////////////////////////////////////////////////////////////////////////////////
#if J40__RECURSING < 0
// comment convention:
// "SPEC" comments are used for incorrect, ambiguous or misleading specification issues.
// "TODO spec" comments are roughly same, but not yet fully confirmed & reported.
////////////////////////////////////////////////////////////////////////////////
// private platform macros
#ifdef __has_attribute // since GCC 5.0.0 and clang 2.9.0
#if __has_attribute(always_inline)
#define J40__HAS_ALWAYS_INLINE_ATTR 1
#endif
#if __has_attribute(warn_unused_result)
#define J40__HAS_WARN_UNUSED_RESULT_ATTR 1
#endif
#endif
#ifdef __has_builtin // since GCC 10.0.0 and clang 1.0.0 (which thus requires no version check)
#if __has_builtin(__builtin_expect)
#define J40__HAS_BUILTIN_EXPECT 1
#endif
#if __has_builtin(__builtin_add_overflow)
#define J40__HAS_BUILTIN_ADD_OVERFLOW 1
#endif
#if __has_builtin(__builtin_sub_overflow)
#define J40__HAS_BUILTIN_SUB_OVERFLOW 1
#endif
#if __has_builtin(__builtin_mul_overflow)
#define J40__HAS_BUILTIN_MUL_OVERFLOW 1
#endif
#if __has_builtin(__builtin_unreachable)
#define J40__HAS_BUILTIN_UNREACHABLE 1
#endif
#if __has_builtin(__builtin_assume_aligned)
#define J40__HAS_BUILTIN_ASSUME_ALIGNED 1
#endif
#endif
// clang (among many others) fakes GCC version by default, but we handle clang separately
#if defined __GNUC__ && !defined __clang__
#define J40__GCC_VER (__GNUC__ * 0x10000 + __GNUC_MINOR__ * 0x100 + __GNUC_PATCHLEVEL__)
#else
#define J40__GCC_VER 0
#endif
#ifdef __clang__
#define J40__CLANG_VER (__clang_major__ * 0x10000 + __clang_minor__ * 0x100 + __clang_patchlevel__)
#else
#define J40__CLANG_VER 0
#endif
#ifndef J40_STATIC
#define J40_STATIC static
#endif
#ifndef J40_INLINE
#define J40_INLINE J40_STATIC inline
#endif
#ifndef J40_ALWAYS_INLINE
#if J40__HAS_ALWAYS_INLINE_ATTR || J40__GCC_VER >= 0x30100 || J40__CLANG_VER >= 0x10000
#define J40_ALWAYS_INLINE __attribute__((always_inline)) J40_INLINE
#elif defined _MSC_VER
#define J40_ALWAYS_INLINE __forceinline
#else
#define J40_ALWAYS_INLINE J40_INLINE
#endif
#endif // !defined J40_ALWAYS_INLINE
#ifndef J40_RESTRICT
#if __STDC_VERSION__ >= 199901L
#define J40_RESTRICT restrict
#elif defined __GNUC__ || __MSC_VER >= 1900 // since pretty much every GCC/Clang and VS 2015
#define J40_RESTRICT __restrict
#else
#define J40_RESTRICT
#endif
#endif // !defined J40_RESTRICT
// most structs in J40 are designed to be zero-initialized, and this avoids useless warnings
#if defined __cplusplus /*|| __STDC_VERSION__ >= 2023xxL*/
#define J40__INIT {}
#else
#define J40__INIT {0}
#endif
#ifndef J40_NODISCARD
#if __cplusplus >= 201703L /*|| __STDC_VERSION__ >= 2023xxL */
#define J40_NODISCARD [[nodiscard]] // since C++17 and C23
#elif J40__HAS_WARN_UNUSED_RESULT_ATTR || J40__GCC_VER >= 0x30400 || J40__CLANG_VER >= 0x10000
// this is stronger than [[nodiscard]] in that it's much harder to suppress; we're okay with that
#define J40_NODISCARD __attribute__((warn_unused_result)) // since GCC 3.4 and clang 1.0.0
#else
#define J40_NODISCARD
#endif
#endif // !defined J40_NODISCARD
#ifndef J40_MAYBE_UNUSED
#if __cplusplus >= 201703L /*|| __STDC_VERSION__ >= 2023xxL */
#define J40_MAYBE_UNUSED [[maybe_unused]] // since C++17 and C23
#elif J40__GCC_VER >= 0x30000 || J40__CLANG_VER >= 0x10000
#define J40_MAYBE_UNUSED __attribute__((unused)) // since GCC 2.95 or earlier (!) and clang 1.0.0
#else
#define J40_MAYBE_UNUSED
#endif
#endif
// rule of thumb: sparingly use them, except for the obvious error cases
#ifndef J40_EXPECT
#if J40__HAS_BUILTIN_EXPECT || J40__GCC_VER >= 0x30000
#define J40_EXPECT(p, v) __builtin_expect(p, v)
#else
#define J40_EXPECT(p, v) (p)
#endif
#endif // !defined J40_EXPECT
#ifndef J40_LIKELY
#define J40_LIKELY(p) J40_EXPECT(!!(p), 1)
#endif
#ifndef J40_UNLIKELY
#define J40_UNLIKELY(p) J40_EXPECT(!!(p), 0)
#endif
#if !defined J40_ADD_OVERFLOW && (J40__HAS_BUILTIN_ADD_OVERFLOW || J40__GCC_VER >= 0x50000)
#define J40_ADD_OVERFLOW(a, b, res) __builtin_add_overflow(a, b, res)
#endif
#if !defined J40_SUB_OVERFLOW && (J40__HAS_BUILTIN_SUB_OVERFLOW || J40__GCC_VER >= 0x50000)
#define J40_SUB_OVERFLOW(a, b, res) __builtin_sub_overflow(a, b, res)
#endif
#if !defined J40_MUL_OVERFLOW && (J40__HAS_BUILTIN_MUL_OVERFLOW || J40__GCC_VER >= 0x50000)
#define J40_MUL_OVERFLOW(a, b, res) __builtin_mul_overflow(a, b, res)
#endif
#if !defined J40_MALLOC && !defined J40_CALLOC && !defined J40_REALLOC && !defined J40_FREE
#define J40_MALLOC malloc
#define J40_CALLOC calloc
#define J40_REALLOC realloc
#define J40_FREE free
#elif !(defined J40_MALLOC && defined J40_CALLOC && defined J40_REALLOC && defined J40_FREE)
#error "J40_MALLOC, J40_CALLOC, J40_REALLOC and J40_FREE should be provided altogether."
#endif
////////////////////////////////////////////////////////////////////////////////
// state
// bit and logical buffer. this is most frequently accessed and thus available without indirection.
//
// the bit buffer (`nbits` least significant bits of `bits`) is the least significant bits available
// for decoding, and the logical buffer [ptr, end) corresponds to subsequent bits.
// the logical buffer is guaranteed to be all in the codestream (which is not always true if
// the file uses a container).
//
// when the bit buffer has been exhausted the next byte from the logical buffer is consumed and
// appended at the *top* of the bit buffer. when the logical buffer has been exhausted
// higher layers (first backing buffer, then container, and finally source) should be consulted.
typedef struct j40__bits_st {
int32_t nbits; // [0, 64]
uint64_t bits;
uint8_t *ptr, *end;
} j40__bits_st;
// a common context ("state") for all internal functions.
// this bears a strong similarity with `struct j40__inner` type in the API layer which would be
// introduced much later. there are multiple reasons for this split:
// - `j40__st` is designed to be in the stack, so it doesn't take up much stack space.
// - `j40__st` allows for partial initialization of subsystems, which makes testing much easier.
// - `j40__st` only holds things relevant to decoding, while `j40__inner` has API contexts.
// - there can be multiple `j40__st` for multi-threaded decoding.
typedef struct {
j40_err err; // first error code encountered, or 0
int saved_errno;
int cannot_retry; // a fatal error was encountered and no more additional input will fix it
// different subsystems make use of additional contexts, all accessible from here.
struct j40__bits_st bits; // very frequently accessed, thus inlined here
struct j40__source_st *source;
struct j40__container_st *container;
struct j40__buffer_st *buffer;
struct j40__image_st *image;
struct j40__frame_st *frame;
struct j40__lf_group_st *lf_group;
const struct j40__limits *limits;
} j40__st;
////////////////////////////////////////////////////////////////////////////////
// error handling and memory allocation
#ifdef J40_DEBUG
#define J40__ASSERT(cond) assert(cond)
#define J40__UNREACHABLE() J40__ASSERT(0)
#elif J40__HAS_BUILTIN_UNREACHABLE || J40__GCC_VER >= 0x40500
#define J40__ASSERT(cond) (J40_UNLIKELY(!(cond)) ? __builtin_unreachable() : (void) 0)
#define J40__UNREACHABLE() __builtin_unreachable()
#else
#define J40__ASSERT(cond) ((void) (cond))
#define J40__UNREACHABLE() ((void) 0) // TODO also check for MSVC __assume
#endif
// J40_NODISCARD should be before `static` or `inline`
#define J40__STATIC_RETURNS_ERR J40_NODISCARD J40_STATIC j40_err
#define J40__INLINE_RETURNS_ERR J40_NODISCARD J40_INLINE j40_err
#define J40__4(s) \
(j40_err) (((uint32_t) (s)[0] << 24) | ((uint32_t) (s)[1] << 16) | ((uint32_t) (s)[2] << 8) | (uint32_t) (s)[3])
#define J40__ERR(s) j40__set_error(st, J40__4(s))
#define J40__SHOULD(cond, s) do { \
if (J40_UNLIKELY(st->err)) goto J40__ON_ERROR; \
if (J40_UNLIKELY((cond) == 0)) { j40__set_error(st, J40__4(s)); goto J40__ON_ERROR; } \
} while (0)
#define J40__RAISE(s) do { j40__set_error(st, J40__4(s)); goto J40__ON_ERROR; } while (0)
#define J40__RAISE_DELAYED() do { if (J40_UNLIKELY(st->err)) goto J40__ON_ERROR; } while (0)
#define J40__TRY(expr) do { if (J40_UNLIKELY(expr)) { J40__ASSERT(st->err); goto J40__ON_ERROR; } } while (0)
// this *should* use casting because C/C++ don't allow comparison between pointers
// that came from different arrays at all: https://stackoverflow.com/a/39161283
#define J40__INBOUNDS(ptr, start, size) ((uintptr_t) (ptr) - (uintptr_t) (start) <= (uintptr_t) (size))
#define J40__TRY_MALLOC(type, ptr, num) \
do { \
type *newptr = (type*) j40__malloc(num, sizeof(type)); \
J40__SHOULD(*(ptr) = newptr, "!mem"); \
} while (0)
#define J40__TRY_CALLOC(type, ptr, num) \
do { \
type *newptr = (type*) j40__calloc(num, sizeof(type)); \
J40__SHOULD(*(ptr) = newptr, "!mem"); \
} while (0)
#define J40__TRY_REALLOC32(type, ptr, len, cap) \
do { \
type *newptr = (type*) j40__realloc32(st, *(ptr), sizeof(type), len, cap); \
if (J40_LIKELY(newptr)) *(ptr) = newptr; else goto J40__ON_ERROR; \
} while (0)
#define J40__TRY_REALLOC64(type, ptr, len, cap) \
do { \
type *newptr = (type*) j40__realloc64(st, *(ptr), sizeof(type), len, cap); \
if (J40_LIKELY(newptr)) *(ptr) = newptr; else goto J40__ON_ERROR; \
} while (0)
J40_STATIC j40_err j40__set_error(j40__st *st, j40_err err);
J40_STATIC void *j40__malloc(size_t num, size_t size);
J40_STATIC void *j40__calloc(size_t num, size_t size);
J40_STATIC void *j40__realloc32(j40__st *st, void *ptr, size_t itemsize, int32_t len, int32_t *cap);
J40_STATIC void *j40__realloc64(j40__st *st, void *ptr, size_t itemsize, int64_t len, int64_t *cap);
J40_STATIC void j40__free(void *ptr);
#ifdef J40_IMPLEMENTATION
J40_STATIC j40_err j40__set_error(j40__st *st, j40_err err) {
if (err != J40__4("shrt")) st->cannot_retry = 1;
if (!st->err) st->err = err;
return err;
}
J40_STATIC void *j40__malloc(size_t num, size_t size) {
if (size == 0 || num > SIZE_MAX / size) return NULL;
return J40_MALLOC(num * size);
}
J40_STATIC void *j40__calloc(size_t num, size_t size) {
return J40_CALLOC(num, size);
}
J40_STATIC void *j40__realloc32(j40__st *st, void *ptr, size_t itemsize, int32_t len, int32_t *cap) {
void *newptr;
uint32_t newcap;
size_t newsize;
J40__ASSERT(len >= 0);
if (len <= *cap) return ptr;
newcap = (uint32_t) *cap * 2;
if (newcap > (uint32_t) INT32_MAX) newcap = (uint32_t) INT32_MAX;
if (newcap < (uint32_t) len) newcap = (uint32_t) len;
J40__SHOULD(newcap <= SIZE_MAX / itemsize, "!mem");
newsize = (size_t) (itemsize * newcap);
J40__SHOULD(newptr = ptr ? J40_REALLOC(ptr, newsize) : J40_MALLOC(newsize), "!mem");
*cap = (int32_t) newcap;
return newptr;
J40__ON_ERROR:
return NULL;
}
J40_STATIC void *j40__realloc64(j40__st *st, void *ptr, size_t itemsize, int64_t len, int64_t *cap) {
void *newptr;
uint64_t newcap;
size_t newsize;
J40__ASSERT(len >= 0);
if (len <= *cap) return ptr;
newcap = (uint64_t) *cap * 2;
if (newcap > (uint64_t) INT64_MAX) newcap = (uint64_t) INT64_MAX;
if (newcap < (uint64_t) len) newcap = (uint64_t) len;
J40__SHOULD(newcap <= SIZE_MAX / itemsize, "!mem");
newsize = (size_t) (itemsize * newcap);
J40__SHOULD(newptr = ptr ? J40_REALLOC(ptr, newsize) : J40_MALLOC(newsize), "!mem");
*cap = (int64_t) newcap;
return newptr;
J40__ON_ERROR:
return NULL;
}
J40_STATIC void j40__free(void *ptr) {
J40_FREE(ptr);
}
#endif // defined J40_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
// utility
#define J40__CONCAT_(a,b) a##b
#define J40__CONCAT(a,b) J40__CONCAT_(a,b)
#define J40__CONCAT3(a,b,c) J40__CONCAT(a,J40__CONCAT(b,c))
// `j40__(foo, X)` and its uppercase version is `j40__foo` followed by a macro `J40__V` expanded;
// this greatly simplifies the construction of templated names.
#define J40__PARAMETRIC_NAME_(prefix, x, J40__V) J40__CONCAT3(prefix, x, J40__V)
#define j40__(x, V) J40__PARAMETRIC_NAME_(j40__, x, J40__CONCAT(J40__, V))
#define J40__(x, V) J40__PARAMETRIC_NAME_(J40__, x, J40__CONCAT(J40__, V))
J40_ALWAYS_INLINE int32_t j40__unpack_signed(int32_t x);
J40_ALWAYS_INLINE int64_t j40__unpack_signed64(int64_t x);
J40_ALWAYS_INLINE int32_t j40__ceil_div32(int32_t x, int32_t y);
J40_ALWAYS_INLINE int64_t j40__ceil_div64(int64_t x, int64_t y);
J40_ALWAYS_INLINE float j40__minf(float x, float y);
J40_ALWAYS_INLINE float j40__maxf(float x, float y);
J40_ALWAYS_INLINE int j40__surely_nonzero(float x);
#ifdef J40_IMPLEMENTATION
J40_ALWAYS_INLINE int32_t j40__unpack_signed(int32_t x) {
return (int32_t) (x & 1 ? -(x / 2 + 1) : x / 2);
}
J40_ALWAYS_INLINE int64_t j40__unpack_signed64(int64_t x) {
return (int64_t) (x & 1 ? -(x / 2 + 1) : x / 2);
}
// equivalent to ceil(x / y)
J40_ALWAYS_INLINE int32_t j40__ceil_div32(int32_t x, int32_t y) { return (x + y - 1) / y; }
J40_ALWAYS_INLINE int64_t j40__ceil_div64(int64_t x, int64_t y) { return (x + y - 1) / y; }
J40_ALWAYS_INLINE float j40__minf(float x, float y) { return (x < y ? x : y); }
J40_ALWAYS_INLINE float j40__maxf(float x, float y) { return (x > y ? x : y); }
// used to guard against division by zero
J40_ALWAYS_INLINE int j40__surely_nonzero(float x) {
return isfinite(x) && fabs(x) >= 1e-8f;
}
#ifdef _MSC_VER // required for j40__floor/ceil_lgN implementations
#include <intrin.h>
#pragma intrinsic(_BitScanReverse)
J40_ALWAYS_INLINE int j40__clz32(uint32_t x) {
unsigned long index;
return _BitScanReverse(&index, x) ? 31 - (int) index : 32;
}
J40_ALWAYS_INLINE int j40__clz16(uint16_t x) { return j40__clz32(x); }
// _BitScanReverse64 is not available at all in x86-32, so we need to detour
#if defined __ia64__ || defined __x86_64
#pragma intrinsic(_BitScanReverse64)
J40_ALWAYS_INLINE int j40__clz64(uint64_t x) {
unsigned long index;
return _BitScanReverse64(&index, x) ? 63 - (int) index : 64;
}
#else
J40_ALWAYS_INLINE int j40__clz64(uint64_t x) {
return x >> 32 ? j40__clz32((uint32_t) (x >> 32)) : 32 + j40__clz32((uint32_t) x);
}
#endif // defined __ia64__ || defined __x86_64
#endif // defined _MSC_VER
#endif // defined J40_IMPLEMENTATION
// ----------------------------------------
// recursion for bit-dependent math functions
#undef J40__RECURSING
#define J40__RECURSING 100
#define J40__N 16
#include J40_FILENAME
#define J40__N 32
#include J40_FILENAME
#define J40__N 64
#include J40_FILENAME
#undef J40__RECURSING
#define J40__RECURSING (-1)
#endif // J40__RECURSING < 0
#if J40__RECURSING == 100
#define j40__intN J40__CONCAT3(int, J40__N, _t)
#define j40__uintN J40__CONCAT3(uint, J40__N, _t)
#define J40__INTN_MAX J40__CONCAT3(INT, J40__N, _MAX)
#define J40__INTN_MIN J40__CONCAT3(INT, J40__N, _MIN)
// ----------------------------------------
J40_ALWAYS_INLINE j40__intN j40__(floor_avg,N)(j40__intN x, j40__intN y);
J40_ALWAYS_INLINE j40__intN j40__(abs,N)(j40__intN x);
J40_ALWAYS_INLINE j40__intN j40__(min,N)(j40__intN x, j40__intN y);
J40_ALWAYS_INLINE j40__intN j40__(max,N)(j40__intN x, j40__intN y);
// returns 1 if overflow or underflow didn't occur
J40_ALWAYS_INLINE int j40__(add,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE int j40__(sub,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE int j40__(mul,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE int j40__(add_fallback,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE int j40__(sub_fallback,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE int j40__(mul_fallback,N)(j40__intN x, j40__intN y, j40__intN *out);
J40_ALWAYS_INLINE j40__intN j40__(clamp_add,N)(j40__intN x, j40__intN y);
J40_ALWAYS_INLINE j40__intN j40__(clamp_mul,N)(j40__intN x, j40__intN y);
#ifdef J40_IMPLEMENTATION
// same to `(a + b) >> 1` but doesn't overflow, useful for tight loops with autovectorization
// https://devblogs.microsoft.com/oldnewthing/20220207-00/?p=106223
J40_ALWAYS_INLINE j40__intN j40__(floor_avg,N)(j40__intN x, j40__intN y) {
return (j40__intN) (x / 2 + y / 2 + (x & y & 1));
}
J40_ALWAYS_INLINE j40__intN j40__(abs,N)(j40__intN x) {
return (j40__intN) (x < 0 ? -x : x);
}
J40_ALWAYS_INLINE j40__intN j40__(min,N)(j40__intN x, j40__intN y) {
return (j40__intN) (x < y ? x : y);
}
J40_ALWAYS_INLINE j40__intN j40__(max,N)(j40__intN x, j40__intN y) {
return (j40__intN) (x > y ? x : y);
}
J40_ALWAYS_INLINE int j40__(add,N)(j40__intN x, j40__intN y, j40__intN *out) {
#ifdef J40_ADD_OVERFLOW
// gcc/clang extension uses an opposite convention, which is unnatural to use with J40__SHOULD
return !J40_ADD_OVERFLOW(x, y, out);
#else
return j40__(add_fallback,N)(x, y, out);
#endif
}
J40_ALWAYS_INLINE int j40__(sub,N)(j40__intN x, j40__intN y, j40__intN *out) {
#ifdef J40_SUB_OVERFLOW
return !J40_SUB_OVERFLOW(x, y, out);
#else
return j40__(sub_fallback,N)(x, y, out);
#endif
}
J40_ALWAYS_INLINE int j40__(mul,N)(j40__intN x, j40__intN y, j40__intN *out) {
#ifdef J40_MUL_OVERFLOW
return !J40_MUL_OVERFLOW(x, y, out);
#else
return j40__(mul_fallback,N)(x, y, out);
#endif
}
J40_ALWAYS_INLINE int j40__(add_fallback,N)(j40__intN x, j40__intN y, j40__intN *out) {
if (J40_UNLIKELY((x > 0 && y > J40__INTN_MAX - x) || (x < 0 && y < J40__INTN_MIN - x))) {
return 0;
} else {
*out = (j40__intN) (x + y);
return 1;
}
}
J40_ALWAYS_INLINE int j40__(sub_fallback,N)(j40__intN x, j40__intN y, j40__intN *out) {
if (J40_UNLIKELY((y < 0 && x > J40__INTN_MAX + y) || (y > 0 && x < J40__INTN_MIN + y))) {
return 0;
} else {
*out = (j40__intN) (x - y);
return 1;
}
}
J40_ALWAYS_INLINE int j40__(mul_fallback,N)(j40__intN x, j40__intN y, j40__intN *out) {
if (J40_UNLIKELY(
x > 0 ?
(y > 0 ? x > J40__INTN_MAX / y : y < J40__INTN_MIN / x) :
(y > 0 ? x < J40__INTN_MIN / y : y != 0 && x < J40__INTN_MAX / y)
)) {
return 0;
} else {
*out = (j40__intN) (x * y);
return 1;
}
}
J40_ALWAYS_INLINE j40__intN j40__(clamp_add,N)(j40__intN x, j40__intN y) {
j40__intN out;
return j40__(add,N)(x, y, &out) ? out : J40__INTN_MAX;
}
J40_ALWAYS_INLINE j40__intN j40__(clamp_mul,N)(j40__intN x, j40__intN y) {
j40__intN out;
return j40__(mul,N)(x, y, &out) ? out : J40__INTN_MAX;
}
#endif // defined J40_IMPLEMENTATION
#ifdef _MSC_VER
#define J40__CLZN j40__(clz, N)
#else
#define J40__UINTN_MAX J40__CONCAT3(UINT, J40__N, _MAX)
#if UINT_MAX == J40__UINTN_MAX
#define J40__CLZN __builtin_clz
#elif ULONG_MAX == J40__UINTN_MAX
#define J40__CLZN __builtin_clzl
#elif ULLONG_MAX == J40__UINTN_MAX
#define J40__CLZN __builtin_clzll
#endif
#undef J40__UINTN_MAX
#endif // !defined _MSC_VER
#ifdef J40__CLZN
J40_ALWAYS_INLINE int j40__(floor_lg,N)(j40__uintN x);
J40_ALWAYS_INLINE int j40__(ceil_lg,N)(j40__uintN x);
#ifdef J40_IMPLEMENTATION
// both requires x to be > 0
J40_ALWAYS_INLINE int j40__(floor_lg,N)(j40__uintN x) {
return J40__N - 1 - J40__CLZN(x);
}
J40_ALWAYS_INLINE int j40__(ceil_lg,N)(j40__uintN x) {
return x > 1 ? J40__N - J40__CLZN(x - 1) : 0;
}
#endif
#undef J40__CLZN
#endif
// ----------------------------------------
// end of recursion
#undef j40__intN
#undef j40__uintN
#undef J40__INTN_MAX
#undef J40__INTN_MIN
#undef J40__N
#endif // J40__RECURSING == 100
#if J40__RECURSING < 0
// ----------------------------------------
////////////////////////////////////////////////////////////////////////////////
// aligned pointers
#ifndef J40_ASSUME_ALIGNED
#if J40__HAS_BUILTIN_ASSUME_ALIGNED || J40__GCC_VER >= 0x40700
#define J40_ASSUME_ALIGNED(p, align) __builtin_assume_aligned(p, align)
#else
#define J40_ASSUME_ALIGNED(p, align) (p)
#endif
#endif // !defined J40_ASSUME_ALIGNED
J40_ALWAYS_INLINE void *j40__alloc_aligned(size_t sz, size_t align, size_t *outmisalign);
J40_ALWAYS_INLINE void j40__free_aligned(void *ptr, size_t align, size_t misalign);
J40_MAYBE_UNUSED J40_STATIC void *j40__alloc_aligned_fallback(size_t sz, size_t align, size_t *outmisalign);
J40_MAYBE_UNUSED J40_STATIC void j40__free_aligned_fallback(void *ptr, size_t align, size_t misalign);
#ifdef J40_IMPLEMENTATION
#if _POSIX_C_SOURCE >= 200112L || _XOPEN_SOURCE >= 600
J40_ALWAYS_INLINE void *j40__alloc_aligned(size_t sz, size_t align, size_t *outmisalign) {
void *ptr = NULL;
*outmisalign = 0;
return posix_memalign(&ptr, align, sz) ? NULL : ptr;
}
J40_ALWAYS_INLINE void j40__free_aligned(void *ptr, size_t align, size_t misalign) {
(void) align; (void) misalign;
free(ptr); // important: do not use j40_free!
}
#elif defined _ISOC11_SOURCE
J40_ALWAYS_INLINE void *j40__alloc_aligned(size_t sz, size_t align, size_t *outmisalign) {
if (sz > SIZE_MAX / align * align) return NULL; // overflow
*outmisalign = 0;
return aligned_alloc(align, (sz + align - 1) / align * align);
}
J40_ALWAYS_INLINE void j40__free_aligned(void *ptr, size_t align, size_t misalign) {
(void) align; (void) misalign;
free(ptr); // important: do not use j40_free!
}
#else
J40_ALWAYS_INLINE void *j40__alloc_aligned(size_t sz, size_t align, size_t *outmisalign) {
return j40__alloc_aligned_fallback(sz, align, outmisalign);
}
J40_ALWAYS_INLINE void j40__free_aligned(void *ptr, size_t align, size_t misalign) {
j40__free_aligned_fallback(ptr, align, misalign);
}
#endif
// a fallback implementation; the caller should store the misalign amount [0, align) separately.
// used when the platform doesn't provide aligned malloc at all, or the platform implementation
// is not necessarily better; e.g. MSVC _aligned_malloc has the same amount of overhead as of Win10
J40_MAYBE_UNUSED J40_STATIC void *j40__alloc_aligned_fallback(size_t sz, size_t align, size_t *outmisalign) {
// while this is almost surely an overestimate (can be improved if we know the malloc alignment)
// there is no standard way to compute a better estimate in C99 so this is inevitable.
size_t maxmisalign = align - 1, misalign;
void *ptr;
if (sz > SIZE_MAX - maxmisalign) return NULL; // overflow
ptr = J40_MALLOC(sz + maxmisalign);
if (!ptr) return NULL;
misalign = align - (uintptr_t) ptr % align;
if (misalign == align) misalign = 0;
*outmisalign = misalign;
return (void*) ((uintptr_t) ptr + misalign);
}
J40_MAYBE_UNUSED J40_ALWAYS_INLINE void j40__free_aligned_fallback(void *ptr, size_t align, size_t misalign) {
if (!ptr) return;
J40__ASSERT((uintptr_t) ptr % align == 0);
j40__free((void*) ((uintptr_t) ptr - misalign));
}
#endif // defined J40_IMPLEMENTATION
////////////////////////////////////////////////////////////////////////////////
// two-dimensional view
typedef struct { int32_t logw, logh; float *J40_RESTRICT ptr; } j40__view_f32;
J40_ALWAYS_INLINE j40__view_f32 j40__make_view_f32(int32_t logw, int32_t logh, float *J40_RESTRICT ptr);
J40_ALWAYS_INLINE void j40__adapt_view_f32(j40__view_f32 *outv, int32_t logw, int32_t logh);
J40_ALWAYS_INLINE void j40__reshape_view_f32(j40__view_f32 *outv, int32_t logw, int32_t logh);
J40_ALWAYS_INLINE void j40__copy_view_f32(j40__view_f32 *outv, const j40__view_f32 inv);
J40_ALWAYS_INLINE void j40__transpose_view_f32(j40__view_f32 *outv, const j40__view_f32 inv);
J40_ALWAYS_INLINE void j40__oddeven_columns_to_halves_f32(j40__view_f32 *outv, const j40__view_f32 inv);
J40_ALWAYS_INLINE void j40__oddeven_rows_to_halves_f32(j40__view_f32 *outv, const j40__view_f32 inv);
J40_MAYBE_UNUSED J40_STATIC void j40__print_view_f32(j40__view_f32 v, const char *name, const char *file, int32_t line);
#ifdef J40_IMPLEMENTATION
J40_ALWAYS_INLINE j40__view_f32 j40__make_view_f32(int32_t logw, int32_t logh, float *J40_RESTRICT ptr) {
j40__view_f32 ret = { logw, logh, ptr };
return ret;
}
J40_ALWAYS_INLINE void j40__adapt_view_f32(j40__view_f32 *outv, int32_t logw, int32_t logh) {
J40__ASSERT(outv->logw + outv->logh >= logw + logh);
outv->logw = logw;
outv->logh = logh;
}
J40_ALWAYS_INLINE void j40__reshape_view_f32(j40__view_f32 *outv, int32_t logw, int32_t logh) {
J40__ASSERT(outv->logw + outv->logh == logw + logh);
outv->logw = logw;
outv->logh = logh;
}
J40_ALWAYS_INLINE void j40__copy_view_f32(j40__view_f32 *outv, const j40__view_f32 inv) {
int32_t x, y;
float *outptr = outv->ptr;
j40__adapt_view_f32(outv, inv.logw, inv.logh);
for (y = 0; y < (1 << inv.logh); ++y) for (x = 0; x < (1 << inv.logw); ++x) {
outptr[y << inv.logw | x] = inv.ptr[y << inv.logw | x];
}
}
J40_ALWAYS_INLINE void j40__transpose_view_f32(j40__view_f32 *outv, const j40__view_f32 inv) {
int32_t x, y;
float *outptr = outv->ptr;
j40__adapt_view_f32(outv, inv.logh, inv.logw);
for (y = 0; y < (1 << inv.logh); ++y) for (x = 0; x < (1 << inv.logw); ++x) {
outptr[x << inv.logh | y] = inv.ptr[y << inv.logw | x];
}
}
// shuffles columns 01234567 into 02461357 and so on
J40_ALWAYS_INLINE void j40__oddeven_columns_to_halves_f32(j40__view_f32 *outv, const j40__view_f32 inv) {
int32_t x, y;
float *outptr = outv->ptr;
J40__ASSERT(inv.logw > 0);
j40__adapt_view_f32(outv, inv.logw, inv.logh);
for (y = 0; y < (1 << inv.logh); ++y) for (x = 0; x < (1 << inv.logw); ++x) {
int32_t outx = ((x & 1) << (inv.logw - 1)) | (x >> 1);
outptr[y << inv.logw | outx] = inv.ptr[y << inv.logw | x];
}
}
// shuffles rows 01234567 into 02461357 and so on
J40_ALWAYS_INLINE void j40__oddeven_rows_to_halves_f32(j40__view_f32 *outv, const j40__view_f32 inv) {
int32_t x, y;
float *outptr = outv->ptr;
J40__ASSERT(inv.logh > 0);
j40__adapt_view_f32(outv, inv.logw, inv.logh);
for (y = 0; y < (1 << inv.logh); ++y) {
int32_t outy = ((y & 1) << (inv.logh - 1)) | (y >> 1);
for (x = 0; x < (1 << inv.logw); ++x) outptr[outy << inv.logw | x] = inv.ptr[y << inv.logw | x];
}
}
#define J40__AT(view, x, y) \
(J40__ASSERT(0 <= (x) && (x) < (1 << (view).logw) && 0 <= (y) && (y) < (1 << (view).logh)), \
(view).ptr + ((y) << (view).logw | (x)))
#define J40__VIEW_FOREACH(view, y, x, v) \
for (y = 0; y < (1 << (view).logh); ++y) \
for (x = 0; x < (1 << (view).logw) && (v = (view).ptr + (y << (view).logw | x), 1); ++x)
J40_MAYBE_UNUSED J40_STATIC void j40__print_view_f32(j40__view_f32 v, const char *name, const char *file, int32_t line) {
int32_t x, y;
printf(".--- %s:%d: %s (w=%d h=%d @%p)", file, line, name, 1 << v.logw, 1 << v.logh, v.ptr);
for (y = 0; y < (1 << v.logh); ++y) {
printf("\n|");
for (x = 0; x < (1 << v.logw); ++x) printf(" %f", *J40__AT(v, x, y));
}
printf("\n'--- %s:%d\n", file, line);
}
#endif // defined J40_IMPLEMENTATION
#define j40__print_view_f32(v) j40__print_view_f32(v, #v, __FILE__, __LINE__)
////////////////////////////////////////////////////////////////////////////////
// plane
enum {
J40__PLANE_U8 = (uint8_t) 0x20,
J40__PLANE_U16 = (uint8_t) 0x21,
J40__PLANE_I16 = (uint8_t) 0x41,
J40__PLANE_U32 = (uint8_t) 0x22,
J40__PLANE_I32 = (uint8_t) 0x42,
J40__PLANE_F32 = (uint8_t) 0x62,