492int main(
int argc,
char * argv[]) {
493 char *input_filename = NULL;
494 char *output_filename = NULL;
495 char prog_order_store[] =
"RPCL";
496 char *prog_order = prog_order_store;
497 char profile_string_store[] =
"";
498 char *profile_string = profile_string_store;
499 char *com_string = NULL;
501 float quantization_step = -1.0f;
503 bool reversible =
false;
504 int employ_color_transform = -1;
506 const int max_precinct_sizes = 33;
508 int num_precincts = -1;
519 ojph::si32 is_signed_store[initial_num_comps] = {-1, -1, -1, -1};
522 ojph::ui32 bit_depth_store[initial_num_comps] = {0, 0, 0, 0};
526 ojph::point *comp_downsampling = downsampling_store;
527 bool tlm_marker =
false;
528 bool tileparts_at_resolutions =
false;
529 bool tileparts_at_components =
false;
533 "\nThe following arguments are necessary:\n"
534#ifdef OJPH_ENABLE_TIFF_SUPPORT
535 " -i input file name (either pgm, ppm, pfm, tif(f), or raw(yuv))\n"
537 " -i input file name (either pgm, ppm, pfm, or raw(yuv))\n"
539 " -o output file name\n\n"
541 "The following option has a default value (optional):\n"
542 " -num_decomps (5) number of decompositions\n"
543 " -qstep (0.00001...0.5) quantization step size for lossy\n"
544 " compression; quantization steps size for all subbands are\n"
545 " derived from this value. {The default value for 8bit\n"
546 " images is 0.0039}\n"
547 " -qfactor (1...100) compression quality factor; 1 is worst\n"
548 " quality and 100 is best quality. Only valid for\n"
549 " images with 1 or 3 components. Cannot be used\n"
550 " together with -qstep.\n"
551 " -reversible <true | false> If this is 'false', an irreversible or\n"
552 " lossy compression is employed, using the 9/7 wavelet\n"
553 " transform; if 'true', a reversible compression is\n"
554 " performed, where the 5/3 wavelet is used.\n"
555 " Default value is 'false'.\n"
556 " -colour_trans <true | false> This option employs a color transform,\n"
557 " to transform RGB color images into the YUV domain.\n"
558 " This option should NOT be used with YUV images, because\n"
559 " they have already been transformed.\n"
560 " If there are three color components that are\n"
561 " downsampled by the same amount then this option can be\n"
562 " 'true' or 'false'. This option is also available when\n"
563 " there are more than three colour components, where it is\n"
564 " applied to the first three colour components.\n"
565 " -prog_order (RPCL) is the progression order, and can be one of:\n"
566 " LRCP, RLCP, RPCL, PCRL, CPRL.\n"
567 " -block_size {x,y} (64,64) where x and y are the height and width of\n"
568 " a codeblock. In unix-like environment, { and } must be\n"
569 " preceded by a ""\\"".\n"
570 " -precincts {x,y},{x,y},...,{x,y} where {x,y} is the precinct size\n"
571 " starting from the coarsest resolution; the last precinct\n"
572 " is repeated for all finer resolutions\n"
573 " -tile_offset {x,y} tile offset. \n"
574 " -tile_size {x,y} tile width and height. \n"
575 " -image_offset {x,y} image offset from origin. \n"
576 " -tileparts (None) employ tilepart divisions at each resolution, \n"
577 " indicated by the letter R, and/or component, indicated \n"
578 " by the letter C. For both, use \"-tileparts RC\".\n"
579 " -tlm_marker <true | false> if 'true', a TLM marker is inserted.\n"
580 " Default value is false.\n"
581 " -profile (None) is the profile, the code will check if the \n"
582 " selected options meet the profile. Currently only \n"
583 " BROADCAST and IMF are supported. This automatically \n"
584 " sets tlm_marker to true and tileparts to C.\n"
585 " -com (None) if set, inserts a COM marker with the specified\n"
586 " string. If the string has spaces, please use\n"
587 " double quotes, as in -com \"This is a comment\".\n"
590 "When the input file is a YUV file, these arguments need to be \n"
592 " -dims {x,y} x is image width, y is height\n"
593 " -num_comps number of components\n"
594 " -signed a comma-separated list of true or false parameters, one\n"
595 " for each component; for example: true,false,false\n"
596 " -bit_depth a comma-separated list of bit depth values, one per \n"
597 " component; for example: 12,10,10\n"
598 " -downsamp {x,y},{x,y},...,{x,y} a list of x,y points, one for each\n"
599 " component; for example {1,1},{2,2},{2,2}\n\n"
602 ".pfm files receive special treatment. Currently, lossy compression\n"
603 "with these files is not supported, only lossless. When these files are\n"
604 "used, the NLT segment marker is automatically inserted into the\n"
605 "codestream when needed, as explained shortly. The following arguments\n"
606 "can be useful for this file type.\n"
607 " -signed a comma-separated list of true or false parameters, one\n"
608 " for each component; for example: true,false,false.\n"
609 " If you are sure that all sample values are positive or 0,\n"
610 " set the corresponding entry to false; otherwise set it to\n"
612 " When a component entry is set to true, an NLT segment\n"
613 " marker segment is inserted into the codestream.\n"
614 " The NLT segment specifies a non-linear transform that\n"
615 " changes only negative values, producing better coding\n"
617 " The NLT segment marker might be less supported in other\n"
619 " -bit_depth a comma-separated list of bit depth values, one per \n"
620 " component; for example: 12,10,10.\n"
621 " Floating value numbers are treated as integers, and they\n"
622 " are shifted to the right, keeping only the specified\n"
623 " number of bits. Up to 32 bits (which is the default) are\n"
629 if (!
get_arguments(argc, argv, input_filename, output_filename,
630 prog_order, profile_string, num_decompositions,
631 quantization_step, qfactor, reversible,
632 employ_color_transform,
633 max_precinct_sizes, num_precincts, precinct_size,
634 block_size, dims, image_offset, tile_size, tile_offset,
635 max_num_comps, num_components,
636 num_comp_downsamps, comp_downsampling,
637 num_bit_depths, bit_depth, num_is_signed, is_signed,
638 tlm_marker, tileparts_at_resolutions,
639 tileparts_at_components, com_string))
644 if (qfactor != -1 && quantization_step != -1.0f)
646 "-qfactor and -qstep cannot be used together\n");
647 if (qfactor != -1 && (qfactor < 1 || qfactor > 100))
649 "-qfactor must be between 1 and 100\n");
651 clock_t begin = clock();
662#ifdef OJPH_ENABLE_TIFF_SUPPORT
667 if (input_filename == NULL)
668 OJPH_ERROR(0x01000007,
"please specify an input file name using"
669 " the -i command line option");
670 if (output_filename == NULL)
671 OJPH_ERROR(0x01000008,
"please specify an output file name using"
672 " the -o command line option");
679 ppm.
open(input_filename);
684 assert(num_comps == 1);
696 if (num_precincts != -1)
701 if (!reversible && quantization_step != -1.0f)
703 if (!reversible && qfactor != -1)
705 if (profile_string[0] !=
'\0')
708 tileparts_at_components);
711 if (employ_color_transform != -1)
713 "-colour_trans option is not needed and was not used\n");
714 if (dims.
w != 0 || dims.
h != 0)
716 "-dims option is not needed and was not used\n");
717 if (num_components != 0)
719 "-num_comps is not needed and was not used\n");
720 if (is_signed[0] != -1)
722 "-signed is not needed and was not used\n");
723 if (bit_depth[0] != 0)
725 "-bit_depth is not needed and was not used\n");
726 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
728 "-downsamp is not needed and was not used\n");
734 ppm.
open(input_filename);
739 assert(num_comps == 3);
751 if (num_precincts != -1)
754 if (employ_color_transform == -1)
759 if (!reversible && quantization_step != -1.0f)
761 if (!reversible && qfactor != -1)
764 if (profile_string[0] !=
'\0')
767 tileparts_at_components);
770 if (dims.
w != 0 || dims.
h != 0)
772 "-dims option is not needed and was not used\n");
773 if (num_components != 0)
775 "-num_comps is not needed and was not used\n");
776 if (is_signed[0] != -1)
778 "-signed is not needed and was not used\n");
779 if (bit_depth[0] != 0)
781 "-bit_depth is not needed and was not used\n");
782 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
784 "-downsamp is not needed and was not used\n");
790 pfm.
open(input_filename);
795 assert(num_comps == 1 || num_comps == 3);
798 if (bit_depth[0] != 0)
799 if (num_bit_depths < num_comps)
800 for (
ojph::ui32 c = num_bit_depths; c < num_comps; ++c)
801 bit_depth[c] = bit_depth[num_bit_depths - 1];
803 bool all_the_same =
true;
805 all_the_same = all_the_same
806 && bit_depth[0] == bit_depth[1]
807 && bit_depth[1] == bit_depth[2];
810 if (bit_depth[c] == 0)
823 if (num_precincts != -1)
828 if (employ_color_transform != -1)
830 "-colour_trans option is not needed and was not used; "
831 "this is because the image has one component only\n");
835 if (employ_color_transform == -1)
841 if (!reversible && qfactor != -1)
843 else if (!reversible) {
844 const float min_step = 1.0f / 16384.0f;
845 if (quantization_step == -1.0f)
846 quantization_step = min_step;
848 quantization_step =
ojph_max(quantization_step, min_step);
869 if (profile_string[0] !=
'\0')
872 tileparts_at_components);
875 if (dims.
w != 0 || dims.
h != 0)
877 "-dims option is not needed and was not used\n");
878 if (num_components != 0)
880 "-num_comps is not needed and was not used\n");
881 if (is_signed[0] != -1)
883 "-signed is not needed and was not used\n");
884 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
886 "-downsamp is not needed and was not used\n");
890#ifdef OJPH_ENABLE_TIFF_SUPPORT
893 tif.open(input_filename);
896 image_offset.
y + tif.get_size().h));
897 ojph::ui32 num_comps = tif.get_num_components();
899 if(num_bit_depths > 0 )
900 tif.set_bit_depth(num_bit_depths, bit_depth);
903 tif.get_bit_depth(c), tif.get_is_signed(c));
911 if (num_precincts != -1)
914 if (employ_color_transform == -1 && num_comps >= 3)
919 if (!reversible && quantization_step != -1)
921 if (!reversible && qfactor != -1) {
922 if (num_comps != 1 && num_comps != 3)
924 "-qfactor is only supported for images with 1 or 3 "
930 if (profile_string[0] !=
'\0')
933 tileparts_at_components);
936 if (dims.
w != 0 || dims.
h != 0)
938 "-dims option is not needed and was not used\n");
939 if (num_components != 0)
941 "-num_comps is not needed and was not used\n");
942 if (is_signed[0] != -1)
944 "-signed is not needed and was not used\n");
945 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
947 "-downsamp is not needed and was not used\n");
955 if (dims.
w == 0 || dims.
h == 0)
957 "-dims option must have positive dimensions\n");
959 image_offset.
y + dims.
h));
960 if (num_components <= 0)
962 "-num_comps option is missing and must be provided\n");
963 if (num_is_signed <= 0)
965 "-signed option is missing and must be provided\n");
966 if (num_bit_depths <= 0)
968 "-bit_depth option is missing and must be provided\n");
969 if (num_comp_downsamps <= 0)
971 "-downsamp option is missing and must be provided\n");
977 ojph::ui32 last_signed_idx = 0, last_bit_depth_idx = 0;
980 for (
ojph::ui32 c = 0; c < num_components; ++c)
983 [c < num_comp_downsamps ? c : last_downsamp_idx];
984 last_downsamp_idx += last_downsamp_idx+1 < num_comp_downsamps ? 1:0;
985 ojph::ui32 bd = bit_depth[c<num_bit_depths ? c : last_bit_depth_idx];
986 last_bit_depth_idx += last_bit_depth_idx + 1 < num_bit_depths ? 1:0;
987 int is = is_signed[c < num_is_signed ? c : last_signed_idx];
988 last_signed_idx += last_signed_idx + 1 < num_is_signed ? 1 : 0;
998 if (num_precincts != -1)
1001 if (employ_color_transform == -1)
1005 "We currently do not support color transform on raw(yuv) files."
1006 " In any case, this not a normal usage scenario. The OpenJPH "
1007 "library however does support that, but ojph_compress.cpp must be "
1008 "modified to send all lines from one component before moving to "
1009 "the next component; this requires buffering components outside"
1010 " of the OpenJPH library");
1012 if (!reversible && quantization_step != -1.0f)
1014 if (!reversible && qfactor != -1) {
1015 if (num_components != 1 && num_components != 3)
1017 "-qfactor is only supported for images with 1 or 3 "
1022 if (profile_string[0] !=
'\0')
1025 tileparts_at_components);
1028 yuv.
open(input_filename);
1034 if (dims.
w == 0 || dims.
h == 0)
1036 "-dims option must have positive dimensions\n");
1038 image_offset.
y + dims.
h));
1039 if (num_components != 1)
1041 "-num_comps must be 1\n");
1042 if (num_is_signed <= 0)
1044 "-signed option is missing and must be provided\n");
1045 if (num_bit_depths <= 0)
1047 "-bit_depth option is missing and must be provided\n");
1048 if (num_comp_downsamps <= 0)
1050 "-downsamp option is missing and must be provided\n");
1055 siz.
set_component(0, comp_downsampling[0], bit_depth[0], is_signed[0]);
1063 if (num_precincts != -1)
1066 if (employ_color_transform != -1)
1068 "color transform is meaningless since .raw files are single "
1071 if (!reversible && quantization_step != -1.0f)
1073 if (!reversible && qfactor != -1)
1076 if (profile_string[0] !=
'\0')
1079 tileparts_at_components);
1082 raw.
open(input_filename);
1087 dpx.
open(input_filename);
1105 if (num_precincts != -1)
1108 if (employ_color_transform == -1 && num_comps >= 3)
1113 if (!reversible && quantization_step != -1)
1115 if (!reversible && qfactor != -1) {
1116 if (num_comps != 1 && num_comps != 3)
1118 "-qfactor is only supported for images with 1 or 3 "
1123 if (profile_string[0] !=
'\0')
1126 tileparts_at_components);
1129 if (dims.
w != 0 || dims.
h != 0)
1131 "-dims option is not needed and was not used\n");
1132 if (num_components != 0)
1134 "-num_comps is not needed and was not used\n");
1135 if (is_signed[0] != -1)
1137 "-signed is not needed and was not used\n");
1138 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
1140 "-downsamp is not needed and was not used\n");
1145#if defined( OJPH_ENABLE_TIFF_SUPPORT)
1147 "unknown input file extension; only pgm, ppm, dpx, tif(f),"
1148 " or raw(yuv) are supported\n");
1151 "unknown input file extension; only pgm, ppm, dpx,"
1152 " or raw(yuv) are supported\n");
1157 "Please supply a proper input filename with a proper three-letter "
1164 j2c_file.
open(output_filename);
1165 codestream.
write_headers(&j2c_file, &com_ex, com_string ? 1 : 0);
1179 assert(c == next_comp);
1180 base->
read(cur_line, next_comp);
1181 cur_line = codestream.
exchange(cur_line, next_comp);
1194 assert(c == next_comp);
1195 base->
read(cur_line, next_comp);
1196 cur_line = codestream.
exchange(cur_line, next_comp);
1205 if (max_num_comps != initial_num_comps)
1207 delete[] comp_downsampling;
1212 catch (
const std::exception& e)
1214 const char *p = e.what();
1215 if (strncmp(p,
"ojph error", 10) != 0)
1220 clock_t end = clock();
1221 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
1222 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, int &qfactor, 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)