forked from syoyo/tinydng
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtiny_dng_loader.h
5907 lines (5079 loc) · 165 KB
/
tiny_dng_loader.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
//
// TinyDNGLoader, single header only DNG/TIFF loader.
//
/*
The MIT License (MIT)
Copyright (c) 2016 - Present, Syoyo Fujita and many contributors.
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/
#ifndef TINY_DNG_LOADER_H_
#define TINY_DNG_LOADER_H_
// @note {
// https://www.adobe.com/content/dam/Adobe/en/products/photoshop/pdfs/dng_spec_1.4.0.0.pdf
// }
#include <string>
#include <vector>
#if !defined(TINY_DNG_NO_EXCEPTION)
#include <stdexcept>
#endif
namespace tinydng {
// TODO: Deal with out-of-memory error
// e.g. limit maximum images in one DNG/TIFF file
const size_t kMaxImages = 10240;
// Avoid stack-overflow of recursive Sub IFD parsing.
const uint32_t kMaxRecursiveIFDParse = 1024;
typedef enum {
LIGHTSOURCE_UNKNOWN = 0,
LIGHTSOURCE_DAYLIGHT = 1,
LIGHTSOURCE_FLUORESCENT = 2,
LIGHTSOURCE_TUNGSTEN = 3,
LIGHTSOURCE_FLASH = 4,
LIGHTSOURCE_FINE_WEATHER = 9,
LIGHTSOURCE_CLOUDY_WEATHER = 10,
LIGHTSOURCE_SHADE = 11,
LIGHTSOURCE_DAYLIGHT_FLUORESCENT = 12,
LIGHTSOURCE_DAY_WHITE_FLUORESCENT = 13,
LIGHTSOURCE_COOL_WHITE_FLUORESCENT = 14,
LIGHTSOURCE_WHITE_FLUORESCENT = 15,
LIGHTSOURCE_STANDARD_LIGHT_A = 17,
LIGHTSOURCE_STANDARD_LIGHT_B = 18,
LIGHTSOURCE_STANDARD_LIGHT_C = 19,
LIGHTSOURCE_D55 = 20,
LIGHTSOURCE_D65 = 21,
LIGHTSOURCE_D75 = 22,
LIGHTSOURCE_D50 = 23,
LIGHTSOURCE_ISO_STUDIO_TUNGSTEN = 24,
LIGHTSOURCE_OTHER_LIGHT_SOURCE = 255
} LightSource;
typedef enum {
COMPRESSION_NONE = 1,
COMPRESSION_LZW = 5, // LZW
COMPRESSION_OLD_JPEG = 6, // JPEG or lossless JPEG
COMPRESSION_NEW_JPEG = 7, // Usually lossles JPEG, may be JPEG
COMPRESSION_ZIP = 8, // ZIP
COMPRESSION_LOSSY = 34892, // Lossy JPEG(usually 8-bit standard JPEG)
COMPRESSION_NEF = 34713 // NIKON RAW
} Compression;
typedef enum {
TYPE_NOTYPE = 0,
TYPE_BYTE = 1,
TYPE_ASCII = 2, // null-terminated string
TYPE_SHORT = 3,
TYPE_LONG = 4,
TYPE_RATIONAL = 5, // 64-bit unsigned fraction
TYPE_SBYTE = 6,
TYPE_UNDEFINED = 7, // 8-bit untyped data */
TYPE_SSHORT = 8,
TYPE_SLONG = 9,
TYPE_SRATIONAL = 10, // 64-bit signed fraction
TYPE_FLOAT = 11,
TYPE_DOUBLE = 12,
TYPE_IFD = 13, // 32-bit unsigned integer (offset)
TYPE_LONG8 = 16, // BigTIFF 64-bit unsigned
TYPE_SLONG8 = 17, // BigTIFF 64-bit signed
TYPE_IFD8 = 18 // BigTIFF 64-bit unsigned integer (offset)
} DataType;
typedef enum {
SAMPLEFORMAT_UINT = 1,
SAMPLEFORMAT_INT = 2,
SAMPLEFORMAT_IEEEFP = 3, // floating point
SAMPLEFORMAT_VOID = 4,
SAMPLEFORMAT_COMPLEXINT = 5,
SAMPLEFORMAT_COMPLEXIEEEFP = 6
} SampleFormat;
struct FieldInfo {
int tag;
short read_count;
short write_count;
DataType type;
unsigned short bit;
unsigned char ok_to_change;
unsigned char pass_count;
std::string name;
FieldInfo()
: tag(0),
read_count(-1),
write_count(-1),
type(TYPE_NOTYPE),
bit(0),
ok_to_change(0),
pass_count(0) {}
};
struct FieldData {
int tag;
DataType type;
std::string name;
std::vector<unsigned char> data;
FieldData() : tag(0), type(TYPE_NOTYPE) {}
};
struct GainMap {
unsigned int idx; // 1, 2 or 3: OpCodeListN. 0 = invalid
unsigned int top, left, bottom, right;
unsigned int plane, planes;
unsigned int row_pitch, col_pitch;
unsigned int map_points_v, map_points_h;
int _pad0;
double map_spacing_v, map_spacing_h;
double map_origin_v, map_origin_h;
unsigned int map_planes;
int _pad1;
std::vector<float> pixels; // size = map_points_v * map_points_h * map_planes
GainMap() : idx(0) {
}
};
struct DNGImage {
int black_level[4]; // for each spp(up to 4)
int white_level[4]; // for each spp(up to 4)
int version; // DNG version
int samples_per_pixel;
int rows_per_strip;
int bits_per_sample_original; // BitsPerSample in stored file.
int bits_per_sample; // Bits per sample after reading(decoding) DNG image.
char cfa_plane_color[4]; // 0:red, 1:green, 2:blue, 3:cyan, 4:magenta,
// 5:yellow, 6:white
int cfa_pattern[2][2]; // @fixme { Support non 2x2 CFA pattern. }
short cfa_pattern_dim;
short _pad_cfa_patern_dim;
int cfa_layout;
int active_area[4]; // top, left, bottom, right
bool has_active_area;
unsigned char pad_has_active_area[3];
int tile_width;
int tile_length;
unsigned int tile_offset;
unsigned int tile_byte_count; // (compressed) size
int pad0;
double analog_balance[3];
bool has_analog_balance;
unsigned char pad1[7];
double as_shot_neutral[3];
int pad3;
bool has_as_shot_neutral;
unsigned char pad4[7];
int pad5;
double color_matrix1[3][3];
double color_matrix2[3][3];
double forward_matrix1[3][3];
double forward_matrix2[3][3];
double camera_calibration1[3][3];
double camera_calibration2[3][3];
LightSource calibration_illuminant1;
LightSource calibration_illuminant2;
int width;
int height;
int compression;
unsigned int offset;
short orientation;
short _pad0;
int strip_byte_count;
int jpeg_byte_count;
short planar_configuration; // 1: chunky, 2: planar
short predictor; // tag 317. 1 = no prediction, 2 = horizontal differencing,
// 3 = floating point horizontal differencing
SampleFormat sample_format;
// For an image with multiple strips.
int strips_per_image;
std::vector<unsigned int> strip_byte_counts;
std::vector<unsigned int> strip_offsets;
// CR2(Canon RAW) specific
unsigned short cr2_slices[3];
unsigned short pad_c;
// Apple ProRAW
std::string semantic_name;
// GainMap
std::vector<GainMap> opcodelist1_gainmap;
std::vector<GainMap> opcodelist2_gainmap;
std::vector<GainMap> opcodelist3_gainmap;
std::vector<unsigned char>
data; // Decoded pixel data(len = spp * width * height * bps / 8)
// Custom fields
std::vector<FieldData> custom_fields;
};
///
/// Loads DNG image and store it to `images`
///
/// If DNG contains multiple images(e.g. full-res image + thumnail image),
/// The function creates `DNGImage` data strucure for each images.
///
/// C++ exception would be trigerred inside the function unless
/// TINY_DNG_NO_EXCEPTION macro is defined.
///
/// @param[in] filename DNG filename.
/// @param[in] custom_fields List of custom fields to parse(optional. can pass
/// empty array).
/// @param[out] images Loaded DNG images.
/// @param[out] warn Warning message.
/// @param[out] err Error message.
///
/// @return true upon success.
/// @return false upon failure and store error message into `err`.
///
bool LoadDNG(const char* filename, std::vector<FieldInfo>& custom_fields,
std::vector<DNGImage>* images, std::string* warn,
std::string* err);
///
/// Check if a file is DNG(TIFF) or not.
/// Extra message will be stored `msg`.
///
bool IsDNG(const char* filename, std::string* msg);
///
/// A variant of `LoadDNG` which loads DNG image from memory.
/// Up to 2GB DNG data.
///
bool LoadDNGFromMemory(const char* mem, unsigned int size,
std::vector<FieldInfo>& custom_fields,
std::vector<DNGImage>* images, std::string* warn,
std::string* err);
///
/// A variant of `IsDNG` which checks if a data is DNG image.
///
bool IsDNGFromMemory(const char* mem, unsigned int size, std::string* msg);
} // namespace tinydng
#ifdef TINY_DNG_LOADER_IMPLEMENTATION
#if defined(_WIN32)
#if defined(__MINGW32__)
#include <windows.h> // wchar apis
#else
#include <Windows.h>
#endif
#endif
#include <stdint.h> // for lj92
#include <cmath>
#include <cstdlib>
#include <cstring>
#include <iterator>
#include <map>
#include <sstream>
#include <limits>
#if defined(TINY_DNG_LOADER_NO_STDIO)
#else
#include <cstdio>
#include <cassert>
#include <iostream>
#endif
// #include <iostream> // dbg
#ifdef TINY_DNG_LOADER_PROFILING
// Requires C++11 feature
#include <chrono>
#endif
#if __cplusplus > 199911L
#ifdef TINY_DNG_LOADER_USE_THREAD
#include <atomic>
#include <thread>
#endif
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
// #define TINY_DNG_LOADER_DEBUG
#ifdef TINY_DNG_LOADER_DEBUG
#define TINY_DNG_DPRINTF(...) printf(__VA_ARGS__)
#else
#define TINY_DNG_DPRINTF(...)
#endif
#if 0 // DBG
#define TINY_DNG_DEBUG_SAVEIMAGE
#if defined(TINY_DNG_DEBUG_SAVEIMAGE)
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "examples/common/stb_image_write.h"
#endif
#endif
#if !defined(TINY_DNG_NO_EXCEPTION)
#define TINY_DNG_ASSERT(assertion, text) \
do { \
if ((assertion) == 0) { \
throw std::runtime_error(text); \
} \
} while (false)
#define TINY_DNG_ABORT(text) \
do { \
throw std::runtime_error(text); \
} while (false)
#else
#if defined(TINY_DNG_LOADER_NO_STDIO)
// No output
#define TINY_DNG_ASSERT(assertion, text) \
do { \
if ((assertion) == 0) { \
abort(); \
} \
} while (false)
#define TINY_DNG_ABORT(text) \
do { \
abort(); \
} while (false)
#else // NO_STDIO
#define TINY_DNG_ASSERT(assertion, text) \
do { \
if ((assertion) == 0) { \
std::cerr << __FILE__ << ":" << __LINE__ << " " << text << std::endl; \
abort(); \
} \
} while (false)
#define TINY_DNG_ABORT(text) \
do { \
std::cerr << __FILE__ << ":" << __LINE__ << " " << text << std::endl; \
abort(); \
} while (false)
#endif // NO_STDIO
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4334)
#pragma warning(disable : 4244)
#endif
#ifdef TINY_DNG_LOADER_ENABLE_ZIP
#ifndef TINY_DNG_LOADER_USE_SYSTEM_ZLIB
#include "miniz.h"
#endif
#endif
#if defined(TINY_DNG_LOADER_NO_STB_IMAGE_INCLUDE)
#else
// STB image to decode jpeg image.
// Assume STB_IMAGE_IMPLEMENTATION is defined elsewhere
#include "stb_image.h"
#endif
#ifdef __clang__
#pragma clang diagnostic pop
#endif
#ifdef _MSC_VER
#pragma warning(pop)
#endif
namespace tinydng {
#ifdef __clang__
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wc++11-extensions"
#pragma clang diagnostic ignored "-Wold-style-cast"
#pragma clang diagnostic ignored "-Wconversion"
#pragma clang diagnostic ignored "-Wunused-parameter"
#pragma clang diagnostic ignored "-Wcast-align"
#pragma clang diagnostic ignored "-Wconditional-uninitialized"
#pragma clang diagnostic ignored "-Wunused-function"
#pragma clang diagnostic ignored "-Wpadded"
#pragma clang diagnostic ignored "-Wmissing-prototypes"
#pragma clang diagnostic ignored "-Wreserved-id-macro"
#pragma clang diagnostic ignored "-Wdisabled-macro-expansion"
#pragma clang diagnostic ignored "-Wdouble-promotion"
#pragma clang diagnostic ignored "-Wimplicit-fallthrough"
#if __has_warning("-Wcomma")
#pragma clang diagnostic ignored "-Wcomma"
#endif
#if __has_warning("-Wcast-qual")
#pragma clang diagnostic ignored "-Wcast-qual"
#endif
#if __has_warning("-Wzero-as-null-pointer-constant")
#pragma clang diagnostic ignored "-Wzero-as-null-pointer-constant"
#endif
#endif
#ifdef _MSC_VER
#pragma warning(push)
#pragma warning(disable : 4100)
#pragma warning(disable : 4334)
#pragma warning(disable : 4244)
#endif
namespace {
// Begin liblj92, Lossless JPEG decode/encoder ------------------------------
//
// With fixes: https://github.com/ilia3101/MLV-App/pull/151
/*
lj92.c
(c) Andrew Baldwin 2014
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
*/
enum LJ92_ERRORS {
LJ92_ERROR_NONE = 0,
LJ92_ERROR_CORRUPT = -1,
LJ92_ERROR_NO_MEMORY = -2,
LJ92_ERROR_BAD_HANDLE = -3,
LJ92_ERROR_TOO_WIDE = -4
};
typedef struct _ljp* lj92;
/* Parse a lossless JPEG (1992) structure returning
* - a handle that can be used to decode the data
* - width/height/bitdepth of the data
* Returns status code.
* If status == LJ92_ERROR_NONE, handle must be closed with lj92_close
*/
int lj92_open(lj92* lj, // Return handle here
const uint8_t* data, int datalen, // The encoded data
int* width, int* height,
int* bitdepth); // Width, height and bitdepth
/* Release a decoder object */
void lj92_close(lj92 lj);
/*
* Decode previously opened lossless JPEG (1992) into a 2D tile of memory
* Starting at target, write writeLength 16bit values, then skip 16bit
* skipLength value before writing again
* If linearize is not NULL, use table at linearize to convert data values from
* output value to target value
* Data is only correct if LJ92_ERROR_NONE is returned
*/
int lj92_decode(
lj92 lj, uint16_t* target, int writeLength,
int skipLength, // The image is written to target as a tile
uint16_t* linearize,
int linearizeLength); // If not null, linearize the data using this table
#if 0
/*
* Encode a grayscale image supplied as 16bit values within the given bitdepth
* Read from tile in the image
* Apply delinearization if given
* Return the encoded lossless JPEG stream
*/
int lj92_encode(uint16_t* image, int width, int height, int bitdepth,
int readLength, int skipLength, uint16_t* delinearize,
int delinearizeLength, uint8_t** encoded,
int* encodedLength);
#endif
typedef uint8_t u8;
typedef uint16_t u16;
typedef uint32_t u32;
//#define SLOW_HUFF
//#define LJ92_DEBUG
#define LJ92_MAX_COMPONENTS (16)
typedef struct _ljp {
u8* data;
u8* dataend;
int datalen;
int scanstart;
int ix;
int x; // Width
int y; // Height
int bits; // Bit depth
int components; // Components(Nf)
int writelen; // Write rows this long
int skiplen; // Skip this many values after each row
u16* linearize; // Linearization table
int linlen;
int sssshist[16];
// Huffman table - only one supported, and probably needed
#ifdef SLOW_HUFF
// NOTE: Huffman table for each components is not supported for SLOW_HUFF code
// path.
int* maxcode;
int* mincode;
int* valptr;
u8* huffval;
int* huffsize;
int* huffcode;
#else
// Huffman table for each components
u16* hufflut[LJ92_MAX_COMPONENTS];
int huffbits[LJ92_MAX_COMPONENTS];
int num_huff_idx;
#endif
// Parse state
int cnt;
u32 b;
u16* image;
u16* rowcache;
u16* outrow[2];
} ljp;
static int find(ljp* self) {
int ix = self->ix;
u8* data = self->data;
while (data[ix] != 0xFF && ix < (self->datalen - 1)) {
ix += 1;
}
ix += 2;
if (ix >= self->datalen) {
// TINY_DNG_DPRINTF("idx = %d, datalen = %\d\n", ix, self->datalen);
return -1;
}
self->ix = ix;
// TINY_DNG_DPRINTF("ix = %d, data = %d\n", ix, data[ix - 1]);
return data[ix - 1];
}
// swap endian
#define BEH(ptr) ((((int)(*&ptr)) << 8) | (*(&ptr + 1)))
static int parseHuff(ljp* self) {
int ret = LJ92_ERROR_CORRUPT;
u8* huffhead =
&self->data
[self->ix]; // xstruct.unpack('>HB16B',self.data[self.ix:self.ix+19])
u8* bits = &huffhead[2];
bits[0] = 0; // Because table starts from 1
int hufflen = BEH(huffhead[0]);
if ((self->ix + hufflen) >= self->datalen) return ret;
#ifdef SLOW_HUFF
u8* huffval = calloc(hufflen - 19, sizeof(u8));
if (huffval == NULL) return LJ92_ERROR_NO_MEMORY;
self->huffval = huffval;
for (int hix = 0; hix < (hufflen - 19); hix++) {
huffval[hix] = self->data[self->ix + 19 + hix];
#ifdef LJ92_DEBUG
TINY_DNG_DPRINTF("huffval[%d]=%d\n", hix, huffval[hix]);
#endif
}
self->ix += hufflen;
// Generate huffman table
int k = 0;
int i = 1;
int j = 1;
int huffsize_needed = 1;
// First calculate how long huffsize needs to be
while (i <= 16) {
while (j <= bits[i]) {
huffsize_needed++;
k = k + 1;
j = j + 1;
}
i = i + 1;
j = 1;
}
// Now allocate and do it
int* huffsize = calloc(huffsize_needed, sizeof(int));
if (huffsize == NULL) return LJ92_ERROR_NO_MEMORY;
self->huffsize = huffsize;
k = 0;
i = 1;
j = 1;
// First calculate how long huffsize needs to be
int hsix = 0;
while (i <= 16) {
while (j <= bits[i]) {
huffsize[hsix++] = i;
k = k + 1;
j = j + 1;
}
i = i + 1;
j = 1;
}
huffsize[hsix++] = 0;
// Calculate the size of huffcode array
int huffcode_needed = 0;
k = 0;
int code = 0;
int si = huffsize[0];
while (1) {
while (huffsize[k] == si) {
huffcode_needed++;
code = code + 1;
k = k + 1;
}
if (huffsize[k] == 0) break;
while (huffsize[k] != si) {
code = code << 1;
si = si + 1;
}
}
// Now fill it
int* huffcode = calloc(huffcode_needed, sizeof(int));
if (huffcode == NULL) return LJ92_ERROR_NO_MEMORY;
self->huffcode = huffcode;
int hcix = 0;
k = 0;
code = 0;
si = huffsize[0];
while (1) {
while (huffsize[k] == si) {
huffcode[hcix++] = code;
code = code + 1;
k = k + 1;
}
if (huffsize[k] == 0) break;
while (huffsize[k] != si) {
code = code << 1;
si = si + 1;
}
}
i = 0;
j = 0;
int* maxcode = calloc(17, sizeof(int));
if (maxcode == NULL) return LJ92_ERROR_NO_MEMORY;
self->maxcode = maxcode;
int* mincode = calloc(17, sizeof(int));
if (mincode == NULL) return LJ92_ERROR_NO_MEMORY;
self->mincode = mincode;
int* valptr = calloc(17, sizeof(int));
if (valptr == NULL) return LJ92_ERROR_NO_MEMORY;
self->valptr = valptr;
while (1) {
while (1) {
i++;
if (i > 16) break;
if (bits[i] != 0) break;
maxcode[i] = -1;
}
if (i > 16) break;
valptr[i] = j;
mincode[i] = huffcode[j];
j = j + bits[i] - 1;
maxcode[i] = huffcode[j];
j++;
}
free(huffsize);
self->huffsize = NULL;
free(huffcode);
self->huffcode = NULL;
ret = LJ92_ERROR_NONE;
#else
/* Calculate huffman direct lut */
// How many bits in the table - find highest entry
u8* huffvals = &self->data[self->ix + 19];
int maxbits = 16;
while (maxbits > 0) {
if (bits[maxbits]) break;
maxbits--;
}
self->huffbits[self->num_huff_idx] = maxbits;
TINY_DNG_DPRINTF("huffbuts[%d] = %d\n", self->num_huff_idx, maxbits);
/* Now fill the lut */
u16* hufflut = (u16*)malloc((1 << maxbits) * sizeof(u16));
// TINY_DNG_DPRINTF("maxbits = %d\n", maxbits);
if (hufflut == NULL) return LJ92_ERROR_NO_MEMORY;
self->hufflut[self->num_huff_idx] = hufflut;
int i = 0;
int hv = 0;
int rv = 0;
int vl = 0; // i
int hcode;
int bitsused = 1;
#ifdef LJ92_DEBUG
TINY_DNG_DPRINTF("%04x:%x:%d:%x\n", i, huffvals[hv], bitsused,
1 << (maxbits - bitsused));
#endif
while (i < 1 << maxbits) {
if (bitsused > maxbits) {
break; // Done. Should never get here!
}
if (vl >= bits[bitsused]) {
bitsused++;
vl = 0;
continue;
}
if (rv == 1 << (maxbits - bitsused)) {
rv = 0;
vl++;
hv++;
#ifdef LJ92_DEBUG
TINY_DNG_DPRINTF("%04x:%x:%d:%x\n", i, huffvals[hv], bitsused,
1 << (maxbits - bitsused));
#endif
continue;
}
hcode = huffvals[hv];
hufflut[i] = hcode << 8 | bitsused;
TINY_DNG_DPRINTF("idx[%d] hufflut[%d] = %d(bitsused = %d, hcode = %d\n",self->num_huff_idx, i, hufflut[i], bitsused,hcode);
i++;
rv++;
}
ret = LJ92_ERROR_NONE;
#endif
self->num_huff_idx++;
return ret;
}
static int parseSof3(ljp* self) {
if (self->ix + 6 >= self->datalen) return LJ92_ERROR_CORRUPT;
self->y = BEH(self->data[self->ix + 3]);
self->x = BEH(self->data[self->ix + 5]);
self->bits = self->data[self->ix + 2];
self->components = self->data[self->ix + 7];
self->ix += BEH(self->data[self->ix]);
if ((self->components >= 1) && (self->components < 6)) {
// ok
} else {
// Invalid number of components.
return LJ92_ERROR_CORRUPT;
}
//TINY_DNG_ASSERT(self->components >= 1 && self->components < 6,
// "Invalid number of components.");
return LJ92_ERROR_NONE;
}
static int parseBlock(ljp* self, int marker) {
(void)marker;
self->ix += BEH(self->data[self->ix]);
if (self->ix >= self->datalen) {
TINY_DNG_DPRINTF("parseBlock: ix %d, datalen %d\n", self->ix,
self->datalen);
return LJ92_ERROR_CORRUPT;
}
return LJ92_ERROR_NONE;
}
#ifdef SLOW_HUFF
static int nextbit(ljp* self) {
u32 b = self->b;
if (self->cnt == 0) {
u8* data = &self->data[self->ix];
u32 next = *data++;
b = next;
if (next == 0xff) {
data++;
self->ix++;
}
self->ix++;
self->cnt = 8;
}
int bit = b >> 7;
self->cnt--;
self->b = (b << 1) & 0xFF;
return bit;
}
static int decode(ljp* self) {
int i = 1;
int code = nextbit(self);
while (code > self->maxcode[i]) {
i++;
code = (code << 1) + nextbit(self);
}
int j = self->valptr[i];
j = j + code - self->mincode[i];
int value = self->huffval[j];
return value;
}
static int receive(ljp* self, int ssss) {
int i = 0;
int v = 0;
while (i != ssss) {
i++;
v = (v << 1) + nextbit(self);
}
return v;
}
static int extend(ljp* self, int v, int t) {
int vt = 1 << (t - 1);
if (v < vt) {
vt = (-1 << t) + 1;
v = v + vt;
}
return v;
}
#endif
inline static int nextdiff(ljp* self, int component_idx, int Px, int *errcode) {
(void)Px;
#ifdef SLOW_HUFF
int t = decode(self);
int diff = receive(self, t);
diff = extend(self, diff, t);
// TINY_DNG_DPRINTF("%d %d %d %x\n",Px+diff,Px,diff,t);//,index,usedbits);
#else
if (component_idx <= self->num_huff_idx) {
// OK
} else {
// "Invalid huff index.");
if (errcode) {
(*errcode) = LJ92_ERROR_CORRUPT;
}
return 0;
}
//TINY_DNG_ASSERT(component_idx <= self->num_huff_idx, "Invalid huff index.");
u32 b = self->b;
int cnt = self->cnt;
int huffbits = self->huffbits[component_idx];
int ix = self->ix;
int next;
while (cnt < huffbits) {
next = *(u16*)&self->data[ix];
int one = next & 0xFF;
int two = next >> 8;
b = (b << 16) | (one << 8) | two;
cnt += 16;
ix += 2;
if (one == 0xFF) {
// TINY_DNG_DPRINTF("%x %x %x %x %d\n",one,two,b,b>>8,cnt);
b >>= 8;
cnt -= 8;
} else if (two == 0xFF)
ix++;
}
int index = b >> (cnt - huffbits);
// TINY_DNG_DPRINTF("component_idx = %d / %d, index = %d\n", component_idx,
// self->components, index);
u16 ssssused = self->hufflut[component_idx][index];
int usedbits = ssssused & 0xFF;
int t = ssssused >> 8;
self->sssshist[t]++;
cnt -= usedbits;
int keepbitsmask = (1 << cnt) - 1;
b &= keepbitsmask;
while (cnt < t) {
next = *(u16*)&self->data[ix];
int one = next & 0xFF;
int two = next >> 8;
b = (b << 16) | (one << 8) | two;
cnt += 16;
ix += 2;
if (one == 0xFF) {
b >>= 8;
cnt -= 8;
} else if (two == 0xFF)
ix++;
}
cnt -= t;
int diff = b >> cnt;
int vt = 1 << (t - 1);
if (diff < vt) {
vt = (-1 << t) + 1;
diff += vt;
}
keepbitsmask = (1 << cnt) - 1;
self->b = b & keepbitsmask;
self->cnt = cnt;
self->ix = ix;
// TINY_DNG_DPRINTF("%d %d\n",t,diff);
// TINY_DNG_DPRINTF("%d %d %d %x %x %d\n",Px+diff,Px,diff,t,index,usedbits);
#ifdef LJ92_DEBUG
#endif
#endif
return diff;
}
static int parsePred6(ljp* self) {
// TODO: Consider self->components
TINY_DNG_DPRINTF("parsePred6\n");
int ret = LJ92_ERROR_CORRUPT;
self->ix = self->scanstart;
// int compcount = self->data[self->ix+2];
self->ix += BEH(self->data[self->ix]);
self->cnt = 0;
self->b = 0;
int write = self->writelen;
// Now need to decode huffman coded values
int c = 0;
int pixels = self->y * self->x;
u16* out = self->image;
u16* temprow;
u16* thisrow = self->outrow[0];
u16* lastrow = self->outrow[1];
// First pixel predicted from base value
int diff;
int Px;
int col = 0;
int row = 0;
int left = 0;
int linear;
if (self->num_huff_idx <= self->components) {