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

📄 hpdz_quant.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
  __next_tile(quantizer_ref base)
{
  hpdz_quantizer_ref self = (hpdz_quantizer_ref) base;

  start_tile(self);
  self->encoder->next_tile(self->encoder);
}

/*****************************************************************************/
/* STATIC                      __push_line_float                             */
/*****************************************************************************/

static void
  __push_line_float(quantizer_ref base, float *line_buf,
                    int component_idx, int level_idx, int band_idx, int width)
{
  hpdz_quantizer_ref self = (hpdz_quantizer_ref) base;
  hpdz_component_info_ptr comp;
  hpdz_level_info_ptr lev;
  float scale, *sp, nz;
  ifc_int *cast_buf, *dp, val;
  int i;

  assert(component_idx < self->num_components);
  comp = self->components + component_idx;
  assert(level_idx <= comp->num_levels);
  lev = comp->levels + level_idx;
  assert((band_idx >= lev->min_band) && (band_idx <= lev->max_band));
  scale = lev->bands[band_idx].scale;

  /* MITRE General Offset/SQ Begin */
  nz = lev->bands[band_idx].nz;
  /* MITRE General Offset/SQ End */

  cast_buf = (ifc_int *) line_buf;

  /* SAIC General Decomp. Begin */
  if (self->wt_output_file != NULL) {
    int line_num;
    int j;

    line_num = self->band_dims[level_idx][band_idx].top_row+
               self->current_band_line[level_idx][band_idx];
    assert(self->band_dims[level_idx][band_idx].cols == width);
    assert(self->current_band_line[level_idx][band_idx] <
           self->band_dims[level_idx][band_idx].rows);
    for (j=self->band_dims[level_idx][band_idx].left_col;
         j<self->band_dims[level_idx][band_idx].left_col +
           self->band_dims[level_idx][band_idx].cols; j++) {
      self->wt_output[line_num][j] =
        line_buf[j-self->band_dims[level_idx][band_idx].left_col];
    }
    self->current_band_line[level_idx][band_idx]++;
  }
  /* SAIC General Decomp. End */

  for (sp=line_buf, dp=cast_buf, i=width; i > 0; i--, sp++, dp++)
    {

      /* MITRE General Offset/SQ Begin mods */
      if (fabs(*sp) >= -nz) {
        if (*sp >= 0)
          val = (ifc_int)((*sp + nz) * scale);
        else
          val = (ifc_int)((*sp - nz) * scale);

      }
      else
        val = 0;
      /* MITRE General Offset/SQ End mods */

      val = (val<0)?(MIN_IFC_INT-val):val; /* Convert to sign-mag. */
      *dp = val;
    }
  self->encoder->push_line(self->encoder,cast_buf,
                           component_idx,level_idx,band_idx,width);  
}

/*****************************************************************************/
/* STATIC                      __push_line_fixed                             */
/*****************************************************************************/

static void
  __push_line_fixed(quantizer_ref base, ifc_int *line_buf,
                    int component_idx, int level_idx, int band_idx, int width)
{
  hpdz_quantizer_ref self = (hpdz_quantizer_ref) base;
  hpdz_component_info_ptr comp;
  hpdz_level_info_ptr lev;
  ifc_int *sp, shift, val;
  int i;

  assert(component_idx < self->num_components);
  comp = self->components + component_idx;
  assert(level_idx <= comp->num_levels);
  lev = comp->levels + level_idx;
  assert((band_idx >= lev->min_band) && (band_idx <= lev->max_band));
  shift = lev->bands[band_idx].right_shift;

  /* SAIC General Decomp. Begin */
  if (self->wt_output_file != NULL) {
    int line_num;
    int j;

    line_num = self->band_dims[level_idx][band_idx].top_row+
               self->current_band_line[level_idx][band_idx];
    assert(self->band_dims[level_idx][band_idx].cols == width);
    assert(self->current_band_line[level_idx][band_idx] <
           self->band_dims[level_idx][band_idx].rows);
    for (j=self->band_dims[level_idx][band_idx].left_col;
         j<self->band_dims[level_idx][band_idx].left_col +
           self->band_dims[level_idx][band_idx].cols; j++) {
      self->wt_output[line_num][j] = (float)
        line_buf[j-self->band_dims[level_idx][band_idx].left_col];
    }
    self->current_band_line[level_idx][band_idx]++;
  }
  /* SAIC General Decomp. End */

  if (shift == 0)
    for (sp=line_buf, i=width; i > 0; i--, sp++)
      { /* It can be shown that the `shift'=0 case occurs almost always. */
        val = *sp;
        val = (val<0)?(MIN_IFC_INT-val):val;
        *sp = val;
      }
  else if (shift > 0)
    for (sp=line_buf, i=width; i > 0; i--, sp++)
      {
        val = *sp;
        if (val < 0)
          val = MIN_IFC_INT + ((-val) >> shift);
        else
          val >>= shift;
        *sp = val;
      }
  else
    for (shift=-shift, sp=line_buf, i=width; i > 0; i--, sp++)
      {
        val = *sp;
        if (val < 0)
          val = MIN_IFC_INT + ((-val) << shift);
        else
          val <<= shift;
        *sp = val;
      }
  self->encoder->push_line(self->encoder,line_buf,
                           component_idx,level_idx,band_idx,width);
}

/*****************************************************************************/
/* STATIC                         __terminate                                */
/*****************************************************************************/

static void
  __terminate(quantizer_ref base)
{
  hpdz_quantizer_ref self = (hpdz_quantizer_ref) base;
  int c;

  /* SAIC General Decomp. Begin */
  if (self->wt_output_file != NULL) {
    canvas_dims tile_dims;
    hpdz_component_info_ptr comp;
    hpdz_level_info_ptr lev;
    float max, min;
    int num_levels;
    int i, j, n, b;
    unsigned char **usc_wt_output;
    FILE *fp;


    comp = self->components;
    num_levels = comp->num_levels;
    /* Begin Aerospace MCT mods */
    self->info->get_fixed_tile_info(self->info, 0, 0, NULL, NULL, &tile_dims,
                                    NULL, NULL, NULL, NULL,NULL,NULL,NULL);
    /* End Aerospace MCT mods */
    usc_wt_output = (unsigned char **) 
      local_malloc(HPDZ_MEM_KEY, tile_dims.rows * sizeof(unsigned char *));
    usc_wt_output[0] = (unsigned char *) 
      local_malloc(HPDZ_MEM_KEY, tile_dims.rows * tile_dims.cols * 
                                 sizeof(unsigned char));
    for (i=1; i<tile_dims.rows; i++) {
      usc_wt_output[i] = usc_wt_output[i-1] + tile_dims.cols;
    }
    for (n=0; n<=num_levels; n++) {
      lev = comp->levels + n;
      for (b=lev->min_band; b<=lev->max_band; b++) {
        if (!self->valid_band[n][b])
          continue;
        max = min = 
          self->wt_output[self->band_dims[n][b].top_row]
                         [self->band_dims[n][b].left_col];
        for (i=self->band_dims[n][b].top_row;
             i<self->band_dims[n][b].top_row +
               self->band_dims[n][b].rows; i++) {
          for (j=self->band_dims[n][b].left_col;
               j<self->band_dims[n][b].left_col +
                 self->band_dims[n][b].cols; j++) {
            if (self->wt_output[i][j] > max)
              max = self->wt_output[i][j];
            if (self->wt_output[i][j] < min)
              min = self->wt_output[i][j];
          }
        }
        for (i=self->band_dims[n][b].top_row;
             i<self->band_dims[n][b].top_row +
               self->band_dims[n][b].rows; i++) {
          for (j=self->band_dims[n][b].left_col;
               j<self->band_dims[n][b].left_col +
                 self->band_dims[n][b].cols; j++) {
            if (n == 0) {
              self->wt_output[i][j] = 
                255*(self->wt_output[i][j] - min)/(max - min);
            }
            else {
              self->wt_output[i][j] = (float)
                (1024*fabs(self->wt_output[i][j])/MAX(fabs(min), fabs(max)));
              if (self->wt_output[i][j] > 255)
                self->wt_output[i][j] = 255;
            }
            usc_wt_output[i-tile_dims.top_row][j-tile_dims.left_col] = 
              (unsigned char) self->wt_output[i][j];
          }
        }
      }
      local_free(self->valid_band[n]);
      local_free(self->current_band_line[n]);
      local_free(self->band_dims[n]);
    }
    local_free(self->valid_band);
    local_free(self->current_band_line);
    local_free(self->band_dims);
    local_free(self->min_band);
    local_free(self->max_band);

    fp = fopen(self->wt_output_file, "wb");
    fprintf(fp, "P5\n%d %d\n255\n", tile_dims.cols, tile_dims.rows);
    fwrite(usc_wt_output[0], sizeof(unsigned char), tile_dims.rows * tile_dims.cols, fp);
    fclose(fp);
    self->wt_output[tile_dims.top_row] += tile_dims.left_col;
    local_free(self->wt_output[tile_dims.top_row]);
    self->wt_output += tile_dims.top_row;
    local_free(self->wt_output);
    local_free(usc_wt_output[0]);
    local_free(usc_wt_output);
  }
  /* SAIC General Decomp. End */

  if (self->components != NULL)
    {
      for (c=0; c < self->num_components; c++)
        destroy_component_structures(self->components + c);
      local_free(self->components);
    }

  local_free(self);
}

/*****************************************************************************/
/* EXTERN                   create_deadzone_quantizer                        */
/*****************************************************************************/

quantizer_ref
  create_deadzone_quantizer(void)
{
  hpdz_quantizer_ref result;

  result = (hpdz_quantizer_ref)
    local_malloc(HPDZ_MEM_KEY,sizeof(hpdz_quantizer_obj));
  result->base.initialize = __initialize;
  result->base.get_usage = __get_usage;
  result->base.push_line_float = __push_line_float;
  result->base.push_line_fixed = __push_line_fixed;
  result->base.next_tile = __next_tile;
  result->base.terminate = __terminate;
  return((quantizer_ref) result);
}

⌨️ 快捷键说明

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