?? jchuff.c
字號(hào):
/* * jchuff.c * * Copyright (C) 1991-1997, Thomas G. Lane. * This file is part of the Independent JPEG Group's software. * For conditions of distribution and use, see the accompanying README file. * * This file contains Huffman entropy encoding routines. * * Much of the complexity here has to do with supporting output suspension. * If the data destination module demands suspension, we want to be able to * back up to the start of the current MCU. To do this, we copy state * variables into local working storage, and update them back to the * permanent JPEG objects only upon successful completion of an MCU. */#define JPEG_INTERNALS#include "jinclude.h"#include "jpeglib.h"#include "jchuff.h" /* Declarations shared with jcphuff.c */#include "local_mem.h"#include "portab.h"/* Expanded entropy encoder object for Huffman encoding. * * The savable_state subrecord contains fields that change within an MCU, * but must not be updated permanently until we complete the MCU. */typedef struct { INT32 put_buffer; /* current bit-accumulation buffer */ int put_bits; /* # of bits now in it */ int last_dc_val[MAX_COMPS_IN_SCAN]; /* last DC coef for each component */} savable_state;/* This macro is to work around compilers with missing or broken * structure assignment. You'll need to fix this code if you have * such a compiler and you change MAX_COMPS_IN_SCAN. */#if 0#ifndef NO_STRUCT_ASSIGN#define ASSIGN_STATE(dest,src) ((dest) = (src))#else#if MAX_COMPS_IN_SCAN == 4#define ASSIGN_STATE(dest,src) \ ((dest).put_buffer = (src).put_buffer, \ (dest).put_bits = (src).put_bits, \ (dest).last_dc_val[0] = (src).last_dc_val[0], \ (dest).last_dc_val[1] = (src).last_dc_val[1], \ (dest).last_dc_val[2] = (src).last_dc_val[2], \ (dest).last_dc_val[3] = (src).last_dc_val[3])#endif#endif#endiftypedef struct { struct jpeg_entropy_encoder pub; /* public fields */ savable_state saved; /* Bit buffer & DC state at start of MCU */ /* These fields are NOT loaded into local working state. */ unsigned int restarts_to_go; /* MCUs left in this restart interval */ int next_restart_num; /* next restart number to write (0-7) */ /* Pointers to derived tables (these workspaces have image lifespan) */ c_derived_tbl * dc_derived_tbls[NUM_HUFF_TBLS]; c_derived_tbl * ac_derived_tbls[NUM_HUFF_TBLS];#ifdef ENTROPY_OPT_SUPPORTED /* Statistics tables for optimization */ long * dc_count_ptrs[NUM_HUFF_TBLS]; long * ac_count_ptrs[NUM_HUFF_TBLS];#endif} huff_entropy_encoder;typedef huff_entropy_encoder * huff_entropy_ptr;/* Working state while writing an MCU. * This struct contains all the fields that are needed by subroutines. */typedef struct { JOCTET * next_output_byte; /* => next byte to write in buffer */ unsigned int * next_output_word; //pwhsu++:20040113 size_t free_in_buffer; /* # of byte spaces remaining in buffer */ savable_state cur; /* Current bit buffer & DC state */ j_compress_ptr cinfo; /* dump_buffer needs access to this */} working_state;/* Forward declarations *///METHODDEF(boolean) encode_mcu_huff JPP((j_compress_ptr cinfo,// JBLOCKROW *MCU_data));METHODDEF(void) finish_pass_huff JPP((j_compress_ptr cinfo));//#ifdef ENTROPY_OPT_SUPPORTED//METHODDEF(boolean) encode_mcu_gather JPP((j_compress_ptr cinfo,// JBLOCKROW *MCU_data));//METHODDEF(void) finish_pass_gather JPP((j_compress_ptr cinfo));//METHODDEF(void) Encode_block JPP((working_state * state, int last_dc_val, // int dc_tbln, int ac_tbln, int vlcnum));//#endif/* * Initialize for a Huffman-compressed scan. * If gather_statistics is TRUE, we do not output anything during the scan, * just count the Huffman symbols used and generate Huffman code tables. */#if 0METHODDEF(void)start_pass_huff (j_compress_ptr cinfo, boolean gather_statistics){ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci, dctbl, actbl; jpeg_component_info * compptr;// entropy->pub.encode_mcu = encode_mcu_huff; entropy->pub.finish_pass = finish_pass_huff; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; dctbl = compptr->dc_tbl_no; actbl = compptr->ac_tbl_no; /* Compute derived values for Huffman tables */ /* We may do this more than once for a table, but it's not expensive */ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, & entropy->dc_derived_tbls[dctbl]); //pwhsu:20031016 玻ネdc huffman table jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, & entropy->ac_derived_tbls[actbl]); //pwhsu:20031016 玻ネac huffman table } /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0;}#endifvoid start_pass_huff1 (j_compress_ptr cinfo){ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; int ci, dctbl, actbl; jpeg_component_info * compptr; entropy->pub.finish_pass = finish_pass_huff; for (ci = 0; ci < cinfo->comps_in_scan; ci++) { compptr = cinfo->cur_comp_info[ci]; dctbl = compptr->dc_tbl_no; actbl = compptr->ac_tbl_no; /* Compute derived values for Huffman tables */ /* We may do this more than once for a table, but it's not expensive */ jpeg_make_c_derived_tbl(cinfo, TRUE, dctbl, & entropy->dc_derived_tbls[dctbl]); //pwhsu:20031016 玻ネdc huffman table jpeg_make_c_derived_tbl(cinfo, FALSE, actbl, & entropy->ac_derived_tbls[actbl]); //pwhsu:20031016 玻ネac huffman table } /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0;}/* * Compute the derived values for a Huffman table. * This routine also performs some validation checks on the table. * * Note this is also used by jcphuff.c. */GLOBAL(void)jpeg_make_c_derived_tbl (j_compress_ptr cinfo, boolean isDC, int tblno, c_derived_tbl ** pdtbl){ JHUFF_TBL *htbl; c_derived_tbl *dtbl; int p, i, l, lastp, si, maxsymbol; char huffsize[257]; unsigned int huffcode[257]; unsigned int code; int k1; //pwhsu++:20031022 unsigned int *curtbl; //pwhsu++:20040107 unsigned int codesize; //pwhsu++:20040107 /* Note that huffsize[] and huffcode[] are filled in code-length order, * paralleling the order of the symbols themselves in htbl->huffval[]. */ /* Find the input Huffman table */ if (tblno < 0 || tblno >= NUM_HUFF_TBLS) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); htbl = isDC ? cinfo->dc_huff_tbl_ptrs[tblno] : cinfo->ac_huff_tbl_ptrs[tblno]; if (htbl == NULL) ERREXIT1(cinfo, JERR_NO_HUFF_TABLE, tblno); /* Allocate a workspace if we haven't already done so. */ if (*pdtbl == NULL) *pdtbl = (c_derived_tbl *) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(c_derived_tbl)); dtbl = *pdtbl; /* Figure C.1: make table of Huffman code length for each symbol */ p = 0; for (l = 1; l <= 16; l++) { i = (int) htbl->bits[l]; if (i < 0 || p + i > 256) /* protect against table overrun */ ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); while (i--) huffsize[p++] = (char) l; } huffsize[p] = 0; lastp = p; /* Figure C.2: generate the codes themselves */ /* We also validate that the counts represent a legal Huffman code tree. */ code = 0; si = huffsize[0]; p = 0; while (huffsize[p]) { while (((int) huffsize[p]) == si) { huffcode[p++] = code; code++; } /* code is now 1 more than the last code used for codelength si; but * it must still fit in si bits, since no code is allowed to be all ones. */ if (((INT32) code) >= (((INT32) 1) << si)) ERREXIT(cinfo, JERR_BAD_HUFF_TABLE); code <<= 1; si++; } /* Figure C.3: generate encoding tables */ /* These are code and size indexed by symbol value */ /* Set all codeless symbols to have code length 0; * this lets us detect duplicate VAL entries here, and later * allows emit_bits to detect any attempt to emit such symbols. */// MEMZERO(dtbl->ehufsi, SIZEOF(dtbl->ehufsi)); //pwhsu--:20040128 memzero //pwhsu++:20031022 /* for ( k1=0; k1<256; k1++){ if (isDC){ rinfo.HuffsiDC[tblno][k1] = 0; } else{ rinfo.HuffsiAC[tblno][k1] = 0; } } //pwhsu++:20031022 */ /* This is also a convenient place to check for out-of-range * and duplicated VAL entries. We allow 0..255 for AC symbols * but only 0..15 for DC. (We could constrain them further * based on data depth and mode, but this seems enough.) */ maxsymbol = isDC ? 15 : 255; //pwhsu++:20040107 if(isDC){ curtbl = tblno ? huftbl1_dc : huftbl0_dc; }else{ curtbl = tblno ? huftbl1_ac : huftbl0_ac; } for (p = 0; p < lastp; p++) { i = htbl->huffval[p]; codesize = (huffcode[p]<< (5 + i%16) ) | (huffsize[p]+i%16); curtbl[i] = codesize; }}LOCAL(boolean)flush_bits (void){ unsigned char curbits; unsigned char curbits2; unsigned char tmp; unsigned int buf1; unsigned char buf2; READ_VADR(curbits); READ_VLASTWORD(buf1); tmp = (((curbits-1) & 0xff) % 8); curbits2 = 7-tmp; /* fill any partial byte with ones */ if (curbits2 != 0){ BitstreamPutBits((0x7F)>>tmp, curbits2); buf2 = ( buf1 >> ( ( 32-((curbits)&0x1F))&0x18 ) ) & 0xFF; switch(tmp){ case 0: buf2 = buf2 & 0x80 | 0x7f; break; case 1: buf2 = buf2 & 0xc0 | 0x3f; break; case 2: buf2 = buf2 & 0xe0 | 0x1f; break; case 3: buf2 = buf2 & 0xf0 | 0x0f; break; case 4: buf2 = buf2 & 0xf8 | 0x07; break; case 5: buf2 = buf2 & 0xfc | 0x03; break; case 6: buf2 = buf2 & 0xfe | 0x01; break; default: buf2 = 0x0; break; } if(buf2 == 0xff){ mFa526DrainWrBuf (); BitstreamPutBits(0, 8); } } return TRUE;}/* * Emit a restart marker & resynchronize predictions. */LOCAL(boolean)emit_restart (int restart_num){ if (! flush_bits()) return FALSE; //BitstreamPutBits(0xFF,8); BitstreamPutBits( 0xFF00 | JPEG_RST0 + restart_num,16); SET_PYDCR(0); SET_PUVDCR(0); return TRUE;}/* * Finish up at the end of a Huffman-compressed scan. */METHODDEF(void)finish_pass_huff (j_compress_ptr cinfo){ flush_bits();}/* * Module initialization routine for Huffman entropy encoding. */GLOBAL(void)jinit_huff_encoder (j_compress_ptr cinfo){ huff_entropy_ptr entropy; int i; entropy = (huff_entropy_ptr) (*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, SIZEOF(huff_entropy_encoder)); cinfo->entropy = (struct jpeg_entropy_encoder *) entropy;// entropy->pub.start_pass = start_pass_huff; /* Mark tables unallocated */ for (i = 0; i < NUM_HUFF_TBLS; i++) { entropy->dc_derived_tbls[i] = entropy->ac_derived_tbls[i] = NULL; }}//pwhsu++:20031030//The modified function of Encode one MCUboolean Encode_mcu (j_compress_ptr cinfo){ huff_entropy_ptr entropy = (huff_entropy_ptr) cinfo->entropy; working_state state; /* 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--; } /* Emit restart marker if needed */ if (cinfo->restart_interval) { if (entropy->restarts_to_go == 0) { if (! emit_restart(entropy->next_restart_num)) return FALSE; } } return TRUE;}//pwhsu++:20031030
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -