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

📄 test_xform.c

📁 JPEG2000 EBCOT算法源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  int i;
  char *name;
  FILE *fp;

  for (name=NULL, i=1; i < (argc-1); i++)
    if (strcmp(argv[i],"-o") == 0)
      {
        name = argv[i+1];
        argv[i] = argv[i+1] = "";
      }
  if (name == NULL)
    {
      fprintf(stderr,"Must supply output file name via `-o' switch!\n");
      exit(-1);
    }
  fp = local_fopen(name,"wb");
  if (fp == NULL)
    {
      fprintf(stderr,"Cannot open output file, \"%s\"!\n",name);
      exit(-1);
    }
  if (num_planes == 1)
    fprintf(fp,"P5\n%d %d\n255\n",num_cols,num_rows);
  else
    {
      assert(num_planes == 3);
      fprintf(fp,"P6\n%d %d\n255\n",num_cols,num_rows);
    }
  return(fp);
}

/*****************************************************************************/
/* STATIC                       read_image_line                              */
/*****************************************************************************/

static void
  read_image_line(ifc_int *buf, FILE *fp, int samples, int components)
{
  int c, elts;
  ifc_int *dp, val, shift;
  unsigned char *sp;

  elts = samples*components;
  shift = STD_NOMINAL_RANGE_BITS - 8;
  fread(buf,1,(size_t) elts,fp);
  sp = ((unsigned char *) buf) + elts;
  dp = buf + elts;
  for (c=elts; c > 0; c--)
    {
      val = (ifc_int) *(--sp);
      val -= 128;
      val <<= shift;
      *(--dp) = val;
    }
}

/*****************************************************************************/
/* STATIC                       write_image_line                             */
/*****************************************************************************/

static void
  write_image_line(ifc_int *buf, FILE *fp, int samples, int components)
{
  int c, elts;
  ifc_int *sp, val, offset, shift;
  unsigned char *dp;

  elts = components * samples;
  sp = buf;
  dp = (unsigned char *) buf;
  shift = STD_NOMINAL_RANGE_BITS - 8;
  offset = (1<<shift) >> 1;
  for (c=elts; c > 0; c--)
    {
      val = *(sp++);
      val = (val + offset) >> shift;
      val += 128;
      if (val & 0xFF00)
        val = (val<0)?0:255;
      *(dp++) = (unsigned char) val;
    }
  fwrite(buf,1,(size_t) elts,fp);
}

/*****************************************************************************/
/*                                   main                                    */
/*****************************************************************************/

int
  main(int argc, char *argv[])
{
  char **copy_argv;
  int filter_set, filter_flags;
  char *filter_id, *local_filter_name, filter_id_buf[2];
  int in_levels, out_levels;
  int in_components, out_components;
  float base_step;
  int in_rows, in_cols, out_rows, out_cols;
  int component_rows_in[3], component_cols_in[3];
  int component_rows_out[3], component_cols_out[3];
  float component_base_steps[3];
  char *component_ids[3];
  int component_flags[3];
  FILE *in_fp, *out_fp;
  ifc_int *interleaved_line, *line_buf;
  float *coeff_buf[3];
  int max_bitplanes;
  int r, n;
  filter_info_ref info;
  analysis_ref analysis;
  synthesis_ref synthesis;
  quantizer_ref dummy_quant;
  dequantizer_ref dummy_dequant;

  /* Set default parameters. */

  filter_set = 0; /* 7/9 filters. */
  local_filter_name = NULL;
  filter_flags = 0; /* No special filter flags. */
  in_levels = out_levels = 5;
  out_components = MAX_COMPONENTS;
  base_step = 1.2F;

  /* Get arguments. */

  if (argc == 1)
    {
      print_usage(in_levels,out_levels,out_components,argv[0],stderr);
      return(-1);
    }
  for (r=1; r < argc; r++)
    if (strcmp(argv[r],"-u") == 0)
      { /* Allow usage statements to be printed to a file or other stdout. */
        FILE *fp;

        fp = stderr;
        r++;
        if ((r < argc) &&
            (((argv[r])[0] != '-') || ((argv[r])[1] == '\0')))
          {
            fp = local_fopen(argv[r],"w");
            if (fp == NULL)
              fp = stderr;
          }
        print_usage(in_levels,out_levels,out_components,argv[0],fp);
        local_fclose(fp);
        return(-1);
      }

  copy_argv = (char **) local_malloc(sizeof(char *)*argc);
  memcpy(copy_argv,argv,sizeof(char *)*argc);
  parse_common_arguments(argc,copy_argv,&in_levels,&out_levels,
                         &out_components,&filter_set,&local_filter_name);
  if (filter_set == 15)
    filter_id = local_filter_name;
  else
    {
      filter_id = filter_id_buf;
      sprintf(filter_id,"%1d",filter_set);
    }
  if (out_levels > in_levels)
    out_levels = in_levels;

  /* Open files. */

  in_fp = open_input_file(argc,copy_argv,&in_rows,&in_cols,&in_components);
  out_rows = 1 + ((in_rows-1)>>(in_levels-out_levels));
  out_cols = 1 + ((in_cols-1)>>(in_levels-out_levels));
  if (out_components > in_components)
    out_components = in_components;
  if (out_components < 3)
    out_components = 1;
  out_fp = open_output_file(argc,copy_argv,out_rows,out_cols,out_components);

  /* Allocate storage. */

  line_buf = interleaved_line = (ifc_int *)
    local_malloc(sizeof(ifc_int) * in_cols * in_components);
  if (in_components > 1)
    line_buf = (ifc_int *)
      local_malloc(sizeof(ifc_int) * in_cols);
  for (n=0; n < in_components; n++)
    coeff_buf[n] = (float *)
      local_malloc(sizeof(float) * in_cols * in_rows);

  /* Create objects. */

  info = info_creator();
  analysis = analysis_creator();
  synthesis = synthesis_creator();
  dummy_quant = quantizer_creator();
  dummy_dequant = dequantizer_creator();

  /* Initialize objects. */

  for (n=0; n < in_components; n++)
    {
      component_rows_in[n] = in_rows;
      component_rows_out[n] = out_rows;
      component_cols_in[n] = in_cols;
      component_cols_out[n] = out_cols;
      component_base_steps[n] = base_step;
      component_ids[n] = filter_id;
      component_flags[n] = filter_flags;
    }

  max_bitplanes = 0;
  info->initialize(info,DECOMPOSITION__MALLAT,in_levels,in_components,
                   component_ids,component_flags,component_base_steps,
                   &max_bitplanes,argc,copy_argv);
  analysis->initialize(analysis,DECOMPOSITION__MALLAT,in_levels,
                       in_components,component_rows_in,component_cols_in,
                       info,dummy_quant,argc,copy_argv);
  dummy_quant->initialize(dummy_quant,DECOMPOSITION__MALLAT,in_levels,
                          in_components,component_rows_in,component_cols_in,
                          info,(encoder_ref) coeff_buf,argc,copy_argv);
  check_all_arguments_used(argc,copy_argv);
  memcpy(copy_argv,argv,sizeof(char *)*argc);

  synthesis->initialize(synthesis,DECOMPOSITION__MALLAT,out_levels,
                        out_components,component_rows_out,component_cols_out,
                        info,dummy_dequant,argc,copy_argv);

  dummy_dequant->initialize(dummy_dequant,DECOMPOSITION__MALLAT,in_levels,
                            in_components,component_rows_in,component_cols_in,
                            info,(decoder_ref) coeff_buf,argc,copy_argv);

  /* Push image rows into the analysis stage one at a time. */

  for (r=0; r < in_rows; r++)
    {
      read_image_line(interleaved_line,in_fp,in_cols,in_components);
      if (in_components == 1)
        analysis->push_line(analysis,interleaved_line,0);
      else
        {
          for (n=0; n < in_components; n++)
            {
              ifc_int *sp, *dp;
              int c;

              for (sp=interleaved_line+n, dp=line_buf,
                   c=in_cols; c > 0; c--, sp += in_components)
                *(dp++) = *sp;
              analysis->push_line(analysis,line_buf,n);
            }
        }
    }

  /* Terminate analysis and quantization. */

  analysis->terminate(analysis); /* Pushes all subband samples out first. */
  dummy_quant->terminate(dummy_quant);

  /* Pull image rows out of the synthesis stage one at a time. */

  for (r=0; r < out_rows; r++)
    {
      if (out_components == 1)
        synthesis->pull_line(synthesis,interleaved_line,0);
      else
        {
          for (n=0; n < out_components; n++)
            {
              ifc_int *sp, *dp;
              int c;

              synthesis->pull_line(synthesis,line_buf,n);
              for (dp=interleaved_line+n, sp=line_buf,
                   c=out_cols; c > 0; c--, dp += out_components)
                *dp = *(sp++);
              }
        }
      write_image_line(interleaved_line,out_fp,out_cols,out_components);
    }

  /* Terminate synthesis and dequantization. */

  synthesis->terminate(synthesis);
  dummy_dequant->terminate(dummy_dequant);

  /* Finish up. */

  info->terminate(info);
  for (n=0; n < in_components; n++)
    local_free(coeff_buf[n]);
  local_free(interleaved_line);
  if (line_buf != interleaved_line)
    local_free(line_buf);
  local_fclose(in_fp);
  local_fclose(out_fp);
  local_free(copy_argv);
  return (0);
}

⌨️ 快捷键说明

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