⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 compress.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
  if ((p = cmdl->extract(cmdl,"-mem",-1,&params)) >= 0)
    {
      *memory_report = 1;
      if (p == 1)
        {
      if (sscanf(params[0],"%d",memory_stop_id) == 0)
        local_error("The `-mem' argument takes either no parameters "
            "or else an integer valued memory stop id; the "
            "supplied parameter, \"%s\", is illegal!",params[0]);
        }
      else if (p != 0)
    local_error("The `-mem' argument takes at most one parameter!");
    }
  if ((p = cmdl->extract(cmdl,"-quiet",-1,&params)) >= 0)
    {
      if (p != 0)
    local_error("The `-quiet' argument takes no parameters!");
      *verbose = 0;
    }
  if ((p = cmdl->extract(cmdl,"-verbose",-1,&params)) >= 0)
    {
      if (p != 0)
    local_error("The `-verbose' argument takes no parameters!");
      *verbose = 1;
    }
  
  /* SAIC/Fuji LRA begin */
  if ((lra_flag || tcq_flag) && (!no_lra_flag))
    {
      *Flra_flag = 1;
      *base_step = 1.0F;
    }
  /* SAIC/Fuji LRA end */
  
  if (!rate_flag && *Flra_flag) {
    local_error("Lagrangian rate allocation (default rate control for TCQ) "
        "cannot converge to the default rate of 100.0 BPP.  Please "
        "input a desired rate with the `-rate' argument.\n");
  }
  if (!rate_flag && tcq_flag) {
    local_error("TCQ with binary search rate control cannot converge to "
        "the default rate of 100.0 BPP.  Please input a desired "
        "rate with the `-rate' argument.\n");
  }
  
  if (*compressed_filename == NULL)
    local_error("Must supply a name for the compressed output file!");
}


/* ========================================================================= */
/* -------------------------- External Functions --------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* EXTERN                             main                                   */
/*****************************************************************************/

int
main(int argc, char *argv[])
{
    extern cmdl_ref create_std_cmdl(void);

    cmdl_ref cmdl;
    enc_constructor_info constructors;
    float max_bpp, base_step;
    /* SAIC/Fuji LRA begin */
    float low_rate_tol, hi_rate_tol;
    int Flra_flag = 0;
    int remove_stats = 0;
    int prev_header_size = 0, prev_remaining_bytes = 0;
    int low_bytes_tol, hi_bytes_tol;
    int tolerances_loosened = 0;
    int first_iteration_done = 0;
    /* SAIC/Fuji LRA end */
    int max_rate_iterations, memory_report, memory_stop_id, verbose;
    char *compressed_filename;
    int iterations, final_iteration, n;
    int found_max_step, found_min_step, remaining_bytes;
    float min_step, max_step;

    /* Initialize the memory system: must be done before any memory is
    allocated. */

    for(n=1;n<argc;n++)
        if(strcmp(argv[n],"-mem")==0)
            local_memory_collect_stats();

    /* Create `cmdl' object and constructors. */
    cmdl=create_std_cmdl();
    cmdl->initialize(cmdl,argc,argv);
    find_enc_constructors(&constructors,cmdl);

    /* Get the common arguments. */
    max_bpp = 100.0F;

    /* SAIC/Fuji LRA begin */
    low_rate_tol = 0.005F;
    hi_rate_tol = 0.0F;
    /* SAIC/Fuji LRA end */

    base_step = 1.0F / 128.0F;
    max_rate_iterations = 0;
    verbose = 1;
    memory_report = memory_stop_id = 0;
    compressed_filename = NULL;
    parse_common_arguments(&max_bpp,&low_rate_tol,&hi_rate_tol,
        &Flra_flag, /* SAIC/FUJI */
        &base_step,&max_rate_iterations,&verbose,&compressed_filename,
        &memory_report,&memory_stop_id,cmdl,argc,&constructors);
    if (memory_stop_id)
        local_memory_set_stop_id(memory_stop_id);

    /* Enter rate control loop.
    No actual loop unless `max_rate_iterations' != 0. */
    iterations = final_iteration = 0;
    found_max_step = found_min_step = 0;
    min_step = max_step = 0.0F; /* Avoid compiler warnings. */
    do {
        image_reader_ref reader;
        stream_out_ref stream;
        forward_info_ref info;
        encoder_ref encoder;
        forward_roi_ref roi;
        quantizer_ref quantizer;
        analysis_ref analysis;
        component_mix_ref mixer;
        int c, num_components;
        /* Begin Aerospace MCT mods (TSW) */
        int num_components_out;
        /* End Aerospace MCT mods */
        int max_rows, max_cols, max_bytes;
        canvas_dims_ptr dims;
        int *bitdepths;

        /* Create objects. */
        reader=constructors.image_reader();
        stream=constructors.stream_out();
        info=constructors.forward_info();
        encoder=constructors.encoder();
        roi=constructors.forward_roi();
        quantizer=constructors.quantizer();
        analysis=constructors.analysis();
        mixer=constructors.component_mix();

        /* Open image and get dimensions. */
        reader->initialize(reader,&num_components,cmdl);
        dims=(canvas_dims_ptr)local_malloc(INVOCATION_MEM_KEY,
            sizeof(canvas_dims)*num_components);
        bitdepths=(int *)local_malloc(INVOCATION_MEM_KEY,
                sizeof(int)*num_components);
        for(max_rows=max_cols=0,c=0;c<num_components;c++){
            reader->get_dims(reader,c,dims+c);
            max_rows=(dims[c].rows>max_rows)?dims[c].rows:max_rows;
            max_cols=(dims[c].cols>max_cols)?dims[c].cols:max_cols;
            bitdepths[c]=reader->get_dynamic_range(reader,c);
        }
        max_bytes = (int)(0.125F*max_bpp*(float)(max_rows*max_cols));

        /* SAIC/Fuji LRA begin */
        /* Loosen tolerance to speed convergence after 5th iteration */
        if(iterations>5){
            tolerances_loosened=1;
            low_rate_tol*=1.2F;
        }
        low_bytes_tol=(int)(0.125F*low_rate_tol*
            (float)(max_rows*max_cols));
        hi_bytes_tol=(int)(-0.125F*hi_rate_tol*
            (float)(max_rows*max_cols));
        /* SAIC/Fuji LRA end */

        /* Initialize all objects and write global header. */
        stream->initialize(stream,max_bytes,compressed_filename,cmdl);
        /* Begin Aerospace MCT mods (TSW) */
        info->initialize(info,num_components,bitdepths,dims,base_step,
            prev_header_size,prev_remaining_bytes,stream,cmdl);
        mixer->initialize(mixer,&num_components_out, num_components,info,
            analysis,stream,cmdl);
        /* As far as the remaining objects that must be initialized are
           concerned, num_components_out is now the number of components
           that are output from a multiple-component transform (if used).
           If the transform is used, the num_components field in the info
           structure is updated to reflect the number of components that
           will be written to the condestream. Therefore, where that number
           is required, reference the field in the info structure. */
        stream->declare_marker_elt(stream,-1,MARKER_SIZ_C,0,16,1);
        stream->set_marker_val(stream,-1,MARKER_SIZ_C,0,(std_uint)
            num_components_out,0);
        roi->initialize(roi,num_components_out,info,stream,cmdl);
        encoder->initialize(encoder,num_components_out,max_bytes,
            info,roi,stream,cmdl);
        quantizer->initialize(quantizer,num_components_out,info,encoder,
            stream,cmdl);
        analysis->initialize(analysis,num_components_out,info,quantizer,
            /* End Aerospace MCT mods */
            /* SAIC/Sharp/Fuji Xmask begin */
            encoder,
            /* SAIC/Sharp/Fuji Xmask end */
            stream,cmdl);

        cmdl->terminate(cmdl,(iterations==0));
        cmdl = NULL;

        /* Now run the compression pipeline. */
        if(verbose&&(iterations==0)){
            int num_tiles = 0;

            /* MITRE General Offset/SQ Begin */ /* Begin Aerospace MCT mods */
            while (info->get_fixed_tile_info(info,0,num_tiles,
                NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL,NULL))
            /* MITRE General Offset/SQ End */ /* End Aerospace MCT mods */
                    num_tiles++;
            local_printf(4,70,"%d components; %d tiles; "
                "Target rate = %g bpp (i.e. %d bytes)",
                /* Begin Aerospace MCT mods (TSW) */
                num_components_out,num_tiles,max_bpp,max_bytes);
                /* End Aerospace MCT mods */
        }
        read_and_compress_the_image(info,reader,mixer,num_components);

        /* Terminate all objects and free storage. */
        reader->terminate(reader);
        mixer->terminate(mixer);
        analysis->terminate(analysis);
        quantizer->terminate(quantizer);
        encoder->terminate(encoder);
        remaining_bytes = stream->get_max_remaining_bytes(stream);
        roi->terminate(roi);

        /* SAIC/Fuji LRA mods begin */
        prev_remaining_bytes = remaining_bytes;
        prev_header_size = stream->get_global_header_size(stream);
        if(verbose){
            if (Flra_flag && (iterations != 0))
                local_printf(4,70,"Lagrangian Rate Allocation -> "
                    "generated %g bpp (i.e. %d bytes)\n%d header bytes",
                    (8.0F*(float)(max_bytes-remaining_bytes))/
                    ((float)(max_rows*max_cols)),max_bytes-remaining_bytes,
                    stream->get_global_header_size(stream));
            else{
                local_printf(4,70,"Base step = %g -> generated %g bpp (i.e."
                    " %d bytes)\n%d header bytes",base_step,
                        (8.0F*(float)(max_bytes-remaining_bytes)) /
                        ((float)(max_rows*max_cols)),max_bytes-remaining_bytes,
                        stream->get_global_header_size(stream));
                if ((remaining_bytes < 0) && max_rate_iterations)
                    local_printf(0,70,"WARNING: File truncated to "
                        "meet rate constraint");
            }
        }
        if((max_rate_iterations==0)||((hi_bytes_tol<=remaining_bytes)&&
            (remaining_bytes<low_bytes_tol)&&first_iteration_done))
            remove_stats = 1;
        info->terminate(info, remove_stats);
        /* SAIC/Fuji LRA end mods */

        stream->terminate(stream);
        local_free(dims);
        local_free(bitdepths);

        /* Check whether or need we need to iterate. */
        /* SAIC/Fuji LRA begin */
        if((hi_bytes_tol<=remaining_bytes)&&(remaining_bytes<low_bytes_tol)&&
            (first_iteration_done))
            break;
        /* SAIC/Fuji LRA end */

        if((max_rate_iterations==0)||final_iteration)
            break;
        if(iterations==0)
            found_min_step=found_max_step = 0;
        if(remaining_bytes<0){
            found_min_step=1;
            min_step=base_step;
        }
        else{
            found_max_step=1;
            max_step=base_step;
        }

        iterations++;
        if((iterations<max_rate_iterations)||(!found_max_step)){
            /* Compute new base step. */
            if (!found_min_step)
                base_step*=0.5F;
            else if (!found_max_step)
                base_step*=2.0F;
            else
                base_step=0.5F*(min_step+max_step);
        }
        else
            final_iteration=1;
        if(final_iteration){
            if(base_step==max_step)
                break;
            base_step=max_step;
        }
        cmdl=create_std_cmdl();
        cmdl->initialize(cmdl,argc,argv);
        first_iteration_done=1; /* SAIC/FUJI */
    }while(remaining_bytes!=0);

    /* SAIC/Fuji LRA begin */
    if((max_rate_iterations>0)&& /* Using some form of rate iteration */
        ((hi_bytes_tol>remaining_bytes)||(remaining_bytes>=low_bytes_tol))){
        local_printf(0,76,"WARNING");
        local_printf(4,70,"Rate control could not converge to desired rate.  "
            "Try looser rate control tolerances (with `-low_rate_tol' "
            "and `-hi_rate_tol') or increase the  number of rate "
            "iterations.");
    }
    else if(tolerances_loosened){
        local_printf(0,76,"WARNING");
        local_printf(4, 70,"Loosened lower rate control tolerance to "
            "allow rate convergence.");
    }
    /* SAIC/Fuji LRA end */

    /* Cleanup. */
    if(cmdl!=NULL)
        cmdl->terminate(cmdl,0);
    if(memory_report)
        local_memory_report();
    return(0);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -