487int main(
int argc,
char * argv[]) {
488 char *input_filename = NULL;
489 char *output_filename = NULL;
490 char prog_order_store[] =
"RPCL";
491 char *prog_order = prog_order_store;
492 char profile_string_store[] =
"";
493 char *profile_string = profile_string_store;
494 char *com_string = NULL;
496 float quantization_step = -1.0f;
497 bool reversible =
false;
498 int employ_color_transform = -1;
500 const int max_precinct_sizes = 33;
502 int num_precincts = -1;
513 ojph::si32 is_signed_store[initial_num_comps] = {-1, -1, -1, -1};
516 ojph::ui32 bit_depth_store[initial_num_comps] = {0, 0, 0, 0};
520 ojph::point *comp_downsampling = downsampling_store;
521 bool tlm_marker =
false;
522 bool tileparts_at_resolutions =
false;
523 bool tileparts_at_components =
false;
527 "\nThe following arguments are necessary:\n"
528#ifdef OJPH_ENABLE_TIFF_SUPPORT
529 " -i input file name (either pgm, ppm, pfm, tif(f), or raw(yuv))\n"
531 " -i input file name (either pgm, ppm, pfm, or raw(yuv))\n"
533 " -o output file name\n\n"
535 "The following option has a default value (optional):\n"
536 " -num_decomps (5) number of decompositions\n"
537 " -qstep (0.00001...0.5) quantization step size for lossy\n"
538 " compression; quantization steps size for all subbands are\n"
539 " derived from this value. {The default value for 8bit\n"
540 " images is 0.0039}\n"
541 " -reversible <true | false> If this is 'false', an irreversible or\n"
542 " lossy compression is employed, using the 9/7 wavelet\n"
543 " transform; if 'true', a reversible compression is\n"
544 " performed, where the 5/3 wavelet is used.\n"
545 " Default value is 'false'.\n"
546 " -colour_trans <true | false> This option employs a color transform,\n"
547 " to transform RGB color images into the YUV domain.\n"
548 " This option should NOT be used with YUV images, because\n"
549 " they have already been transformed.\n"
550 " If there are three color components that are\n"
551 " downsampled by the same amount then this option can be\n"
552 " 'true' or 'false'. This option is also available when\n"
553 " there are more than three colour components, where it is\n"
554 " applied to the first three colour components.\n"
555 " -prog_order (RPCL) is the progression order, and can be one of:\n"
556 " LRCP, RLCP, RPCL, PCRL, CPRL.\n"
557 " -block_size {x,y} (64,64) where x and y are the height and width of\n"
558 " a codeblock. In unix-like environment, { and } must be\n"
559 " preceded by a ""\\"".\n"
560 " -precincts {x,y},{x,y},...,{x,y} where {x,y} is the precinct size\n"
561 " starting from the coarsest resolution; the last precinct\n"
562 " is repeated for all finer resolutions\n"
563 " -tile_offset {x,y} tile offset. \n"
564 " -tile_size {x,y} tile width and height. \n"
565 " -image_offset {x,y} image offset from origin. \n"
566 " -tileparts (None) employ tilepart divisions at each resolution, \n"
567 " indicated by the letter R, and/or component, indicated \n"
568 " by the letter C. For both, use \"-tileparts RC\".\n"
569 " -tlm_marker <true | false> if 'true', a TLM marker is inserted.\n"
570 " Default value is false.\n"
571 " -profile (None) is the profile, the code will check if the \n"
572 " selected options meet the profile. Currently only \n"
573 " BROADCAST and IMF are supported. This automatically \n"
574 " sets tlm_marker to true and tileparts to C.\n"
575 " -com (None) if set, inserts a COM marker with the specified\n"
576 " string. If the string has spaces, please use\n"
577 " double quotes, as in -com \"This is a comment\".\n"
580 "When the input file is a YUV file, these arguments need to be \n"
582 " -dims {x,y} x is image width, y is height\n"
583 " -num_comps number of components\n"
584 " -signed a comma-separated list of true or false parameters, one\n"
585 " for each component; for example: true,false,false\n"
586 " -bit_depth a comma-separated list of bit depth values, one per \n"
587 " component; for example: 12,10,10\n"
588 " -downsamp {x,y},{x,y},...,{x,y} a list of x,y points, one for each\n"
589 " component; for example {1,1},{2,2},{2,2}\n\n"
592 ".pfm files receive special treatment. Currently, lossy compression\n"
593 "with these files is not supported, only lossless. When these files are\n"
594 "used, the NLT segment marker is automatically inserted into the\n"
595 "codestream when needed, as explained shortly. The following arguments\n"
596 "can be useful for this file type.\n"
597 " -signed a comma-separated list of true or false parameters, one\n"
598 " for each component; for example: true,false,false.\n"
599 " If you are sure that all sample values are positive or 0,\n"
600 " set the corresponding entry to false; otherwise set it to\n"
602 " When a component entry is set to true, an NLT segment\n"
603 " marker segment is inserted into the codestream.\n"
604 " The NLT segment specifies a non-linear transform that\n"
605 " changes only negative values, producing better coding\n"
607 " The NLT segment marker might be less supported in other\n"
609 " -bit_depth a comma-separated list of bit depth values, one per \n"
610 " component; for example: 12,10,10.\n"
611 " Floating value numbers are treated as integers, and they\n"
612 " are shifted to the right, keeping only the specified\n"
613 " number of bits. Up to 32 bits (which is the default) are\n"
619 if (!
get_arguments(argc, argv, input_filename, output_filename,
620 prog_order, profile_string, num_decompositions,
621 quantization_step, reversible, employ_color_transform,
622 max_precinct_sizes, num_precincts, precinct_size,
623 block_size, dims, image_offset, tile_size, tile_offset,
624 max_num_comps, num_components,
625 num_comp_downsamps, comp_downsampling,
626 num_bit_depths, bit_depth, num_is_signed, is_signed,
627 tlm_marker, tileparts_at_resolutions,
628 tileparts_at_components, com_string))
633 clock_t begin = clock();
644#ifdef OJPH_ENABLE_TIFF_SUPPORT
649 if (input_filename == NULL)
650 OJPH_ERROR(0x01000007,
"please specify an input file name using"
651 " the -i command line option");
652 if (output_filename == NULL)
653 OJPH_ERROR(0x01000008,
"please specify an output file name using"
654 " the -o command line option");
661 ppm.
open(input_filename);
666 assert(num_comps == 1);
678 if (num_precincts != -1)
683 if (!reversible && quantization_step != -1.0f)
685 if (profile_string[0] !=
'\0')
688 tileparts_at_components);
691 if (employ_color_transform != -1)
693 "-colour_trans option is not needed and was not used\n");
694 if (dims.
w != 0 || dims.
h != 0)
696 "-dims option is not needed and was not used\n");
697 if (num_components != 0)
699 "-num_comps is not needed and was not used\n");
700 if (is_signed[0] != -1)
702 "-signed is not needed and was not used\n");
703 if (bit_depth[0] != 0)
705 "-bit_depth is not needed and was not used\n");
706 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
708 "-downsamp is not needed and was not used\n");
714 ppm.
open(input_filename);
719 assert(num_comps == 3);
731 if (num_precincts != -1)
734 if (employ_color_transform == -1)
739 if (!reversible && quantization_step != -1.0f)
742 if (profile_string[0] !=
'\0')
745 tileparts_at_components);
748 if (dims.
w != 0 || dims.
h != 0)
750 "-dims option is not needed and was not used\n");
751 if (num_components != 0)
753 "-num_comps is not needed and was not used\n");
754 if (is_signed[0] != -1)
756 "-signed is not needed and was not used\n");
757 if (bit_depth[0] != 0)
759 "-bit_depth is not needed and was not used\n");
760 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
762 "-downsamp is not needed and was not used\n");
768 pfm.
open(input_filename);
773 assert(num_comps == 1 || num_comps == 3);
776 if (bit_depth[0] != 0)
777 if (num_bit_depths < num_comps)
778 for (
ojph::ui32 c = num_bit_depths; c < num_comps; ++c)
779 bit_depth[c] = bit_depth[num_bit_depths - 1];
781 bool all_the_same =
true;
783 all_the_same = all_the_same
784 && bit_depth[0] == bit_depth[1]
785 && bit_depth[1] == bit_depth[2];
788 if (bit_depth[c] == 0)
801 if (num_precincts != -1)
806 if (employ_color_transform != -1)
808 "-colour_trans option is not needed and was not used; "
809 "this is because the image has one component only\n");
813 if (employ_color_transform == -1)
820 const float min_step = 1.0f / 16384.0f;
821 if (quantization_step == -1.0f)
822 quantization_step = min_step;
824 quantization_step =
ojph_max(quantization_step, min_step);
845 if (profile_string[0] !=
'\0')
848 tileparts_at_components);
851 if (dims.
w != 0 || dims.
h != 0)
853 "-dims option is not needed and was not used\n");
854 if (num_components != 0)
856 "-num_comps is not needed and was not used\n");
857 if (is_signed[0] != -1)
859 "-signed is not needed and was not used\n");
860 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
862 "-downsamp is not needed and was not used\n");
866#ifdef OJPH_ENABLE_TIFF_SUPPORT
869 tif.open(input_filename);
872 image_offset.
y + tif.get_size().h));
873 ojph::ui32 num_comps = tif.get_num_components();
875 if(num_bit_depths > 0 )
876 tif.set_bit_depth(num_bit_depths, bit_depth);
879 tif.get_bit_depth(c), tif.get_is_signed(c));
887 if (num_precincts != -1)
890 if (employ_color_transform == -1 && num_comps >= 3)
895 if (!reversible && quantization_step != -1)
898 if (profile_string[0] !=
'\0')
901 tileparts_at_components);
904 if (dims.
w != 0 || dims.
h != 0)
906 "-dims option is not needed and was not used\n");
907 if (num_components != 0)
909 "-num_comps is not needed and was not used\n");
910 if (is_signed[0] != -1)
912 "-signed is not needed and was not used\n");
913 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
915 "-downsamp is not needed and was not used\n");
923 if (dims.
w == 0 || dims.
h == 0)
925 "-dims option must have positive dimensions\n");
927 image_offset.
y + dims.
h));
928 if (num_components <= 0)
930 "-num_comps option is missing and must be provided\n");
931 if (num_is_signed <= 0)
933 "-signed option is missing and must be provided\n");
934 if (num_bit_depths <= 0)
936 "-bit_depth option is missing and must be provided\n");
937 if (num_comp_downsamps <= 0)
939 "-downsamp option is missing and must be provided\n");
945 ojph::ui32 last_signed_idx = 0, last_bit_depth_idx = 0;
948 for (
ojph::ui32 c = 0; c < num_components; ++c)
951 [c < num_comp_downsamps ? c : last_downsamp_idx];
952 last_downsamp_idx += last_downsamp_idx+1 < num_comp_downsamps ? 1:0;
953 ojph::ui32 bd = bit_depth[c<num_bit_depths ? c : last_bit_depth_idx];
954 last_bit_depth_idx += last_bit_depth_idx + 1 < num_bit_depths ? 1:0;
955 int is = is_signed[c < num_is_signed ? c : last_signed_idx];
956 last_signed_idx += last_signed_idx + 1 < num_is_signed ? 1 : 0;
966 if (num_precincts != -1)
969 if (employ_color_transform == -1)
973 "We currently do not support color transform on raw(yuv) files."
974 " In any case, this not a normal usage scenario. The OpenJPH "
975 "library however does support that, but ojph_compress.cpp must be "
976 "modified to send all lines from one component before moving to "
977 "the next component; this requires buffering components outside"
978 " of the OpenJPH library");
980 if (!reversible && quantization_step != -1.0f)
983 if (profile_string[0] !=
'\0')
986 tileparts_at_components);
989 yuv.
open(input_filename);
995 if (dims.
w == 0 || dims.
h == 0)
997 "-dims option must have positive dimensions\n");
999 image_offset.
y + dims.
h));
1000 if (num_components != 1)
1002 "-num_comps must be 1\n");
1003 if (num_is_signed <= 0)
1005 "-signed option is missing and must be provided\n");
1006 if (num_bit_depths <= 0)
1008 "-bit_depth option is missing and must be provided\n");
1009 if (num_comp_downsamps <= 0)
1011 "-downsamp option is missing and must be provided\n");
1016 siz.
set_component(0, comp_downsampling[0], bit_depth[0], is_signed[0]);
1024 if (num_precincts != -1)
1027 if (employ_color_transform != -1)
1029 "color transform is meaningless since .raw files are single "
1032 if (!reversible && quantization_step != -1.0f)
1035 if (profile_string[0] !=
'\0')
1038 tileparts_at_components);
1041 raw.
open(input_filename);
1046 dpx.
open(input_filename);
1064 if (num_precincts != -1)
1067 if (employ_color_transform == -1 && num_comps >= 3)
1072 if (!reversible && quantization_step != -1)
1075 if (profile_string[0] !=
'\0')
1078 tileparts_at_components);
1081 if (dims.
w != 0 || dims.
h != 0)
1083 "-dims option is not needed and was not used\n");
1084 if (num_components != 0)
1086 "-num_comps is not needed and was not used\n");
1087 if (is_signed[0] != -1)
1089 "-signed is not needed and was not used\n");
1090 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
1092 "-downsamp is not needed and was not used\n");
1097#if defined( OJPH_ENABLE_TIFF_SUPPORT)
1099 "unknown input file extension; only pgm, ppm, dpx, tif(f),"
1100 " or raw(yuv) are supported\n");
1103 "unknown input file extension; only pgm, ppm, dpx,"
1104 " or raw(yuv) are supported\n");
1109 "Please supply a proper input filename with a proper three-letter "
1116 j2c_file.
open(output_filename);
1117 codestream.
write_headers(&j2c_file, &com_ex, com_string ? 1 : 0);
1131 assert(c == next_comp);
1132 base->
read(cur_line, next_comp);
1133 cur_line = codestream.
exchange(cur_line, next_comp);
1146 assert(c == next_comp);
1147 base->
read(cur_line, next_comp);
1148 cur_line = codestream.
exchange(cur_line, next_comp);
1157 if (max_num_comps != initial_num_comps)
1159 delete[] comp_downsampling;
1164 catch (
const std::exception& e)
1166 const char *p = e.what();
1167 if (strncmp(p,
"ojph error", 10) != 0)
1172 clock_t end = clock();
1173 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
1174 printf(
"Elapsed time = %f\n", elapsed_secs);
static bool get_arguments(int argc, char *argv[], char *&input_filename, char *&output_filename, char *&progression_order, char *&profile_string, ojph::ui32 &num_decompositions, float &quantization_step, bool &reversible, int &employ_color_transform, const int max_num_precincts, int &num_precincts, ojph::size *precinct_size, ojph::size &block_size, ojph::size &dims, ojph::point &image_offset, ojph::size &tile_size, ojph::point &tile_offset, ojph::ui32 &max_num_comps, ojph::ui32 &num_comps, ojph::ui32 &num_comp_downsamps, ojph::point *&comp_downsamp, ojph::ui32 &num_bit_depths, ojph::ui32 *&bit_depth, ojph::ui32 &num_is_signed, ojph::si32 *&is_signed, bool &tlm_marker, bool &tileparts_at_resolutions, bool &tileparts_at_components, char *&com_string)