-
Notifications
You must be signed in to change notification settings - Fork 690
Expand file tree
/
Copy pathojph_params.cpp
More file actions
2669 lines (2430 loc) · 92.3 KB
/
ojph_params.cpp
File metadata and controls
2669 lines (2430 loc) · 92.3 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
//***************************************************************************/
// This software is released under the 2-Clause BSD license, included
// below.
//
// Copyright (c) 2019, Aous Naman
// Copyright (c) 2019, Kakadu Software Pty Ltd, Australia
// Copyright (c) 2019, The University of New South Wales, Australia
//
// 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.
//
// 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.
//***************************************************************************/
// This file is part of the OpenJPH software implementation.
// File: ojph_params.cpp
// Author: Aous Naman
// Date: 28 August 2019
//***************************************************************************/
#define _USE_MATH_DEFINES
#include <cmath>
#include "ojph_base.h"
#include "ojph_file.h"
#include "ojph_params.h"
#include "ojph_params_local.h"
#include "ojph_message.h"
namespace ojph {
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void param_siz::set_image_extent(point dims)
{
state->set_image_extent(dims);
}
////////////////////////////////////////////////////////////////////////////
void param_siz::set_tile_size(size s)
{
state->set_tile_size(s);
}
////////////////////////////////////////////////////////////////////////////
void param_siz::set_image_offset(point offset)
{
state->set_image_offset(offset);
}
////////////////////////////////////////////////////////////////////////////
void param_siz::set_tile_offset(point offset)
{
state->set_tile_offset(offset);
}
////////////////////////////////////////////////////////////////////////////
void param_siz::set_num_components(ui32 num_comps)
{
state->set_num_components(num_comps);
}
////////////////////////////////////////////////////////////////////////////
void param_siz::set_component(ui32 comp_num, const point& downsampling,
ui32 bit_depth, bool is_signed)
{
state->set_comp_info(comp_num, downsampling, bit_depth, is_signed);
}
////////////////////////////////////////////////////////////////////////////
point param_siz::get_image_extent() const
{
return point(state->Xsiz, state->Ysiz);
}
////////////////////////////////////////////////////////////////////////////
point param_siz::get_image_offset() const
{
return point(state->XOsiz, state->YOsiz);
}
////////////////////////////////////////////////////////////////////////////
size param_siz::get_tile_size() const
{
return size(state->XTsiz, state->YTsiz);
}
////////////////////////////////////////////////////////////////////////////
point param_siz::get_tile_offset() const
{
return point(state->XTOsiz, state->YTOsiz);
}
////////////////////////////////////////////////////////////////////////////
ui32 param_siz::get_num_components() const
{
return state->Csiz;
}
////////////////////////////////////////////////////////////////////////////
ui32 param_siz::get_bit_depth(ui32 comp_num) const
{
return state->get_bit_depth(comp_num);
}
////////////////////////////////////////////////////////////////////////////
bool param_siz::is_signed(ui32 comp_num) const
{
return state->is_signed(comp_num);
}
////////////////////////////////////////////////////////////////////////////
point param_siz::get_downsampling(ui32 comp_num) const
{
return state->get_downsampling(comp_num);
}
////////////////////////////////////////////////////////////////////////////
ui32 param_siz::get_recon_width(ui32 comp_num) const
{
return state->get_recon_width(comp_num);
}
////////////////////////////////////////////////////////////////////////////
ui32 param_siz::get_recon_height(ui32 comp_num) const
{
return state->get_recon_height(comp_num);
}
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void param_cod::set_num_decomposition(ui32 num_decompositions)
{
if (num_decompositions > 32)
OJPH_ERROR(0x00050001,
"maximum number of decompositions cannot exceed 32");
state->SPcod.num_decomp = (ui8)num_decompositions;
}
////////////////////////////////////////////////////////////////////////////
void param_cod::set_block_dims(ui32 width, ui32 height)
{
ui32 log_width = 31 - count_leading_zeros(width);
ui32 log_height = 31 - count_leading_zeros(height);
if (width == 0 || width != (1u << log_width)
|| height == 0 || height != (1u << log_height)
|| log_width < 2 || log_height < 2
|| log_width + log_height > 12)
OJPH_ERROR(0x00050011, "incorrect code block dimensions");
state->SPcod.block_width = (ui8)(log_width - 2);
state->SPcod.block_height = (ui8)(log_height - 2);
}
////////////////////////////////////////////////////////////////////////////
void param_cod::set_precinct_size(int num_levels, size* precinct_size)
{
if (num_levels == 0 || precinct_size == NULL)
state->Scod &= 0xFE;
else
{
state->Scod |= 1;
for (int i = 0; i <= state->SPcod.num_decomp; ++i)
{
size t = precinct_size[i < num_levels ? i : num_levels - 1];
ui32 PPx = 31 - count_leading_zeros(t.w);
ui32 PPy = 31 - count_leading_zeros(t.h);
if (t.w == 0 || t.h == 0)
OJPH_ERROR(0x00050021, "precinct width or height cannot be 0");
if (t.w != (1u<<PPx) || t.h != (1u<<PPy))
OJPH_ERROR(0x00050022,
"precinct width and height should be a power of 2");
if (PPx > 15 || PPy > 15)
OJPH_ERROR(0x00050023, "precinct size is too large");
if (i > 0 && (PPx == 0 || PPy == 0))
OJPH_ERROR(0x00050024, "precinct size is too small");
state->SPcod.precinct_size[i] = (ui8)(PPx | (PPy << 4));
}
}
}
////////////////////////////////////////////////////////////////////////////
void param_cod::set_progression_order(const char *name)
{
int prog_order = 0;
size_t len = strlen(name);
if (len == 4)
{
if (strncmp(name, OJPH_PO_STRING_LRCP, 4) == 0)
prog_order = OJPH_PO_LRCP;
else if (strncmp(name, OJPH_PO_STRING_RLCP, 4) == 0)
prog_order = OJPH_PO_RLCP;
else if (strncmp(name, OJPH_PO_STRING_RPCL, 4) == 0)
prog_order = OJPH_PO_RPCL;
else if (strncmp(name, OJPH_PO_STRING_PCRL, 4) == 0)
prog_order = OJPH_PO_PCRL;
else if (strncmp(name, OJPH_PO_STRING_CPRL, 4) == 0)
prog_order = OJPH_PO_CPRL;
else
OJPH_ERROR(0x00050031, "unknown progression order");
}
else
OJPH_ERROR(0x00050032, "improper progression order");
state->SGCod.prog_order = (ui8)prog_order;
}
////////////////////////////////////////////////////////////////////////////
void param_cod::set_color_transform(bool color_transform)
{
state->employ_color_transform(color_transform ? 1 : 0);
}
////////////////////////////////////////////////////////////////////////////
void param_cod::set_reversible(bool reversible)
{
state->set_reversible(reversible);
}
////////////////////////////////////////////////////////////////////////////
param_coc param_cod::get_coc(ui32 component_idx)
{
local::param_cod *p = state->get_coc(component_idx);
if (p == state) // no COC segment marker for this component
p = state->add_coc_object(component_idx);
return param_coc(p);
}
////////////////////////////////////////////////////////////////////////////
ui32 param_cod::get_num_decompositions() const
{
return state->get_num_decompositions();
}
////////////////////////////////////////////////////////////////////////////
size param_cod::get_block_dims() const
{
return state->get_block_dims();
}
////////////////////////////////////////////////////////////////////////////
size param_cod::get_log_block_dims() const
{
return state->get_log_block_dims();
}
////////////////////////////////////////////////////////////////////////////
bool param_cod::is_reversible() const
{
return state->is_reversible();
}
////////////////////////////////////////////////////////////////////////////
size param_cod::get_precinct_size(ui32 level_num) const
{
return state->get_precinct_size(level_num);
}
////////////////////////////////////////////////////////////////////////////
size param_cod::get_log_precinct_size(ui32 level_num) const
{
return state->get_log_precinct_size(level_num);
}
////////////////////////////////////////////////////////////////////////////
int param_cod::get_progression_order() const
{
return state->SGCod.prog_order;
}
////////////////////////////////////////////////////////////////////////////
const char* param_cod::get_progression_order_as_string() const
{
if (state->SGCod.prog_order == OJPH_PO_LRCP)
return OJPH_PO_STRING_LRCP;
else if (state->SGCod.prog_order == OJPH_PO_RLCP)
return OJPH_PO_STRING_RLCP;
else if (state->SGCod.prog_order == OJPH_PO_RPCL)
return OJPH_PO_STRING_RPCL;
else if (state->SGCod.prog_order == OJPH_PO_PCRL)
return OJPH_PO_STRING_PCRL;
else if (state->SGCod.prog_order == OJPH_PO_CPRL)
return OJPH_PO_STRING_CPRL;
else
assert(0);
return "";
}
////////////////////////////////////////////////////////////////////////////
int param_cod::get_num_layers() const
{
return state->SGCod.num_layers;
}
////////////////////////////////////////////////////////////////////////////
bool param_cod::is_using_color_transform() const
{
return state->is_employing_color_transform();
}
////////////////////////////////////////////////////////////////////////////
bool param_cod::packets_may_use_sop() const
{
return state->packets_may_use_sop();
}
////////////////////////////////////////////////////////////////////////////
bool param_cod::packets_use_eph() const
{
return state->packets_use_eph();
}
////////////////////////////////////////////////////////////////////////////
bool param_cod::get_block_vertical_causality() const
{
return state->get_block_vertical_causality();
}
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void param_coc::set_num_decomposition(ui32 num_decompositions)
{ ojph::param_cod(state).set_num_decomposition(num_decompositions); }
////////////////////////////////////////////////////////////////////////////
void param_coc::set_block_dims(ui32 width, ui32 height)
{ ojph::param_cod(state).set_block_dims(width, height); }
////////////////////////////////////////////////////////////////////////////
void param_coc::set_precinct_size(int num_levels, size* precinct_size)
{ ojph::param_cod(state).set_precinct_size(num_levels, precinct_size); }
////////////////////////////////////////////////////////////////////////////
void param_coc::set_reversible(bool reversible)
{ ojph::param_cod(state).set_reversible(reversible); }
////////////////////////////////////////////////////////////////////////////
ui32 param_coc::get_num_decompositions() const
{ return ojph::param_cod(state).get_num_decompositions(); }
////////////////////////////////////////////////////////////////////////////
size param_coc::get_block_dims() const
{ return ojph::param_cod(state).get_block_dims(); }
////////////////////////////////////////////////////////////////////////////
size param_coc::get_log_block_dims() const
{ return ojph::param_cod(state).get_log_block_dims(); }
////////////////////////////////////////////////////////////////////////////
bool param_coc::is_reversible() const
{ return ojph::param_cod(state).is_reversible(); }
////////////////////////////////////////////////////////////////////////////
size param_coc::get_precinct_size(ui32 level_num) const
{ return ojph::param_cod(state).get_precinct_size(level_num); }
////////////////////////////////////////////////////////////////////////////
size param_coc::get_log_precinct_size(ui32 level_num) const
{ return ojph::param_cod(state).get_log_precinct_size(level_num); }
////////////////////////////////////////////////////////////////////////////
bool param_coc::get_block_vertical_causality() const
{ return ojph::param_cod(state).get_block_vertical_causality(); }
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void param_qcd::set_irrev_quant(float delta)
{
state->set_delta(delta);
}
//////////////////////////////////////////////////////////////////////////
void param_qcd::set_irrev_quant(ui32 comp_idx, float delta)
{
state->set_delta(comp_idx, delta);
}
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
void param_nlt::set_nonlinear_transform(ui32 comp_num, ui8 nl_type)
{
state->set_nonlinear_transform(comp_num, nl_type);
}
////////////////////////////////////////////////////////////////////////////
bool param_nlt::get_nonlinear_transform(ui32 comp_num, ui8& bit_depth,
bool& is_signed, ui8& nl_type) const
{
return state->get_nonlinear_transform(comp_num, bit_depth, is_signed,
nl_type);
}
////////////////////////////////////////////////////////////////////////////
//
//
//
//
//
////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
void comment_exchange::set_string(const char* str)
{
size_t t = strlen(str);
if (len > 65531)
OJPH_ERROR(0x000500C1,
"COM marker string length cannot be larger than 65531");
this->data = str;
this->len = (ui16)t;
this->Rcom = 1;
}
//////////////////////////////////////////////////////////////////////////
void comment_exchange::set_data(const char* data, ui16 len)
{
if (len > 65531)
OJPH_ERROR(0x000500C2,
"COM marker string length cannot be larger than 65531");
this->data = data;
this->len = len;
this->Rcom = 0;
}
//////////////////////////////////////////////////////////////////////////
//
//
// LOCAL
//
//
//////////////////////////////////////////////////////////////////////////
namespace local {
//////////////////////////////////////////////////////////////////////////
static inline
ui16 swap_byte(ui16 t)
{
return (ui16)((t << 8) | (t >> 8));
}
//////////////////////////////////////////////////////////////////////////
static inline
ui32 swap_byte(ui32 t)
{
ui32 u = swap_byte((ui16)(t & 0xFFFFu));
u <<= 16;
u |= swap_byte((ui16)(t >> 16));
return u;
}
//////////////////////////////////////////////////////////////////////////
static inline
ui64 swap_byte(ui64 t)
{
ui64 u = swap_byte((ui32)(t & 0xFFFFFFFFu));
u <<= 32;
u |= swap_byte((ui32)(t >> 32));
return u;
}
//////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
//static
class sqrt_energy_gains
{
public:
static float get_gain_l(ui32 num_decomp, bool reversible)
{ return reversible ? gain_5x3_l[num_decomp] : gain_9x7_l[num_decomp]; }
static float get_gain_h(ui32 num_decomp, bool reversible)
{ return reversible ? gain_5x3_h[num_decomp] : gain_9x7_h[num_decomp]; }
private:
static const float gain_9x7_l[34];
static const float gain_9x7_h[34];
static const float gain_5x3_l[34];
static const float gain_5x3_h[34];
};
//////////////////////////////////////////////////////////////////////////
const float sqrt_energy_gains::gain_9x7_l[34] = { 1.0000e+00f,
1.4021e+00f, 2.0304e+00f, 2.9012e+00f, 4.1153e+00f, 5.8245e+00f,
8.2388e+00f, 1.1652e+01f, 1.6479e+01f, 2.3304e+01f, 3.2957e+01f,
4.6609e+01f, 6.5915e+01f, 9.3217e+01f, 1.3183e+02f, 1.8643e+02f,
2.6366e+02f, 3.7287e+02f, 5.2732e+02f, 7.4574e+02f, 1.0546e+03f,
1.4915e+03f, 2.1093e+03f, 2.9830e+03f, 4.2185e+03f, 5.9659e+03f,
8.4371e+03f, 1.1932e+04f, 1.6874e+04f, 2.3864e+04f, 3.3748e+04f,
4.7727e+04f, 6.7496e+04f, 9.5454e+04f };
const float sqrt_energy_gains::gain_9x7_h[34] = { 1.4425e+00f,
1.9669e+00f, 2.8839e+00f, 4.1475e+00f, 5.8946e+00f, 8.3472e+00f,
1.1809e+01f, 1.6701e+01f, 2.3620e+01f, 3.3403e+01f, 4.7240e+01f,
6.6807e+01f, 9.4479e+01f, 1.3361e+02f, 1.8896e+02f, 2.6723e+02f,
3.7792e+02f, 5.3446e+02f, 7.5583e+02f, 1.0689e+03f, 1.5117e+03f,
2.1378e+03f, 3.0233e+03f, 4.2756e+03f, 6.0467e+03f, 8.5513e+03f,
1.2093e+04f, 1.7103e+04f, 2.4187e+04f, 3.4205e+04f, 4.8373e+04f,
6.8410e+04f, 9.6747e+04f, 1.3682e+05f };
const float sqrt_energy_gains::gain_5x3_l[34] = { 1.0000e+00f,
1.2247e+00f, 1.3229e+00f, 1.5411e+00f, 1.7139e+00f, 1.9605e+00f,
2.2044e+00f, 2.5047e+00f, 2.8277e+00f, 3.2049e+00f, 3.6238e+00f,
4.1033e+00f, 4.6423e+00f, 5.2548e+00f, 5.9462e+00f, 6.7299e+00f,
7.6159e+00f, 8.6193e+00f, 9.7544e+00f, 1.1039e+01f, 1.2493e+01f,
1.4139e+01f, 1.6001e+01f, 1.8108e+01f, 2.0493e+01f, 2.3192e+01f,
2.6246e+01f, 2.9702e+01f, 3.3614e+01f, 3.8041e+01f, 4.3051e+01f,
4.8721e+01f, 5.5138e+01f, 6.2399e+01f };
const float sqrt_energy_gains::gain_5x3_h[34] = { 1.0458e+00f,
1.3975e+00f, 1.4389e+00f, 1.7287e+00f, 1.8880e+00f, 2.1841e+00f,
2.4392e+00f, 2.7830e+00f, 3.1341e+00f, 3.5576e+00f, 4.0188e+00f,
4.5532e+00f, 5.1494e+00f, 5.8301e+00f, 6.5963e+00f, 7.4663e+00f,
8.4489e+00f, 9.5623e+00f, 1.0821e+01f, 1.2247e+01f, 1.3860e+01f,
1.5685e+01f, 1.7751e+01f, 2.0089e+01f, 2.2735e+01f, 2.5729e+01f,
2.9117e+01f, 3.2952e+01f, 3.7292e+01f, 4.2203e+01f, 4.7761e+01f,
5.4051e+01f, 6.1170e+01f, 6.9226e+01f };
//////////////////////////////////////////////////////////////////////////
//static
class bibo_gains
{
public:
static float get_bibo_gain_l(ui32 num_decomp, bool reversible)
{ return reversible ? gain_5x3_l[num_decomp] : gain_9x7_l[num_decomp]; }
static float get_bibo_gain_h(ui32 num_decomp, bool reversible)
{ return reversible ? gain_5x3_h[num_decomp] : gain_9x7_h[num_decomp]; }
private:
static const float gain_9x7_l[34];
static const float gain_9x7_h[34];
static const float gain_5x3_l[34];
static const float gain_5x3_h[34];
};
//////////////////////////////////////////////////////////////////////////
const float bibo_gains::gain_9x7_l[34] = { 1.0000e+00f, 1.3803e+00f,
1.3328e+00f, 1.3067e+00f, 1.3028e+00f, 1.3001e+00f, 1.2993e+00f,
1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f, 1.2992e+00f,
1.2992e+00f, 1.2992e+00f };
const float bibo_gains::gain_9x7_h[34] = { 1.2976e+00f, 1.3126e+00f,
1.2757e+00f, 1.2352e+00f, 1.2312e+00f, 1.2285e+00f, 1.2280e+00f,
1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f, 1.2278e+00f,
1.2278e+00f, 1.2278e+00f };
const float bibo_gains::gain_5x3_l[34] = { 1.0000e+00f, 1.5000e+00f,
1.6250e+00f, 1.6875e+00f, 1.6963e+00f, 1.7067e+00f, 1.7116e+00f,
1.7129e+00f, 1.7141e+00f, 1.7145e+00f, 1.7151e+00f, 1.7152e+00f,
1.7155e+00f, 1.7155e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f, 1.7156e+00f,
1.7156e+00f, 1.7156e+00f };
const float bibo_gains::gain_5x3_h[34] = { 2.0000e+00f, 2.5000e+00f,
2.7500e+00f, 2.8047e+00f, 2.8198e+00f, 2.8410e+00f, 2.8558e+00f,
2.8601e+00f, 2.8628e+00f, 2.8656e+00f, 2.8662e+00f, 2.8667e+00f,
2.8669e+00f, 2.8670e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f, 2.8671e+00f,
2.8671e+00f, 2.8671e+00f };
//////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
bool param_siz::write(outfile_base *file)
{
//marker size excluding header
Lsiz = (ui16)(38 + 3 * Csiz);
ui8 buf[4];
bool result = true;
*(ui16*)buf = JP2K_MARKER::SIZ;
*(ui16*)buf = swap_byte(*(ui16*)buf);
result &= file->write(&buf, 2) == 2;
*(ui16*)buf = swap_byte(Lsiz);
result &= file->write(&buf, 2) == 2;
*(ui16*)buf = swap_byte(Rsiz);
result &= file->write(&buf, 2) == 2;
*(ui32*)buf = swap_byte(Xsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(Ysiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(XOsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(YOsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(XTsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(YTsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(XTOsiz);
result &= file->write(&buf, 4) == 4;
*(ui32*)buf = swap_byte(YTOsiz);
result &= file->write(&buf, 4) == 4;
*(ui16*)buf = swap_byte(Csiz);
result &= file->write(&buf, 2) == 2;
for (int c = 0; c < Csiz; ++c)
{
buf[0] = cptr[c].SSiz;
buf[1] = cptr[c].XRsiz;
buf[2] = cptr[c].YRsiz;
result &= file->write(&buf, 3) == 3;
}
return result;
}
//////////////////////////////////////////////////////////////////////////
void param_siz::read(infile_base *file)
{
if (file->read(&Lsiz, 2) != 2)
OJPH_ERROR(0x00050041, "error reading SIZ marker");
Lsiz = swap_byte(Lsiz);
int num_comps = (Lsiz - 38) / 3;
if (Lsiz != 38 + 3 * num_comps)
OJPH_ERROR(0x00050042, "error in SIZ marker length");
if (file->read(&Rsiz, 2) != 2)
OJPH_ERROR(0x00050043, "error reading SIZ marker");
Rsiz = swap_byte(Rsiz);
if ((Rsiz & 0x4000) == 0)
OJPH_ERROR(0x00050044,
"Rsiz bit 14 is not set (this is not a JPH file)");
if ((Rsiz & 0x8000) != 0 && (Rsiz & 0xD5F) != 0)
OJPH_WARN(0x00050001, "Rsiz in SIZ has unimplemented fields");
if (file->read(&Xsiz, 4) != 4)
OJPH_ERROR(0x00050045, "error reading SIZ marker");
Xsiz = swap_byte(Xsiz);
if (file->read(&Ysiz, 4) != 4)
OJPH_ERROR(0x00050046, "error reading SIZ marker");
Ysiz = swap_byte(Ysiz);
ui32 t_XOsiz, t_YOsiz;
if (file->read(&t_XOsiz, 4) != 4)
OJPH_ERROR(0x00050047, "error reading SIZ marker");
if (file->read(&t_YOsiz, 4) != 4)
OJPH_ERROR(0x00050048, "error reading SIZ marker");
set_image_offset(point(swap_byte(t_XOsiz), swap_byte(t_YOsiz)));
ui32 t_XTsiz, t_YTsiz;
if (file->read(&t_XTsiz, 4) != 4)
OJPH_ERROR(0x00050049, "error reading SIZ marker");
if (file->read(&t_YTsiz, 4) != 4)
OJPH_ERROR(0x0005004A, "error reading SIZ marker");
set_tile_size(size(swap_byte(t_XTsiz), swap_byte(t_YTsiz)));
ui32 t_XTOsiz, t_YTOsiz;
if (file->read(&t_XTOsiz, 4) != 4)
OJPH_ERROR(0x0005004B, "error reading SIZ marker");
if (file->read(&t_YTOsiz, 4) != 4)
OJPH_ERROR(0x0005004C, "error reading SIZ marker");
set_tile_offset(point(swap_byte(t_XTOsiz), swap_byte(t_YTOsiz)));
if (file->read(&Csiz, 2) != 2)
OJPH_ERROR(0x0005004D, "error reading SIZ marker");
Csiz = swap_byte(Csiz);
if (Csiz != num_comps)
OJPH_ERROR(0x0005004E, "Csiz does not match the SIZ marker size");
set_num_components(Csiz);
for (int c = 0; c < Csiz; ++c)
{
if (file->read(&cptr[c].SSiz, 1) != 1)
OJPH_ERROR(0x00050051, "error reading SIZ marker");
if (file->read(&cptr[c].XRsiz, 1) != 1)
OJPH_ERROR(0x00050052, "error reading SIZ marker");
if (file->read(&cptr[c].YRsiz, 1) != 1)
OJPH_ERROR(0x00050053, "error reading SIZ marker");
if ((cptr[c].SSiz & 0x7F) > 37)
OJPH_ERROR(0x00050054, "Wrong SIZ-SSiz value of %d", cptr[c].SSiz);
if (cptr[c].XRsiz == 0)
OJPH_ERROR(0x00050055, "Wrong SIZ-XRsiz value of %d", cptr[c].XRsiz);
if (cptr[c].YRsiz == 0)
OJPH_ERROR(0x00050056, "Wrong SIZ-YRsiz value of %d", cptr[c].YRsiz);
}
ws_kern_support_needed = (Rsiz & 0x20) != 0;
dfs_support_needed = (Rsiz & 0x80) != 0;
check_validity();
}
//////////////////////////////////////////////////////////////////////////
point param_siz::get_recon_downsampling(ui32 comp_num) const
{
assert(comp_num < get_num_components());
point factor(1u << skipped_resolutions, 1u << skipped_resolutions);
const param_cod* cdp = cod->get_coc(comp_num);
if (dfs && cdp && cdp->is_dfs_defined()) {
const param_dfs* d = dfs->get_dfs(cdp->get_dfs_index());
factor = d->get_res_downsamp(skipped_resolutions);
}
factor.x *= (ui32)cptr[comp_num].XRsiz;
factor.y *= (ui32)cptr[comp_num].YRsiz;
return factor;
}
//////////////////////////////////////////////////////////////////////////
point param_siz::get_recon_size(ui32 comp_num) const
{
assert(comp_num < get_num_components());
point factor = get_recon_downsampling(comp_num);
point r;
r.x = ojph_div_ceil(Xsiz, factor.x) - ojph_div_ceil(XOsiz, factor.x);
r.y = ojph_div_ceil(Ysiz, factor.y) - ojph_div_ceil(YOsiz, factor.y);
return r;
}
//////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
bool param_cap::write(outfile_base *file)
{
//marker size excluding header
Lcap = 8;
char buf[4];
bool result = true;
*(ui16*)buf = JP2K_MARKER::CAP;
*(ui16*)buf = swap_byte(*(ui16*)buf);
result &= file->write(&buf, 2) == 2;
*(ui16*)buf = swap_byte(Lcap);
result &= file->write(&buf, 2) == 2;
*(ui32*)buf = swap_byte(Pcap);
result &= file->write(&buf, 4) == 4;
*(ui16*)buf = swap_byte(Ccap[0]);
result &= file->write(&buf, 2) == 2;
return result;
}
//////////////////////////////////////////////////////////////////////////
void param_cap::read(infile_base *file)
{
if (file->read(&Lcap, 2) != 2)
OJPH_ERROR(0x00050061, "error reading CAP marker");
Lcap = swap_byte(Lcap);
if (file->read(&Pcap, 4) != 4)
OJPH_ERROR(0x00050062, "error reading CAP marker");
Pcap = swap_byte(Pcap);
ui32 count = population_count(Pcap);
if (Pcap & 0xFFFDFFFF)
OJPH_ERROR(0x00050063,
"error Pcap in CAP has options that are not supported");
if ((Pcap & 0x00020000) == 0)
OJPH_ERROR(0x00050064,
"error Pcap should have its 15th MSB set, Pcap^15. "
" This is not a JPH file");
for (ui32 i = 0; i < count; ++i)
if (file->read(Ccap+i, 2) != 2)
OJPH_ERROR(0x00050065, "error reading CAP marker");
if (Lcap != 6 + 2 * count)
OJPH_ERROR(0x00050066, "error in CAP marker length");
}
//////////////////////////////////////////////////////////////////////////
//
//
//
//
//
//////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////
bool param_cod::is_reversible() const
{
if (SPcod.wavelet_trans <= 1)
return get_wavelet_kern() == local::param_cod::DWT_REV53;
else {
assert(atk != NULL);
return atk->is_reversible();
}
}
//////////////////////////////////////////////////////////////////////////
bool param_cod::write(outfile_base *file)
{
assert(type == COD_MAIN);
//marker size excluding header
Lcod = 12;
Lcod = (ui16)(Lcod + (Scod & 1 ? 1 + SPcod.num_decomp : 0));
ui8 buf[4];
bool result = true;
*(ui16*)buf = JP2K_MARKER::COD;
*(ui16*)buf = swap_byte(*(ui16*)buf);
result &= file->write(&buf, 2) == 2;
*(ui16*)buf = swap_byte(Lcod);
result &= file->write(&buf, 2) == 2;
*(ui8*)buf = Scod;
result &= file->write(&buf, 1) == 1;
*(ui8*)buf = SGCod.prog_order;
result &= file->write(&buf, 1) == 1;
*(ui16*)buf = swap_byte(SGCod.num_layers);
result &= file->write(&buf, 2) == 2;
*(ui8*)buf = SGCod.mc_trans;
result &= file->write(&buf, 1) == 1;
buf[0] = SPcod.num_decomp;
buf[1] = SPcod.block_width;
buf[2] = SPcod.block_height;
buf[3] = SPcod.block_style;
result &= file->write(&buf, 4) == 4;
*(ui8*)buf = SPcod.wavelet_trans;
result &= file->write(&buf, 1) == 1;
if (Scod & 1)
for (int i = 0; i <= SPcod.num_decomp; ++i)
{
*(ui8*)buf = SPcod.precinct_size[i];
result &= file->write(&buf, 1) == 1;
}
return result;
}
//////////////////////////////////////////////////////////////////////////
bool param_cod::write_coc(outfile_base *file, ui32 num_comps)
{
assert(type == COD_MAIN);
bool result = true;
param_cod *p = this->next;
while (p)
{
if (p->comp_idx < num_comps)
result &= p->internal_write_coc(file, num_comps);
p = p->next;
}
return result;
}
//////////////////////////////////////////////////////////////////////////
bool param_cod::internal_write_coc(outfile_base *file, ui32 num_comps)
{
assert(type == COC_MAIN);
//marker size excluding header
Lcod = num_comps < 257 ? 9 : 10;
Lcod = (ui16)(Lcod + (Scod & 1 ? 1 + SPcod.num_decomp : 0));
ui8 buf[4];
bool result = true;
*(ui16*)buf = JP2K_MARKER::COC;
*(ui16*)buf = swap_byte(*(ui16*)buf);
result &= file->write(&buf, 2) == 2;
*(ui16*)buf = swap_byte(Lcod);
result &= file->write(&buf, 2) == 2;
if (num_comps < 257)
{
*(ui8*)buf = (ui8)comp_idx;
result &= file->write(&buf, 1) == 1;
}
else
{
*(ui16*)buf = swap_byte(comp_idx);
result &= file->write(&buf, 2) == 2;
}
*(ui8*)buf = Scod;
result &= file->write(&buf, 1) == 1;
buf[0] = SPcod.num_decomp;
buf[1] = SPcod.block_width;
buf[2] = SPcod.block_height;
buf[3] = SPcod.block_style;
result &= file->write(&buf, 4) == 4;
*(ui8*)buf = SPcod.wavelet_trans;
result &= file->write(&buf, 1) == 1;
if (Scod & 1)
for (int i = 0; i <= SPcod.num_decomp; ++i)
{
*(ui8*)buf = SPcod.precinct_size[i];
result &= file->write(&buf, 1) == 1;
}
return result;
}
//////////////////////////////////////////////////////////////////////////
void param_cod::read(infile_base *file)
{
assert(type == COD_MAIN);
if (file->read(&Lcod, 2) != 2)
OJPH_ERROR(0x00050071, "error reading COD segment");
Lcod = swap_byte(Lcod);
if (file->read(&Scod, 1) != 1)
OJPH_ERROR(0x00050072, "error reading COD segment");
if (file->read(&SGCod.prog_order, 1) != 1)
OJPH_ERROR(0x00050073, "error reading COD segment");
if (file->read(&SGCod.num_layers, 2) != 2)
{ OJPH_ERROR(0x00050074, "error reading COD segment"); }
else
SGCod.num_layers = swap_byte(SGCod.num_layers);
if (file->read(&SGCod.mc_trans, 1) != 1)
OJPH_ERROR(0x00050075, "error reading COD segment");
if (file->read(&SPcod.num_decomp, 1) != 1)
OJPH_ERROR(0x00050076, "error reading COD segment");
if (file->read(&SPcod.block_width, 1) != 1)
OJPH_ERROR(0x00050077, "error reading COD segment");
if (file->read(&SPcod.block_height, 1) != 1)
OJPH_ERROR(0x00050078, "error reading COD segment");
if (file->read(&SPcod.block_style, 1) != 1)
OJPH_ERROR(0x00050079, "error reading COD segment");
if (file->read(&SPcod.wavelet_trans, 1) != 1)
OJPH_ERROR(0x0005007A, "error reading COD segment");
if (get_num_decompositions() > 32
|| SPcod.block_width > 8
|| SPcod.block_height > 8
|| SPcod.block_width + SPcod.block_height > 8
|| (SPcod.block_style & 0x40) != 0x40
|| (SPcod.block_style & 0xB7) != 0x00)
OJPH_ERROR(0x0005007D, "wrong settings in a COD-SPcod parameter");
if ((SPcod.block_style & 0x40) != 0x40
|| (SPcod.block_style & 0xB7) != 0x00)
OJPH_ERROR(0x0005007E, "unsupported settings in a COD-SPcod parameter");
ui8 num_decompositions = get_num_decompositions();
if (Scod & 1) {
for (int i = 0; i <= num_decompositions; ++i) {
if (file->read(&SPcod.precinct_size[i], 1) != 1)
OJPH_ERROR(0x0005007B, "error reading COD segment");
if (i)
if ((SPcod.precinct_size[i] & 0x0F) == 0 ||