491int main(
int argc,
char * argv[]) {
492 char *input_filename = NULL;
493 char *output_filename = NULL;
494 char prog_order_store[] =
"RPCL";
495 char *prog_order = prog_order_store;
496 char profile_string_store[] =
"";
497 char *profile_string = profile_string_store;
498 char *com_string = NULL;
500 float quantization_step = -1.0f;
501 bool reversible =
false;
502 int employ_color_transform = -1;
504 const int max_precinct_sizes = 33;
506 int num_precincts = -1;
517 ojph::si32 is_signed_store[initial_num_comps] = {-1, -1, -1, -1};
520 ojph::ui32 bit_depth_store[initial_num_comps] = {0, 0, 0, 0};
524 ojph::point *comp_downsampling = downsampling_store;
525 bool tlm_marker =
false;
526 bool tileparts_at_resolutions =
false;
527 bool tileparts_at_components =
false;
531 "\nThe following arguments are necessary:\n"
532#ifdef OJPH_ENABLE_TIFF_SUPPORT
533 " -i input file name (either pgm, ppm, pfm, tif(f), or raw(yuv))\n"
535 " -i input file name (either pgm, ppm, pfm, or raw(yuv))\n"
537 " -o output file name\n\n"
539 "The following option has a default value (optional):\n"
540 " -num_decomps (5) number of decompositions\n"
541 " -qstep (0.00001...0.5) quantization step size for lossy\n"
542 " compression; quantization steps size for all subbands are\n"
543 " derived from this value. {The default value for 8bit\n"
544 " images is 0.0039}\n"
545 " -reversible <true | false> If this is 'false', an irreversible or\n"
546 " lossy compression is employed, using the 9/7 wavelet\n"
547 " transform; if 'true', a reversible compression is\n"
548 " performed, where the 5/3 wavelet is used.\n"
549 " Default value is 'false'.\n"
550 " -colour_trans <true | false> This option employs a color transform,\n"
551 " to transform RGB color images into the YUV domain.\n"
552 " This option should NOT be used with YUV images, because\n"
553 " they have already been transformed.\n"
554 " If there are three color components that are\n"
555 " downsampled by the same amount then this option can be\n"
556 " 'true' or 'false'. This option is also available when\n"
557 " there are more than three colour components, where it is\n"
558 " applied to the first three colour components.\n"
559 " -prog_order (RPCL) is the progression order, and can be one of:\n"
560 " LRCP, RLCP, RPCL, PCRL, CPRL.\n"
561 " -block_size {x,y} (64,64) where x and y are the height and width of\n"
562 " a codeblock. In unix-like environment, { and } must be\n"
563 " preceded by a ""\\"".\n"
564 " -precincts {x,y},{x,y},...,{x,y} where {x,y} is the precinct size\n"
565 " starting from the coarsest resolution; the last precinct\n"
566 " is repeated for all finer resolutions\n"
567 " -tile_offset {x,y} tile offset. \n"
568 " -tile_size {x,y} tile width and height. \n"
569 " -image_offset {x,y} image offset from origin. \n"
570 " -tileparts (None) employ tilepart divisions at each resolution, \n"
571 " indicated by the letter R, and/or component, indicated \n"
572 " by the letter C. For both, use \"-tileparts RC\".\n"
573 " -tlm_marker <true | false> if 'true', a TLM marker is inserted.\n"
574 " Default value is false.\n"
575 " -profile (None) is the profile, the code will check if the \n"
576 " selected options meet the profile. Currently only \n"
577 " BROADCAST and IMF are supported. This automatically \n"
578 " sets tlm_marker to true and tileparts to C.\n"
579 " -com (None) if set, inserts a COM marker with the specified\n"
580 " string. If the string has spaces, please use\n"
581 " double quotes, as in -com \"This is a comment\".\n"
584 "When the input file is a YUV file, these arguments need to be \n"
586 " -dims {x,y} x is image width, y is height\n"
587 " -num_comps number of components\n"
588 " -signed a comma-separated list of true or false parameters, one\n"
589 " for each component; for example: true,false,false\n"
590 " -bit_depth a comma-separated list of bit depth values, one per \n"
591 " component; for example: 12,10,10\n"
592 " -downsamp {x,y},{x,y},...,{x,y} a list of x,y points, one for each\n"
593 " component; for example {1,1},{2,2},{2,2}\n\n"
596 ".pfm files receive special treatment. Currently, lossy compression\n"
597 "with these files is not supported, only lossless. When these files are\n"
598 "used, the NLT segment marker is automatically inserted into the\n"
599 "codestream when needed, as explained shortly. The following arguments\n"
600 "can be useful for this file type.\n"
601 " -signed a comma-separated list of true or false parameters, one\n"
602 " for each component; for example: true,false,false.\n"
603 " If you are sure that all sample values are positive or 0,\n"
604 " set the corresponding entry to false; otherwise set it to\n"
606 " When a component entry is set to true, an NLT segment\n"
607 " marker segment is inserted into the codestream.\n"
608 " The NLT segment specifies a non-linear transform that\n"
609 " changes only negative values, producing better coding\n"
611 " The NLT segment marker might be less supported in other\n"
613 " -bit_depth a comma-separated list of bit depth values, one per \n"
614 " component; for example: 12,10,10.\n"
615 " Floating value numbers are treated as integers, and they\n"
616 " are shifted to the right, keeping only the specified\n"
617 " number of bits. Up to 32 bits (which is the default) are\n"
623 if (!
get_arguments(argc, argv, input_filename, output_filename,
624 prog_order, profile_string, num_decompositions,
625 quantization_step, reversible, employ_color_transform,
626 max_precinct_sizes, num_precincts, precinct_size,
627 block_size, dims, image_offset, tile_size, tile_offset,
628 max_num_comps, num_components,
629 num_comp_downsamps, comp_downsampling,
630 num_bit_depths, bit_depth, num_is_signed, is_signed,
631 tlm_marker, tileparts_at_resolutions,
632 tileparts_at_components, com_string))
637 clock_t begin = clock();
648#ifdef OJPH_ENABLE_TIFF_SUPPORT
653 if (input_filename == NULL)
654 OJPH_ERROR(0x01000007,
"please specify an input file name using"
655 " the -i command line option");
656 if (output_filename == NULL)
657 OJPH_ERROR(0x01000008,
"please specify an output file name using"
658 " the -o command line option");
665 ppm.
open(input_filename);
670 assert(num_comps == 1);
682 if (num_precincts != -1)
687 if (!reversible && quantization_step != -1.0f)
689 if (profile_string[0] !=
'\0')
692 tileparts_at_components);
695 if (employ_color_transform != -1)
697 "-colour_trans option is not needed and was not used\n");
698 if (dims.
w != 0 || dims.
h != 0)
700 "-dims option is not needed and was not used\n");
701 if (num_components != 0)
703 "-num_comps is not needed and was not used\n");
704 if (is_signed[0] != -1)
706 "-signed is not needed and was not used\n");
707 if (bit_depth[0] != 0)
709 "-bit_depth is not needed and was not used\n");
710 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
712 "-downsamp is not needed and was not used\n");
718 ppm.
open(input_filename);
723 assert(num_comps == 3);
735 if (num_precincts != -1)
738 if (employ_color_transform == -1)
743 if (!reversible && quantization_step != -1.0f)
746 if (profile_string[0] !=
'\0')
749 tileparts_at_components);
752 if (dims.
w != 0 || dims.
h != 0)
754 "-dims option is not needed and was not used\n");
755 if (num_components != 0)
757 "-num_comps is not needed and was not used\n");
758 if (is_signed[0] != -1)
760 "-signed is not needed and was not used\n");
761 if (bit_depth[0] != 0)
763 "-bit_depth is not needed and was not used\n");
764 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
766 "-downsamp is not needed and was not used\n");
772 pfm.
open(input_filename);
777 assert(num_comps == 1 || num_comps == 3);
780 if (bit_depth[0] != 0)
781 if (num_bit_depths < num_comps)
782 for (
ojph::ui32 c = num_bit_depths; c < num_comps; ++c)
783 bit_depth[c] = bit_depth[num_bit_depths - 1];
785 bool all_the_same =
true;
787 all_the_same = all_the_same
788 && bit_depth[0] == bit_depth[1]
789 && bit_depth[1] == bit_depth[2];
792 if (bit_depth[c] == 0)
805 if (num_precincts != -1)
810 if (employ_color_transform != -1)
812 "-colour_trans option is not needed and was not used; "
813 "this is because the image has one component only\n");
817 if (employ_color_transform == -1)
824 const float min_step = 1.0f / 16384.0f;
825 if (quantization_step == -1.0f)
826 quantization_step = min_step;
828 quantization_step =
ojph_max(quantization_step, min_step);
849 if (profile_string[0] !=
'\0')
852 tileparts_at_components);
855 if (dims.
w != 0 || dims.
h != 0)
857 "-dims option is not needed and was not used\n");
858 if (num_components != 0)
860 "-num_comps is not needed and was not used\n");
861 if (is_signed[0] != -1)
863 "-signed is not needed and was not used\n");
864 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
866 "-downsamp is not needed and was not used\n");
870#ifdef OJPH_ENABLE_TIFF_SUPPORT
873 tif.open(input_filename);
876 image_offset.
y + tif.get_size().h));
877 ojph::ui32 num_comps = tif.get_num_components();
879 if(num_bit_depths > 0 )
880 tif.set_bit_depth(num_bit_depths, bit_depth);
883 tif.get_bit_depth(c), tif.get_is_signed(c));
891 if (num_precincts != -1)
894 if (employ_color_transform == -1 && num_comps >= 3)
899 if (!reversible && quantization_step != -1)
902 if (profile_string[0] !=
'\0')
905 tileparts_at_components);
908 if (dims.
w != 0 || dims.
h != 0)
910 "-dims option is not needed and was not used\n");
911 if (num_components != 0)
913 "-num_comps is not needed and was not used\n");
914 if (is_signed[0] != -1)
916 "-signed is not needed and was not used\n");
917 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
919 "-downsamp is not needed and was not used\n");
927 if (dims.
w == 0 || dims.
h == 0)
929 "-dims option must have positive dimensions\n");
931 image_offset.
y + dims.
h));
932 if (num_components <= 0)
934 "-num_comps option is missing and must be provided\n");
935 if (num_is_signed <= 0)
937 "-signed option is missing and must be provided\n");
938 if (num_bit_depths <= 0)
940 "-bit_depth option is missing and must be provided\n");
941 if (num_comp_downsamps <= 0)
943 "-downsamp option is missing and must be provided\n");
949 ojph::ui32 last_signed_idx = 0, last_bit_depth_idx = 0;
952 for (
ojph::ui32 c = 0; c < num_components; ++c)
955 [c < num_comp_downsamps ? c : last_downsamp_idx];
956 last_downsamp_idx += last_downsamp_idx+1 < num_comp_downsamps ? 1:0;
957 ojph::ui32 bd = bit_depth[c<num_bit_depths ? c : last_bit_depth_idx];
958 last_bit_depth_idx += last_bit_depth_idx + 1 < num_bit_depths ? 1:0;
959 int is = is_signed[c < num_is_signed ? c : last_signed_idx];
960 last_signed_idx += last_signed_idx + 1 < num_is_signed ? 1 : 0;
970 if (num_precincts != -1)
973 if (employ_color_transform == -1)
977 "We currently do not support color transform on raw(yuv) files."
978 " In any case, this not a normal usage scenario. The OpenJPH "
979 "library however does support that, but ojph_compress.cpp must be "
980 "modified to send all lines from one component before moving to "
981 "the next component; this requires buffering components outside"
982 " of the OpenJPH library");
984 if (!reversible && quantization_step != -1.0f)
987 if (profile_string[0] !=
'\0')
990 tileparts_at_components);
993 yuv.
open(input_filename);
999 if (dims.
w == 0 || dims.
h == 0)
1001 "-dims option must have positive dimensions\n");
1003 image_offset.
y + dims.
h));
1004 if (num_components != 1)
1006 "-num_comps must be 1\n");
1007 if (num_is_signed <= 0)
1009 "-signed option is missing and must be provided\n");
1010 if (num_bit_depths <= 0)
1012 "-bit_depth option is missing and must be provided\n");
1013 if (num_comp_downsamps <= 0)
1015 "-downsamp option is missing and must be provided\n");
1020 siz.
set_component(0, comp_downsampling[0], bit_depth[0], is_signed[0]);
1028 if (num_precincts != -1)
1031 if (employ_color_transform != -1)
1033 "color transform is meaningless since .raw files are single "
1036 if (!reversible && quantization_step != -1.0f)
1039 if (profile_string[0] !=
'\0')
1042 tileparts_at_components);
1045 raw.
open(input_filename);
1050 dpx.
open(input_filename);
1068 if (num_precincts != -1)
1071 if (employ_color_transform == -1 && num_comps >= 3)
1076 if (!reversible && quantization_step != -1)
1079 if (profile_string[0] !=
'\0')
1082 tileparts_at_components);
1085 if (dims.
w != 0 || dims.
h != 0)
1087 "-dims option is not needed and was not used\n");
1088 if (num_components != 0)
1090 "-num_comps is not needed and was not used\n");
1091 if (is_signed[0] != -1)
1093 "-signed is not needed and was not used\n");
1094 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
1096 "-downsamp is not needed and was not used\n");
1101#if defined( OJPH_ENABLE_TIFF_SUPPORT)
1103 "unknown input file extension; only pgm, ppm, dpx, tif(f),"
1104 " or raw(yuv) are supported\n");
1107 "unknown input file extension; only pgm, ppm, dpx,"
1108 " or raw(yuv) are supported\n");
1113 "Please supply a proper input filename with a proper three-letter "
1120 j2c_file.
open(output_filename);
1121 codestream.
write_headers(&j2c_file, &com_ex, com_string ? 1 : 0);
1135 assert(c == next_comp);
1136 base->
read(cur_line, next_comp);
1137 cur_line = codestream.
exchange(cur_line, next_comp);
1150 assert(c == next_comp);
1151 base->
read(cur_line, next_comp);
1152 cur_line = codestream.
exchange(cur_line, next_comp);
1161 if (max_num_comps != initial_num_comps)
1163 delete[] comp_downsampling;
1168 catch (
const std::exception& e)
1170 const char *p = e.what();
1171 if (strncmp(p,
"ojph error", 10) != 0)
1176 clock_t end = clock();
1177 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
1178 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)