-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathj2kmarkers.cpp
More file actions
1991 lines (1816 loc) · 62.7 KB
/
Copy pathj2kmarkers.cpp
File metadata and controls
1991 lines (1816 loc) · 62.7 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
// Copyright (c) 2019 - 2026, Osamu Watanabe
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this
// list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice,
// this list of conditions and the following disclaimer in the documentation
// and/or other materials provided with the distribution.
//
// 3. Neither the name of the copyright holder nor the names of its
// contributors may be used to endorse or promote products derived from
// this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "j2kmarkers.hpp"
#include <cstdio>
#include <cmath>
#include <string>
constexpr uint8_t YCC444 = 0;
constexpr uint8_t YCC420 = 1;
constexpr uint8_t YCC422 = 2;
/********************************************************************************
* j2k_marker_io_base
*******************************************************************************/
void j2k_marker_io_base::set_buf(uint8_t *p) { buf = p; }
OPENHTJ2K_MAYBE_UNUSED uint16_t j2k_marker_io_base::get_marker() const { return this->code; }
uint16_t j2k_marker_io_base::get_length() const { return this->Lmar; }
uint8_t *j2k_marker_io_base::get_buf() { return buf + pos; }
uint8_t j2k_marker_io_base::get_byte() {
assert(pos < Lmar);
uint8_t out = buf[pos];
pos++;
return out;
}
uint16_t j2k_marker_io_base::get_word() {
assert(pos < Lmar - 1);
auto out = static_cast<uint16_t>((get_byte() << 8) + get_byte());
return out;
}
uint32_t j2k_marker_io_base::get_dword() {
assert(pos < Lmar - 3);
uint32_t out = (static_cast<uint32_t>(get_word()) << 16) + static_cast<uint32_t>(get_word());
return out;
}
/********************************************************************************
* SIZ_marker
*******************************************************************************/
SIZ_marker::SIZ_marker(j2c_src_memory &in) : j2k_marker_io_base(_SIZ) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
Rsiz = get_word();
Xsiz = get_dword();
Ysiz = get_dword();
XOsiz = get_dword();
YOsiz = get_dword();
XTsiz = get_dword();
YTsiz = get_dword();
XTOsiz = get_dword();
YTOsiz = get_dword();
Csiz = get_word();
for (unsigned long i = 0; i < Csiz; ++i) {
Ssiz.push_back(get_byte());
XRsiz.push_back(get_byte());
YRsiz.push_back(get_byte());
}
is_set = true;
}
SIZ_marker::SIZ_marker(uint16_t R, uint32_t X, uint32_t Y, uint32_t XO, uint32_t YO, uint32_t XT,
uint32_t YT, uint32_t XTO, uint32_t YTO, uint16_t C, std::vector<uint8_t> &S,
std::vector<uint8_t> &XR, std::vector<uint8_t> &YR, bool needCAP)
: j2k_marker_io_base(_SIZ),
Rsiz(R | static_cast<uint16_t>(needCAP ? 1 << 14 : 0)),
Xsiz(X),
Ysiz(Y),
XOsiz(XO),
YOsiz(YO),
XTsiz(XT),
YTsiz(YT),
XTOsiz(XTO),
YTOsiz(YTO),
Csiz(C) {
Lmar = static_cast<uint16_t>(38 + 3 * C);
for (unsigned long i = 0; i < Csiz; ++i) {
Ssiz.push_back(S[i]);
XRsiz.push_back(XR[i]);
YRsiz.push_back(YR[i]);
}
is_set = true;
}
int SIZ_marker::write(j2c_dst_memory &dst) {
if (!is_set) {
printf("ERROR: illegal attempt to call write() for SIZ_marker not yet set.\n");
throw std::exception();
}
dst.put_word(code);
dst.put_word(Lmar);
dst.put_word(Rsiz);
dst.put_dword(Xsiz);
dst.put_dword(Ysiz);
dst.put_dword(XOsiz);
dst.put_dword(YOsiz);
dst.put_dword(XTsiz);
dst.put_dword(YTsiz);
dst.put_dword(XTOsiz);
dst.put_dword(YTOsiz);
dst.put_word(Csiz);
for (unsigned long i = 0; i < Csiz; ++i) {
dst.put_byte(Ssiz[i]);
dst.put_byte(XRsiz[i]);
dst.put_byte(YRsiz[i]);
}
return EXIT_SUCCESS;
}
bool SIZ_marker::is_signed(uint16_t c) {
assert(c < Csiz);
if (Ssiz[c] & 0x80) {
return true;
} else {
return false;
}
}
uint8_t SIZ_marker::get_bitdepth(uint16_t c) {
assert(c < Csiz);
return static_cast<uint8_t>((Ssiz[c] & 0x7F) + 1);
}
void SIZ_marker::get_image_size(element_siz &siz) const {
siz.x = Xsiz;
siz.y = Ysiz;
}
uint32_t SIZ_marker::get_component_stride(uint16_t c) const {
if (c >= Csiz) {
printf("ERROR: invalid component index\n");
throw std::exception();
}
return Xsiz / XRsiz[c] - XOsiz;
}
void SIZ_marker::get_image_origin(element_siz &siz) const {
siz.x = XOsiz;
siz.y = YOsiz;
}
void SIZ_marker::get_tile_size(element_siz &siz) const {
siz.x = XTsiz;
siz.y = YTsiz;
}
void SIZ_marker::get_tile_origin(element_siz &siz) const {
siz.x = XTOsiz;
siz.y = YTOsiz;
}
void SIZ_marker::get_subsampling_factor(element_siz &siz, uint16_t c) {
siz.x = XRsiz[c];
siz.y = YRsiz[c];
}
uint16_t SIZ_marker::get_num_components() const { return Csiz; }
uint8_t SIZ_marker::get_chroma_format() const {
uint8_t chroma_format = YCC444;
// determine type of chroma subsampling
if (Csiz != 3) {
return chroma_format;
} else {
if (XRsiz[1] == 2 && XRsiz[2] == 2) {
if (YRsiz[1] == 2 && YRsiz[2] == 2) {
chroma_format = YCC420;
}
if (YRsiz[1] == 1 && YRsiz[2] == 1) {
chroma_format = YCC422;
}
}
}
return chroma_format;
}
/********************************************************************************
* CAP_marker
*******************************************************************************/
CAP_marker::CAP_marker() : j2k_marker_io_base(_CAP), Pcap(0), Ccap{0} { Lmar = 6; }
CAP_marker::CAP_marker(j2c_src_memory &in) : j2k_marker_io_base(_CAP), Ccap{0} {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
unsigned long n = (Lmar - 6U) / 2U;
Pcap = get_dword();
for (int i = 0; i < 32; ++i) {
if (Pcap & static_cast<uint32_t>(1 << (31 - i))) {
Ccap[i] = get_word();
n--;
}
// } else {
// Ccap[i] = 0;
// }
// if (Ccap[i]) {
// n--;
// }
}
if (n != 0) {
printf("ERROR: Lcap and number of Ccap does not match\n");
throw std::exception();
}
is_set = true;
}
OPENHTJ2K_MAYBE_UNUSED uint32_t CAP_marker::get_Pcap() const { return Pcap; }
uint16_t CAP_marker::get_Ccap(uint8_t n) {
assert(n < 32);
return Ccap[n - 1];
}
void CAP_marker::set_Ccap(uint16_t val, uint8_t part) {
assert(part > 0 && part < 33);
Ccap[part - 1] = val;
set_Pcap(part);
}
void CAP_marker::set_Pcap(uint8_t part) {
// implemented only for Part 15
Pcap |= static_cast<uint32_t>(1 << (32 - part));
Lmar++;
Lmar++;
is_set = true;
}
int CAP_marker::write(j2c_dst_memory &dst) {
assert(is_set == true);
dst.put_word(code);
dst.put_word(Lmar);
dst.put_dword(Pcap);
for (uint8_t n = 0; n < 32; ++n) {
if (Pcap & static_cast<uint32_t>(1 << (32 - n - 1))) {
dst.put_word(Ccap[n]);
}
}
return EXIT_SUCCESS;
}
/********************************************************************************
* CPF_marker
*******************************************************************************/
CPF_marker::CPF_marker() : j2k_marker_io_base(_CPF) { Pcpf = {0}; }
CPF_marker::CPF_marker(j2c_src_memory &in) : j2k_marker_io_base(_CPF) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
uint16_t len = 2; // Lcpf
size_t n = static_cast<size_t>(Lmar - len) / 2U;
for (size_t i = 0; i < n; ++i) {
if (i < Pcpf.size()) {
Pcpf[i] = get_word();
} else {
Pcpf.push_back(get_word());
}
}
is_set = true;
}
/********************************************************************************
* PRF_marker
*******************************************************************************/
PRF_marker::PRF_marker() : j2k_marker_io_base(_PRF), PRFnum(0) {}
PRF_marker::PRF_marker(j2c_src_memory &in) : j2k_marker_io_base(_PRF), PRFnum(0) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
// Lprf = 2 + 2N; N 16-bit Pprf words follow. PRFnum is informational only
// (no codec processing) — see Rec. ITU-T T.800 | ISO/IEC 15444-1, A.5.3:
// PRFnum = 4095 + sum_{i=1..N} Pprf_i * 2^(16*(i-1)).
const size_t n = static_cast<size_t>(Lmar - 2U) / 2U;
uint64_t prfnum = 4095;
for (size_t i = 0; i < n; ++i) {
const uint16_t w = get_word();
Pprf.push_back(w);
if (16U * i < 64U) prfnum += static_cast<uint64_t>(w) << (16U * i);
}
PRFnum = prfnum;
is_set = true;
}
/********************************************************************************
* COD_marker
*******************************************************************************/
COD_marker::COD_marker(j2c_src_memory &in)
: j2k_marker_io_base(_COD), Scod(0), SGcod(0), SPcod({0, 0, 0, 0, 0}) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
uint16_t len = 2; // tmp length including Lcod
Scod = get_byte();
len++;
SGcod = get_dword();
len = static_cast<uint16_t>(len + 4);
{
const size_t splen = static_cast<size_t>(Lmar - len);
if (splen > SPcod.size()) SPcod.resize(splen);
for (size_t i = 0; i < splen; ++i) {
SPcod[i] = get_byte();
}
}
is_set = true;
}
COD_marker::COD_marker(bool is_max_precincts, bool use_SOP, bool use_EPH, uint8_t progression_order,
uint16_t number_of_layers, uint8_t use_color_trafo, uint8_t dwt_levels,
uint8_t cblksizx_log2, uint8_t cblksizy_log2, uint8_t codeblock_style,
uint8_t reversible_flag, std::vector<uint8_t> PPx, std::vector<uint8_t> PPy)
: j2k_marker_io_base(_COD), Scod(0), SGcod(0), SPcod({0, 0, 0, 0, 0}) {
Lmar = static_cast<uint16_t>((is_max_precincts) ? 12 : 13 + dwt_levels);
if (!is_max_precincts) {
Scod |= 0x01;
}
if (use_SOP) {
Scod |= 0x02;
}
if (use_EPH) {
Scod |= 0x04;
}
// Scod += (is_max_precincts) ? 0 : 1;
// Scod += (use_SOP) ? 2 : 0;
// Scod += (use_EPH) ? 4 : 0;
SGcod += static_cast<uint32_t>(progression_order) << 24;
SGcod += static_cast<uint32_t>(number_of_layers) << 8;
SGcod += use_color_trafo;
SPcod[0] = dwt_levels;
SPcod[1] = cblksizx_log2;
SPcod[2] = cblksizy_log2;
SPcod[3] = codeblock_style;
SPcod[4] = reversible_flag;
if (PPx.size() != PPy.size()) {
printf(
"ERROR: Length of parameters to specify horizontal and vertical precinct size shall be the "
"same.\n");
throw std::exception();
}
size_t PPlength = PPx.size();
uint8_t last_PPx = '\0', last_PPy = '\0';
if (!is_max_precincts) {
std::vector<uint8_t> tmpPP;
tmpPP.reserve(static_cast<size_t>(dwt_levels) + 1);
for (size_t i = 0; i <= dwt_levels; ++i) {
if (i < PPlength) {
last_PPx = PPx[i];
last_PPy = PPy[i];
}
tmpPP.push_back(static_cast<unsigned char>(last_PPx + (last_PPy << 4)));
}
SPcod.reserve(SPcod.size() + static_cast<size_t>(dwt_levels) + 1);
for (size_t i = 0; i <= dwt_levels; ++i) {
SPcod.push_back(tmpPP[dwt_levels - i]);
}
}
is_set = true;
}
int COD_marker::write(j2c_dst_memory &dst) {
assert(is_set == true);
dst.put_word(code);
dst.put_word(Lmar);
dst.put_byte(Scod);
dst.put_dword(SGcod);
for (unsigned char &i : SPcod) {
dst.put_byte(i);
}
return EXIT_SUCCESS;
}
bool COD_marker::is_maximum_precincts() const { return (Scod & 1) == 0; }
bool COD_marker::is_use_SOP() const { return (Scod & 2) != 0; }
bool COD_marker::is_use_EPH() const { return (Scod & 4) != 0; }
uint8_t COD_marker::get_progression_order() const { return static_cast<uint8_t>(SGcod >> 24); }
uint16_t COD_marker::get_number_of_layers() const { return static_cast<uint16_t>((SGcod >> 8) & 0xFFFF); }
uint8_t COD_marker::use_color_trafo() const { return static_cast<uint8_t>(SGcod & 0xFF); }
uint8_t COD_marker::get_dwt_levels() { return SPcod[0]; }
void COD_marker::get_codeblock_size(element_siz &out) {
out.x = static_cast<uint32_t>(1 << (SPcod[1] + 2));
out.y = static_cast<uint32_t>(1 << (SPcod[2] + 2));
}
void COD_marker::get_precinct_size(element_siz &out, uint8_t resolution) {
if (is_maximum_precincts()) {
out.x = 15;
out.y = 15;
} else {
out.x = (SPcod[5U + resolution] & 0x0F);
out.y = (SPcod[5U + resolution] & 0xF0) >> 4;
}
}
uint8_t COD_marker::get_Cmodes() { return SPcod[3]; }
uint8_t COD_marker::get_transformation() { return SPcod[4]; }
/********************************************************************************
* COC_marker
*******************************************************************************/
COC_marker::COC_marker() : j2k_marker_io_base(_COC) {
Ccoc = 0;
Scoc = 0;
SPcoc = {0, 0, 0, 0, 0};
}
COC_marker::COC_marker(j2c_src_memory &in, uint16_t Csiz) : j2k_marker_io_base(_COC) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
uint16_t len = 2; // tmp length including Lcoc
if (Csiz < 257) {
Ccoc = get_byte();
len++;
} else {
Ccoc = get_word();
len++;
len++;
}
Scoc = get_byte();
len++;
for (size_t i = 0; i < static_cast<size_t>(Lmar - len); ++i) {
if (i < SPcoc.size()) {
SPcoc[i] = get_byte();
} else {
SPcoc.push_back(get_byte());
}
}
is_set = true;
}
uint16_t COC_marker::get_component_index() const { return Ccoc; }
bool COC_marker::is_maximum_precincts() const { return (Scoc & 1) == 0; }
bool COC_marker::is_dfs_defined() const { return (SPcoc[0] & 0x80) != 0; }
uint8_t COC_marker::get_dfs_index() const { return SPcoc[0] & 0x0F; }
// When DFS is active, SPcoc[0] encodes the DFS index rather than the level count.
// Callers must check is_dfs_defined() and use COD's level count instead.
uint8_t COC_marker::get_dwt_levels() { return SPcoc[0] & 0x1F; }
void COC_marker::get_codeblock_size(element_siz &out) {
out.x = static_cast<uint32_t>(1 << (SPcoc[1] + 2));
out.y = static_cast<uint32_t>(1 << (SPcoc[2] + 2));
}
void COC_marker::get_precinct_size(element_siz &out, uint8_t resolution) {
if (is_maximum_precincts()) {
out.x = 15;
out.y = 15;
} else {
out.x = (SPcoc[5U + resolution] & 0x0F);
out.y = (SPcoc[5U + resolution] & 0xF0) >> 4;
}
}
uint8_t COC_marker::get_Cmodes() { return SPcoc[3]; }
uint8_t COC_marker::get_transformation() { return SPcoc[4]; }
/********************************************************************************
* RGN_marker
*******************************************************************************/
RGN_marker::RGN_marker() : j2k_marker_io_base(_RGN) {
Crgn = 0;
Srgn = 0;
SPrgn = 0;
}
RGN_marker::RGN_marker(j2c_src_memory &in, uint16_t Csiz) : j2k_marker_io_base(_RGN) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
uint16_t len = 2; // tmp length including Lrgn
if (Csiz < 257) {
Crgn = get_byte();
len++;
} else {
Crgn = get_word();
len++;
len++;
}
if (len != 5 && len != 6) {
// TODO: generate Length error (Lrgn shall be 5 or 6).
}
Srgn = get_byte();
assert(Srgn == 0);
SPrgn = get_byte();
is_set = true;
}
uint16_t RGN_marker::get_component_index() const { return Crgn; }
uint8_t RGN_marker::get_ROIshift() const { return SPrgn; }
/********************************************************************************
* DFS_marker (Part 2, 0xFF72)
*******************************************************************************/
DFS_marker::DFS_marker(j2c_src_memory &in) : j2k_marker_io_base(_DFS) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
Sdfs = get_word();
Ids = get_byte();
// Ddfs: 2 bits per level, packed MSB-first, ceil(Ids/4) bytes
const uint8_t nbytes = static_cast<uint8_t>((Ids + 3) / 4);
Ddfs.resize(Ids, DWT_BIDIR);
for (uint8_t b = 0, lvl = 0; b < nbytes; ++b) {
uint8_t byte = get_byte();
for (int shift = 6; shift >= 0 && lvl < Ids; shift -= 2, ++lvl) {
Ddfs[lvl] = static_cast<dwt_type>((byte >> shift) & 0x3);
}
}
// Precompute cumulative decomp depths: hor_depth[k]/ver_depth[k] = number of
// horizontal/vertical splits among the k finest DWT levels.
// DFS level 1 = finest; Ddfs[l-1] = type for DFS level l.
hor_depth[0] = 0;
ver_depth[0] = 0;
for (uint8_t k = 1; k <= Ids; ++k) {
dwt_type t = Ddfs[k - 1];
hor_depth[k] = static_cast<uint8_t>(hor_depth[k - 1] + ((t == DWT_BIDIR || t == DWT_HORZ) ? 1 : 0));
ver_depth[k] = static_cast<uint8_t>(ver_depth[k - 1] + ((t == DWT_BIDIR || t == DWT_VERT) ? 1 : 0));
}
// Precompute qcd_offset[r] = starting flat SPqcd index for resolution r (1..Ids).
// Resolution r=1 is coarsest non-LL, corresponding to DFS level Ids (coarsest).
// QCD orders from coarsest to finest: 3 entries for BIDIR, 1 for HORZ/VERT.
qcd_offset[0] = 0; // r=0 is LL at flat index 0; not used via this offset
uint8_t flat = 1;
for (uint8_t r = 1; r <= Ids; ++r) {
uint8_t dfs_lev = static_cast<uint8_t>(Ids - r + 1); // coarsest first
qcd_offset[r] = flat;
dwt_type t = Ddfs[dfs_lev - 1];
flat = static_cast<uint8_t>(flat + ((t == DWT_BIDIR) ? 3 : (t == DWT_NO) ? 0 : 1));
}
is_set = true;
}
uint8_t DFS_marker::get_index() const { return static_cast<uint8_t>(Sdfs & 0x0F); }
uint8_t DFS_marker::get_num_levels() const { return Ids; }
dwt_type DFS_marker::get_dwt_type(uint8_t level) const {
// level is 1-based (1 = finest DWT decomposition level)
if (level == 0 || level > Ids) return DWT_BIDIR;
return Ddfs[level - 1];
}
uint8_t DFS_marker::get_num_bands(uint8_t r, uint8_t NL) const {
if (r == 0) return 1;
// DFS level (1-indexed from finest); resolution NL = finest → DFS level 1.
uint8_t dfs_lev = static_cast<uint8_t>(NL - r + 1);
if (dfs_lev == 0 || dfs_lev > Ids) return 3;
dwt_type t = Ddfs[dfs_lev - 1];
return (t == DWT_BIDIR) ? 3 : (t == DWT_NO) ? 0 : 1;
}
uint8_t DFS_marker::get_max_safe_reduce() const {
uint8_t n = 0;
for (uint8_t i = 0; i < Ids; ++i) {
if (Ddfs[i] == DWT_BIDIR)
++n;
else
break;
}
return n;
}
/********************************************************************************
* ATK_marker (Part 2, 0xFF79)
*******************************************************************************/
ATK_marker::ATK_marker(j2c_src_memory &in) : j2k_marker_io_base(_ATK), Katk(1.0f), Natk(0) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
Satk = get_word();
if (!is_reversible()) {
// Katk is a big-endian float32
uint32_t bits = get_dword();
memcpy(&Katk, &bits, sizeof(float));
}
Natk = get_byte();
if (is_reversible()) {
printf("WARNING: ATK reversible kernels are not supported\n");
} else {
steps.resize(Natk);
for (uint8_t k = 0; k < Natk; ++k) {
steps[k].mk = get_byte();
uint32_t bits = get_dword();
memcpy(&steps[k].Aatk, &bits, sizeof(float));
}
}
is_set = true;
}
uint8_t ATK_marker::get_index() const { return static_cast<uint8_t>(Satk & 0x0F); }
bool ATK_marker::is_reversible() const { return (Satk & 0x1000) != 0; }
float ATK_marker::get_Katk() const { return Katk; }
uint8_t ATK_marker::get_num_steps() const { return Natk; }
const atk_step &ATK_marker::get_step(uint8_t k) const { return steps[k]; }
QCD_marker::QCD_marker(j2c_src_memory &in) : j2k_marker_io_base(_QCD), Sqcd(0) {
Lmar = in.get_word();
this->set_buf(in.get_buf_pos());
in.get_N_byte(this->get_buf(), Lmar - 2U);
uint16_t len = 2; // tmp length including Lqcd
Sqcd = get_byte();
len++;
if ((Sqcd & 0x1F) == 0) {
// reversible transform
const size_t splen = static_cast<size_t>(Lmar - len);
if (splen > SPqcd.size()) SPqcd.resize(splen);
for (size_t i = 0; i < splen; ++i) {
SPqcd[i] = get_byte();
}
} else {
// irreversible transformation
assert((Lmar - len) % 2 == 0);
const size_t splen = static_cast<size_t>(Lmar - len) / 2U;
if (splen > SPqcd.size()) SPqcd.resize(splen);
for (size_t i = 0; i < splen; ++i) {
SPqcd[i] = get_word();
}
}
is_set = true;
}
QCD_marker::QCD_marker(uint8_t number_of_guardbits, uint8_t dwt_levels, uint8_t transformation,
bool is_derived, uint8_t RI, uint8_t use_ycc, double basestep, uint8_t qfactor)
: j2k_marker_io_base(_QCD), Sqcd(0), is_reversible(transformation == 1) {
const size_t num_bands = static_cast<size_t>(3 * dwt_levels + 1);
std::vector<double> wmse_or_BIBO;
wmse_or_BIBO.reserve(num_bands);
std::vector<uint8_t> epsilon(num_bands, 0);
std::vector<uint16_t> mu(num_bands, 0);
if (is_derived && qfactor != 0xFF) {
is_derived = false;
// TODO: show warning??
}
if (is_reversible) {
Lmar = static_cast<uint16_t>(4 + 3 * dwt_levels);
} else if (is_derived) {
Lmar = 5;
Sqcd = 0x01;
} else {
Lmar = static_cast<uint16_t>(5 + 6 * dwt_levels);
Sqcd = 0x02;
}
assert(number_of_guardbits < 8 && number_of_guardbits >= 0);
Sqcd = static_cast<uint8_t>(Sqcd + (number_of_guardbits << 5U));
const std::vector<double> CDF53L = {-0.125, 0.25, 0.75, 0.25, -0.125};
const std::vector<double> CDF53H = {-0.5, 1, -0.5}; // gain is doubled(x2)
const std::vector<double> D97SL = {-0.091271763114250, -0.057543526228500, 0.591271763114250,
1.115087052457000, 0.5912717631142500, -0.05754352622850,
-0.091271763114250};
const std::vector<double> D97SH = {0.053497514821622, 0.033728236885750,
-0.156446533057980, -0.533728236885750,
1.205898036472720, -0.533728236885750,
-0.156446533057980, 0.033728236885750,
0.053497514821622}; // gain is doubled(x2)
// Square roots of the visual weighting factors for Y content
const std::vector<double> W_b_Y = {0.0901, 0.2758, 0.2758, 0.7018, 0.8378, 0.8378, 1.0000, 1.0000,
1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000, 1.0000};
// The squared Euclidean norm of the multi-component synthesis operator that represents the contribution
// of component 𝑐 (e.g., Y, Cb or Cr) to reconstructed image samples (usually R, G and B)
const double G_c_sqrt[3] = {1.7321, 1.8051, 1.5734};
double gain_low = 0.0, gain_high = 0.0;
std::vector<double> L, H;
L = (is_reversible) ? CDF53L : D97SL;
H = (is_reversible) ? CDF53H : D97SH;
std::vector<double> outL(L);
std::vector<double> outH(H);
// derive BIBO gain for lossless, or
// derive weighted mse for lossy
if (dwt_levels == 0) {
wmse_or_BIBO.push_back(1.0);
} else {
for (uint8_t level = 0; level < dwt_levels; ++level) {
gain_low = 0.0;
gain_high = 0.0;
for (const auto &e : outL) {
gain_low += (is_reversible) ? fabs(e) : e * e;
}
for (const auto &e : outH) {
gain_high += (is_reversible) ? fabs(e) : e * e;
}
wmse_or_BIBO.push_back(gain_high * gain_high); // HH
wmse_or_BIBO.push_back(gain_low * gain_high); // LH
wmse_or_BIBO.push_back(gain_high * gain_low); // HL
std::vector<double> L2, H2;
// upsampling
for (auto &i : outL) {
L2.push_back(i);
L2.push_back(0.0);
}
for (auto &i : outH) {
H2.push_back(i);
H2.push_back(0.0);
}
std::vector<double> tmpL(L.size() + L2.size() - 1, 0.0);
for (size_t i = 0; i < L.size(); ++i) {
for (size_t j = 0; j < L2.size(); ++j) {
tmpL[i + j] += L[i] * L2[j];
}
}
std::vector<double> tmpH(L.size() + H2.size() - 1, 0.0);
for (size_t i = 0; i < L.size(); ++i) {
for (size_t j = 0; j < H2.size(); ++j) {
tmpH[i + j] += L[i] * H2[j];
}
}
outL = tmpL;
outH = tmpH;
}
wmse_or_BIBO.push_back(gain_low * gain_low);
}
// construct epsilon and mu
if (is_reversible) {
// lossless
for (size_t i = 0; i < epsilon.size(); ++i) {
epsilon[epsilon.size() - i - 1] = static_cast<uint8_t>(RI - number_of_guardbits + use_ycc);
while (wmse_or_BIBO[i] > 0.9) {
epsilon[epsilon.size() - i - 1]++;
wmse_or_BIBO[i] *= 0.5;
}
}
} else {
// lossy
if (qfactor == 0xFF) {
for (size_t i = 0; i < epsilon.size(); ++i) {
int32_t exponent, mantissa;
double fval = basestep / sqrt(wmse_or_BIBO[i]);
for (exponent = 0; fval < 1.0; exponent++) {
fval *= 2.0;
}
mantissa = static_cast<int32_t>(floor((fval - 1.0) * static_cast<double>(1 << 11) + 0.5));
if (mantissa >= (1 << 11)) {
mantissa = 0;
exponent--;
}
if (exponent > 31) {
exponent = 31;
mantissa = 0;
}
if (exponent < 0) {
exponent = 0;
mantissa = (1 << 11) - 1;
}
epsilon[epsilon.size() - i - 1] = static_cast<uint8_t>(exponent);
mu[epsilon.size() - i - 1] = static_cast<uint16_t>(mantissa);
}
} else {
// lossy with qfactor: The detail of Qfactor feature is described in HTJ2K white paper at
// https://htj2k.com/wp-content/uploads/white-paper.pdf
double M_Q;
uint8_t t0 = 65, t1 = 97;
const double alpha_T0 = 0.04;
const double alpha_T1 = 0.10;
const double M_T0 = 2.0 * (1.0 - t0 / 100.0);
const double M_T1 = 2.0 * (1.0 - t1 / 100.0);
double alpha_Q = alpha_T0;
double qfactor_power = 1.0;
if (qfactor < 50) {
M_Q = 50.0 / qfactor;
} else {
M_Q = 2.0 * (1.0 - qfactor / 100.0);
}
// adjust the scaling
if (qfactor >= t1) {
qfactor_power = 0.0;
alpha_Q = alpha_T1;
} else if (qfactor > t0) {
qfactor_power = (log(M_T1) - log(M_Q)) / (log(M_T1) - log(M_T0));
alpha_Q = alpha_T1 * pow(alpha_T0 / alpha_T1, qfactor_power);
}
const double eps0 = sqrt(0.5) / static_cast<double>(1 << RI);
double delta_Q = alpha_Q * M_Q + eps0;
double delta_ref = delta_Q * G_c_sqrt[0];
double G_c = G_c_sqrt[0]; // gain of color transform
for (size_t i = 0; i < epsilon.size(); ++i) {
int32_t exponent, mantissa;
double w_b;
// w_b for the LL band (always the last entry) shall be 1.0, as must any extra
// low-frequency bands beyond the 5-level table when dwt_levels > 5.
w_b = (i == epsilon.size() - 1 || i >= W_b_Y.size()) ? 1.0 : pow(W_b_Y[i], qfactor_power);
// qfactor == 0xFF is handled by the branch above, so it is always != 0xFF here.
double fval = delta_ref / (sqrt(wmse_or_BIBO[i]) * w_b * G_c);
for (exponent = 0; fval < 1.0; exponent++) {
fval *= 2.0;
}
mantissa = static_cast<int32_t>(floor((fval - 1.0) * static_cast<double>(1 << 11) + 0.5));
if (mantissa >= (1 << 11)) {
mantissa = 0;
exponent--;
}
if (exponent > 31) {
exponent = 31;
mantissa = 0;
}
if (exponent < 0) {
exponent = 0;
mantissa = (1 << 11) - 1;
}
epsilon[epsilon.size() - i - 1] = static_cast<uint8_t>(exponent);
mu[epsilon.size() - i - 1] = static_cast<uint16_t>(mantissa);
}
}
}
// set SPqcd from epsilon and mu
if (is_derived) {
if (is_reversible) {
printf("ERROR: Derived quantization stepsize is not valid for reversible transform.\n");
throw std::exception();
}
// Quantization style -> Scalar derived (values signalled for LL subband only)
SPqcd.push_back(static_cast<uint16_t>((epsilon[0] << 11) + mu[0]));
} else {
for (size_t i = 0; i < num_bands; ++i) {
if (is_reversible) {
SPqcd.push_back(static_cast<uint16_t>(epsilon[i] << 3));
} else {
// Quantization style -> Scalar expounded (values signalled for each sub-band)
SPqcd.push_back(static_cast<uint16_t>((epsilon[i] << 11) + mu[i]));
}
}
}
is_set = true;
}
int QCD_marker::write(j2c_dst_memory &dst) {
assert(is_set == true);
dst.put_word(code);
dst.put_word(Lmar);
dst.put_byte(Sqcd);
if (is_reversible) {
for (unsigned short &i : SPqcd) {
dst.put_byte(static_cast<uint8_t>(i));
}
} else {
for (unsigned short &i : SPqcd) {
dst.put_word(i);
}
}
return EXIT_SUCCESS;
}
uint8_t QCD_marker::get_quantization_style() const { return (Sqcd & 0x1F); }
uint8_t QCD_marker::get_exponents(uint8_t nb) {
uint8_t qstyle = get_quantization_style();
if (qstyle == 0) {
// lossless
return static_cast<uint8_t>(SPqcd[nb] >> 3);
} else if (qstyle == 1) {
// lossy derived
return static_cast<uint8_t>(SPqcd[0] >> 11);
} else {
// lossy expounded
assert(qstyle == 2);
return static_cast<uint8_t>(SPqcd[nb] >> 11);
}
}
uint16_t QCD_marker::get_mantissas(uint8_t nb) {
uint8_t qstyle = get_quantization_style();
if (qstyle == 1) {
// lossy derived
return (SPqcd[0] & 0x7FF);
} else {
// lossy expounded
assert(qstyle == 2);
return (SPqcd[nb] & 0x7FF);
}
}
uint8_t QCD_marker::get_number_of_guardbits() const { return static_cast<uint8_t>(Sqcd >> 5); }
uint8_t QCD_marker::get_MAGB() {
uint8_t qstyle = get_quantization_style();
uint8_t tmp = (qstyle > 0) ? 0xFF : 0;
for (uint16_t &val : SPqcd) {
if (qstyle == 0) {
tmp = (tmp < (val >> 3)) ? static_cast<uint8_t>(val >> 3) : tmp;
} else {
tmp = (tmp > (val >> 11)) ? static_cast<uint8_t>(val >> 11) : tmp;
}
}
return tmp;
}
/********************************************************************************
* QCC_marker
*******************************************************************************/
QCC_marker::QCC_marker(uint16_t Csiz, uint16_t c, uint8_t number_of_guardbits, uint8_t dwt_levels,
uint8_t transformation, bool is_derived, uint8_t RI, uint8_t use_ycc,
uint8_t qfactor, uint8_t chroma_format)
: j2k_marker_io_base(_QCC), max_components(Csiz), Cqcc(c), Sqcc(0), is_reversible(transformation == 1) {
size_t num_bands = static_cast<size_t>(3 * dwt_levels + 1);
std::vector<double> wmse_or_BIBO;
wmse_or_BIBO.reserve(num_bands);
std::vector<uint8_t> epsilon(num_bands, 0);
std::vector<uint16_t> mu(num_bands, 0);
if (is_derived && qfactor != 0xFF) {
is_derived = false;
// TODO: show warning??
}
if (is_reversible) {
Lmar = static_cast<uint16_t>(5 + 3 * dwt_levels);
} else if (is_derived) {
Lmar = 6;
Sqcc = 0x01;
} else {
Lmar = static_cast<uint16_t>(6 + 6 * dwt_levels);
Sqcc = 0x02;
}
if (max_components >= 257) {
Lmar++;
}
assert(number_of_guardbits < 8 && number_of_guardbits >= 0);
Sqcc = static_cast<uint8_t>(Sqcc + (number_of_guardbits << 5));
const std::vector<double> CDF53L = {-0.125, 0.25, 0.75, 0.25, -0.125};
const std::vector<double> CDF53H = {-0.5, 1, -0.5}; // gain is doubled(x2)
const std::vector<double> D97SL = {-0.091271763114250, -0.057543526228500, 0.591271763114250,
1.115087052457000, 0.5912717631142500, -0.05754352622850,
-0.091271763114250};
const std::vector<double> D97SH = {0.053497514821622, 0.033728236885750,
-0.156446533057980, -0.533728236885750,
1.205898036472720, -0.533728236885750,
-0.156446533057980, 0.033728236885750,
0.053497514821622}; // gain is doubled(x2)
// Square roots of the visual weighting factors for 4:4:4 YCbCr content