📄 kdu_compress.cpp
字号:
/*****************************************************************************//* STATIC get_bpp_dims *//*****************************************************************************/static int get_bpp_dims(siz_params *siz){ int comps, max_width, max_height, n; siz->get(Scomponents,0,0,comps); max_width = max_height = 0; for (n=0; n < comps; n++) { int width, height; siz->get(Sdims,n,0,height); siz->get(Sdims,n,1,width); if (width > max_width) max_width = width; if (height > max_height) max_height = height; } return max_height * max_width;}/*****************************************************************************//* STATIC check_jp2_suffix *//*****************************************************************************/static bool check_jp2_suffix(char *fname) /* Returns true if the file-name has the suffix, ".jp2", where the check is case insensitive. */{ char *cp = strrchr(fname,'.'); if (cp == NULL) return false; cp++; if ((*cp != 'j') && (*cp != 'J')) return false; cp++; if ((*cp != 'p') && (*cp != 'P')) return false; cp++; if (*cp != '2') return false; return true;}/*****************************************************************************//* STATIC set_jp2_attributes *//*****************************************************************************/static void set_jp2_attributes(jp2_target &jp2_out, siz_params *siz, kdu_rgb8_palette &palette, int num_components, kdu_args &args){ // Set dimensional information (all redundant with the SIZ marker segment) jp2_dimensions dims = jp2_out.access_dimensions(); dims.init(siz); // Set resolution information (optional) if (args.find("-jp2_aspect") != NULL) { float aspect_ratio; char *string = args.advance(); if ((string == NULL) || (sscanf(string,"%f",&aspect_ratio) != 1) || (aspect_ratio <= 0.0F)) { kdu_error e; e << "Missing or illegal aspect ratio " "parameter supplied with the `-jp2_aspect' argument!"; } args.advance(); jp2_resolution res = jp2_out.access_resolution(); res.init(aspect_ratio); } // Set colour space information (mandatory) jp2_colour colour = jp2_out.access_colour(); int num_colours = (num_components < 3)?1:3; if (palette.exists()) num_colours = (palette.is_monochrome())?1:3; if (args.find("-jp2_space") != NULL) { char *string = args.advance(); if (string == NULL) { kdu_error e; e << "The `-jp2_space' argument requires a parameter " "string!"; } char *delim = strchr(string,','); if (delim != NULL) *delim = '\0'; if (strcmp(string,"sRGB") == 0) colour.init(JP2_sRGB_SPACE); else if (strcmp(string,"sYCC") == 0) colour.init(JP2_sYCC_SPACE); else if (strcmp(string,"sLUM") == 0) colour.init(JP2_sLUM_SPACE); else if (strcmp(string,"iccLUM") == 0) { float gamma, beta; string = delim+1; if ((delim == NULL) || ((delim = strchr(string,',')) == NULL) || ((*delim = '\0') > 0) || (sscanf(string,"%f",&gamma) != 1) || (sscanf(delim+1,"%f",&beta) != 1) || (gamma < 1.0F) || (beta < 0.0F) || (beta >= 1.0F)) { kdu_error e; e << "Missing or illegal gamma/beta parameters " "supplied in comma-separated parameter list which must follow " "the \"sLUM\" JP2 colour space specification supplied via the " "`-jp2_space' argument. `gamma' must be no less than 1 and " "`beta' must be in the range 0 to 1."; } colour.init(gamma,beta); } else if (strcmp(string,"iccRGB") == 0) { float val, gamma, beta, matrix3x3[9]; for (int p=0; p < 11; p++) { string = delim+1; if ((delim == NULL) || ((p < 10) && ((delim = strchr(string,',')) == NULL)) || ((*delim = '\0') > 0) || (sscanf(string,"%f",&val) != 1)) { kdu_error e; e << "The \"iccRGB\" specification must be " "followed immediately by a comma-separated list of 11 " "parameters, all within the single parameter string " "supplied with the `-jp2_space' argument. For more details " "review the usage statement."; } if (p == 0) gamma = val; else if (p == 1) beta = val; else matrix3x3[p-2] = val; } if ((gamma < 1.0F) || (beta < 0.0F) || (beta >= 1.0F)) { kdu_error e; e << "Illegal gamma or beta value supplied in the " "\"iccRGB\" specification following the `-jp2_space' argument. " "`gamma' must not be less than 1 and `beta' must be in the " "range 0 to 1."; } colour.init(matrix3x3,gamma,beta); } else { kdu_error e; e << "Invalid parameter string following `-jp2_space' " "argument. The string must identify the colour space as one of " "\"sLUM\", \"sRGB\", \"sYCC\", \"iccLUM\" or \"iccRGB\"."; } args.advance(); if (colour.get_num_colours() > num_colours) { kdu_error e; e << "Colour space specified using `-jp2_space' " "requires more image components or a colour palette."; } } else colour.init((num_colours==3)?JP2_sRGB_SPACE:JP2_sLUM_SPACE); // Set the colour palette and channel mapping as needed. if (palette.exists()) { jp2_palette pclr = jp2_out.access_palette(); if (palette.is_monochrome()) { pclr.init(1,1<<palette.input_bits); pclr.set_lut(0,palette.red,palette.output_bits); assert(num_colours == 1); jp2_channels channels = jp2_out.access_channels(); channels.init(1); channels.set_colour_mapping(0,palette.source_component,0); } else { pclr.init(3,1<<palette.input_bits); pclr.set_lut(0,palette.red,palette.output_bits); pclr.set_lut(1,palette.green,palette.output_bits); pclr.set_lut(2,palette.blue,palette.output_bits); assert(num_colours == 3); jp2_channels channels = jp2_out.access_channels(); channels.init(3); channels.set_colour_mapping(0,palette.source_component,0); channels.set_colour_mapping(1,palette.source_component,1); channels.set_colour_mapping(2,palette.source_component,2); } }}/*****************************************************************************//* STATIC assign_layer_bytes *//*****************************************************************************/static int * assign_layer_bytes(kdu_args &args, siz_params *siz, int &num_specs) /* Returns a pointer to an array of `num_specs' quality layer byte targets. The value of `num_specs' is determined in this function, based on the number of rates (or slopes) specified on the command line, together with any knowledge about the number of desired quality layers. Before calling this function, you must parse all parameter attribute strings into the code-stream parameter lists rooted at `siz'. Note that the returned array will contain 0's whenever a quality layer's bit-rate is unspecified. This allows the compressor's rate allocator to assign the target size for those quality layers on the fly. */{ char *cp; char *string = NULL; int arg_specs = 0; int slope_specs = 0; int cod_specs = 0; if (args.find("-slope") != NULL) { string = args.advance(false); // Need to process this arg again later. if (string != NULL) { while (string != NULL) { slope_specs++; string = strchr(string+1,','); } } } // Determine how many rates are specified on the command-line if (args.find("-rate") != NULL) { string = args.advance(); if (string == NULL) { kdu_error e; e << "\"-rate\" argument must be followed by a " "string identifying one or more bit-rates, separated by commas."; } cp = string; while (cp != NULL) { arg_specs++; cp = strchr(cp,','); if (cp != NULL) cp++; } } // Find the number of layers specified by the main COD marker kdu_params *cod = siz->access_cluster(COD_params); assert(cod != NULL); cod->get(Clayers,0,0,cod_specs); if (!cod_specs) cod_specs = (arg_specs>slope_specs)?arg_specs:slope_specs; num_specs = cod_specs; if (num_specs == 0) num_specs = 1; if ((arg_specs != num_specs) && ((arg_specs > 2) || ((arg_specs == 2) && (num_specs == 1)))) { kdu_error e; e << "The relationship between the number of bit-rates " "specified by the \"-rate\" argument and the number of quality layers " "explicitly specified via \"Clayers\" does not conform to the rules " "supplied in the description of the \"-rate\" argument. Use \"-u\" " "to print the usage statement."; } cod->set(Clayers,0,0,num_specs); int n; int *result = new int[num_specs]; for (n=0; n < num_specs; n++) result[n] = 0; int total_pels = get_bpp_dims(siz); bool have_dash = false; for (n=0; n < arg_specs; n++) { cp = strchr(string,','); if (cp != NULL) *cp = '\0'; // Temporarily terminate string. if (strcmp(string,"-") == 0) { have_dash = true; result[n] = INT_MAX; } else { double bpp; if ((!sscanf(string,"%lf",&bpp)) || (bpp <= 0.0)) { kdu_error e; e << "Illegal sub-string encoutered in parameter " "string supplied to the \"-rate\" argument. Rate parameters " "must be strictly positive real numbers, with multiple " "parameters separated by commas only. Problem encountered at " "sub-string: \"" << string << "\"."; } result[n] = (int) floor(bpp * 0.125 * total_pels); } if (cp != NULL) { *cp = ','; string = cp+1; } } if (arg_specs) { // Bubble sort the supplied specs. bool done = false; while (!done) { // Use trivial bubble sort. done = true; for (int n=1; n < arg_specs; n++) if (result[n-1] > result[n]) { // Swap misordered pair. int tmp=result[n]; result[n]=result[n-1];
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -