-
Notifications
You must be signed in to change notification settings - Fork 455
/
opj_compress.c
2346 lines (2096 loc) · 88.7 KB
/
opj_compress.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) 2006-2007, Parvatha Elangovan
* Copyright (c) 2008, Jerome Fimes, Communications & Systemes <jerome.fimes@c-s.fr>
* Copyright (c) 2011-2012, Centre National d'Etudes Spatiales (CNES), France
* 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <math.h>
#include <assert.h>
#include <limits.h>
#ifdef _WIN32
#include "windirent.h"
#else
#include <dirent.h>
#endif /* _WIN32 */
#ifdef _WIN32
#include <windows.h>
#define strcasecmp _stricmp
#define strncasecmp _strnicmp
#else
#include <strings.h>
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/times.h>
#endif /* _WIN32 */
#include "opj_apps_config.h"
#include "openjpeg.h"
#include "opj_getopt.h"
#include "convert.h"
#include "index.h"
#include "format_defs.h"
#include "opj_string.h"
typedef struct dircnt {
/** Buffer for holding images read from Directory*/
char *filename_buf;
/** Pointer to the buffer*/
char **filename;
} dircnt_t;
typedef struct img_folder {
/** The directory path of the folder containing input images*/
char *imgdirpath;
/** Output format*/
char *out_format;
/** Enable option*/
char set_imgdir;
/** Enable Cod Format for output*/
char set_out_format;
} img_fol_t;
static void encode_help_display(void)
{
fprintf(stdout,
"\nThis is the opj_compress utility from the OpenJPEG project.\n"
"It compresses various image formats with the JPEG 2000 algorithm.\n"
"It has been compiled against openjp2 library v%s.\n\n", opj_version());
fprintf(stdout, "Default encoding options:\n");
fprintf(stdout, "-------------------------\n");
fprintf(stdout, "\n");
fprintf(stdout, " * Lossless\n");
fprintf(stdout, " * 1 tile\n");
fprintf(stdout, " * RGB->YCC conversion if at least 3 components\n");
fprintf(stdout, " * Size of precinct : 2^15 x 2^15 (means 1 precinct)\n");
fprintf(stdout, " * Size of code-block : 64 x 64\n");
fprintf(stdout, " * Number of resolutions: 6\n");
fprintf(stdout, " * No SOP marker in the codestream\n");
fprintf(stdout, " * No EPH marker in the codestream\n");
fprintf(stdout, " * No sub-sampling in x or y direction\n");
fprintf(stdout, " * No mode switch activated\n");
fprintf(stdout, " * Progression order: LRCP\n");
#ifdef FIXME_INDEX
fprintf(stdout, " * No index file\n");
#endif /* FIXME_INDEX */
fprintf(stdout, " * No ROI upshifted\n");
fprintf(stdout, " * No offset of the origin of the image\n");
fprintf(stdout, " * No offset of the origin of the tiles\n");
fprintf(stdout, " * Reversible DWT 5-3\n");
/* UniPG>> */
#ifdef USE_JPWL
fprintf(stdout, " * No JPWL protection\n");
#endif /* USE_JPWL */
/* <<UniPG */
fprintf(stdout, "\n");
fprintf(stdout, "Note:\n");
fprintf(stdout, "-----\n");
fprintf(stdout, "\n");
fprintf(stdout,
"The markers written to the main_header are : SOC SIZ COD QCD COM.\n");
fprintf(stdout, "COD and QCD never appear in the tile_header.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Parameters:\n");
fprintf(stdout, "-----------\n");
fprintf(stdout, "\n");
fprintf(stdout, "Required Parameters (except with -h):\n");
fprintf(stdout, "One of the two options -ImgDir or -i must be used\n");
fprintf(stdout, "\n");
fprintf(stdout, "-i <file>\n");
fprintf(stdout, " Input file\n");
fprintf(stdout,
" Known extensions are <PBM|PGM|PPM|PNM|PAM|PGX|PNG|BMP|TIF|TIFF|RAW|YUV|RAWL|TGA>\n");
fprintf(stdout, " If used, '-o <file>' must be provided\n");
fprintf(stdout, "-o <compressed file>\n");
fprintf(stdout, " Output file (accepted extensions are j2k or jp2).\n");
fprintf(stdout, "-ImgDir <dir>\n");
fprintf(stdout, " Image file Directory path (example ../Images) \n");
fprintf(stdout, " When using this option -OutFor must be used\n");
fprintf(stdout, "-OutFor <J2K|J2C|JP2>\n");
fprintf(stdout, " Output format for compressed files.\n");
fprintf(stdout, " Required only if -ImgDir is used\n");
fprintf(stdout,
"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
fprintf(stdout, " Characteristics of the raw or yuv input image\n");
fprintf(stdout,
" If subsampling is omitted, 1x1 is assumed for all components\n");
fprintf(stdout, " Example: -F 512,512,3,8,u@1x1:2x2:2x2\n");
fprintf(stdout,
" for raw or yuv 512x512 size with 4:2:0 subsampling\n");
fprintf(stdout, " Required only if RAW or RAWL input file is provided.\n");
fprintf(stdout, "\n");
fprintf(stdout, "Optional Parameters:\n");
fprintf(stdout, "\n");
fprintf(stdout, "-h\n");
fprintf(stdout, " Display the help information.\n");
fprintf(stdout, "-r <compression ratio>,<compression ratio>,...\n");
fprintf(stdout, " Different compression ratios for successive layers.\n");
fprintf(stdout,
" The rate specified for each quality level is the desired\n");
fprintf(stdout, " compression factor (use 1 for lossless)\n");
fprintf(stdout, " Decreasing ratios required.\n");
fprintf(stdout, " Example: -r 20,10,1 means \n");
fprintf(stdout, " quality layer 1: compress 20x, \n");
fprintf(stdout, " quality layer 2: compress 10x \n");
fprintf(stdout, " quality layer 3: compress lossless\n");
fprintf(stdout, " Options -r and -q cannot be used together.\n");
fprintf(stdout, "-q <psnr value>,<psnr value>,<psnr value>,...\n");
fprintf(stdout, " Different psnr for successive layers (-q 30,40,50).\n");
fprintf(stdout, " Increasing PSNR values required, except 0 which can\n");
fprintf(stdout, " be used for the last layer to indicate it is lossless.\n");
fprintf(stdout, " Options -r and -q cannot be used together.\n");
fprintf(stdout, "-n <number of resolutions>\n");
fprintf(stdout, " Number of resolutions.\n");
fprintf(stdout,
" It corresponds to the number of DWT decompositions +1. \n");
fprintf(stdout, " Default: 6.\n");
fprintf(stdout, "-TargetBitDepth <target bit depth>\n");
fprintf(stdout, " Target bit depth.\n");
fprintf(stdout, " Number of bits per component to use from input image\n");
fprintf(stdout, " if all bits are unwanted.\n");
fprintf(stdout, " (Currently only implemented for TIF.)\n");
fprintf(stdout, "-b <cblk width>,<cblk height>\n");
fprintf(stdout,
" Code-block size. The dimension must respect the constraint \n");
fprintf(stdout,
" defined in the JPEG-2000 standard (no dimension smaller than 4 \n");
fprintf(stdout,
" or greater than 1024, no code-block with more than 4096 coefficients).\n");
fprintf(stdout, " The maximum value authorized is 64x64. \n");
fprintf(stdout, " Default: 64x64.\n");
fprintf(stdout,
"-c [<prec width>,<prec height>],[<prec width>,<prec height>],...\n");
fprintf(stdout, " Precinct size. Values specified must be power of 2. \n");
fprintf(stdout,
" Multiple records may be supplied, in which case the first record refers\n");
fprintf(stdout,
" to the highest resolution level and subsequent records to lower \n");
fprintf(stdout,
" resolution levels. The last specified record is halved successively for each \n");
fprintf(stdout, " remaining lower resolution levels.\n");
fprintf(stdout, " Default: 2^15x2^15 at each resolution.\n");
fprintf(stdout, "-t <tile width>,<tile height>\n");
fprintf(stdout, " Tile size.\n");
fprintf(stdout,
" Default: the dimension of the whole image, thus only one tile.\n");
fprintf(stdout, "-p <LRCP|RLCP|RPCL|PCRL|CPRL>\n");
fprintf(stdout, " Progression order.\n");
fprintf(stdout, " Default: LRCP.\n");
fprintf(stdout, "-s <subX,subY>\n");
fprintf(stdout, " Subsampling factor.\n");
fprintf(stdout, " Subsampling bigger than 2 can produce error\n");
fprintf(stdout, " Default: no subsampling.\n");
fprintf(stdout,
"-POC <progression order change>/<progression order change>/...\n");
fprintf(stdout, " Progression order change.\n");
fprintf(stdout,
" The syntax of a progression order change is the following:\n");
fprintf(stdout,
" T<tile>=<resStart>,<compStart>,<layerEnd>,<resEnd>,<compEnd>,<progOrder>\n");
fprintf(stdout, " Example: -POC T1=0,0,1,5,3,CPRL/T1=5,0,1,6,3,CPRL\n");
fprintf(stdout, "-SOP\n");
fprintf(stdout, " Write SOP marker before each packet.\n");
fprintf(stdout, "-EPH\n");
fprintf(stdout, " Write EPH marker after each header packet.\n");
fprintf(stdout, "-PLT\n");
fprintf(stdout, " Write PLT marker in tile-part header.\n");
fprintf(stdout, "-TLM\n");
fprintf(stdout, " Write TLM marker in main header.\n");
fprintf(stdout, "-M <key value>\n");
fprintf(stdout, " Mode switch.\n");
fprintf(stdout, " [1=BYPASS(LAZY) 2=RESET 4=RESTART(TERMALL)\n");
fprintf(stdout, " 8=VSC 16=ERTERM(SEGTERM) 32=SEGMARK(SEGSYM)]\n");
fprintf(stdout, " Indicate multiple modes by adding their values.\n");
fprintf(stdout,
" Example: RESTART(4) + RESET(2) + SEGMARK(32) => -M 38\n");
fprintf(stdout, "-TP <R|L|C>\n");
fprintf(stdout, " Divide packets of every tile into tile-parts.\n");
fprintf(stdout,
" Division is made by grouping Resolutions (R), Layers (L)\n");
fprintf(stdout, " or Components (C).\n");
#ifdef FIXME_INDEX
fprintf(stdout, "-x <index file>\n");
fprintf(stdout, " Create an index file.\n");
#endif /*FIXME_INDEX*/
fprintf(stdout, "-ROI c=<component index>,U=<upshifting value>\n");
fprintf(stdout, " Quantization indices upshifted for a component. \n");
fprintf(stdout,
" Warning: This option does not implement the usual ROI (Region of Interest).\n");
fprintf(stdout,
" It should be understood as a 'Component of Interest'. It offers the \n");
fprintf(stdout,
" possibility to upshift the value of a component during quantization step.\n");
fprintf(stdout,
" The value after c= is the component number [0, 1, 2, ...] and the value \n");
fprintf(stdout,
" after U= is the value of upshifting. U must be in the range [0, 37].\n");
fprintf(stdout, "-d <image offset X,image offset Y>\n");
fprintf(stdout, " Offset of the origin of the image.\n");
fprintf(stdout, "-T <tile offset X,tile offset Y>\n");
fprintf(stdout, " Offset of the origin of the tiles.\n");
fprintf(stdout, "-I\n");
fprintf(stdout, " Use the irreversible DWT 9-7.\n");
fprintf(stdout, "-mct <0|1|2>\n");
fprintf(stdout,
" Explicitly specifies if a Multiple Component Transform has to be used.\n");
fprintf(stdout, " 0: no MCT ; 1: RGB->YCC conversion ; 2: custom MCT.\n");
fprintf(stdout,
" If custom MCT, \"-m\" option has to be used (see hereunder).\n");
fprintf(stdout,
" By default, RGB->YCC conversion is used if there are 3 components or more,\n");
fprintf(stdout, " no conversion otherwise.\n");
fprintf(stdout, "-m <file>\n");
fprintf(stdout,
" Use array-based MCT, values are coma separated, line by line\n");
fprintf(stdout,
" No specific separators between lines, no space allowed between values.\n");
fprintf(stdout,
" If this option is used, it automatically sets \"-mct\" option to 2.\n");
fprintf(stdout, "-cinema2K <24|48>\n");
fprintf(stdout, " Digital Cinema 2K profile compliant codestream.\n");
fprintf(stdout,
" Need to specify the frames per second for a 2K resolution.\n");
fprintf(stdout, " Only 24 or 48 fps are currently allowed.\n");
fprintf(stdout, "-cinema4K\n");
fprintf(stdout, " Digital Cinema 4K profile compliant codestream.\n");
fprintf(stdout, " Frames per second not required. Default value is 24fps.\n");
fprintf(stdout, "-IMF <PROFILE>[,mainlevel=X][,sublevel=Y][,framerate=FPS]\n");
fprintf(stdout, " Interoperable Master Format compliant codestream.\n");
fprintf(stdout, " <PROFILE>=2K, 4K, 8K, 2K_R, 4K_R or 8K_R.\n");
fprintf(stdout, " X >= 0 and X <= 11.\n");
fprintf(stdout, " Y >= 0 and Y <= 9.\n");
fprintf(stdout,
" framerate > 0 may be specified to enhance checks and set maximum bit rate when Y > 0.\n");
fprintf(stdout, "-GuardBits value\n");
fprintf(stdout,
" Number of guard bits in [0,7] range. Usually 1 or 2 (default value).\n");
fprintf(stdout, "-jpip\n");
fprintf(stdout, " Write jpip codestream index box in JP2 output file.\n");
fprintf(stdout, " Currently supports only RPCL order.\n");
fprintf(stdout, "-C <comment>\n");
fprintf(stdout, " Add <comment> in the comment marker segment.\n");
if (opj_has_thread_support()) {
fprintf(stdout, "-threads <num_threads|ALL_CPUS>\n"
" Number of threads to use for encoding or ALL_CPUS for all available cores.\n");
}
/* UniPG>> */
#ifdef USE_JPWL
fprintf(stdout, "-W <params>\n");
fprintf(stdout, " Adoption of JPWL (Part 11) capabilities (-W params)\n");
fprintf(stdout,
" The <params> field can be written and repeated in any order:\n");
fprintf(stdout, " [h<tilepart><=type>,s<tilepart><=method>,a=<addr>,...\n");
fprintf(stdout, " ...,z=<size>,g=<range>,p<tilepart:pack><=type>]\n");
fprintf(stdout,
" h selects the header error protection (EPB): 'type' can be\n");
fprintf(stdout,
" [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
fprintf(stdout,
" if 'tilepart' is absent, it is for main and tile headers\n");
fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
fprintf(stdout,
" onwards, up to the next h<> spec, or to the last tilepart\n");
fprintf(stdout, " in the codestream (max. %d specs)\n",
JPWL_MAX_NO_TILESPECS);
fprintf(stdout,
" p selects the packet error protection (EEP/UEP with EPBs)\n");
fprintf(stdout, " to be applied to raw or yuv data: 'type' can be\n");
fprintf(stdout,
" [0=none 1,absent=predefined 16=CRC-16 32=CRC-32 37-128=RS]\n");
fprintf(stdout,
" if 'tilepart:pack' is absent, it is from tile 0, packet 0\n");
fprintf(stdout,
" if 'tilepart:pack' is present, it applies from that tile\n");
fprintf(stdout,
" and that packet onwards, up to the next packet spec\n");
fprintf(stdout,
" or to the last packet in the last tilepart in the stream\n");
fprintf(stdout, " (max. %d specs)\n", JPWL_MAX_NO_PACKSPECS);
fprintf(stdout,
" s enables sensitivity data insertion (ESD): 'method' can be\n");
fprintf(stdout,
" [-1=NO ESD 0=RELATIVE ERROR 1=MSE 2=MSE REDUCTION 3=PSNR\n");
fprintf(stdout, " 4=PSNR INCREMENT 5=MAXERR 6=TSE 7=RESERVED]\n");
fprintf(stdout, " if 'tilepart' is absent, it is for main header only\n");
fprintf(stdout, " if 'tilepart' is present, it applies from that tile\n");
fprintf(stdout,
" onwards, up to the next s<> spec, or to the last tilepart\n");
fprintf(stdout, " in the codestream (max. %d specs)\n",
JPWL_MAX_NO_TILESPECS);
fprintf(stdout, " g determines the addressing mode: <range> can be\n");
fprintf(stdout, " [0=PACKET 1=BYTE RANGE 2=PACKET RANGE]\n");
fprintf(stdout,
" a determines the size of data addressing: <addr> can be\n");
fprintf(stdout,
" 2/4 bytes (small/large codestreams). If not set, auto-mode\n");
fprintf(stdout,
" z determines the size of sensitivity values: <size> can be\n");
fprintf(stdout,
" 1/2 bytes, for the transformed pseudo-floating point value\n");
fprintf(stdout, " ex.:\n");
fprintf(stdout,
" h,h0=64,h3=16,h5=32,p0=78,p0:24=56,p1,p3:0=0,p3:20=32,s=0,\n");
fprintf(stdout, " s0=6,s3=-1,a=0,g=1,z=1\n");
fprintf(stdout, " means\n");
fprintf(stdout,
" predefined EPB in MH, rs(64,32) from TPH 0 to TPH 2,\n");
fprintf(stdout,
" CRC-16 in TPH 3 and TPH 4, CRC-32 in remaining TPHs,\n");
fprintf(stdout, " UEP rs(78,32) for packets 0 to 23 of tile 0,\n");
fprintf(stdout,
" UEP rs(56,32) for packs. 24 to the last of tilepart 0,\n");
fprintf(stdout, " UEP rs default for packets of tilepart 1,\n");
fprintf(stdout, " no UEP for packets 0 to 19 of tilepart 3,\n");
fprintf(stdout,
" UEP CRC-32 for packs. 20 of tilepart 3 to last tilepart,\n");
fprintf(stdout, " relative sensitivity ESD for MH,\n");
fprintf(stdout,
" TSE ESD from TPH 0 to TPH 2, byte range with automatic\n");
fprintf(stdout,
" size of addresses and 1 byte for each sensitivity value\n");
fprintf(stdout, " ex.:\n");
fprintf(stdout, " h,s,p\n");
fprintf(stdout, " means\n");
fprintf(stdout,
" default protection to headers (MH and TPHs) as well as\n");
fprintf(stdout, " data packets, one ESD in MH\n");
fprintf(stdout,
" N.B.: use the following recommendations when specifying\n");
fprintf(stdout, " the JPWL parameters list\n");
fprintf(stdout,
" - when you use UEP, always pair the 'p' option with 'h'\n");
#endif /* USE_JPWL */
/* <<UniPG */
fprintf(stdout, "\n");
#ifdef FIXME_INDEX
fprintf(stdout, "Index structure:\n");
fprintf(stdout, "----------------\n");
fprintf(stdout, "\n");
fprintf(stdout, "Image_height Image_width\n");
fprintf(stdout, "progression order\n");
fprintf(stdout, "Tiles_size_X Tiles_size_Y\n");
fprintf(stdout, "Tiles_nb_X Tiles_nb_Y\n");
fprintf(stdout, "Components_nb\n");
fprintf(stdout, "Layers_nb\n");
fprintf(stdout, "decomposition_levels\n");
fprintf(stdout, "[Precincts_size_X_res_Nr Precincts_size_Y_res_Nr]...\n");
fprintf(stdout, " [Precincts_size_X_res_0 Precincts_size_Y_res_0]\n");
fprintf(stdout, "Main_header_start_position\n");
fprintf(stdout, "Main_header_end_position\n");
fprintf(stdout, "Codestream_size\n");
fprintf(stdout, "\n");
fprintf(stdout, "INFO ON TILES\n");
fprintf(stdout,
"tileno start_pos end_hd end_tile nbparts disto nbpix disto/nbpix\n");
fprintf(stdout,
"Tile_0 start_pos end_Theader end_pos NumParts TotalDisto NumPix MaxMSE\n");
fprintf(stdout,
"Tile_1 '' '' '' '' '' '' ''\n");
fprintf(stdout, "...\n");
fprintf(stdout,
"Tile_Nt '' '' '' '' '' '' ''\n");
fprintf(stdout, "...\n");
fprintf(stdout, "TILE 0 DETAILS\n");
fprintf(stdout, "part_nb tileno num_packs start_pos end_tph_pos end_pos\n");
fprintf(stdout, "...\n");
fprintf(stdout, "Progression_string\n");
fprintf(stdout,
"pack_nb tileno layno resno compno precno start_pos end_ph_pos end_pos disto\n");
fprintf(stdout,
"Tpacket_0 Tile layer res. comp. prec. start_pos end_pos disto\n");
fprintf(stdout, "...\n");
fprintf(stdout,
"Tpacket_Np '' '' '' '' '' '' '' ''\n");
fprintf(stdout, "MaxDisto\n");
fprintf(stdout, "TotalDisto\n\n");
#endif /*FIXME_INDEX*/
}
static OPJ_PROG_ORDER give_progression(const char progression[4])
{
if (strncmp(progression, "LRCP", 4) == 0) {
return OPJ_LRCP;
}
if (strncmp(progression, "RLCP", 4) == 0) {
return OPJ_RLCP;
}
if (strncmp(progression, "RPCL", 4) == 0) {
return OPJ_RPCL;
}
if (strncmp(progression, "PCRL", 4) == 0) {
return OPJ_PCRL;
}
if (strncmp(progression, "CPRL", 4) == 0) {
return OPJ_CPRL;
}
return OPJ_PROG_UNKNOWN;
}
static unsigned int get_num_images(char *imgdirpath)
{
DIR *dir;
struct dirent* content;
unsigned int num_images = 0;
/*Reading the input images from given input directory*/
dir = opendir(imgdirpath);
if (!dir) {
fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
return 0;
}
num_images = 0;
while ((content = readdir(dir)) != NULL) {
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
}
if (num_images == UINT_MAX) {
fprintf(stderr, "Too many files in folder %s\n", imgdirpath);
num_images = 0;
break;
}
num_images++;
}
closedir(dir);
return num_images;
}
static int load_images(dircnt_t *dirptr, char *imgdirpath)
{
DIR *dir;
struct dirent* content;
int i = 0;
/*Reading the input images from given input directory*/
dir = opendir(imgdirpath);
if (!dir) {
fprintf(stderr, "Could not open Folder %s\n", imgdirpath);
return 1;
} else {
fprintf(stderr, "Folder opened successfully\n");
}
while ((content = readdir(dir)) != NULL) {
if (strcmp(".", content->d_name) == 0 || strcmp("..", content->d_name) == 0) {
continue;
}
strcpy(dirptr->filename[i], content->d_name);
i++;
}
closedir(dir);
return 0;
}
static int get_file_format(char *filename)
{
unsigned int i;
static const char *extension[] = {
"pgx", "pnm", "pgm", "ppm", "pbm", "pam", "bmp", "tif", "tiff", "raw", "yuv", "rawl", "tga", "png", "j2k", "jp2", "j2c", "jpc"
};
static const int format[] = {
PGX_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, PXM_DFMT, BMP_DFMT, TIF_DFMT, TIF_DFMT, RAW_DFMT, RAW_DFMT, RAWL_DFMT, TGA_DFMT, PNG_DFMT, J2K_CFMT, JP2_CFMT, J2K_CFMT, J2K_CFMT
};
char * ext = strrchr(filename, '.');
if (ext == NULL) {
return -1;
}
ext++;
for (i = 0; i < sizeof(format) / sizeof(*format); i++) {
if (strcasecmp(ext, extension[i]) == 0) {
return format[i];
}
}
return -1;
}
static char * get_file_name(char *name)
{
char *fname = strtok(name, ".");
return fname;
}
static char get_next_file(unsigned int imageno, dircnt_t *dirptr,
img_fol_t *img_fol,
opj_cparameters_t *parameters)
{
char image_filename[OPJ_PATH_LEN], infilename[OPJ_PATH_LEN],
outfilename[OPJ_PATH_LEN], temp_ofname[OPJ_PATH_LEN];
char *temp_p, temp1[OPJ_PATH_LEN] = "";
strcpy(image_filename, dirptr->filename[imageno]);
fprintf(stderr, "File Number %u \"%s\"\n", imageno, image_filename);
parameters->decod_format = get_file_format(image_filename);
if (parameters->decod_format == -1) {
return 1;
}
if (strlen(img_fol->imgdirpath) + 1 + strlen(image_filename) + 1 > sizeof(
infilename)) {
return 1;
}
strcpy(infilename, img_fol->imgdirpath);
strcat(infilename, "/");
strcat(infilename, image_filename);
if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile),
infilename) != 0) {
return 1;
}
/*Set output file*/
strcpy(temp_ofname, get_file_name(image_filename));
while ((temp_p = strtok(NULL, ".")) != NULL) {
strcat(temp_ofname, temp1);
sprintf(temp1, ".%s", temp_p);
}
if (img_fol->set_out_format == 1) {
if (strlen(img_fol->imgdirpath) + 1 + strlen(temp_ofname) + 1 + strlen(
img_fol->out_format) + 1 > sizeof(outfilename)) {
return 1;
}
strcpy(outfilename, img_fol->imgdirpath);
strcat(outfilename, "/");
strcat(outfilename, temp_ofname);
strcat(outfilename, ".");
strcat(outfilename, img_fol->out_format);
if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
outfilename) != 0) {
return 1;
}
}
return 0;
}
/* ------------------------------------------------------------------------------------ */
static int parse_cmdline_encoder(int argc, char **argv,
opj_cparameters_t *parameters,
img_fol_t *img_fol, raw_cparameters_t *raw_cp, char *indexfilename,
size_t indexfilename_size,
int* pOutFramerate,
OPJ_BOOL* pOutPLT,
OPJ_BOOL* pOutTLM,
int* pOutGuardBits,
int* pOutNumThreads,
unsigned int* pTarget_bitdepth)
{
OPJ_UINT32 i, j;
int totlen, c;
opj_option_t long_option[] = {
{"cinema2K", REQ_ARG, NULL, 'w'},
{"cinema4K", NO_ARG, NULL, 'y'},
{"ImgDir", REQ_ARG, NULL, 'z'},
{"TP", REQ_ARG, NULL, 'u'},
{"SOP", NO_ARG, NULL, 'S'},
{"EPH", NO_ARG, NULL, 'E'},
{"OutFor", REQ_ARG, NULL, 'O'},
{"POC", REQ_ARG, NULL, 'P'},
{"ROI", REQ_ARG, NULL, 'R'},
{"jpip", NO_ARG, NULL, 'J'},
{"mct", REQ_ARG, NULL, 'Y'},
{"IMF", REQ_ARG, NULL, 'Z'},
{"PLT", NO_ARG, NULL, 'A'},
{"threads", REQ_ARG, NULL, 'B'},
{"TLM", NO_ARG, NULL, 'D'},
{"TargetBitDepth", REQ_ARG, NULL, 'X'},
{"GuardBits", REQ_ARG, NULL, 'G'}
};
/* parse the command line */
const char optlist[] = "i:o:r:q:n:b:c:t:p:s:SEM:x:R:d:T:If:P:C:F:u:JY:X:G:"
#ifdef USE_JPWL
"W:"
#endif /* USE_JPWL */
"h";
totlen = sizeof(long_option);
img_fol->set_out_format = 0;
raw_cp->rawWidth = 0;
do {
c = opj_getopt_long(argc, argv, optlist, long_option, totlen);
if (c == -1) {
break;
}
switch (c) {
case 'i': { /* input file */
char *infile = opj_optarg;
parameters->decod_format = get_file_format(infile);
switch (parameters->decod_format) {
case PGX_DFMT:
case PXM_DFMT:
case BMP_DFMT:
case TIF_DFMT:
case RAW_DFMT:
case RAWL_DFMT:
case TGA_DFMT:
case PNG_DFMT:
break;
default:
fprintf(stderr,
"[ERROR] Unknown input file format: %s \n"
" Known file formats are *.pnm, *.pgm, *.ppm, *.pgx, *png, *.bmp, *.tif(f), *.raw, *.yuv or *.tga\n",
infile);
return 1;
}
if (opj_strcpy_s(parameters->infile, sizeof(parameters->infile), infile) != 0) {
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'o': { /* output file */
char *outfile = opj_optarg;
parameters->cod_format = get_file_format(outfile);
switch (parameters->cod_format) {
case J2K_CFMT:
case JP2_CFMT:
break;
default:
fprintf(stderr,
"Unknown output format image %s [only *.j2k, *.j2c or *.jp2]!! \n", outfile);
return 1;
}
if (opj_strcpy_s(parameters->outfile, sizeof(parameters->outfile),
outfile) != 0) {
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'O': { /* output format */
char outformat[50];
char *of = opj_optarg;
sprintf(outformat, ".%s", of);
img_fol->set_out_format = 1;
parameters->cod_format = get_file_format(outformat);
switch (parameters->cod_format) {
case J2K_CFMT:
case JP2_CFMT:
img_fol->out_format = opj_optarg;
break;
default:
fprintf(stderr, "Unknown output format image [only j2k, j2c, jp2]!! \n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'r': { /* rates rates/distortion */
char *s = opj_optarg;
parameters->tcp_numlayers = 0;
while (sscanf(s, "%f", ¶meters->tcp_rates[parameters->tcp_numlayers]) ==
1) {
parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s) {
break;
}
s++;
}
parameters->cp_disto_alloc = 1;
}
break;
/* ----------------------------------------------------- */
case 'F': { /* Raw image format parameters */
OPJ_BOOL wrong = OPJ_FALSE;
char *substr1;
char *substr2;
char *sep;
char signo;
int width, height, bitdepth, ncomp;
OPJ_UINT32 len;
OPJ_BOOL raw_signed = OPJ_FALSE;
substr2 = strchr(opj_optarg, '@');
if (substr2 == NULL) {
len = (OPJ_UINT32) strlen(opj_optarg);
} else {
len = (OPJ_UINT32)(substr2 - opj_optarg);
substr2++; /* skip '@' character */
}
substr1 = (char*) malloc((len + 1) * sizeof(char));
if (substr1 == NULL) {
return 1;
}
memcpy(substr1, opj_optarg, len);
substr1[len] = '\0';
if (sscanf(substr1, "%d,%d,%d,%d,%c", &width, &height, &ncomp, &bitdepth,
&signo) == 5) {
if (signo == 's') {
raw_signed = OPJ_TRUE;
} else if (signo == 'u') {
raw_signed = OPJ_FALSE;
} else {
wrong = OPJ_TRUE;
}
} else {
wrong = OPJ_TRUE;
}
if (!wrong) {
int compno;
int lastdx = 1;
int lastdy = 1;
raw_cp->rawWidth = width;
raw_cp->rawHeight = height;
raw_cp->rawComp = ncomp;
raw_cp->rawBitDepth = bitdepth;
raw_cp->rawSigned = raw_signed;
raw_cp->rawComps = (raw_comp_cparameters_t*) malloc(((OPJ_UINT32)(
ncomp)) * sizeof(raw_comp_cparameters_t));
if (raw_cp->rawComps == NULL) {
free(substr1);
return 1;
}
for (compno = 0; compno < ncomp && !wrong; compno++) {
if (substr2 == NULL) {
raw_cp->rawComps[compno].dx = lastdx;
raw_cp->rawComps[compno].dy = lastdy;
} else {
int dx, dy;
sep = strchr(substr2, ':');
if (sep == NULL) {
if (sscanf(substr2, "%dx%d", &dx, &dy) == 2) {
lastdx = dx;
lastdy = dy;
raw_cp->rawComps[compno].dx = dx;
raw_cp->rawComps[compno].dy = dy;
substr2 = NULL;
} else {
wrong = OPJ_TRUE;
}
} else {
if (sscanf(substr2, "%dx%d:%s", &dx, &dy, substr2) == 3) {
raw_cp->rawComps[compno].dx = dx;
raw_cp->rawComps[compno].dy = dy;
} else {
wrong = OPJ_TRUE;
}
}
}
}
}
free(substr1);
if (wrong) {
fprintf(stderr, "\nError: invalid raw or yuv image parameters\n");
fprintf(stderr, "Please use the Format option -F:\n");
fprintf(stderr,
"-F <width>,<height>,<ncomp>,<bitdepth>,{s,u}@<dx1>x<dy1>:...:<dxn>x<dyn>\n");
fprintf(stderr,
"If subsampling is omitted, 1x1 is assumed for all components\n");
fprintf(stderr,
"Example: -i image.raw -o image.j2k -F 512,512,3,8,u@1x1:2x2:2x2\n");
fprintf(stderr,
" for raw or yuv 512x512 size with 4:2:0 subsampling\n");
fprintf(stderr, "Aborting.\n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'q': { /* layer allocation by distortion ratio (PSNR) */
char *s = opj_optarg;
while (sscanf(s, "%f", ¶meters->tcp_distoratio[parameters->tcp_numlayers])
== 1) {
parameters->tcp_numlayers++;
while (*s && *s != ',') {
s++;
}
if (!*s) {
break;
}
s++;
}
parameters->cp_fixed_quality = 1;
}
break;
/* dda */
/* ----------------------------------------------------- */
case 'f': { /* layer allocation by fixed layer */
int *row = NULL, *col = NULL;
OPJ_UINT32 numlayers = 0, numresolution = 0, matrix_width = 0;
char *s = opj_optarg;
sscanf(s, "%u", &numlayers);
s++;
if (numlayers > 9) {
s++;
}
parameters->tcp_numlayers = (int)numlayers;
numresolution = (OPJ_UINT32)parameters->numresolution;
matrix_width = numresolution * 3;
parameters->cp_matrice = (int *) malloc(sizeof(int) * numlayers * matrix_width);
if (parameters->cp_matrice == NULL) {
return 1;
}
s = s + 2;
for (i = 0; i < numlayers; i++) {
row = ¶meters->cp_matrice[i * matrix_width];
col = row;
parameters->tcp_rates[i] = 1;
sscanf(s, "%d,", &col[0]);
s += 2;
if (col[0] > 9) {
s++;
}
col[1] = 0;
col[2] = 0;
for (j = 1; j < numresolution; j++) {
col += 3;
sscanf(s, "%d,%d,%d", &col[0], &col[1], &col[2]);
s += 6;
if (col[0] > 9) {
s++;
}
if (col[1] > 9) {
s++;
}
if (col[2] > 9) {
s++;
}
}
if (i < numlayers - 1) {
s++;
}
}
parameters->cp_fixed_alloc = 1;
}
break;
/* ----------------------------------------------------- */
case 't': { /* tiles */
sscanf(opj_optarg, "%d,%d", ¶meters->cp_tdx, ¶meters->cp_tdy);
parameters->tile_size_on = OPJ_TRUE;
}
break;
/* ----------------------------------------------------- */
case 'X': { /* target bitdepth */
char *s = opj_optarg;
sscanf(s, "%u", pTarget_bitdepth);
if (*pTarget_bitdepth == 0) {
fprintf(stderr, "Target bitdepth must be at least 1 bit.\n");
return 1;
}
}
break;
/* ----------------------------------------------------- */
case 'G': { /* guard bits */
char *s = opj_optarg;
sscanf(s, "%d", pOutGuardBits);
}
break;
/* ----------------------------------------------------- */
case 'n': { /* resolution */
sscanf(opj_optarg, "%d", ¶meters->numresolution);
}
break;
/* ----------------------------------------------------- */
case 'c': { /* precinct dimension */
char sep;
int res_spec = 0;
char *s = opj_optarg;
int ret;
do {
sep = 0;
ret = sscanf(s, "[%d,%d]%c", ¶meters->prcw_init[res_spec],
¶meters->prch_init[res_spec], &sep);
if (!(ret == 2 && sep == 0) && !(ret == 3 && sep == ',')) {
fprintf(stderr, "\nError: could not parse precinct dimension: '%s' %x\n", s,
sep);
fprintf(stderr, "Example: -i lena.raw -o lena.j2k -c [128,128],[128,128]\n");
return 1;
}
parameters->csty |= 0x01;
res_spec++;
s = strpbrk(s, "]") + 2;
} while (sep == ',');
parameters->res_spec = res_spec;
}
break;
/* ----------------------------------------------------- */
case 'b': { /* code-block dimension */
int cblockw_init = 0, cblockh_init = 0;
sscanf(opj_optarg, "%d,%d", &cblockw_init, &cblockh_init);
if (cblockw_init > 1024 || cblockw_init < 4 ||
cblockh_init > 1024 || cblockh_init < 4 ||
cblockw_init * cblockh_init > 4096) {
fprintf(stderr,
"!! Size of code_block error (option -b) !!\n\nRestriction :\n"
" * width*height<=4096\n * 4<=width,height<= 1024\n\n");
return 1;
}
parameters->cblockw_init = cblockw_init;
parameters->cblockh_init = cblockh_init;
}
break;
/* ----------------------------------------------------- */
case 'x': { /* creation of index file */