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

📄 hpdz_dequant.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 2 页
字号:
/*****************************************************************************/
/* Copyright 1998, Hewlett-Packard Company                                   */
/* All rights reserved                                                       */
/* File: "hpdz_dequant.c"                                                    */
/* Description: Deadzone dequantizer implementation.                         */
/* Author: David Taubman                                                     */
/* Affiliation: Hewlett-Packard and                                          */
/*              The University of New South Wales, Australia                 */
/* Version: VM9.0                                                            */
/* Last Revised: 20 April, 2001                                              */
/*****************************************************************************/

/*****************************************************************************/
/* Modified to include TCQ                                                   */
/* Copyright 1999 Science Applications International Corporation (SAIC).     */
/* Copyright 1995 University of Arizona, Arizona Board of Regents.           */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified by David Taubman to support interface modifications, arbitrary   */
/* changes in coding parameters from component to component and from tile    */
/* to tile, to support the full generality of PART-1 of the JPEG2000         */
/* standard, and to support most anticipated generality of PART-2.  Changes  */
/* are too numerous to flag individually within the code, which in some      */
/* places has been completely rewritten.  All changes copyrighted by HP with */
/* all rights reserved for the modified parts.                               */
/*****************************************************************************/

/*****************************************************************************/
/* Modified for general wavelet decompositions.                              */
/* Copyright 2000 Science Applications International Corporation (SAIC).     */
/* Copyright 1995 University of Arizona, Arizona Board of Regents.           */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

/*****************************************************************************/
/* Modified for general offset and scalar quantization.                      */
/* Copyright 2000 The MITRE Corporation.                                     */
/* All Rights Reserved for modified parts.                                   */
/*****************************************************************************/

#include <local_services.h>
#include <stdlib.h>
#include <stdio.h>

/* SAIC General Decomp. Begin */
#ifndef MAX
#define MAX(A,B) ((A)>(B)?(A):(B))
#endif
/* SAIC General Decomp. End */

#include <math.h>
#include <string.h>
#include <assert.h>
#include <ifc.h>
#include <markers.h>
#include "hpdz_dequant_local.h"

static std_byte designalling_lut[256];

/* ========================================================================= */
/* --------------------------------- Macros -------------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* MACRO                        designal_lsb_idx                             */
/*****************************************************************************/

#if (IMPLEMENTATION_PRECISION == 16)
#  define designal_lsb_idx(_val,_lsb_idx)                                 \
    /* [ifc_int _val, ifc_int _lsb_idx] */                                \
    {                                                                     \
      _lsb_idx = designalling_lut[_val & 0x00FF];                         \
      if (_lsb_idx == 8)                                                  \
        _lsb_idx += designalling_lut[(_val >> 8) & 0x00FF];               \
      _lsb_idx++; /* Index of the least significant decoded bit-plane. */ \
    }
#else
#  define designal_lsb_idx(_val,_lsb_idx)                                 \
    /* [ifc_int _val, ifc_int _lsb_idx] */                                \
    {                                                                     \
      _lsb_idx = designalling_lut[_val & 0x00FF];                         \
      if (_lsb_idx == 8)                                                  \
        {                                                                 \
          _lsb_idx += designalling_lut[(_val >> 8) & 0x00FF];             \
          if (_lsb_idx == 16)                                             \
            {                                                             \
              _lsb_idx += designalling_lut[(_val >> 16) & 0x00FF];        \
              if (_lsb_idx == 24)                                         \
                _lsb_idx += designalling_lut[(_val >> 24) & 0x00FF];      \
            }                                                             \
        }                                                                 \
      _lsb_idx++; /* Index of the least significant decoded bit-plane. */ \
    }
#endif /* IMPLEMENTATION_PRECISION */


/* ========================================================================= */
/* ---------------------------- Internal Functions ------------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* STATIC                   complete_designalling_lut                        */
/*****************************************************************************/

static void
  complete_designalling_lut(void)

 /* Fills in the `designalling_lut' entries to hold the index of the least
    significant non-zero bit of an 8-bit quantity.  The return value should
    be 8 if all 8 bits are zero. */

{
  int n, val;
  std_byte idx;

  designalling_lut[0] = 8;
  for (n=1; n < 256; n++)
    {
      for (idx=0, val=n; (val & 1) == 0; val >>= 1)
        idx++;
      designalling_lut[n] = idx;      
    }
}

/*****************************************************************************/
/* STATIC                   destroy_component_structures                     */
/*****************************************************************************/

static void
  destroy_component_structures(hpdz_component_info_ptr comp)
{
  hpdz_level_info_ptr lev;
  int n;

  if (comp->levels == NULL)
    return;
  for (n=0; n <= comp->num_levels; n++)
    {
      lev = comp->levels + n;
      if (lev->bands != NULL)
        local_free(lev->bands);
    }
  local_free(comp->levels);
}

/* MITRE General Offset/SQ Begin mods */
/*****************************************************************************/
/* STATIC                          start_tile                                */
/*****************************************************************************/

static void
  start_tile(hpdz_dequantizer_ref self)
{
  reverse_info_ref info = self->info;
  hpdz_component_info_ptr comp;
  hpdz_level_info_ptr lev;
  hpdz_band_info_ptr band;
  int num_components, num_levels, c, n, b;
  float scale_nz;

  num_components = self->num_components;
  for (c=0; c < num_components; c++)
    {
      comp = self->components + c;
      destroy_component_structures(comp);

      comp->num_levels = num_levels =
        info->get_var_tile_info(info,c,NULL,NULL,NULL,NULL,NULL);
      comp->levels = (hpdz_level_info_ptr)
        local_malloc(HPDZ_MEM_KEY,sizeof(hpdz_level_info)*(num_levels+1));
      for (n=0; n <= num_levels; n++)
        {
          int max_hp_descent;
          float step_size;
          int exponent, extra_lsbs;
          int valid_band;

          lev = comp->levels + n;
          if (n == 0)
            lev->min_band = lev->max_band = LL_BAND;
          else
            {
              info->get_level_info(info,c,n,NULL,&max_hp_descent,NULL,NULL);
              if (max_hp_descent == 0)
                {
                  lev->min_band = 0;
                  lev->max_band = -1;
                  continue;
                }
              else
                {
                  lev->min_band = 1 << (max_hp_descent+max_hp_descent-2);
                  lev->max_band = (1<<(max_hp_descent+max_hp_descent)) - 1;
                }
            }
          lev->bands = (hpdz_band_info_ptr)
            local_malloc(HPDZ_MEM_KEY,
                         sizeof(hpdz_band_info)*(lev->max_band+1));
          for (b=lev->min_band; b <= lev->max_band; b++)
            {
              band = lev->bands + b;
              info->get_band_info(info,c,n,b,NULL,NULL,NULL,NULL,NULL,NULL,&valid_band);
              if (!valid_band)
                continue;
              info->get_quant_info(info,c,n,b,&step_size,&exponent,
                                   &extra_lsbs, &scale_nz);
              if (extra_lsbs >= 0)
                band->scale = step_size / ((float)(1<<extra_lsbs));
              else
                band->scale = step_size * ((float)(1<<-extra_lsbs));
              band->extra_lsbs = extra_lsbs; /* SAIC */
              band->left_shift = (ifc_int)(exponent - extra_lsbs);
              /* MITRE General Offset/SQ Begin */
              band->nz = scale_nz * step_size;
              /* MITRE General Offset/SQ End */
            }
        }
    }
}


/* ========================================================================= */
/* ------------------------- Interface Implementation ---------------------- */
/* ========================================================================= */

/*****************************************************************************/
/* STATIC                         __initialize                               */
/*****************************************************************************/

static void
  __initialize(dequantizer_ref base, int num_components,
               reverse_info_ref info, decoder_ref decoder,
               stream_in_ref stream, cmdl_ref cmdl)
{
  hpdz_dequantizer_ref self = (hpdz_dequantizer_ref) base;
  char **params;
  int p;

  self->num_components = num_components;
  self->info = info;
  self->decoder = decoder;
  self->components = (hpdz_component_info_ptr)
    local_malloc(HPDZ_MEM_KEY,sizeof(hpdz_component_info)*num_components);

  /* SAIC General Decomp. Begin */
  {
    canvas_dims tile_dims;
    int max_hp_descent;
    int num_levels;
    int b, n, p;
    char **params;

    if ((p = cmdl->extract(cmdl,"-Xwt_output",-1,&params)) >= 0) {
      if (p != 1)
        local_error("The `-Xwt_output' argument requires on parameter!");
      self->wt_output_file = params[0];
      /* 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 */
      self->wt_output = (float **)
        local_malloc(HPDZ_MEM_KEY, tile_dims.rows*sizeof(float *));
      self->wt_output -= tile_dims.top_row;
      self->wt_output[tile_dims.top_row] = (float *)
        local_malloc(HPDZ_MEM_KEY, tile_dims.rows * tile_dims.cols *
                                   sizeof(float));
      self->wt_output[tile_dims.top_row] -= tile_dims.left_col;
      for (n=(tile_dims.top_row+1); n<(tile_dims.top_row+tile_dims.rows); n++) {
        self->wt_output[n] = self->wt_output[n-1] + tile_dims.cols;
      }
      num_levels = info->get_var_tile_info(info,0,NULL,NULL,NULL,NULL,NULL);
      self->min_band = (int *)
        local_malloc(HPDZ_MEM_KEY, (num_levels+1)*sizeof(int));
      self->max_band = (int *)
        local_malloc(HPDZ_MEM_KEY, (num_levels+1)*sizeof(int));
      self->band_dims = (canvas_dims **)
        local_malloc(HPDZ_MEM_KEY, (num_levels+1)*sizeof(canvas_dims *));
      self->current_band_line = (int **)
        local_malloc(HPDZ_MEM_KEY, (num_levels+1)*sizeof(int *));
      self->valid_band = (int **)
        local_malloc(HPDZ_MEM_KEY, (num_levels+1)*sizeof(int *));
      for (n=0; n<(num_levels+1); n++) {
        if (n == 0)
          self->min_band[n] = self->max_band[n] = LL_BAND;
        else {
          info->get_level_info(self->info,0,n,NULL, &max_hp_descent,NULL,NULL);
          self->min_band[n] = 1 << (max_hp_descent+max_hp_descent-2);
          self->max_band[n] = (1<<(max_hp_descent+max_hp_descent)) - 1;

        }
        self->band_dims[n] = (canvas_dims *)
          local_malloc(HPDZ_MEM_KEY, sizeof(canvas_dims)*(self->max_band[n]+1));
        self->current_band_line[n] = (int *)
          local_malloc(HPDZ_MEM_KEY, sizeof(int)*(self->max_band[n]+1));
        self->valid_band[n] = (int *)
          local_malloc(HPDZ_MEM_KEY, sizeof(int)*(self->max_band[n]+1));
        for (b=self->min_band[n]; b<=self->max_band[n];
             b++) {
          self->info->get_band_info(info,0,n,b,NULL,NULL,NULL,NULL,NULL,NULL,&(self->valid_band[n][b]));
          if (!self->valid_band[n][b])
            continue;
          self->info->get_band_location_info(info,0,n,b,&(self->band_dims[n][b]));
        }
      }
    }
  }
  /* SAIC General Decomp. End */

  /* Now let's see whether or not we need to do anything special about
     representation levels. */

  if ((p = cmdl->extract(cmdl,"-Q3_8",-1,&params)) >= 0)
    {
      int val;

      if ((p != 1) || (sscanf(params[0],"%d",&val) == 0) || (val < 0))
        local_error("The `-Q3_8' argument requires a single "
                    "non-negative integer parameter!");
      self->three_eighths_mag = (ifc_int) val;
    }
  if (self->three_eighths_mag)
    complete_designalling_lut();
  start_tile(self);
}

/*****************************************************************************/
/* STATIC                          __get_usage                               */
/*****************************************************************************/

static char **
  __get_usage(dequantizer_ref base)
{
  static char *args[] =
    {"-Q3_8 <max magnitude> (default = 0)",
      "This argument allows the user to control the representation levels "
      "used for quantized sample values with small magnitudes.  By default, "
      "all sample values are represented by the mid-point of the relevant "
      "quantization interval, i.e. half way between the two thresholds which "
      "delineate the interval.  If this argument is provided, however, then "
      "some samples will be represented by a value which lies only 3/8 of "
      "a step past the threshold of lower magnitude, toward the threshold "
      "of larger magnitude.  The samples which are treated in this way are "
      "all those whose quantized symbol indices have a magnitude no larger "
      "than M, where M is the <max magnitude> value.  It should be noted "
      "that this quantized magnitude is assessed with respect to the "
      "effective quantizer associated with the decoding process, which may "
      "be much coarser than that which was originally used during encoding, "
      "since one or more bit-planes may have been discarded.  Moreover, this "
      "interpretation may vary from sample to sample, not just from subband "
      "to subband, since bit-plane discarding is modulated at the individual "
      "sample level.  Typical values of M range from 1 to 4.",
     "-Qno_tcq",
      "If the input encoded file was obtained using TCQ, this option will "
      "turn off inverse TCQ processing and instead use the same scalar "
      "dequantizer like process which is used for non-fully decoded TCQ "
      "files.",
     NULL};

  return(args);
}

/*****************************************************************************/
/* STATIC                         __next_tile                                */
/*****************************************************************************/

static void
  __next_tile(dequantizer_ref base)

⌨️ 快捷键说明

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