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

📄 tcq_dequant.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 3 页
字号:
static void
  __pull_line_float(dequantizer_ref base, float *line_buf,
                    int component_idx, int level_idx, int band_idx, int width)
{
  tcq_dequantizer_ref self = (tcq_dequantizer_ref) base;
  tcq_component_info_ptr comp;
  tcq_level_info_ptr lev;
  tcq_band_info_ptr band;
  float scale;
  float **block_buffer;
  int extra_lsbs, n;

  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));  
  band = lev->bands + band_idx;
  if (band->line_buffer == NULL)
    {
      assert(band->lines_left_in_buffer == 0);
      band->line_buffer =
        AllocateBlockBuffer(width,band->block_height);
    }
  block_buffer = band->line_buffer;
  if (band->lines_left_in_buffer == 0)
    { /* Load and quantize the next row of blocks. */
      int block_height, block_width, block_offset;

      scale = band->scale;
      extra_lsbs = band->extra_lsbs;
      block_height = band->next_block_height;
      assert(block_height > 0);
      for (n=0; n < block_height; n++)
        {
          ifc_int *cast_buf;

          cast_buf = (ifc_int *)(block_buffer[n]);
          self->decoder->pull_line(self->decoder,cast_buf,
                                   component_idx,level_idx,band_idx,width);
#if (IMPLEMENTATION_PRECISION != 32)
          {
            int m;
            ifc_int *sp, val;
            std_int *dp;

            for (sp=cast_buf+width, dp=((std_int *) cast_buf)+width,
                 m=width; m > 0; m--)
              {
                val = *(--sp);
                if (val < 0)
                  *(--dp) = 0x80000000 + (ifc_int)(val & MAX_IFC_INT);
                else
                  *(--dp) = val;
              }
          }
#endif /* IMPLEMENTATION_PRECISION */
        }

      block_width = band->first_block_width;
      block_offset = 0;
      while (block_width > 0)
        {
          DeTCQBlock(block_buffer,block_width,block_height,
                     block_offset,scale,extra_lsbs,self);
          block_offset += block_width;
          block_width = width - block_offset;
          if (block_width > band->block_width)
            block_width = band->block_width;
        }
      band->lines_left_in_buffer = block_height;
      band->next_line_idx = 0;
      band->remaining_tile_height -= band->next_block_height;
      band->next_block_height = band->block_height;
      if (band->next_block_height > band->remaining_tile_height)
        band->next_block_height = band->remaining_tile_height;
    }
  memcpy(line_buf,block_buffer[band->next_line_idx],width*sizeof(float));
  band->next_line_idx++;
  band->lines_left_in_buffer--;

  /* 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 */

}

/*****************************************************************************/
/* STATIC                      __pull_line_fixed                             */
/*****************************************************************************/

static void
  __pull_line_fixed(dequantizer_ref base, ifc_int *line_buf,
                    int component_idx, int level_idx, int band_idx, int width)
{

  /* SAIC General Decomp. Begin */
  tcq_dequantizer_ref self = (tcq_dequantizer_ref) base;
  /* SAIC General Decomp. End */

  int i;
  float *tmpbuf;

  /* This keeps the code modular until I am sure about what we are going
     to do */
  tmpbuf = (float *)local_malloc(TCQ_MEM_KEY,sizeof(float)*width);
  __pull_line_float(base, tmpbuf, component_idx, level_idx, band_idx, width);
  for (i = 0; i < width; i++)
    line_buf[i] = (ifc_int) tmpbuf[i];

  /* 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 */

  local_free(tmpbuf);    
}

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

static void
  __terminate(dequantizer_ref base)
{
  tcq_dequantizer_ref self = (tcq_dequantizer_ref) base;
  int c;

  /* SAIC General Decomp. Begin */
  if (self->wt_output_file != NULL) {
    canvas_dims tile_dims;
    tcq_component_info_ptr comp;
    tcq_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(TCQ_MEM_KEY, tile_dims.rows * sizeof(unsigned char *));
    usc_wt_output[0] = (unsigned char *)
      local_malloc(TCQ_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];
          }
        }
        if ((min == 0.0F) && (max == 0.0F))
          max = 1.0F;  /* Avoid divide by zero */
        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);
    }
  DeTCQBlock(NULL,0,0,0,0.0F,0,NULL);/* Force deallocation of static resources;
                                  ideally, these should be removed to make the
                                  code re-entrant. */
  local_free(self);
}

/*****************************************************************************/
/* EXTERN                  create_tcq_dequantizer                            */
/*****************************************************************************/

dequantizer_ref
  create_tcq_dequantizer(void)
{
  tcq_dequantizer_ref result;

  result = (tcq_dequantizer_ref)
    local_malloc(TCQ_MEM_KEY,sizeof(tcq_dequantizer_obj));
  result->base.initialize = __initialize;
  result->base.get_usage = __get_usage;
  result->base.pull_line_float = __pull_line_float;
  result->base.pull_line_fixed = __pull_line_fixed;
  result->base.next_tile = __next_tile;
  result->base.terminate = __terminate;
  return((dequantizer_ref) result);
}

⌨️ 快捷键说明

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