📄 jchuff.c
字号:
/* * 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 "huftbl_new.h"#include "huftbl_default.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; unsigned int *curtbl; int i; entropy->pub.finish_pass = finish_pass_huff;#if 0 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 }#else if(hufftbl_no==1){ //default table //dc 0 huffman table curtbl = huftbl0_dc; //__asm{ //STR dchuftbl0[0], [curtbl] //STR dchuftbl0[1], [curtbl+1] //STR dchuftbl0[2], [curtbl+2] //STR dchuftbl0[3], [curtbl+3] //STR dchuftbl0[4], [curtbl+4] //STR dchuftbl0[5], [curtbl+5] //STR dchuftbl0[6], [curtbl+6] //STR dchuftbl0[7], [curtbl+7] //STR dchuftbl0[8], [curtbl+8] //STR dchuftbl0[9], [curtbl+9] //STR dchuftbl0[10], [curtbl+10] //STR dchuftbl0[11], [curtbl+11] //STR dchuftbl0[12], [curtbl+12] //STR dchuftbl0[13], [curtbl+13] //STR dchuftbl0[14], [curtbl+14] //} curtbl[0]=dchuftbl0[0]; curtbl[1]=dchuftbl0[1]; curtbl[2]=dchuftbl0[2]; curtbl[3]=dchuftbl0[3]; curtbl[4]=dchuftbl0[4]; curtbl[5]=dchuftbl0[5]; curtbl[6]=dchuftbl0[6]; curtbl[7]=dchuftbl0[7]; curtbl[8]=dchuftbl0[8]; curtbl[9]=dchuftbl0[9]; curtbl[10]=dchuftbl0[10]; curtbl[11]=dchuftbl0[11]; curtbl[12]=dchuftbl0[12]; curtbl[13]=dchuftbl0[13]; curtbl[14]=dchuftbl0[14]; //dc 1 huffman table curtbl = huftbl1_dc; //__asm{ //STR dchuftbl1[0], [curtbl+0] //STR dchuftbl1[1], [curtbl+1] //STR dchuftbl1[2], [curtbl+2] //STR dchuftbl1[3], [curtbl+3] //STR dchuftbl1[4], [curtbl+4] //STR dchuftbl1[5], [curtbl+5] //STR dchuftbl1[6], [curtbl+6] //STR dchuftbl1[7], [curtbl+7] //STR dchuftbl1[8], [curtbl+8] //STR dchuftbl1[9], [curtbl+9] //STR dchuftbl1[10], [curtbl+10] //STR dchuftbl1[11], [curtbl+11] //STR dchuftbl1[12], [curtbl+12] //STR dchuftbl1[13], [curtbl+13] //STR dchuftbl1[14], [curtbl+14] //} curtbl[0]=dchuftbl1[0]; curtbl[1]=dchuftbl1[1]; curtbl[2]=dchuftbl1[2]; curtbl[3]=dchuftbl1[3]; curtbl[4]=dchuftbl1[4]; curtbl[5]=dchuftbl1[5]; curtbl[6]=dchuftbl1[6]; curtbl[7]=dchuftbl1[7]; curtbl[8]=dchuftbl1[8]; curtbl[9]=dchuftbl1[9]; curtbl[10]=dchuftbl1[10]; curtbl[11]=dchuftbl1[11]; curtbl[12]=dchuftbl1[12]; curtbl[13]=dchuftbl1[13]; curtbl[14]=dchuftbl1[14]; //ac 0 huffman table curtbl = huftbl0_ac; for(i=0;i<256;i++){ //__asm{ //STR achuftbl0[i], [curtbl+i] //} curtbl[i]=achuftbl0[i]; } //ac 1 huffman table curtbl = huftbl1_ac; for(i=0;i<256;i++){ //__asm{ //STR achuftbl1[i], [curtbl+i] //} curtbl[i]=achuftbl1[i]; } }else{ //new table //dc 0 huffman table curtbl = huftbl0_dc; for(i=0;i<15;i++){ //__asm{ //STR dchuftbln0[i], [curtbl+i] //} curtbl[i]=dchuftbln0[i]; } //dc 1 huffman table curtbl = huftbl1_dc; for(i=0;i<15;i++){ //__asm{ //STR dchuftbln1[i], [curtbl+i] //} curtbl[i]=dchuftbln1[i]; } //ac 0 huffman table curtbl = huftbl0_ac; for(i=0;i<256;i++){ //__asm{ //STR achuftbln0[i], [curtbl+i] //} curtbl[i]=achuftbln0[i]; } //ac 1 huffman table curtbl = huftbl1_ac; for(i=0;i<256;i++){ //__asm{ //STR achuftbln1[i], [curtbl+i] //} curtbl[i]=achuftbln1[i]; } }#endif /* Initialize restart stuff */ entropy->restarts_to_go = cinfo->restart_interval; entropy->next_restart_num = 0;}/* * Compute the derived values for a Huffman table.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -