-
Notifications
You must be signed in to change notification settings - Fork 466
/
Copy pathjp2.c
3448 lines (2938 loc) · 115 KB
/
jp2.c
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
/*
* The copyright in this software is being made available under the 2-clauses
* BSD License, included below. This software may be subject to other third
* party and contributor rights, including patent rights, and no such rights
* are granted under this license.
*
* Copyright (c) 2002-2014, Universite catholique de Louvain (UCL), Belgium
* Copyright (c) 2002-2014, Professor Benoit Macq
* Copyright (c) 2001-2003, David Janssens
* Copyright (c) 2002-2003, Yannick Verschueren
* Copyright (c) 2003-2007, Francois-Olivier Devaux
* Copyright (c) 2003-2014, Antonin Descampe
* Copyright (c) 2005, Herve Drolon, FreeImage Team
* Copyright (c) 2010-2011, Kaori Hagihara
* Copyright (c) 2008, 2011-2012, Centre National d'Etudes Spatiales (CNES), FR
* Copyright (c) 2012, CS Systemes d'Information, France
* 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.
*
* 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 OWNER 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 "opj_includes.h"
/** @defgroup JP2 JP2 - JPEG-2000 file format reader/writer */
/*@{*/
#define OPJ_BOX_SIZE 1024
#define OPJ_UNUSED(x) (void)x
/** @name Local static functions */
/*@{*/
/*static void jp2_write_url(opj_cio_t *cio, char *Idx_file);*/
/**
* Reads a IHDR box - Image Header box
*
* @param p_image_header_data pointer to actual data (already read from file)
* @param jp2 the jpeg2000 file codec.
* @param p_image_header_size the size of the image header
* @param p_manager the user event manager.
*
* @return true if the image header is valid, false else.
*/
static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
OPJ_BYTE *p_image_header_data,
OPJ_UINT32 p_image_header_size,
opj_event_mgr_t * p_manager);
/**
* Writes the Image Header box - Image Header box.
*
* @param jp2 jpeg2000 file codec.
* @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
*
* @return the data being copied.
*/
static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written);
/**
* Writes the Bit per Component box.
*
* @param jp2 jpeg2000 file codec.
* @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
*
* @return the data being copied.
*/
static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written);
/**
* Reads a Bit per Component box.
*
* @param p_bpc_header_data pointer to actual data (already read from file)
* @param jp2 the jpeg2000 file codec.
* @param p_bpc_header_size the size of the bpc header
* @param p_manager the user event manager.
*
* @return true if the bpc header is valid, false else.
*/
static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
OPJ_BYTE * p_bpc_header_data,
OPJ_UINT32 p_bpc_header_size,
opj_event_mgr_t * p_manager);
static OPJ_BOOL opj_jp2_read_cdef(opj_jp2_t * jp2,
OPJ_BYTE * p_cdef_header_data,
OPJ_UINT32 p_cdef_header_size,
opj_event_mgr_t * p_manager);
static void opj_jp2_apply_cdef(opj_image_t *image, opj_jp2_color_t *color,
opj_event_mgr_t *);
/**
* Writes the Channel Definition box.
*
* @param jp2 jpeg2000 file codec.
* @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
*
* @return the data being copied.
*/
static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written);
/**
* Writes the Colour Specification box.
*
* @param jp2 jpeg2000 file codec.
* @param p_nb_bytes_written pointer to store the nb of bytes written by the function.
*
* @return the data being copied.
*/
static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written);
/**
* Writes a FTYP box - File type box
*
* @param cio the stream to write data to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager the user event manager.
*
* @return true if writing was successful.
*/
static OPJ_BOOL opj_jp2_write_ftyp(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
* Reads a a FTYP box - File type box
*
* @param p_header_data the data contained in the FTYP box.
* @param jp2 the jpeg2000 file codec.
* @param p_header_size the size of the data contained in the FTYP box.
* @param p_manager the user event manager.
*
* @return true if the FTYP box is valid.
*/
static OPJ_BOOL opj_jp2_read_ftyp(opj_jp2_t *jp2,
OPJ_BYTE * p_header_data,
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
static OPJ_BOOL opj_jp2_skip_jp2c(opj_jp2_t *jp2,
opj_stream_private_t *stream,
opj_event_mgr_t * p_manager);
/**
* Reads the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
*
* @param p_header_data the data contained in the file header box.
* @param jp2 the jpeg2000 file codec.
* @param p_header_size the size of the data contained in the file header box.
* @param p_manager the user event manager.
*
* @return true if the JP2 Header box was successfully recognized.
*/
static OPJ_BOOL opj_jp2_read_jp2h(opj_jp2_t *jp2,
OPJ_BYTE *p_header_data,
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
/**
* Writes the Jpeg2000 file Header box - JP2 Header box (warning, this is a super box).
*
* @param jp2 the jpeg2000 file codec.
* @param stream the stream to write data to.
* @param p_manager user event manager.
*
* @return true if writing was successful.
*/
static OPJ_BOOL opj_jp2_write_jp2h(opj_jp2_t *jp2,
opj_stream_private_t *stream,
opj_event_mgr_t * p_manager);
/**
* Writes the Jpeg2000 codestream Header box - JP2C Header box. This function must be called AFTER the coding has been done.
*
* @param cio the stream to write data to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager user event manager.
*
* @return true if writing was successful.
*/
static OPJ_BOOL opj_jp2_write_jp2c(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
#ifdef USE_JPIP
/**
* Write index Finder box
* @param cio the stream to write to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager user event manager.
*/
static OPJ_BOOL opj_jpip_write_iptr(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
* Write index Finder box
* @param cio the stream to write to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager user event manager.
*/
static OPJ_BOOL opj_jpip_write_cidx(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
* Write file Index (superbox)
* @param cio the stream to write to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager user event manager.
*/
static OPJ_BOOL opj_jpip_write_fidx(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
#endif /* USE_JPIP */
/**
* Reads a jpeg2000 file signature box.
*
* @param p_header_data the data contained in the signature box.
* @param jp2 the jpeg2000 file codec.
* @param p_header_size the size of the data contained in the signature box.
* @param p_manager the user event manager.
*
* @return true if the file signature box is valid.
*/
static OPJ_BOOL opj_jp2_read_jp(opj_jp2_t *jp2,
OPJ_BYTE * p_header_data,
OPJ_UINT32 p_header_size,
opj_event_mgr_t * p_manager);
/**
* Writes a jpeg2000 file signature box.
*
* @param cio the stream to write data to.
* @param jp2 the jpeg2000 file codec.
* @param p_manager the user event manager.
*
* @return true if writing was successful.
*/
static OPJ_BOOL opj_jp2_write_jp(opj_jp2_t *jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
Apply collected palette data
@param image Image.
@param color Collector for profile, cdef and pclr data.
@param p_manager the user event manager.
@return true in case of success
*/
static OPJ_BOOL opj_jp2_apply_pclr(opj_image_t *image,
opj_jp2_color_t *color,
opj_event_mgr_t * p_manager);
static void opj_jp2_free_pclr(opj_jp2_color_t *color);
/**
* Collect palette data
*
* @param jp2 JP2 handle
* @param p_pclr_header_data FIXME DOC
* @param p_pclr_header_size FIXME DOC
* @param p_manager
*
* @return Returns true if successful, returns false otherwise
*/
static OPJ_BOOL opj_jp2_read_pclr(opj_jp2_t *jp2,
OPJ_BYTE * p_pclr_header_data,
OPJ_UINT32 p_pclr_header_size,
opj_event_mgr_t * p_manager);
/**
* Collect component mapping data
*
* @param jp2 JP2 handle
* @param p_cmap_header_data FIXME DOC
* @param p_cmap_header_size FIXME DOC
* @param p_manager FIXME DOC
*
* @return Returns true if successful, returns false otherwise
*/
static OPJ_BOOL opj_jp2_read_cmap(opj_jp2_t * jp2,
OPJ_BYTE * p_cmap_header_data,
OPJ_UINT32 p_cmap_header_size,
opj_event_mgr_t * p_manager);
/**
* Reads the Color Specification box.
*
* @param p_colr_header_data pointer to actual data (already read from file)
* @param jp2 the jpeg2000 file codec.
* @param p_colr_header_size the size of the color header
* @param p_manager the user event manager.
*
* @return true if the bpc header is valid, false else.
*/
static OPJ_BOOL opj_jp2_read_colr(opj_jp2_t *jp2,
OPJ_BYTE * p_colr_header_data,
OPJ_UINT32 p_colr_header_size,
opj_event_mgr_t * p_manager);
/*@}*/
/*@}*/
/**
* Sets up the procedures to do on writing header after the codestream.
* Developers wanting to extend the library can add their own writing procedures.
*/
static OPJ_BOOL opj_jp2_setup_end_header_writing(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
/**
* Sets up the procedures to do on reading header after the codestream.
* Developers wanting to extend the library can add their own writing procedures.
*/
static OPJ_BOOL opj_jp2_setup_end_header_reading(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
/**
* Reads a jpeg2000 file header structure.
*
* @param jp2 the jpeg2000 file header structure.
* @param stream the stream to read data from.
* @param p_manager the user event manager.
*
* @return true if the box is valid.
*/
static OPJ_BOOL opj_jp2_read_header_procedure(opj_jp2_t *jp2,
opj_stream_private_t *stream,
opj_event_mgr_t * p_manager);
/**
* Executes the given procedures on the given codec.
*
* @param p_procedure_list the list of procedures to execute
* @param jp2 the jpeg2000 file codec to execute the procedures on.
* @param stream the stream to execute the procedures on.
* @param p_manager the user manager.
*
* @return true if all the procedures were successfully executed.
*/
static OPJ_BOOL opj_jp2_exec(opj_jp2_t * jp2,
opj_procedure_list_t * p_procedure_list,
opj_stream_private_t *stream,
opj_event_mgr_t * p_manager);
/**
* Reads a box header. The box is the way data is packed inside a jpeg2000 file structure.
*
* @param cio the input stream to read data from.
* @param box the box structure to fill.
* @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
* @param p_manager user event manager.
*
* @return true if the box is recognized, false otherwise
*/
static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
OPJ_UINT32 * p_number_bytes_read,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
* Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
* are valid. Developers wanting to extend the library can add their own validation procedures.
*/
static OPJ_BOOL opj_jp2_setup_encoding_validation(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
/**
* Sets up the procedures to do on writing header. Developers wanting to extend the library can add their own writing procedures.
*/
static OPJ_BOOL opj_jp2_setup_header_writing(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
static OPJ_BOOL opj_jp2_default_validation(opj_jp2_t * jp2,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager);
/**
* Finds the image execution function related to the given box id.
*
* @param p_id the id of the handler to fetch.
*
* @return the given handler or NULL if it could not be found.
*/
static const opj_jp2_header_handler_t * opj_jp2_img_find_handler(
OPJ_UINT32 p_id);
/**
* Finds the execution function related to the given box id.
*
* @param p_id the id of the handler to fetch.
*
* @return the given handler or NULL if it could not be found.
*/
static const opj_jp2_header_handler_t * opj_jp2_find_handler(OPJ_UINT32 p_id);
static const opj_jp2_header_handler_t jp2_header [] = {
{JP2_JP, opj_jp2_read_jp},
{JP2_FTYP, opj_jp2_read_ftyp},
{JP2_JP2H, opj_jp2_read_jp2h}
};
static const opj_jp2_header_handler_t jp2_img_header [] = {
{JP2_IHDR, opj_jp2_read_ihdr},
{JP2_COLR, opj_jp2_read_colr},
{JP2_BPCC, opj_jp2_read_bpcc},
{JP2_PCLR, opj_jp2_read_pclr},
{JP2_CMAP, opj_jp2_read_cmap},
{JP2_CDEF, opj_jp2_read_cdef}
};
/**
* Reads a box header. The box is the way data is packed inside a jpeg2000 file structure. Data is read from a character string
*
* @param box the box structure to fill.
* @param p_data the character string to read data from.
* @param p_number_bytes_read pointer to an int that will store the number of bytes read from the stream (shoul usually be 2).
* @param p_box_max_size the maximum number of bytes in the box.
* @param p_manager FIXME DOC
*
* @return true if the box is recognized, false otherwise
*/
static OPJ_BOOL opj_jp2_read_boxhdr_char(opj_jp2_box_t *box,
OPJ_BYTE * p_data,
OPJ_UINT32 * p_number_bytes_read,
OPJ_UINT32 p_box_max_size,
opj_event_mgr_t * p_manager);
/**
* Sets up the validation ,i.e. adds the procedures to launch to make sure the codec parameters
* are valid. Developers wanting to extend the library can add their own validation procedures.
*/
static OPJ_BOOL opj_jp2_setup_decoding_validation(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
/**
* Sets up the procedures to do on reading header.
* Developers wanting to extend the library can add their own writing procedures.
*/
static OPJ_BOOL opj_jp2_setup_header_reading(opj_jp2_t *jp2,
opj_event_mgr_t * p_manager);
/* ----------------------------------------------------------------------- */
static OPJ_BOOL opj_jp2_read_boxhdr(opj_jp2_box_t *box,
OPJ_UINT32 * p_number_bytes_read,
opj_stream_private_t *cio,
opj_event_mgr_t * p_manager)
{
/* read header from file */
OPJ_BYTE l_data_header [8];
/* preconditions */
assert(cio != 00);
assert(box != 00);
assert(p_number_bytes_read != 00);
assert(p_manager != 00);
*p_number_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio, l_data_header, 8,
p_manager);
if (*p_number_bytes_read != 8) {
return OPJ_FALSE;
}
/* process read data */
opj_read_bytes(l_data_header, &(box->length), 4);
opj_read_bytes(l_data_header + 4, &(box->type), 4);
if (box->length == 0) { /* last box */
const OPJ_OFF_T bleft = opj_stream_get_number_byte_left(cio);
if (bleft > (OPJ_OFF_T)(0xFFFFFFFFU - 8U)) {
opj_event_msg(p_manager, EVT_ERROR,
"Cannot handle box sizes higher than 2^32\n");
return OPJ_FALSE;
}
box->length = (OPJ_UINT32)bleft + 8U;
assert((OPJ_OFF_T)box->length == bleft + 8);
return OPJ_TRUE;
}
/* do we have a "special very large box ?" */
/* read then the XLBox */
if (box->length == 1) {
OPJ_UINT32 l_xl_part_size;
OPJ_UINT32 l_nb_bytes_read = (OPJ_UINT32)opj_stream_read_data(cio,
l_data_header, 8, p_manager);
if (l_nb_bytes_read != 8) {
if (l_nb_bytes_read > 0) {
*p_number_bytes_read += l_nb_bytes_read;
}
return OPJ_FALSE;
}
*p_number_bytes_read = 16;
opj_read_bytes(l_data_header, &l_xl_part_size, 4);
if (l_xl_part_size != 0) {
opj_event_msg(p_manager, EVT_ERROR,
"Cannot handle box sizes higher than 2^32\n");
return OPJ_FALSE;
}
opj_read_bytes(l_data_header + 4, &(box->length), 4);
}
return OPJ_TRUE;
}
#if 0
static void jp2_write_url(opj_cio_t *cio, char *Idx_file)
{
OPJ_UINT32 i;
opj_jp2_box_t box;
box.init_pos = cio_tell(cio);
cio_skip(cio, 4);
cio_write(cio, JP2_URL, 4); /* DBTL */
cio_write(cio, 0, 1); /* VERS */
cio_write(cio, 0, 3); /* FLAG */
if (Idx_file) {
for (i = 0; i < strlen(Idx_file); i++) {
cio_write(cio, Idx_file[i], 1);
}
}
box.length = cio_tell(cio) - box.init_pos;
cio_seek(cio, box.init_pos);
cio_write(cio, box.length, 4); /* L */
cio_seek(cio, box.init_pos + box.length);
}
#endif
static OPJ_BOOL opj_jp2_read_ihdr(opj_jp2_t *jp2,
OPJ_BYTE *p_image_header_data,
OPJ_UINT32 p_image_header_size,
opj_event_mgr_t * p_manager)
{
/* preconditions */
assert(p_image_header_data != 00);
assert(jp2 != 00);
assert(p_manager != 00);
if (jp2->comps != NULL) {
opj_event_msg(p_manager, EVT_WARNING,
"Ignoring ihdr box. First ihdr box already read\n");
return OPJ_TRUE;
}
if (p_image_header_size != 14) {
opj_event_msg(p_manager, EVT_ERROR, "Bad image header box (bad size)\n");
return OPJ_FALSE;
}
opj_read_bytes(p_image_header_data, &(jp2->h), 4); /* HEIGHT */
p_image_header_data += 4;
opj_read_bytes(p_image_header_data, &(jp2->w), 4); /* WIDTH */
p_image_header_data += 4;
opj_read_bytes(p_image_header_data, &(jp2->numcomps), 2); /* NC */
p_image_header_data += 2;
if (jp2->h < 1 || jp2->w < 1 || jp2->numcomps < 1) {
opj_event_msg(p_manager, EVT_ERROR,
"Wrong values for: w(%d) h(%d) numcomps(%d) (ihdr)\n",
jp2->w, jp2->h, jp2->numcomps);
return OPJ_FALSE;
}
if ((jp2->numcomps - 1U) >=
16384U) { /* unsigned underflow is well defined: 1U <= jp2->numcomps <= 16384U */
opj_event_msg(p_manager, EVT_ERROR, "Invalid number of components (ihdr)\n");
return OPJ_FALSE;
}
/* allocate memory for components */
jp2->comps = (opj_jp2_comps_t*) opj_calloc(jp2->numcomps,
sizeof(opj_jp2_comps_t));
if (jp2->comps == 0) {
opj_event_msg(p_manager, EVT_ERROR,
"Not enough memory to handle image header (ihdr)\n");
return OPJ_FALSE;
}
opj_read_bytes(p_image_header_data, &(jp2->bpc), 1); /* BPC */
++ p_image_header_data;
opj_read_bytes(p_image_header_data, &(jp2->C), 1); /* C */
++ p_image_header_data;
/* Should be equal to 7 cf. chapter about image header box of the norm */
if (jp2->C != 7) {
opj_event_msg(p_manager, EVT_INFO,
"JP2 IHDR box: compression type indicate that the file is not a conforming JP2 file (%d) \n",
jp2->C);
}
opj_read_bytes(p_image_header_data, &(jp2->UnkC), 1); /* UnkC */
++ p_image_header_data;
opj_read_bytes(p_image_header_data, &(jp2->IPR), 1); /* IPR */
++ p_image_header_data;
jp2->j2k->m_cp.allow_different_bit_depth_sign = (jp2->bpc == 255);
jp2->j2k->ihdr_w = jp2->w;
jp2->j2k->ihdr_h = jp2->h;
jp2->has_ihdr = 1;
return OPJ_TRUE;
}
static OPJ_BYTE * opj_jp2_write_ihdr(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written
)
{
OPJ_BYTE * l_ihdr_data, * l_current_ihdr_ptr;
/* preconditions */
assert(jp2 != 00);
assert(p_nb_bytes_written != 00);
/* default image header is 22 bytes wide */
l_ihdr_data = (OPJ_BYTE *) opj_calloc(1, 22);
if (l_ihdr_data == 00) {
return 00;
}
l_current_ihdr_ptr = l_ihdr_data;
opj_write_bytes(l_current_ihdr_ptr, 22, 4); /* write box size */
l_current_ihdr_ptr += 4;
opj_write_bytes(l_current_ihdr_ptr, JP2_IHDR, 4); /* IHDR */
l_current_ihdr_ptr += 4;
opj_write_bytes(l_current_ihdr_ptr, jp2->h, 4); /* HEIGHT */
l_current_ihdr_ptr += 4;
opj_write_bytes(l_current_ihdr_ptr, jp2->w, 4); /* WIDTH */
l_current_ihdr_ptr += 4;
opj_write_bytes(l_current_ihdr_ptr, jp2->numcomps, 2); /* NC */
l_current_ihdr_ptr += 2;
opj_write_bytes(l_current_ihdr_ptr, jp2->bpc, 1); /* BPC */
++l_current_ihdr_ptr;
opj_write_bytes(l_current_ihdr_ptr, jp2->C, 1); /* C : Always 7 */
++l_current_ihdr_ptr;
opj_write_bytes(l_current_ihdr_ptr, jp2->UnkC,
1); /* UnkC, colorspace unknown */
++l_current_ihdr_ptr;
opj_write_bytes(l_current_ihdr_ptr, jp2->IPR,
1); /* IPR, no intellectual property */
++l_current_ihdr_ptr;
*p_nb_bytes_written = 22;
return l_ihdr_data;
}
static OPJ_BYTE * opj_jp2_write_bpcc(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written
)
{
OPJ_UINT32 i;
/* room for 8 bytes for box and 1 byte for each component */
OPJ_UINT32 l_bpcc_size;
OPJ_BYTE * l_bpcc_data, * l_current_bpcc_ptr;
/* preconditions */
assert(jp2 != 00);
assert(p_nb_bytes_written != 00);
l_bpcc_size = 8 + jp2->numcomps;
l_bpcc_data = (OPJ_BYTE *) opj_calloc(1, l_bpcc_size);
if (l_bpcc_data == 00) {
return 00;
}
l_current_bpcc_ptr = l_bpcc_data;
opj_write_bytes(l_current_bpcc_ptr, l_bpcc_size,
4); /* write box size */
l_current_bpcc_ptr += 4;
opj_write_bytes(l_current_bpcc_ptr, JP2_BPCC, 4); /* BPCC */
l_current_bpcc_ptr += 4;
for (i = 0; i < jp2->numcomps; ++i) {
opj_write_bytes(l_current_bpcc_ptr, jp2->comps[i].bpcc,
1); /* write each component information */
++l_current_bpcc_ptr;
}
*p_nb_bytes_written = l_bpcc_size;
return l_bpcc_data;
}
static OPJ_BOOL opj_jp2_read_bpcc(opj_jp2_t *jp2,
OPJ_BYTE * p_bpc_header_data,
OPJ_UINT32 p_bpc_header_size,
opj_event_mgr_t * p_manager
)
{
OPJ_UINT32 i;
/* preconditions */
assert(p_bpc_header_data != 00);
assert(jp2 != 00);
assert(p_manager != 00);
if (jp2->bpc != 255) {
opj_event_msg(p_manager, EVT_WARNING,
"A BPCC header box is available although BPC given by the IHDR box (%d) indicate components bit depth is constant\n",
jp2->bpc);
}
/* and length is relevant */
if (p_bpc_header_size != jp2->numcomps) {
opj_event_msg(p_manager, EVT_ERROR, "Bad BPCC header box (bad size)\n");
return OPJ_FALSE;
}
/* read info for each component */
for (i = 0; i < jp2->numcomps; ++i) {
opj_read_bytes(p_bpc_header_data, &jp2->comps[i].bpcc,
1); /* read each BPCC component */
++p_bpc_header_data;
}
return OPJ_TRUE;
}
static OPJ_BYTE * opj_jp2_write_cdef(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written)
{
/* room for 8 bytes for box, 2 for n */
OPJ_UINT32 l_cdef_size = 10;
OPJ_BYTE * l_cdef_data, * l_current_cdef_ptr;
OPJ_UINT32 l_value;
OPJ_UINT16 i;
/* preconditions */
assert(jp2 != 00);
assert(p_nb_bytes_written != 00);
assert(jp2->color.jp2_cdef != 00);
assert(jp2->color.jp2_cdef->info != 00);
assert(jp2->color.jp2_cdef->n > 0U);
l_cdef_size += 6U * jp2->color.jp2_cdef->n;
l_cdef_data = (OPJ_BYTE *) opj_malloc(l_cdef_size);
if (l_cdef_data == 00) {
return 00;
}
l_current_cdef_ptr = l_cdef_data;
opj_write_bytes(l_current_cdef_ptr, l_cdef_size, 4); /* write box size */
l_current_cdef_ptr += 4;
opj_write_bytes(l_current_cdef_ptr, JP2_CDEF, 4); /* BPCC */
l_current_cdef_ptr += 4;
l_value = jp2->color.jp2_cdef->n;
opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* N */
l_current_cdef_ptr += 2;
for (i = 0U; i < jp2->color.jp2_cdef->n; ++i) {
l_value = jp2->color.jp2_cdef->info[i].cn;
opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Cni */
l_current_cdef_ptr += 2;
l_value = jp2->color.jp2_cdef->info[i].typ;
opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Typi */
l_current_cdef_ptr += 2;
l_value = jp2->color.jp2_cdef->info[i].asoc;
opj_write_bytes(l_current_cdef_ptr, l_value, 2); /* Asoci */
l_current_cdef_ptr += 2;
}
*p_nb_bytes_written = l_cdef_size;
return l_cdef_data;
}
static OPJ_BYTE * opj_jp2_write_colr(opj_jp2_t *jp2,
OPJ_UINT32 * p_nb_bytes_written
)
{
/* room for 8 bytes for box 3 for common data and variable upon profile*/
OPJ_UINT32 l_colr_size = 11;
OPJ_BYTE * l_colr_data, * l_current_colr_ptr;
/* preconditions */
assert(jp2 != 00);
assert(p_nb_bytes_written != 00);
assert(jp2->meth == 1 || jp2->meth == 2);
switch (jp2->meth) {
case 1 :
l_colr_size += 4; /* EnumCS */
break;
case 2 :
assert(jp2->color.icc_profile_len); /* ICC profile */
l_colr_size += jp2->color.icc_profile_len;
break;
default :
return 00;
}
l_colr_data = (OPJ_BYTE *) opj_calloc(1, l_colr_size);
if (l_colr_data == 00) {
return 00;
}
l_current_colr_ptr = l_colr_data;
opj_write_bytes(l_current_colr_ptr, l_colr_size,
4); /* write box size */
l_current_colr_ptr += 4;
opj_write_bytes(l_current_colr_ptr, JP2_COLR, 4); /* BPCC */
l_current_colr_ptr += 4;
opj_write_bytes(l_current_colr_ptr, jp2->meth, 1); /* METH */
++l_current_colr_ptr;
opj_write_bytes(l_current_colr_ptr, jp2->precedence, 1); /* PRECEDENCE */
++l_current_colr_ptr;
opj_write_bytes(l_current_colr_ptr, jp2->approx, 1); /* APPROX */
++l_current_colr_ptr;
if (jp2->meth ==
1) { /* Meth value is restricted to 1 or 2 (Table I.9 of part 1) */
opj_write_bytes(l_current_colr_ptr, jp2->enumcs, 4);
} /* EnumCS */
else {
if (jp2->meth == 2) { /* ICC profile */
OPJ_UINT32 i;
for (i = 0; i < jp2->color.icc_profile_len; ++i) {
opj_write_bytes(l_current_colr_ptr, jp2->color.icc_profile_buf[i], 1);
++l_current_colr_ptr;
}
}
}
*p_nb_bytes_written = l_colr_size;
return l_colr_data;
}
static void opj_jp2_free_pclr(opj_jp2_color_t *color)
{
opj_free(color->jp2_pclr->channel_sign);
opj_free(color->jp2_pclr->channel_size);
opj_free(color->jp2_pclr->entries);
if (color->jp2_pclr->cmap) {
opj_free(color->jp2_pclr->cmap);
}
opj_free(color->jp2_pclr);
color->jp2_pclr = NULL;
}
static OPJ_BOOL opj_jp2_check_color(opj_image_t *image, opj_jp2_color_t *color,
opj_event_mgr_t *p_manager)
{
OPJ_UINT16 i;
/* testcase 4149.pdf.SIGSEGV.cf7.3501 */
if (color->jp2_cdef) {
opj_jp2_cdef_info_t *info = color->jp2_cdef->info;
OPJ_UINT16 n = color->jp2_cdef->n;
OPJ_UINT32 nr_channels =
image->numcomps; /* FIXME image->numcomps == jp2->numcomps before color is applied ??? */
/* cdef applies to cmap channels if any */
if (color->jp2_pclr && color->jp2_pclr->cmap) {
nr_channels = (OPJ_UINT32)color->jp2_pclr->nr_channels;
}
for (i = 0; i < n; i++) {
if (info[i].cn >= nr_channels) {
opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
info[i].cn, nr_channels);
return OPJ_FALSE;
}
if (info[i].asoc == 65535U) {
continue;
}
if (info[i].asoc > 0 && (OPJ_UINT32)(info[i].asoc - 1) >= nr_channels) {
opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
info[i].asoc - 1, nr_channels);
return OPJ_FALSE;
}
}
/* issue 397 */
/* ISO 15444-1 states that if cdef is present, it shall contain a complete list of channel definitions. */
while (nr_channels > 0) {
for (i = 0; i < n; ++i) {
if ((OPJ_UINT32)info[i].cn == (nr_channels - 1U)) {
break;
}
}
if (i == n) {
opj_event_msg(p_manager, EVT_ERROR, "Incomplete channel definitions.\n");
return OPJ_FALSE;
}
--nr_channels;
}
}
/* testcases 451.pdf.SIGSEGV.f4c.3723, 451.pdf.SIGSEGV.5b5.3723 and
66ea31acbb0f23a2bbc91f64d69a03f5_signal_sigsegv_13937c0_7030_5725.pdf */
if (color->jp2_pclr && color->jp2_pclr->cmap) {
OPJ_UINT16 nr_channels = color->jp2_pclr->nr_channels;
opj_jp2_cmap_comp_t *cmap = color->jp2_pclr->cmap;
OPJ_BOOL *pcol_usage, is_sane = OPJ_TRUE;
/* verify that all original components match an existing one */
for (i = 0; i < nr_channels; i++) {
if (cmap[i].cmp >= image->numcomps) {
opj_event_msg(p_manager, EVT_ERROR, "Invalid component index %d (>= %d).\n",
cmap[i].cmp, image->numcomps);
is_sane = OPJ_FALSE;
}
}
pcol_usage = (OPJ_BOOL *) opj_calloc(nr_channels, sizeof(OPJ_BOOL));
if (!pcol_usage) {
opj_event_msg(p_manager, EVT_ERROR, "Unexpected OOM.\n");
return OPJ_FALSE;
}
/* verify that no component is targeted more than once */
for (i = 0; i < nr_channels; i++) {
OPJ_BYTE mtyp = cmap[i].mtyp;
OPJ_BYTE pcol = cmap[i].pcol;
/* See ISO 15444-1 Table I.14 – MTYPi field values */
if (mtyp != 0 && mtyp != 1) {
opj_event_msg(p_manager, EVT_ERROR,
"Invalid value for cmap[%d].mtyp = %d.\n", i,
mtyp);
is_sane = OPJ_FALSE;
} else if (pcol >= nr_channels) {
opj_event_msg(p_manager, EVT_ERROR,
"Invalid component/palette index for direct mapping %d.\n", pcol);
is_sane = OPJ_FALSE;
} else if (pcol_usage[pcol] && mtyp == 1) {
opj_event_msg(p_manager, EVT_ERROR, "Component %d is mapped twice.\n", pcol);
is_sane = OPJ_FALSE;
} else if (mtyp == 0 && pcol != 0) {
/* I.5.3.5 PCOL: If the value of the MTYP field for this channel is 0, then
* the value of this field shall be 0. */
opj_event_msg(p_manager, EVT_ERROR, "Direct use at #%d however pcol=%d.\n", i,
pcol);
is_sane = OPJ_FALSE;
} else if (mtyp == 1 && pcol != i) {
/* OpenJPEG implementation limitation. See assert(i == pcol); */
/* in opj_jp2_apply_pclr() */
opj_event_msg(p_manager, EVT_ERROR,
"Implementation limitation: for palette mapping, "
"pcol[%d] should be equal to %d, but is equal "
"to %d.\n", i, i, pcol);
is_sane = OPJ_FALSE;
} else {
pcol_usage[pcol] = OPJ_TRUE;
}
}
/* verify that all components are targeted at least once */