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

📄 hpbit_stream_in.c

📁 JPEG2000实现的源码
💻 C
📖 第 1 页 / 共 5 页
字号:
  last_entry = NULL;
  do {
    byte_val = 0;
    while (byte_val != 0x00FF)
      {
        byte_val = fetch_non_data_byte(self);
        if (byte_val == EOF)
          return(0);
      }
    if ((byte_val = fetch_non_data_byte(self)) == EOF)
      return(0);
    if (byte_val == (MARKER_SOD & 0x00FF))
      return(MARKER_SOD);
    if (byte_val == (MARKER_SOT & 0x00FF))
      {
        self->have_sot = 1;
        return(MARKER_SOT);
      }
    if (byte_val == (MARKER_EOC & 0x00FF))
      return(MARKER_EOC);

    start[0] = 0xFF;
    start[1] = (std_byte) byte_val;
    if ((byte_val = fetch_non_data_byte(self)) == EOF)
      return(0);
    start[2] = (std_byte) byte_val;
    if ((byte_val = fetch_non_data_byte(self)) == EOF)
      return(0);
    start[3] = (std_byte) byte_val;
    /* Begin Aerospace MCT mods (TSW) */
    if ( (start[1] == 0x74) || (start[1] == 0x75) || (start[1] == 0x77) ) {
      if ((byte_val = fetch_non_data_byte(self)) == EOF)
	return(0);
      start[4] = (std_byte) byte_val;
      if ((byte_val = fetch_non_data_byte(self)) == EOF)
	return(0);
      start[5] = (std_byte) byte_val;
    }
    /* End Aerospace MCT mods */
    
    length = start[2];
    length <<= 8; length += start[3];
    /* Begin Aerospace MCT mods (TSW) */
    if ((start[1] == 0x74) || (start[1] == 0x75) || (start[1] == 0x77)) {
      length <<= 8; length += start[4];
      length <<= 8; length += start[5];
      pre_len = 6;
    }
    /* End Aerospace MCT mods */
    length += 2; /* Length does not include the marker code itself. */
    entry = (hpbit_codestream_marker_ptr)
      local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_codestream_marker));
    if (last_entry == NULL)
      {
        assert(markers->codestream_markers == NULL);
        markers->codestream_markers = entry;
      }
    else
      last_entry->next = entry;
    last_entry = entry;
    entry->buf = local_malloc(HPBIT_MEM_KEY,length);
    /* Begin Aerospace MCT mods */
    /*    for (n=0; n < 4; n++)*/
    for (n=0; n < pre_len; n++)
      entry->buf[n] = start[n];
    /*    length = read_non_data_bytes(self,entry->buf+4,length-4);
      entry->num_bytes = length + 4;*/
    length = read_non_data_bytes(self,entry->buf+pre_len,length-pre_len);
    entry->num_bytes = length + pre_len;
    /* End Aerospace MCT mods */
    markers->total_codestream_bytes += entry->num_bytes;
  } while (1);
}

/*****************************************************************************/
/* STATIC                         add_elt                                    */
/*****************************************************************************/

static hpbit_marker_elt_ptr
  add_elt(hpbit_pre_marker_ptr pm, const char *elt_id, int size)

 /* Adds a new element to the list managed by `pm' and returns a pointer
    to this new element.  The new element has `size' entries. */

{
  hpbit_marker_elt_ptr elt;
  int marker_id, element_id;

  marker_id = elt_id[0];
  element_id = elt_id[1];
  assert(marker_id == pm->id);
  assert(size > 0);
  for (elt=pm->elements; elt != NULL; elt=elt->next)
    assert(elt->element_id != element_id);

  elt = (hpbit_marker_elt_ptr)
    local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_marker_elt));
  elt->next = pm->elements;
  pm->elements = elt;
  elt->marker_id = marker_id;
  elt->element_id = element_id;
  elt->description = elt_id + 2;
  elt->size = size;
  elt->buf = (std_uint *) local_malloc(HPBIT_MEM_KEY,size*sizeof(std_uint));
  return(elt);
}

/* Begin Aerospace MCT mods (TSW) */
/*****************************************************************************/
/* STATIC                         add_elt_indirect                           */
/*****************************************************************************/
static hpbit_marker_elt_ptr
add_elt_indirect(hpbit_pre_marker_ptr pm,const char *elt_id,
				 int data_size_indirect)
/* Adds a new element to the list managed by `pm' and returns a pointer
   to this new element.  The new element has only a pointer to a
   data buffer - data_size_direct specifies how many bytes the buffer
   will contain. The size field is set to 1 to indicate that the buffer
   is to be treated as a single data element. The calling routine
   must fill in the buffer pointer, either by setting it to a valid
   memory location or by allocating memory that is filled by 
   translation of a marker field. */
{
	hpbit_marker_elt_ptr elt;
	int marker_id, element_id;
	
	marker_id = elt_id[0];
	element_id = elt_id[1];
	assert(marker_id == pm->id);
	assert(data_size_indirect > 0);
	for (elt=pm->elements; elt != NULL; elt=elt->next)
		assert(elt->element_id != element_id);
	
	elt = (hpbit_marker_elt_ptr)
		local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_marker_elt));
	elt->next = pm->elements;
	pm->elements = elt;
	elt->marker_id = marker_id;
	elt->element_id = element_id;
	elt->description = elt_id + 2;
	elt->size = 1;
	elt->data_size_indirect = data_size_indirect;
	elt->buf = NULL;
	return(elt);
}
/* End Aerospace MCT mods */

/*****************************************************************************/
/* STATIC                 get_tile_component_bands                           */
/*****************************************************************************/

static int
  get_tile_component_bands(hpbit_stream_in_ref self, int tnum, int comp_idx,
                           int num_levels)

 /* This function computes the total number of bands for the relevant
    tile-component based on the number of decomposition levels and any
    special decomposition structure. */

{
  int length, hp_descent, num_bands, n;

  length = (int)
    self->base.size_marker_elt(&(self->base),tnum,MARKER_COC_DECOMP,comp_idx);
  if (length == 0)
    return(3*num_levels+1); /* Mallat decomposition. */
  num_bands = 1;
  hp_descent = 0;
  for (n=0; n < num_levels; n++)
    {
      if (n < length)
        hp_descent = (int)
          self->base.get_marker_val(&(self->base),tnum,MARKER_COC_DECOMP,
                                    comp_idx,n);
      if (hp_descent == 1)
        num_bands += 3;
      else if (hp_descent == 2)
        num_bands += 12;
      else if (hp_descent == 3)
        num_bands += 48;
      else
        num_bands += 3; /* Error; use Mallat as default. */
    }      
  return(num_bands);
}

/*****************************************************************************/
/* STATIC                   process_ppt_markers                              */
/*****************************************************************************/

static void
  process_ppt_markers(hpbit_markers_ptr markers, hpbit_tilepart_ptr tp)
{
  std_byte z;
  hpbit_codestream_marker_ptr cp, prev;
  hpbit_packed_head_ptr ph, last_ph;
  int length;

  assert(tp->packed_heads == NULL);
  z = 0;
  last_ph = NULL;
  do {
    for (prev=NULL,
         cp=markers->codestream_markers; cp != NULL; prev=cp, cp=cp->next)
      if ((cp->buf[1] == (MARKER_PPT & 0x00FF)) &&
          (cp->num_bytes > 5) && (cp->buf[4] == z))
        {
          assert(!cp->accesses);
          if (prev == NULL)
            markers->codestream_markers = cp->next;
          else
            prev->next = cp->next;
          ph = (hpbit_packed_head_ptr)
            local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_packed_head));
          if (last_ph == NULL)
            tp->packed_heads = ph;
          else
            last_ph->next = ph;
          last_ph = ph;
          ph->data_handle = cp->buf;
          ph->data = cp->buf + 5;
          length = (int) cp->buf[2];
          length <<= 8; length |= (int) cp->buf[3];
          ph->bytes_left = length - 3;
          local_free(cp);
          break;
        }
    z++;
  } while (cp != NULL);
}

/*****************************************************************************/
/* STATIC                   process_ppm_markers                              */
/*****************************************************************************/

/* Begin DST PPM fix. */
static void
  process_ppm_markers(hpbit_markers_ptr markers, hpbit_stream_in_ref self)
{
  std_byte z;
  hpbit_codestream_marker_ptr cp, prev;
  hpbit_stored_heads_ptr sh=NULL, last_sh;  /* TJF: =NULL to avoid warnings */
  hpbit_packed_head_ptr ph;
  std_byte *buf;
  int length, tp_bytes;

  assert(self->stored_heads == NULL);
  z = 0;
  last_sh = NULL;
  tp_bytes = 0;
  do {
    for (prev=NULL,
         cp=markers->codestream_markers; cp != NULL; prev=cp, cp=cp->next)
      if ((cp->buf[1] == (MARKER_PPM & 0x00FF)) &&
          (cp->num_bytes > 5) && (cp->buf[4] == z))
        {
          assert(!cp->accesses);
          if (prev == NULL)
            markers->codestream_markers = cp->next;
          else
            prev->next = cp->next;
          length = (int) cp->buf[2];
          length <<= 8; length |= (int) cp->buf[3];
          length -= 3;
          buf = cp->buf + 5;
          ph = NULL;
          while (length > 0)
            {
              int ph_bytes;

              if (tp_bytes == 0)
                {
                  sh = (hpbit_stored_heads_ptr)
                    local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_stored_heads));
                  if (last_sh == NULL)
                    self->stored_heads = sh;
                  else
                    last_sh->next = sh;
                  last_sh = sh;
                  if (length < 4)
                    local_error("PPM marker segment too short to contain "
                                "required Nppm field!");
                  tp_bytes = *(buf++); length--;
                  tp_bytes <<= 8; tp_bytes += *(buf++); length--;
                  tp_bytes <<= 8; tp_bytes += *(buf++); length--;
                  tp_bytes <<= 8; tp_bytes += *(buf++); length--;
                  continue;
                }
              ph_bytes = (tp_bytes<length)?tp_bytes:length;
              if (sh->packed_heads == NULL)
                sh->packed_heads = ph = (hpbit_packed_head_ptr)
                  local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_packed_head));
              else
                {
                  for (ph=sh->packed_heads; ph->next != NULL; ph=ph->next);
                  ph = ph->next = (hpbit_packed_head_ptr)
                    local_malloc(HPBIT_MEM_KEY,sizeof(hpbit_packed_head));
                }
              ph->next = NULL;
              ph->bytes_left = ph_bytes;
              ph->data = buf;
              ph->data_handle = NULL;
              length -= ph_bytes;
              buf += ph_bytes;
            }
          if (ph != NULL)
            ph->data_handle = cp->buf;
          else
            local_free(cp->buf);
          local_free(cp);

          break;
        }
    z++;
  } while (cp != NULL);
}
/* End DST PPM fix. */

/*****************************************************************************/
/* STATIC                 translate_cod_coc_markers                          */
/*****************************************************************************/

static int
  translate_cod_coc_markers(hpbit_stream_in_ref self,
                            hpbit_markers_ptr markers)

 /* Looks for COD and COC markers in the `markers->codestream_markers' list
    and uses these to construct the COD and COC pre-markers, putting them
    on the `markers->pre_markers' list.  If no COD marker can be found, the
    function returns 0.  Otherwise, the function returns 1.  The function
    should not raise asserts or terminate the program abnormally.  If marker
    translation fails, `self->failed_marker_translations' should be
    incremented instead.  Note: only the `num_components' field of
    `markers->params' is valid on entry; the function should not itself
    attempt to set any fields in the `markers->params' structure, since these
    are set later.  You should be able to extract any other coding parameters
    you need from the COD/COC markers themselves. */

{
  /* RSV mjg & gkw */
  hpbit_codestream_marker_ptr cp;
  int failure;
  cod_marker cod;
  coc_marker coc;
  int comp, i;
  hpbit_pre_marker_ptr pm;
  hpbit_marker_elt_ptr elt, ppx, ppy;
  std_byte *bp, cod_ppx[20], cod_ppy[20], coc_ppx[20], coc_ppy[20], ppxy;

  memset(&cod,0,sizeof(cod));
  memset(&coc,0,sizeof(coc));
  failure = 0;

  for (cp=markers->codestream_markers; cp != NULL; cp=cp->next)
    if (cp->buf[1] == (MARKER_COD & 0x00FF))
      {
        hpbit_pre_marker_ptr pm;

⌨️ 快捷键说明

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