Repository navigation
Expand file tree
/
Copy pathkoi_image.h
More file actions
1339 lines (1143 loc) · 43.9 KB
/
Copy pathkoi_image.h
File metadata and controls
1339 lines (1143 loc) · 43.9 KB
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
/* koi_image - v1.03 - public domain stb like image loader
for more extensions - https://github.com/Muppetsg2/koi
no warranty implied; use at your own risk
Do this:
#define KOI_IMAGE_IMPLEMENTATION
before you include this file in *one* C or C++ file to create the implementation.
// i.e. it should look like this:
#include ...
#include ...
#include ...
#define KOI_IMAGE_IMPLEMENTATION
#include "koi_image.h"
You can #define KOI_ASSERT(x) before the #include to avoid using assert.h.
And #define KOI_MALLOC and KOI_FREE to avoid using malloc, free
QUICK NOTES:
Primarily of interest to game developers and other people who can
avoid problematic images and only need the trivial interface
- decode from memory or through FILE (define KOI_NO_STDIO to remove code)
- decode from arbitrary I/O callbacks
Full documentation under "DOCUMENTATION" below.
LICENSE
See end of file for license information.
RECENT REVISION HISTORY:
1.03 (2026-07-24) Fixed integer conversion warning in QOI loader
1.02 (2026-04-30) Improved memory safety and made Windows file handling more robust
1.01 (2025-11-20) Unified formatting of functions and tabs
1.00 (2025-05-09) QOI file loader
See end of file for full revision history.
============================ Contributors =========================
Image formats
Marceli Antosik (qoi)
*/
#if !defined(KOI_INCLUDE_KOI_IMAGE_H)
#define KOI_INCLUDE_KOI_IMAGE_H
// DOCUMENTATION
//
// Basic usage:
// int x,y,n;
// unsigned char *data = koi_load(filename, &x, &y, &n, 0);
// // ... process data if not NULL ...
// // ... x = width, y = height, n = # 8-bit components per pixel ...
// // ... replace '0' with '1'..'4' to force that many components per pixel
// // ... 'n' will always be the number of channels in file
// koi_image_free(data);
//
// Standard parameters:
// int *x -- outputs image width in pixels
// int *y -- outputs image height in pixels
// int *channels_in_file -- outputs # of image components in image file
// int desired_channels -- if non-zero, # of image components requested in result
//
// The return value from an image loader is an 'unsigned char *' which points
// to the pixel data, or NULL on an allocation failure or if the image is
// corrupt or invalid. The pixel data consists of *y scanlines of *x pixels,
// with each pixel consisting of N interleaved 8-bit components; the first
// pixel pointed to is top-left-most in the image. There is no padding between
// image scanlines or between pixels, regardless of format. The number of
// components N is 'desired_channels' if desired_channels is non-zero, or
// *channels_in_file otherwise. If desired_channels is non-zero,
// *channels_in_file has the number of components that _would_ have been
// output otherwise. E.g. if you set desired_channels to 4, you will always
// get RGBA output, but you can check *channels_in_file to see if it's trivially
// opaque because e.g. there were only 3 channels in the source image.
//
// An output image with N components has the following components interleaved
// in this order in each pixel:
//
// N=#comp components
// 1 grey
// 2 grey, alpha
// 3 red, green, blue
// 4 red, green, blue, alpha
//
// If image loading fails for any reason, the return value will be NULL,
// and *x, *y, *channels_in_file will be unchanged. The function
// koi_failure_reason() can be queried for an extremely brief, end-user
// unfriendly explanation of why the load failed. Define KOI_NO_FAILURE_STRINGS
// to avoid compiling these strings at all, and KOI_FAILURE_USERMSG to get slightly
// more user-friendly ones.
//
// To query the width, height and component count of an image without having to
// decode the full file, you can use the koi_info family of functions:
//
// int x,y,n,ok;
// ok = koi_info(filename, &x, &y, &n);
// // returns ok=1 and sets x, y, n if image is a supported format,
// // 0 otherwise.
//
// Note that koi_image pervasively uses ints in its public API for sizes,
// including sizes of memory buffers. This is now part of the API and thus
// hard to change without causing breakage. As a result, the various image
// loaders all have certain limits on image size; these differ somewhat
// by format but generally boil down to either just under 2GB or just under
// 1GB. When the decoded image would be larger than this, koi_image decoding
// will fail.
//
// Additionally, koi_image will reject image files that have any of their
// dimensions set to a larger value than the configurable KOI_MAX_DIMENSIONS,
// which defaults to 2**24 = 16777216 pixels. Due to the above memory limit,
// the only way to have an image with such dimensions load correctly
// is for it to have a rather extreme aspect ratio. Either way, the
// assumption here is that such larger images are likely to be malformed
// or malicious. If you do need to load an image with individual dimensions
// larger than that, and it still fits in the overall size limit, you can
// #define KOI_MAX_DIMENSIONS on your own to be something larger.
//
// ===========================================================================
//
// UNICODE
//
// If compiling for Windows and you wish to use Unicode filenames, compile
// with
// #define KOI_WINDOWS_UTF8
// and pass utf8-encoded filenames. Call koi_convert_wchar_to_utf8 to convert
// Windows wchar_t filenames to utf8.
//
// ===========================================================================
//
// PHILOSOPHY
//
// koi libraries are designed with the following priorities:
//
// 1. easy to use
// 2. easy to maintain
// 3. good performance
//
// Sometimes I let "good performance" creep up in priority over "easy to maintain",
// and for best performance I may provide less-easy-to-use APIs that give higher
// performance, in addition to the easy-to-use ones. Nevertheless, it's important
// to keep in mind that from the standpoint of you, a client of this library,
// all you care about is #1 and #3, and koi libraries DO NOT emphasize #3 above all.
//
// Some secondary priorities arise directly from the first two, some of which
// provide more explicit reasons why performance can't be emphasized.
//
// - Portable ("ease of use")
// - Small source code footprint ("easy to maintain")
// - No dependencies ("ease of use")
//
// ===========================================================================
//
// I/O CALLBACKS
//
// I/O callbacks allow you to read from arbitrary sources, like packaged
// files or some other source. Data read from callbacks are processed
// through a small internal buffer (currently 128 bytes) to try to reduce
// overhead.
//
// The three functions you must define are "read" (reads some bytes of data),
// "skip" (skips some bytes of data).
//
// ===========================================================================
//
// ADDITIONAL CONFIGURATION
//
// - You can suppress implementation of any of the decoders to reduce
// your code footprint by #defining one or more of the following
// symbols before creating the implementation.
//
// KOI_NO_QOI
//
// - You can request *only* certain decoders and suppress all other ones
// (this will be more forward-compatible, as addition of new decoders
// doesn't require you to disable them explicitly):
//
// KOI_ONLY_QOI
//
// - You can suppress implementation of any koi_loadf function to reduce
// your code footprint by #defining KOI_NO_LINEAR before creating
// the implementation.
//
// - If you define KOI_MAX_DIMENSIONS, koi_image will reject images greater
// than that size (in either width or height) without further processing.
// This is to let programs in the wild set an upper bound to prevent
// denial-of-service attacks on untrusted data, as one could generate a
// valid image of gigantic dimensions and force koi_image to allocate a
// huge block of memory and spend disproportionate time decoding it. By
// default this is set to (1 << 24), which is 16777216, but that's still
// very big.
#if !defined(KOI_NO_STDIO)
#include <stdio.h>
#endif // KOI_NO_STDIO
#define KOI_IMAGE_VERSION 2
enum
{
KOI_default = 0, // only used for desired_channels
KOI_grey = 1,
KOI_grey_alpha = 2,
KOI_rgb = 3,
KOI_rgb_alpha = 4
};
#include <stdlib.h>
typedef unsigned char koi_uc;
typedef unsigned short koi_us;
#if defined(__cplusplus)
extern "C" {
#endif
#if !defined(KOIDEF)
#if defined(KOI_IMAGE_STATIC)
#define KOIDEF static
#else
#define KOIDEF extern
#endif
#endif
//////////////////////////////////////////////////////////////////////////////
//
// PRIMARY API - works on images of any type
//
//
// load image by filename, open file, or memory buffer
//
typedef struct
{
int (*read) (void *user, char *data, int size); // fill 'data' with 'size' bytes. return number of bytes actually read
void (*skip) (void *user, int n); // skip the next 'n' bytes, or 'unget' the last -n bytes if negative
} koi_io_callbacks;
////////////////////////////////////
//
// 8-bits-per-channel interface
//
KOIDEF koi_uc *koi_load_from_memory (koi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF koi_uc *koi_load_from_callbacks (koi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#if !defined(KOI_NO_STDIO)
KOIDEF koi_uc *koi_load (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF koi_uc *koi_load_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
// for koi_load_from_file, file pointer is left pointing immediately after image
#endif
#if defined(KOI_WINDOWS_UTF8)
KOIDEF int koi_convert_wchar_to_utf8 (char *buffer, size_t bufferlen, const wchar_t *input);
#endif
////////////////////////////////////
//
// 16-bits-per-channel interface
//
KOIDEF koi_us *koi_load_16_from_memory (koi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF koi_us *koi_load_16_from_callbacks (koi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#if !defined(STBI_NO_STDIO)
KOIDEF koi_us *koi_load_16 (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF koi_us *koi_load_from_file_16 (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
#endif
////////////////////////////////////
//
// float-per-channel interface
//
#if !defined(KOI_NO_LINEAR)
KOIDEF float *koi_loadf_from_memory (koi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF float *koi_loadf_from_callbacks (koi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels);
#if !defined(KOI_NO_STDIO)
KOIDEF float *koi_loadf (char const *filename, int *x, int *y, int *channels_in_file, int desired_channels);
KOIDEF float *koi_loadf_from_file (FILE *f, int *x, int *y, int *channels_in_file, int desired_channels);
#endif
KOIDEF void koi_ldr_to_hdr_gamma(float gamma);
KOIDEF void koi_ldr_to_hdr_scale(float scale);
#endif // KOI_NO_LINEAR
// get a VERY brief reason for failure
// on most compilers (and ALL modern mainstream compilers) this is threadsafe
KOIDEF const char *koi_failure_reason (void);
// free the loaded image -- this is just free()
KOIDEF void koi_image_free (void *retval_from_koi_load);
// get image dimensions & components without fully decoding
KOIDEF int koi_info_from_memory (koi_uc const *buffer, int len, int *x, int *y, int *comp);
KOIDEF int koi_info_from_callbacks (koi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp);
#if !defined(KOI_NO_STDIO)
KOIDEF int koi_info (char const *filename, int *x, int *y, int *comp);
KOIDEF int koi_info_from_file (FILE *f, int *x, int *y, int *comp);
#endif
// flip the image vertically, so the first pixel in the output array is the bottom left
KOIDEF void koi_set_flip_vertically_on_load(int flag_true_if_should_flip);
// as above, but only applies to images loaded on the thread that calls the function
// this function is only available if your compiler supports thread-local variables;
// calling it will fail to link if your compiler doesn't
KOIDEF void koi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip);
#if defined(__cplusplus)
}
#endif
//
//
//// end header file /////////////////////////////////////////////////////
#endif // KOI_INCLUDE_KOI_IMAGE_H
#if defined(KOI_IMAGE_IMPLEMENTATION)
#if defined(KOI_ONLY_QOI)
#if !defined(KOI_ONLY_QOI)
#define KOI_NO_QOI
#endif
#endif
#include <string.h>
#if !defined(KOI_NO_LINEAR)
#include <math.h> // ldexp, pow
#endif
#if !defined(KOI_NO_STDIO)
#include <stdio.h>
#endif
#if !defined(KOI_ASSERT)
#include <assert.h>
#define KOI_ASSERT(x) assert(x)
#endif
#if defined(__cplusplus)
#define KOI_EXTERN extern "C"
#else
#define KOI_EXTERN extern
#endif
#if !defined(_MSC_VER)
#if defined(__cplusplus)
#define koi_inline inline
#else
#define koi_inline
#endif
#else
#define koi_inline __forceinline
#endif
#if !defined(KOI_NO_THREAD_LOCALS)
#if defined(__cplusplus) && __cplusplus >= 201103L
#define KOI_THREAD_LOCAL thread_local
#elif defined(__GNUC__) && __GNUC__ < 5
#define KOI_THREAD_LOCAL __thread
#elif defined(_MSC_VER)
#define KOI_THREAD_LOCAL __declspec(thread)
#elif defined(__STDC_VERSION__) && __STDC_VERSION__ >= 201112L && !defined(__STDC_NO_THREADS__)
#define KOI_THREAD_LOCAL _Thread_local
#endif
#if !defined(KOI_THREAD_LOCAL)
#if defined(__GNUC__)
#define KOI_THREAD_LOCAL __thread
#endif
#endif
#endif
#if defined(_MSC_VER) || defined(__SYMBIAN32__)
typedef unsigned short koi__uint16;
typedef unsigned int koi__uint32;
typedef signed int koi__int32;
#else
#include <stdint.h>
typedef uint16_t koi__uint16;
typedef uint32_t koi__uint32;
typedef int32_t koi__int32;
#endif
// should produce compiler error if size is wrong
typedef unsigned char koi_validate_uint32[sizeof(koi__uint32) == 4 ? 1 : -1];
#if defined(_MSC_VER)
#define KOI_NOTUSED(v) (void)(v)
#else
#define KOI_NOTUSED(v) (void)sizeof(v)
#endif
#if defined(KOI_MALLOC) && defined(KOI_FREE)
// ok
#elif !defined(KOI_MALLOC) && !defined(KOI_FREE)
// ok
#else
#error "Must define all or none of KOI_MALLOC and KOI_FREE"
#endif
#if !defined(KOI_MALLOC)
#define KOI_MALLOC(sz) malloc(sz)
#define KOI_FREE(p) free(p)
#endif
#if !defined(KOI_MAX_DIMENSIONS)
#define KOI_MAX_DIMENSIONS (1 << 24)
#endif
///////////////////////////////////////////////
//
// koi__context struct and start_xxx functions
// koi__context structure is our basic context used by all images, so it
// contains all the IO context, plus some basic image information
typedef struct
{
koi__uint32 img_x, img_y;
int img_n, img_out_n;
koi_io_callbacks io;
void *io_user_data;
int read_from_callbacks;
int buflen;
koi_uc buffer_start[128];
int callback_already_read;
koi_uc *img_buffer, *img_buffer_end;
koi_uc *img_buffer_original, *img_buffer_original_end;
} koi__context;
static void koi__refill_buffer(koi__context *s);
// initialize a memory-decode context
static void koi__start_mem(koi__context *s, koi_uc const *buffer, int len)
{
s->io.read = NULL;
s->read_from_callbacks = 0;
s->callback_already_read = 0;
s->img_buffer = s->img_buffer_original = (koi_uc*)buffer;
s->img_buffer_end = s->img_buffer_original_end = (koi_uc*)buffer + len;
}
// initialize a callback-based context
static void koi__start_callbacks(koi__context *s, koi_io_callbacks *c, void *user)
{
s->io = *c;
s->io_user_data = user;
s->buflen = sizeof(s->buffer_start);
s->read_from_callbacks = 1;
s->callback_already_read = 0;
s->img_buffer = s->img_buffer_original = s->buffer_start;
koi__refill_buffer(s);
s->img_buffer_original_end = s->img_buffer_end;
}
#if !defined(KOI_NO_STDIO)
static int koi__stdio_read(void *user, char *data, int size)
{
return (int)fread(data, 1, size, (FILE*)user);
}
static void koi__stdio_skip(void *user, int n)
{
int ch;
fseek((FILE*)user, n, SEEK_CUR);
ch = fgetc((FILE*)user); /* have to read a byte to reset feof()'s flag */
if (ch != EOF) {
ungetc(ch, (FILE*)user); /* push byte back onto stream if valid. */
}
}
static koi_io_callbacks koi__stdio_callbacks =
{
koi__stdio_read,
koi__stdio_skip,
};
static void koi__start_file(koi__context *s, FILE *f)
{
koi__start_callbacks(s, &koi__stdio_callbacks, (void*)f);
}
#endif // !KOI_NO_STDIO
static void koi__rewind(koi__context *s)
{
// conceptually rewind SHOULD rewind to the beginning of the stream,
// but we just rewind to the beginning of the initial buffer, because
// we only use it after doing 'test', which only ever looks at at most 92 bytes
s->img_buffer = s->img_buffer_original;
s->img_buffer_end = s->img_buffer_original_end;
}
typedef struct
{
int bits_per_channel;
int num_channels;
} koi__result_info;
#if !defined(KOI_NO_QOI)
static int koi__qoi_test(koi__context *s);
static void *koi__qoi_load(koi__context *s, int *x, int *y, int *comp, int req_comp, koi__result_info *ri);
static int koi__qoi_info(koi__context *s, int *x, int *y, int *comp);
#endif
static
#if defined(KOI_THREAD_LOCAL)
KOI_THREAD_LOCAL
#endif
const char *koi__g_failure_reason;
KOIDEF const char *koi_failure_reason(void)
{
return koi__g_failure_reason;
}
#if !defined(KOI_NO_FAILURE_STRINGS)
static int koi__err(const char *str)
{
koi__g_failure_reason = str;
return 0;
}
#endif
static void *koi__malloc(size_t size)
{
return KOI_MALLOC(size);
}
// koi_image uses ints pervasively, including for offset calculations.
// therefore the largest decoded image size we can support with the
// current code, even on 64-bit targets, is INT_MAX. this is not a
// significant limitation for the intended use case.
//
// we do, however, need to make sure our size calculations don't
// overflow. hence a few helper functions for size calculations that
// multiply integers together, making sure that they're non-negative
// and no overflow occurs.
// return 1 if the sum is valid, 0 on overflow.
// negative terms are considered invalid.
static int koi__addsizes_valid(int a, int b)
{
if (b < 0) return 0;
// now 0 <= b <= INT_MAX, hence also
// 0 <= INT_MAX - b <= INTMAX.
// And "a + b <= INT_MAX" (which might overflow) is the
// same as a <= INT_MAX - b (no overflow)
return a <= INT_MAX - b;
}
// returns 1 if the product is valid, 0 on overflow.
// negative factors are considered invalid.
static int koi__mul2sizes_valid(int a, int b)
{
if (a < 0 || b < 0) return 0;
if (b == 0) return 1; // mul-by-0 is always safe
// portable way to check for no overflows in a*b
return a <= INT_MAX / b;
}
// returns 1 if "a*b*c + add" has no negative terms/factors and doesn't overflow
static int koi__mad3sizes_valid(int a, int b, int c, int add)
{
return koi__mul2sizes_valid(a, b) && koi__mul2sizes_valid(a * b, c) &&
koi__addsizes_valid(a * b * c, add);
}
// returns 1 if "a*b*c*d + add" has no negative terms/factors and doesn't overflow
#if !defined(KOI_NO_LINEAR)
static int koi__mad4sizes_valid(int a, int b, int c, int d, int add)
{
return koi__mul2sizes_valid(a, b) && koi__mul2sizes_valid(a * b, c) &&
koi__mul2sizes_valid(a * b * c, d) && koi__addsizes_valid(a * b * c * d, add);
}
#endif
static void *koi__malloc_mad3(int a, int b, int c, int add)
{
if (!koi__mad3sizes_valid(a, b, c, add)) return NULL;
return koi__malloc((size_t)a * b * c + add);
}
#if !defined(KOI_NO_LINEAR)
static void *koi__malloc_mad4(int a, int b, int c, int d, int add)
{
if (!koi__mad4sizes_valid(a, b, c, d, add)) return NULL;
return koi__malloc((size_t)a * b * c * d + add);
}
#endif
// koi__err - error
// koi__errpf - error returning pointer to float
// koi__errpuc - error returning pointer to unsigned char
#if defined(KOI_NO_FAILURE_STRINGS)
#define koi__err(x,y) 0
#elif defined(KOI_FAILURE_USERMSG)
#define koi__err(x,y) koi__err(y)
#else
#define koi__err(x,y) koi__err(x)
#endif
#define koi__errpf(x,y) ((float*)(size_t)(koi__err(x,y) ? NULL : NULL))
#define koi__errpuc(x,y) ((unsigned char*)(size_t)(koi__err(x,y) ? NULL : NULL))
KOIDEF void koi_image_free(void *retval_from_koi_load)
{
KOI_FREE(retval_from_koi_load);
}
#if !defined(KOI_NO_LINEAR)
static float *koi__ldr_to_hdr(koi_uc *data, int x, int y, int comp);
#endif
static int koi__vertically_flip_on_load_global = 0;
KOIDEF void koi_set_flip_vertically_on_load(int flag_true_if_should_flip)
{
koi__vertically_flip_on_load_global = flag_true_if_should_flip;
}
#if !defined(KOI_THREAD_LOCAL)
#define koi__vertically_flip_on_load koi__vertically_flip_on_load_global
#else
static KOI_THREAD_LOCAL int koi__vertically_flip_on_load_local, koi__vertically_flip_on_load_set;
KOIDEF void koi_set_flip_vertically_on_load_thread(int flag_true_if_should_flip)
{
koi__vertically_flip_on_load_local = flag_true_if_should_flip;
koi__vertically_flip_on_load_set = 1;
}
#define koi__vertically_flip_on_load (koi__vertically_flip_on_load_set \
? koi__vertically_flip_on_load_local \
: koi__vertically_flip_on_load_global)
#endif // KOI_THREAD_LOCAL
static void *koi__load_main(koi__context *s, int *x, int *y, int *comp, int req_comp, koi__result_info *ri)
{
memset(ri, 0, sizeof(*ri)); // make sure it's initialized if we add new fields
ri->bits_per_channel = 8; // default is 8 so most paths don't have to be changed
ri->num_channels = 0;
#if !defined(KOI_NO_QOI)
if (koi__qoi_test(s)) return koi__qoi_load(s, x, y, comp, req_comp, ri);
#endif
return koi__errpuc("unknown image type", "Image not of any known type, or corrupt");
}
static koi__uint16 *koi__convert_8_to_16(koi_uc *orig, int w, int h, int channels)
{
int i;
int img_len;
koi__uint16 *enlarged;
enlarged = (koi__uint16*)koi__malloc_mad4(w, h, channels, 2, 0);
if (enlarged == NULL) {
KOI_FREE(orig);
return (koi__uint16*)koi__errpuc("outofmem", "Out of memory");
}
img_len = w * h * channels;
for (i = 0; i < img_len; ++i)
enlarged[i] = (koi__uint16)((orig[i] << 8) + orig[i]); // replicate to high and low byte, maps 0->0, 255->0xffff
KOI_FREE(orig);
return enlarged;
}
static void koi__vertical_flip(void *image, int w, int h, int bytes_per_pixel)
{
int row;
size_t bytes_per_row = (size_t)w * bytes_per_pixel;
koi_uc temp[2048];
koi_uc *bytes = (koi_uc*)image;
for (row = 0; row < (h >> 1); row++) {
koi_uc *row0 = bytes + row * bytes_per_row;
koi_uc *row1 = bytes + (size_t)(h - row - 1) * bytes_per_row;
// swap row0 with row1
size_t bytes_left = bytes_per_row;
while (bytes_left) {
size_t bytes_copy = (bytes_left < sizeof(temp)) ? bytes_left : sizeof(temp);
memcpy(temp, row0, bytes_copy);
memcpy(row0, row1, bytes_copy);
memcpy(row1, temp, bytes_copy);
row0 += bytes_copy;
row1 += bytes_copy;
bytes_left -= bytes_copy;
}
}
}
static unsigned char *koi__load_and_postprocess_8bit(koi__context *s, int *x, int *y, int *comp, int req_comp)
{
koi__result_info ri;
void *result = koi__load_main(s, x, y, comp, req_comp, &ri);
if (result == NULL)
return NULL;
// it is the responsibility of the loaders to make sure we get 8 bit.
KOI_ASSERT(ri.bits_per_channel == 8);
if (koi__vertically_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
koi__vertical_flip(result, *x, *y, channels * sizeof(koi_uc));
}
return (unsigned char*)result;
}
static koi__uint16 *koi__load_and_postprocess_16bit(koi__context *s, int *x, int *y, int *comp, int req_comp)
{
koi__result_info ri;
void *result = koi__load_main(s, x, y, comp, req_comp, &ri);
if (result == NULL)
return NULL;
// it is the responsibility of the loaders to make sure we get either 8 or 16 bit.
KOI_ASSERT(ri.bits_per_channel == 8 || ri.bits_per_channel == 16);
if (ri.bits_per_channel != 16) {
result = koi__convert_8_to_16((koi_uc*)result, *x, *y, req_comp == 0 ? *comp : req_comp);
ri.bits_per_channel = 16;
}
if (koi__vertically_flip_on_load) {
int channels = req_comp ? req_comp : *comp;
koi__vertical_flip(result, *x, *y, channels * sizeof(koi__uint16));
}
return (koi__uint16*)result;
}
#if defined(KOI_NO_LINEAR)
static void koi__float_postprocess(float *result, int *x, int *y, int *comp, int req_comp)
{
if (koi__vertically_flip_on_load && result != NULL) {
int channels = req_comp ? req_comp : *comp;
koi__vertical_flip(result, *x, *y, channels * sizeof(float));
}
}
#endif
#if !defined(KOI_NO_STDIO)
#if defined(_WIN32) && defined(KOI_WINDOWS_UTF8)
KOI_EXTERN __declspec(dllimport) int __stdcall MultiByteToWideChar(unsigned int cp, unsigned long flags, const char *str, int cbmb, wchar_t *widestr, int cchwide);
KOI_EXTERN __declspec(dllimport) int __stdcall WideCharToMultiByte(unsigned int cp, unsigned long flags, const wchar_t *widestr, int cchwide, char *str, int cbmb, const char *defchar, int *used_default);
KOIDEF int koi_convert_wchar_to_utf8(char *buffer, size_t bufferlen, const wchar_t *input)
{
int len = (bufferlen > 0x7FFFFFFF) ? 0x7FFFFFFF : (int)bufferlen;
return WideCharToMultiByte(65001 /* UTF8 */, 0, input, -1, buffer, len, NULL, NULL);
}
#endif
static FILE *koi__fopen(char const *filename, char const *mode)
{
FILE *f;
#if defined(_WIN32) && defined(KOI_WINDOWS_UTF8)
wchar_t wMode[64];
wchar_t wFilename[4096];
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, filename, -1, wFilename, (int)(sizeof(wFilename) / sizeof(*wFilename))))
return NULL;
if (0 == MultiByteToWideChar(65001 /* UTF8 */, 0, mode, -1, wMode, (int)(sizeof(wMode) / sizeof(*wMode))))
return NULL;
#if defined(_MSC_VER) && _MSC_VER >= 1400
if (0 != _wfopen_s(&f, wFilename, wMode))
f = NULL;
#else
f = _wfopen(wFilename, wMode);
#endif
#elif defined(_MSC_VER) && _MSC_VER >= 1400
if (0 != fopen_s(&f, filename, mode))
f = NULL;
#else
f = fopen(filename, mode);
#endif
return f;
}
KOIDEF koi_uc *koi_load(char const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = koi__fopen(filename, "rb");
unsigned char *result;
if (f == NULL) return koi__errpuc("can't fopen", "Unable to open file");
result = koi_load_from_file(f, x, y, comp, req_comp);
fclose(f);
return result;
}
KOIDEF koi_uc *koi_load_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
unsigned char *result;
koi__context s;
koi__start_file(&s, f);
result = koi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
if (result) {
// need to 'unget' all the characters in the IO buffer
fseek(f, -(int)(s.img_buffer_end - s.img_buffer), SEEK_CUR);
}
return result;
}
KOIDEF koi__uint16 *koi_load_from_file_16(FILE *f, int *x, int *y, int *comp, int req_comp)
{
koi__uint16 *result;
koi__context s;
koi__start_file(&s, f);
result = koi__load_and_postprocess_16bit(&s, x, y, comp, req_comp);
if (result) {
// need to 'unget' all the characters in the IO buffer
fseek(f, -(int)(s.img_buffer_end - s.img_buffer), SEEK_CUR);
}
return result;
}
KOIDEF koi_us *koi_load_16(char const *filename, int *x, int *y, int *comp, int req_comp)
{
FILE *f = koi__fopen(filename, "rb");
koi__uint16 *result;
if (f == NULL) return (koi_us*)koi__errpuc("can't fopen", "Unable to open file");
result = koi_load_from_file_16(f, x, y, comp, req_comp);
fclose(f);
return result;
}
#endif // !KOI_NO_STDIO
KOIDEF koi_us *koi_load_16_from_memory(koi_uc const *buffer, int len, int *x, int *y, int *channels_in_file, int desired_channels)
{
koi__context s;
koi__start_mem(&s, buffer, len);
return koi__load_and_postprocess_16bit(&s, x, y, channels_in_file, desired_channels);
}
KOIDEF koi_us *koi_load_16_from_callbacks(koi_io_callbacks const *clbk, void *user, int *x, int *y, int *channels_in_file, int desired_channels)
{
koi__context s;
koi__start_callbacks(&s, (koi_io_callbacks*)clbk, user);
return koi__load_and_postprocess_16bit(&s, x, y, channels_in_file, desired_channels);
}
KOIDEF koi_uc *koi_load_from_memory(koi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
{
koi__context s;
koi__start_mem(&s, buffer, len);
return koi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
}
KOIDEF koi_uc *koi_load_from_callbacks(koi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
koi__context s;
koi__start_callbacks(&s, (koi_io_callbacks*)clbk, user);
return koi__load_and_postprocess_8bit(&s, x, y, comp, req_comp);
}
#if !defined(KOI_NO_LINEAR)
static float *koi__loadf_main(koi__context *s, int *x, int *y, int *comp, int req_comp)
{
unsigned char *data;
data = koi__load_and_postprocess_8bit(s, x, y, comp, req_comp);
if (data)
return koi__ldr_to_hdr(data, *x, *y, req_comp ? req_comp : *comp);
return koi__errpf("unknown image type", "Image not of any known type, or corrupt");
}
KOIDEF float *koi_loadf_from_memory(koi_uc const *buffer, int len, int *x, int *y, int *comp, int req_comp)
{
koi__context s;
koi__start_mem(&s,buffer,len);
return koi__loadf_main(&s, x, y, comp, req_comp);
}
KOIDEF float *koi_loadf_from_callbacks(koi_io_callbacks const *clbk, void *user, int *x, int *y, int *comp, int req_comp)
{
koi__context s;
koi__start_callbacks(&s, (koi_io_callbacks*)clbk, user);
return koi__loadf_main(&s, x, y, comp, req_comp);
}
#if !defined(KOI_NO_STDIO)
KOIDEF float *koi_loadf(char const *filename, int *x, int *y, int *comp, int req_comp)
{
float *result;
FILE *f = koi__fopen(filename, "rb");
if (f == NULL) return koi__errpf("can't fopen", "Unable to open file");
result = koi_loadf_from_file(f, x, y, comp, req_comp);
fclose(f);
return result;
}
KOIDEF float *koi_loadf_from_file(FILE *f, int *x, int *y, int *comp, int req_comp)
{
koi__context s;
koi__start_file(&s,f);
return koi__loadf_main(&s, x, y, comp, req_comp);
}
#endif // !KOI_NO_STDIO
#endif // !KOI_NO_LINEAR
#if !defined(KOI_NO_LINEAR)
static float koi__l2h_gamma = 2.2f, koi__l2h_scale = 1.0f;
KOIDEF void koi_ldr_to_hdr_gamma(float gamma) { koi__l2h_gamma = gamma; }
KOIDEF void koi_ldr_to_hdr_scale(float scale) { koi__l2h_scale = scale; }
#endif
//////////////////////////////////////////////////////////////////////////////
//
// Common code used by all image loaders
//
enum
{
KOI__SCAN_load = 0,
KOI__SCAN_type,
KOI__SCAN_header
};
static void koi__refill_buffer(koi__context *s)
{
int n = (s->io.read)(s->io_user_data, (char*)s->buffer_start, s->buflen);
s->callback_already_read += (int)(s->img_buffer - s->img_buffer_original);
if (n == 0) {
// at end of file, treat same as if from memory, but need to handle case
// where s->img_buffer isn't pointing to safe memory, e.g. 0-byte file
s->read_from_callbacks = 0;
s->img_buffer = s->buffer_start;
s->img_buffer_end = s->buffer_start + 1;
*s->img_buffer = 0;
}
else {
s->img_buffer = s->buffer_start;
s->img_buffer_end = s->buffer_start + n;
}
}
koi_inline static koi_uc koi__get8(koi__context *s)
{
if (s->img_buffer < s->img_buffer_end)
return *s->img_buffer++;
if (s->read_from_callbacks) {
koi__refill_buffer(s);
return *s->img_buffer++;
}
return 0;
}
static int koi__get16be(koi__context *s)
{
int z = koi__get8(s) << 8;
return z + koi__get8(s);
}
static koi__uint32 koi__get32be(koi__context *s)
{
koi__uint32 z = koi__get16be(s) << 16;
z += (koi__uint32)koi__get16be(s);
return z;
}
static koi_uc koi__compute_y(int r, int g, int b)
{
return (koi_uc)(((r * 77) + (g * 150) + (29 * b)) >> 8);
}
static unsigned char *koi__convert_format(unsigned char *data, int img_n, int req_comp, unsigned int x, unsigned int y)
{
int i, j;
unsigned char *good;
if (req_comp == img_n) return data;
KOI_ASSERT(req_comp >= 1 && req_comp <= 4);
good = (unsigned char*)koi__malloc_mad3(x, y, req_comp, 0);
if (good == NULL) {
KOI_FREE(data);