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

📄 jchuff.c

📁 这是在PCA下的基于IPP库示例代码例子,在网上下了IPP的库之后,设置相关参数就可以编译该代码.
💻 C
📖 第 1 页 / 共 3 页
字号:
  state->next_output_byte = dest->next_output_byte;
  state->free_in_buffer   = dest->free_in_buffer;

  return TRUE;
} /* dump_buffer_intellib() */
#endif

/* Outputting bits to the file */

/* Only the right 24 bits of put_buffer are used; the valid bits are
 * left-justified in this part.  At most 16 bits can be passed to emit_bits
 * in one call, and we never retain more than 7 bits in put_buffer
 * between calls, so 24 bits are sufficient.
 */

INLINE
LOCAL(boolean)
emit_bits (working_state * state, unsigned int code, int size)
/* Emit some bits; return TRUE if successful, FALSE if must suspend */
{
  /* This routine is heavily used, so it's worth coding tightly. */
  register INT32 put_buffer = (INT32) code;
  register int put_bits = state->cur.put_bits;

  /* if size is 0, caller used an invalid Huffman table entry */
  if (size == 0)
    ERREXIT(state->cinfo, JERR_HUFF_MISSING_CODE);

  put_buffer &= (((INT32) 1)<<size) - 1; /* mask off any extra bits in code */
  
  put_bits += size;   /* new number of bits in buffer */
  
  put_buffer <<= 24 - put_bits; /* align incoming bits */

  put_buffer |= state->cur.put_buffer; /* and merge with old buffer contents */
  
  while (put_bits >= 8) {
    int c = (int) ((put_buffer >> 16) & 0xFF);
    
    emit_byte(state, c, return FALSE);
    if (c == 0xFF) {    /* need to stuff a zero byte? */
      emit_byte(state, 0, return FALSE);
    }
    put_buffer <<= 8;
    put_bits -= 8;
  }

  state->cur.put_buffer = put_buffer; /* update state variables */
  state->cur.put_bits = put_bits;

  return TRUE;
}


LOCAL(boolean)
flush_bits (working_state * state)
{
  if (! emit_bits(state, 0x7F, 7)) /* fill any partial byte with ones */
    return FALSE;
  state->cur.put_buffer = 0;  /* and reset bit-buffer to empty */
  state->cur.put_bits = 0;
  return TRUE;
}

#ifdef IPPJ_HUFF
LOCAL(boolean)
flush_bits_intellib (working_state * state)
{
  int currPos = 0;
  IppStatus status;

  if(state->free_in_buffer < 128)
  {
    dump_buffer(state);
  }

  status = ippiEncodeHuffman8x8_JPEG_16s1u_C1(
    0,
    state->next_output_byte,
    state->free_in_buffer,
    &currPos,
    0,
    0,
    0,
    state->cur.pEncHuffState,
    1);

  if(ippStsNoErr != status)
  {
    return FALSE;
  }

  state->next_output_byte += currPos;
  state->free_in_buffer   -= currPos;

  return TRUE;
} /* flush_bits_intellib() */
#endif

/* Encode a single block's worth of coefficients */

LOCAL(boolean)
encode_one_block(
  working_state* state,
  JCOEFPTR       block,
  int            last_dc_val,
  c_derived_tbl* dctbl,
  c_derived_tbl* actbl)
{
  register int temp, temp2;
  register int nbits;
  register int k, r, i;
  
  /* Encode the DC coefficient difference per section F.1.2.1 */
  
  temp = temp2 = block[0] - last_dc_val;

  if (temp < 0) {
    temp = -temp;   /* temp is abs value of input */
    /* For a negative input, want temp2 = bitwise complement of abs(input) */
    /* This code assumes we are on a two's complement machine */
    temp2--;
  }
  
  /* Find the number of bits needed for the magnitude of the coefficient */
  nbits = 0;
  while (temp) {
    nbits++;
    temp >>= 1;
  }
  /* Check for out-of-range coefficient values.
   * Since we're encoding a difference, the range limit is twice as much.
   */
  if (nbits > MAX_COEF_BITS+1)
    ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
  
  /* Emit the Huffman-coded symbol for the number of bits */
  if (! emit_bits(state, dctbl->ehufco[nbits], dctbl->ehufsi[nbits]))
    return FALSE;

  /* Emit that number of bits of the value, if positive, */
  /* or the complement of its magnitude, if negative. */
  if (nbits)      /* emit_bits rejects calls with size 0 */
    if (! emit_bits(state, (unsigned int) temp2, nbits))
      return FALSE;

  /* Encode the AC coefficients per section F.1.2.2 */
  
  r = 0;      /* r = run length of zeros */
  
  for (k = 1; k < DCTSIZE2; k++) {
    if ((temp = block[jpeg_natural_order[k]]) == 0) {
      r++;
    } else {
      /* if run length > 15, must emit special run-length-16 codes (0xF0) */
      while (r > 15) {
        if (! emit_bits(state, actbl->ehufco[0xF0], actbl->ehufsi[0xF0]))
          return FALSE;
        r -= 16;
      }

      temp2 = temp;
      if (temp < 0) {
        temp = -temp;   /* temp is abs value of input */
        /* This code assumes we are on a two's complement machine */
        temp2--;
      }
      
      /* Find the number of bits needed for the magnitude of the coefficient */
      nbits = 1;    /* there must be at least one 1 bit */
      while ((temp >>= 1))
        nbits++;
      /* Check for out-of-range coefficient values */
      if (nbits > MAX_COEF_BITS)
        ERREXIT(state->cinfo, JERR_BAD_DCT_COEF);
      
      /* Emit Huffman symbol for run length / number of bits */
      i = (r << 4) + nbits;
      if (! emit_bits(state, actbl->ehufco[i], actbl->ehufsi[i]))
        return FALSE;

      /* Emit that number of bits of the value, if positive, */
      /* or the complement of its magnitude, if negative. */
      if (! emit_bits(state, (unsigned int) temp2, nbits))
        return FALSE;
      
      r = 0;
    }
  }

  /* If the last coef(s) were zero, emit an end-of-block code */
  if (r > 0)
    if (! emit_bits(state, actbl->ehufco[0], actbl->ehufsi[0]))
      return FALSE;

  return TRUE;
}

#ifdef IPPJ_HUFF
LOCAL(boolean)
encode_one_block_intellib(
  working_state* state,
  JCOEFPTR       block,
  int            last_dc_val,
  c_derived_tbl* dctbl,
  c_derived_tbl* actbl)
{
  int currPos = 0;
  Ipp16s lastDC = (Ipp16s)last_dc_val;
  IppStatus status;

  if(state->free_in_buffer < 128)
  {
    dump_buffer_intellib(state);
  }

  status = ippiEncodeHuffman8x8_JPEG_16s1u_C1(
    block,
    state->next_output_byte,
    state->free_in_buffer,
    &currPos,
    &lastDC,
    dctbl->pHuffTbl,
    actbl->pHuffTbl,
    state->cur.pEncHuffState,
    0);

  if(ippStsNoErr != status)
  {
    return FALSE;
  }

  state->next_output_byte += currPos;
  state->free_in_buffer   -= currPos;

  return TRUE;
} /* encode_one_block_intellib() */
#endif

/*
 * Emit a restart marker & resynchronize predictions.
 */

LOCAL(boolean)
emit_restart (working_state * state, int restart_num)
{
  int ci;

  if (! flush_bits(state))
    return FALSE;

  emit_byte(state, 0xFF, return FALSE);
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);

  /* Re-initialize DC predictions to 0 */
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
    state->cur.last_dc_val[ci] = 0;

  /* The restart counter is not updated until we successfully write the MCU. */

  return TRUE;
}

#ifdef IPPJ_HUFF
LOCAL(boolean)
emit_restart_intellib (working_state * state, int restart_num)
{
  int ci;

  if (! flush_bits_intellib(state))
    return FALSE;

  emit_byte(state, 0xFF, return FALSE);
  emit_byte(state, JPEG_RST0 + restart_num, return FALSE);

  /* Re-initialize DC predictions to 0 */
  for (ci = 0; ci < state->cinfo->comps_in_scan; ci++)
    state->cur.last_dc_val[ci] = 0;

  /* The restart counter is not updated until we successfully write the MCU. */

  return TRUE;
}
#endif

/*
 * Encode and output one MCU's worth of Huffman-compressed coefficients.
 */

METHODDEF(boolean)
encode_mcu_huff (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;
  int blkn, ci;
  jpeg_component_info * compptr;

  /* Load up working state */
  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  /* Emit restart marker if needed */
  if(cinfo->restart_interval)
  {
    if(entropy->restarts_to_go == 0)
      if(! emit_restart(&state, entropy->next_restart_num))
        return FALSE;
  }

  /* Encode the MCU data blocks */
  for(blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++)
  {
    ci = cinfo->MCU_membership[blkn];
    compptr = cinfo->cur_comp_info[ci];
    if(! encode_one_block(&state,
         MCU_data[blkn][0], state.cur.last_dc_val[ci],
         entropy->dc_derived_tbls[compptr->dc_tbl_no],
         entropy->ac_derived_tbls[compptr->ac_tbl_no]))
      return FALSE;
    /* Update last_dc_val */
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  }

  /* Completed MCU, so update state */
  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);

  /* Update restart-interval state too */
  if(cinfo->restart_interval)
  {
    if(entropy->restarts_to_go == 0)
    {
      entropy->restarts_to_go = cinfo->restart_interval;
      entropy->next_restart_num++;
      entropy->next_restart_num &= 7;
    }
    entropy->restarts_to_go--;
  }

  return TRUE;
}

#ifdef IPPJ_HUFF
METHODDEF(boolean)
encode_mcu_huff_intellib (j_compress_ptr cinfo, JBLOCKROW *MCU_data)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;
  int blkn, ci;
  jpeg_component_info * compptr;

  /* Load up working state */
  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  /* Emit restart marker if needed */
  if(cinfo->restart_interval)
  {
    if(entropy->restarts_to_go == 0)
      if(! emit_restart_intellib(&state, entropy->next_restart_num))
        return FALSE;
  }

  /* Encode the MCU data blocks */
  for(blkn = 0; blkn < cinfo->blocks_in_MCU; blkn++)
  {
    ci = cinfo->MCU_membership[blkn];
    compptr = cinfo->cur_comp_info[ci];
    if(! encode_one_block_intellib(&state,
         MCU_data[blkn][0], state.cur.last_dc_val[ci],
         entropy->dc_derived_tbls[compptr->dc_tbl_no],
         entropy->ac_derived_tbls[compptr->ac_tbl_no]))
      return FALSE;
    /* Update last_dc_val */
    state.cur.last_dc_val[ci] = MCU_data[blkn][0][0];
  }

  /* Completed MCU, so update state */
  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);

  /* Update restart-interval state too */
  if(cinfo->restart_interval)
  {
    if(entropy->restarts_to_go == 0)
    {
      entropy->restarts_to_go = cinfo->restart_interval;
      entropy->next_restart_num++;
      entropy->next_restart_num &= 7;
    }
    entropy->restarts_to_go--;
  }

  return TRUE;
} /* encode_mcu_huff_intellib() */
#endif

/*
 * Finish up at the end of a Huffman-compressed scan.
 */

METHODDEF(void)
finish_pass_huff (j_compress_ptr cinfo)
{
  huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy;
  working_state state;

  /* Load up working state ... flush_bits needs it */
  state.next_output_byte = cinfo->dest->next_output_byte;
  state.free_in_buffer = cinfo->dest->free_in_buffer;
  ASSIGN_STATE(state.cur, entropy->saved);
  state.cinfo = cinfo;

  /* Flush out the last data */
#ifndef IPPJ_HUFF
    if (! flush_bits(&state))
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
#else
  if(cinfo->UseIPP) {
    if (! flush_bits_intellib(&state))
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
  } else {
    if (! flush_bits(&state))
      ERREXIT(cinfo, JERR_CANT_SUSPEND);
  }
#endif
  /* Update state */
  cinfo->dest->next_output_byte = state.next_output_byte;
  cinfo->dest->free_in_buffer = state.free_in_buffer;
  ASSIGN_STATE(entropy->saved, state.cur);
}


/*
 * Huffman coding optimization.
 *
 * We first scan the supplied data and count the number of uses of each symbol
 * that is to be Huffman-coded. (This process MUST agree with the code above.)
 * Then we build a Huffman coding tree for the observed counts.
 * Symbols which are not needed at all for the particular image are not
 * assigned any code, which saves space in the DHT marker as well as in
 * the compressed data.
 */

⌨️ 快捷键说明

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