60 const char *next_char = str;
66 if (*next_char !=
',')
67 throw "sizes in a sizes list must be separated by a comma";
71 if (*next_char !=
'{')
72 throw "size must start with {";
76 if (endptr == next_char)
77 throw "size number is improperly formatted";
79 if (*next_char !=
',')
80 throw "size must have a "","" between the two numbers";
83 if (endptr == next_char)
84 throw "number is improperly formatted";
86 if (*next_char !=
'}')
87 throw "size must end with }";
96 throw "size elements must separated by a "",""";
99 throw "there are too many elements in the size list";
118 const char *next_char = str;
124 if (*next_char !=
',')
125 throw "sizes in a sizes list must be separated by a comma";
129 if (*next_char !=
'{')
130 throw "size must start with {";
134 if (endptr == next_char)
135 throw "point number is improperly formatted";
137 if (*next_char !=
',')
138 throw "point must have a "","" between the two numbers";
141 if (endptr == next_char)
142 throw "number is improperly formatted";
144 if (*next_char !=
'}')
145 throw "point must end with }";
154 throw "size elements must separated by a "",""";
157 throw "there are too many elements in the size list";
171 const char *next_char = str;
172 if (*next_char !=
'{')
173 throw "size must start with {";
177 if (endptr == next_char)
178 throw "size number is improperly formatted";
180 if (*next_char !=
',')
181 throw "size must have a "","" between the two numbers";
184 if (endptr == next_char)
185 throw "number is improperly formatted";
187 if (*next_char !=
'}')
188 throw "size must end with }";
190 if (*next_char !=
'\0')
191 throw "size has extra characters";
202 const char *next_char = str;
203 if (*next_char !=
'{')
204 throw "size must start with {";
208 if (endptr == next_char)
209 throw "size number is improperly formatted";
211 if (*next_char !=
',')
212 throw "size must have a "","" between the two numbers";
215 if (endptr == next_char)
216 throw "number is improperly formatted";
218 if (*next_char !=
'}')
219 throw "size must end with }";
221 if (*next_char !=
'\0')
222 throw "size has extra characters";
239 const char *next_char = str;
245 if (*next_char !=
',')
246 throw "sizes in a sizes list must be separated by a comma";
251 if (endptr == next_char)
252 throw "size number is improperly formatted";
260 throw "list elements must separated by a "",""";
263 throw "there are too many elements in the size list";
282 const char *next_char = str;
288 if (*next_char !=
',')
289 throw "sizes in a sizes list must be separated by a comma";
292 if (strncmp(next_char,
"true", 4) == 0)
297 else if (strncmp(next_char,
"false", 5) == 0)
303 throw "unknown bool value";
310 throw "size elements must separated by a "",""";
313 throw "there are too many elements in the size list";
333 size_t len = strlen(str);
334 if (len == 1 && strncmp(str,
"C", 2) == 0)
339 else if (len == 1 && strncmp(str,
"R", 2) == 0)
345 (strncmp(str,
"RC", 3) == 0 || strncmp(str,
"CR", 3) == 0))
351 throw "could not interpret -tileparts fields; allowed values are "
352 "\"R\" \"C\" and \"RC\"";
362 char *&output_filename,
char *&progression_order,
363 char *&profile_string,
ojph::ui32 &num_decompositions,
364 float &quantization_step,
bool &reversible,
365 int &employ_color_transform,
366 const int max_num_precincts,
int &num_precincts,
374 bool& tlm_marker,
bool& tileparts_at_resolutions,
375 bool& tileparts_at_components,
char *&com_string)
378 interpreter.
init(argc, argv);
382 interpreter.
reinterpret(
"-prog_order", progression_order);
383 interpreter.
reinterpret(
"-profile", profile_string);
384 interpreter.
reinterpret(
"-num_decomps", num_decompositions);
385 interpreter.
reinterpret(
"-qstep", quantization_step);
386 interpreter.
reinterpret(
"-reversible", reversible);
389 interpreter.
reinterpret(
"-tlm_marker", tlm_marker);
398 throw "more than 255 components is not supported";
399 if (num_comps > max_num_comps)
401 max_num_comps = num_comps;
421 tileparts_at_components);
424 interpreter.
reinterpret(
"-block_size", &block_interpreter);
425 interpreter.
reinterpret(
"-dims", &dims_interpreter);
426 interpreter.
reinterpret(
"-image_offset", &img_off_interpreter);
427 interpreter.
reinterpret(
"-tile_size", &tile_size_interpreter);
428 interpreter.
reinterpret(
"-tile_offset", &tile_off_interpreter);
433 interpreter.
reinterpret(
"-tileparts", &tp_div_interpreter);
435 catch (
const char *s)
442 printf(
"The following arguments were not interpreted:\n");
446 printf(
"%s\n", t.
arg);
458 size_t len = strlen(filename);
459 const char* p = strrchr(filename,
'.');
460 if (p == NULL || p == filename + len - 1)
462 "no file extension is found, or there are no characters "
463 "after the dot \'.\' for filename \"%s\" \n", filename);
471 size_t num_ele = strlen(ref);
473 if (num_ele != strlen(other))
477 if (ref[i] != other[i] && ref[i] != tolower(other[i]))
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, tif(f), or raw(yuv))\n"
531 " -i input file name (either pgm, ppm, 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 (false) for irreversible; this should be false to perform\n"
542 " lossy compression using the 9/7 wavelet transform;\n"
543 " or true to perform reversible compression, where\n"
544 " the 5/3 wavelet is employed with lossless compression.\n"
545 " -colour_trans (true) this option employs a color transform, to\n"
546 " transform RGB color images into the YUV domain.\n"
547 " This option should not be used with YUV images, because\n"
548 " they have already been transformed.\n"
549 " If there are three color components that are\n"
550 " downsampled by the same amount then the color transform\n"
551 " can be true or false. This option is also available\n"
552 " when there are more than three colour components,\n"
553 " where it is applied to the first three colour\n"
555 " it has already been applied to convert the original RGB\n"
556 " or whatever the original format to YUV.\n"
557 " -prog_order (RPCL) is the progression order, and can be one of:\n"
558 " LRCP, RLCP, RPCL, PCRL, CPRL\n"
559 " -block_size {x,y} (64,64) where x and y are the height and width of\n"
560 " a codeblock. In unix-like environment, { and } must be\n"
561 " proceeded by a ""\\""\n"
562 " -precincts {x,y},{x,y},...,{x,y} where {x,y} is the precinct size\n"
563 " starting from the coarest resolution; the last precinct\n"
564 " is repeated for all finer resolutions\n"
565 " -tile_offset {x,y} tile offset. \n"
566 " -tile_size {x,y} tile width and height. \n"
567 " -image_offset {x,y} image offset from origin. \n"
568 " -tileparts (None) employ tilepart divisions at each resolution, \n"
569 " indicated by the letter R, and/or component, indicated \n"
570 " by the letter C. For both, use \"-tileparts RC\".\n"
571 " -tlm_marker (false) insert a TLM marker, either \"true\" or \"false\"\n"
572 " -profile (None) is the profile, the code will check if the \n"
573 " selected options meet the profile. Currently only \n"
574 " BROADCAST and IMF are supported. This automatically \n"
575 " sets tlm_marker to true and tileparts to C.\n"
576 " -com (None) if set, inserts a COM marker with the specified\n"
577 " string. If the string has spaces, please use\n"
578 " double quotes, as in -com \"This is a comment\"\n"
581 "When the input file is a YUV file, these arguments need to be \n"
583 " -dims {x,y} x is image width, y is height\n"
584 " -num_comps number of components\n"
585 " -signed a comma-separated list of true or false parameters, one\n"
586 " for each component; for example: true,false,false\n"
587 " -bit_depth a comma-separated list of bit depth values, one per \n"
588 " component; for example: 12,10,10\n"
589 " -downsamp {x,y},{x,y},...,{x,y} a list of x,y points, one for each\n"
590 " component; for example {1,1},{2,2},{2,2}\n\n"
594 if (!
get_arguments(argc, argv, input_filename, output_filename,
595 prog_order, profile_string, num_decompositions,
596 quantization_step, reversible, employ_color_transform,
597 max_precinct_sizes, num_precincts, precinct_size,
598 block_size, dims, image_offset, tile_size, tile_offset,
599 max_num_comps, num_components,
600 num_comp_downsamps, comp_downsampling,
601 num_bit_depths, bit_depth, num_is_signed, is_signed,
602 tlm_marker, tileparts_at_resolutions,
603 tileparts_at_components, com_string))
608 clock_t begin = clock();
618#ifdef OJPH_ENABLE_TIFF_SUPPORT
623 if (input_filename == NULL)
624 OJPH_ERROR(0x01000007,
"please specify an input file name using"
625 " the -i command line option");
626 if (output_filename == NULL)
627 OJPH_ERROR(0x01000008,
"please specify an output file name using"
628 " the -o command line option");
635 ppm.
open(input_filename);
640 assert(num_comps == 1);
652 if (num_precincts != -1)
657 if (!reversible && quantization_step != -1.0f)
659 if (profile_string[0] !=
'\0')
662 tileparts_at_components);
665 if (employ_color_transform != -1)
667 "-colour_trans option is not needed and was not used\n");
668 if (dims.
w != 0 || dims.
h != 0)
670 "-dims option is not needed and was not used\n");
671 if (num_components != 0)
673 "-num_comps is not needed and was not used\n");
674 if (is_signed[0] != -1)
676 "-signed is not needed and was not used\n");
677 if (bit_depth[0] != 0)
679 "-bit_depth is not needed and was not used\n");
680 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
682 "-downsamp is not needed and was not used\n");
688 ppm.
open(input_filename);
693 assert(num_comps == 3);
705 if (num_precincts != -1)
708 if (employ_color_transform == -1)
713 if (!reversible && quantization_step != -1.0f)
716 if (profile_string[0] !=
'\0')
719 tileparts_at_components);
722 if (dims.
w != 0 || dims.
h != 0)
724 "-dims option is not needed and was not used\n");
725 if (num_components != 0)
727 "-num_comps is not needed and was not used\n");
728 if (is_signed[0] != -1)
730 "-signed is not needed and was not used\n");
731 if (bit_depth[0] != 0)
733 "-bit_depth is not needed and was not used\n");
734 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
736 "-downsamp is not needed and was not used\n");
740#ifdef OJPH_ENABLE_TIFF_SUPPORT
743 tif.
open(input_filename);
746 image_offset.
y + tif.get_size().h));
747 ojph::ui32 num_comps = tif.get_num_components();
749 if(num_bit_depths > 0 )
750 tif.set_bit_depth(num_bit_depths, bit_depth);
753 tif.get_bit_depth(c), tif.get_is_signed(c));
761 if (num_precincts != -1)
764 if (employ_color_transform == -1 && num_comps >= 3)
769 if (!reversible && quantization_step != -1)
772 if (profile_string[0] !=
'\0')
775 tileparts_at_components);
778 if (dims.
w != 0 || dims.
h != 0)
780 "-dims option is not needed and was not used\n");
781 if (num_components != 0)
783 "-num_comps is not needed and was not used\n");
784 if (is_signed[0] != -1)
786 "-signed is not needed and was not used\n");
787 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
789 "-downsamp is not needed and was not used\n");
797 if (dims.
w == 0 || dims.
h == 0)
799 "-dims option must have positive dimensions\n");
801 image_offset.
y + dims.
h));
802 if (num_components <= 0)
804 "-num_comps option is missing and must be provided\n");
805 if (num_is_signed <= 0)
807 "-signed option is missing and must be provided\n");
808 if (num_bit_depths <= 0)
810 "-bit_depth option is missing and must be provided\n");
811 if (num_comp_downsamps <= 0)
813 "-downsamp option is missing and must be provided\n");
819 ojph::ui32 last_signed_idx = 0, last_bit_depth_idx = 0;
822 for (
ojph::ui32 c = 0; c < num_components; ++c)
825 [c < num_comp_downsamps ? c : last_downsamp_idx];
826 last_downsamp_idx += last_downsamp_idx+1 < num_comp_downsamps ? 1:0;
827 ojph::ui32 bd = bit_depth[c<num_bit_depths ? c : last_bit_depth_idx];
828 last_bit_depth_idx += last_bit_depth_idx + 1 < num_bit_depths ? 1:0;
829 int is = is_signed[c < num_is_signed ? c : last_signed_idx];
830 last_signed_idx += last_signed_idx + 1 < num_is_signed ? 1 : 0;
840 if (num_precincts != -1)
843 if (employ_color_transform == -1)
847 "We currently do not support color transform on raw(yuv) files."
848 " In any case, this not a normal usage scenario. The OpenJPH "
849 "library however does support that, but ojph_compress.cpp must be "
850 "modified to send all lines from one component before moving to "
851 "the next component; this requires buffering components outside"
852 " of the OpenJPH library");
854 if (!reversible && quantization_step != -1.0f)
857 if (profile_string[0] !=
'\0')
860 tileparts_at_components);
863 yuv.
open(input_filename);
869 if (dims.
w == 0 || dims.
h == 0)
871 "-dims option must have positive dimensions\n");
873 image_offset.
y + dims.
h));
874 if (num_components != 1)
876 "-num_comps must be 1\n");
877 if (num_is_signed <= 0)
879 "-signed option is missing and must be provided\n");
880 if (num_bit_depths <= 0)
882 "-bit_depth option is missing and must be provided\n");
883 if (num_comp_downsamps <= 0)
885 "-downsamp option is missing and must be provided\n");
890 siz.
set_component(0, comp_downsampling[0], bit_depth[0], is_signed[0]);
898 if (num_precincts != -1)
901 if (employ_color_transform != -1)
903 "color transform is not meaningless since .raw files are single "
906 if (!reversible && quantization_step != -1.0f)
909 if (profile_string[0] !=
'\0')
912 tileparts_at_components);
915 raw.
open(input_filename);
920 dpx.
open(input_filename);
938 if (num_precincts != -1)
941 if (employ_color_transform == -1 && num_comps >= 3)
946 if (!reversible && quantization_step != -1)
949 if (profile_string[0] !=
'\0')
952 tileparts_at_components);
955 if (dims.
w != 0 || dims.
h != 0)
957 "-dims option is not needed and was not used\n");
958 if (num_components != 0)
960 "-num_comps is not needed and was not used\n");
961 if (is_signed[0] != -1)
963 "-signed is not needed and was not used\n");
964 if (comp_downsampling[0].x != 0 || comp_downsampling[0].y != 0)
966 "-downsamp is not needed and was not used\n");
971#if defined( OJPH_ENABLE_TIFF_SUPPORT)
973 "unknown input file extension; only pgm, ppm, dpx, tif(f),"
974 " or raw(yuv) are supported\n");
977 "unknown input file extension; only pgm, ppm, dpx,"
978 " or raw(yuv) are supported\n");
983 "Please supply a proper input filename with a proper three-letter "
990 j2c_file.
open(output_filename);
991 codestream.
write_headers(&j2c_file, &com_ex, com_string ? 1 : 0);
1005 assert(c == next_comp);
1006 base->
read(cur_line, next_comp);
1007 cur_line = codestream.
exchange(cur_line, next_comp);
1020 assert(c == next_comp);
1021 base->
read(cur_line, next_comp);
1022 cur_line = codestream.
exchange(cur_line, next_comp);
1031 if (max_num_comps != initial_num_comps)
1033 delete[] comp_downsampling;
1038 catch (
const std::exception& e)
1040 const char *p = e.what();
1041 if (strncmp(p,
"ojph error", 10) != 0)
1046 clock_t end = clock();
1047 double elapsed_secs = double(end - begin) / CLOCKS_PER_SEC;
1048 printf(
"Elapsed time = %f\n", elapsed_secs);
void init(int argc, char *argv[])
void reinterpret_to_bool(const char *str, int &val)
void reinterpret(const char *str, int &val)
argument get_argument_zero()
argument get_next_avail_argument(const argument &arg)
OJPH_EXPORT param_siz access_siz()
OJPH_EXPORT param_cod access_cod()
OJPH_EXPORT void set_planar(bool planar)
OJPH_EXPORT line_buf * exchange(line_buf *line, ui32 &next_component)
OJPH_EXPORT void request_tlm_marker(bool needed)
OJPH_EXPORT void set_profile(const char *s)
OJPH_EXPORT param_qcd access_qcd()
OJPH_EXPORT void set_tilepart_divisions(bool at_resolutions, bool at_components)
OJPH_EXPORT void write_headers(outfile_base *file, const comment_exchange *comments=NULL, ui32 num_comments=0)
OJPH_EXPORT bool is_planar() const
ui32 get_num_components()
bool get_is_signed(ui32 comp_num)
void open(const char *filename)
ui32 get_bit_depth(ui32 comp_num)
point get_comp_subsampling(ui32 comp_num)
virtual ui32 read(const line_buf *line, ui32 comp_num)=0
void open(const char *filename)
OJPH_EXPORT void set_num_decomposition(ui32 num_decompositions)
OJPH_EXPORT void set_precinct_size(int num_levels, size *precinct_size)
OJPH_EXPORT void set_progression_order(const char *name)
OJPH_EXPORT void set_block_dims(ui32 width, ui32 height)
OJPH_EXPORT void set_color_transform(bool color_transform)
OJPH_EXPORT void set_reversible(bool reversible)
OJPH_EXPORT void set_irrev_quant(float delta)
OJPH_EXPORT void set_tile_size(size s)
OJPH_EXPORT point get_image_extent() const
OJPH_EXPORT void set_component(ui32 comp_num, const point &downsampling, ui32 bit_depth, bool is_signed)
OJPH_EXPORT void set_num_components(ui32 num_comps)
OJPH_EXPORT void set_tile_offset(point offset)
OJPH_EXPORT point get_image_offset() const
OJPH_EXPORT void set_image_offset(point offset)
OJPH_EXPORT point get_downsampling(ui32 comp_num) const
OJPH_EXPORT void set_image_extent(point extent)
OJPH_EXPORT ui32 get_num_components() const
void open(const char *filename)
ui32 get_num_components()
ui32 get_bit_depth(ui32 comp_num)
point get_comp_subsampling(ui32 comp_num)
bool get_is_signed(ui32 comp_num)
void set_img_props(const size &s, ui32 bit_depth, bool is_signed)
void open(const char *filename)
void open(const char *filename)
void set_img_props(const size &s, ui32 num_components, ui32 num_downsampling, const point *downsampling)
void set_bit_depth(ui32 num_bit_depths, ui32 *bit_depth)
int main(int argc, char *argv[])
static const char * get_file_extension(const char *filename)
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)
static bool is_matching(const char *ref, const char *other)
#define ojph_div_ceil(a, b)
#define OJPH_ERROR(t,...)
point_interpreter(ojph::point &val)
virtual void operate(const char *str)
const ojph::ui32 max_num_eles
virtual void operate(const char *str)
point_list_interpreter(const ojph::ui32 max_num_elements, ojph::ui32 &num_elements, ojph::point *list)
si32_to_bool_list_interpreter(const ojph::ui32 max_num_elements, ojph::ui32 &num_elements, ojph::si32 *list)
const ojph::ui32 max_num_eles
ojph::ui32 get_num_elements()
virtual void operate(const char *str)
virtual void operate(const char *str)
size_interpreter(ojph::size &val)
virtual void operate(const char *str)
size_list_interpreter(const int max_num_elements, int &num_elements, ojph::size *list)
tileparts_division_interpreter(bool &at_resolutions, bool &at_components)
virtual void operate(const char *str)
virtual void operate(const char *str)
ui32_list_interpreter(const ojph::ui32 max_num_elements, ojph::ui32 &num_elements, ojph::ui32 *list)
const ojph::ui32 max_num_eles