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

📄 jcphuff.pas

📁 用pascal寫的jpeg codec, 測試過的
💻 PAS
📖 第 1 页 / 共 2 页
字号:
    if ishift_temp < 0 then
      temp2 := (ishift_temp shr Al) or ((not 0) shl (16-Al))
    else
      temp2 := ishift_temp shr Al;


    { DC differences are figured on the point-transformed values. }
    temp := temp2 - entropy^.last_dc_val[ci];
    entropy^.last_dc_val[ci] := temp2;

    { Encode the DC coefficient difference per section G.1.2.1 }
    temp2 := temp;
    if (temp < 0) then
    begin
      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 }
      Dec(temp2);
    end;

    { Find the number of bits needed for the magnitude of the coefficient }
    nbits := 0;
    while (temp <> 0) do
    begin
      Inc(nbits);
      temp := temp shr 1;
    end;

    { Count/emit the Huffman-coded symbol for the number of bits }
    emit_symbol(entropy, compptr^.dc_tbl_no, nbits);

    { Emit that number of bits of the value, if positive, }
    { or the complement of its magnitude, if negative. }
    if (nbits <> 0) then       { emit_bits rejects calls with size 0 }
      emit_bits(entropy, uInt(temp2), nbits);
  end;

  cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;

  { Update restart-interval state too }
  if (cinfo^.restart_interval <> 0) then
  begin
    if (entropy^.restarts_to_go = 0) then
    begin
      entropy^.restarts_to_go := cinfo^.restart_interval;
      Inc(entropy^.next_restart_num);
      with entropy^ do
        next_restart_num := next_restart_num and 7;
    end;
    Dec(entropy^.restarts_to_go);
  end;

  encode_mcu_DC_first := TRUE;
end;


{ MCU encoding for AC initial scan (either spectral selection,
  or first pass of successive approximation). }

{METHODDEF}
function encode_mcu_AC_first (cinfo : j_compress_ptr;
                              const MCU_data: array of JBLOCKROW) : boolean;
var
  entropy : phuff_entropy_ptr;
  {register} temp, temp2 : int;
  {register} nbits : int;
  {register} r, k : int;
  Se : int;
  Al : int;
  block : JBLOCK_PTR;
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);
  Se := cinfo^.Se;
  Al := cinfo^.Al;

  entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;

  { Emit restart marker if needed }
  if (cinfo^.restart_interval <> 0) then
    if (entropy^.restarts_to_go = 0) then
      emit_restart(entropy, entropy^.next_restart_num);

  { Encode the MCU data block }
  block := JBLOCK_PTR(MCU_data[0]);

  { Encode the AC coefficients per section G.1.2.2, fig. G.3 }

  r := 0;			{ r := run length of zeros }

  for k := cinfo^.Ss to Se do
  begin
    temp := (block^[jpeg_natural_order[k]]);
    if (temp = 0) then
    begin
      Inc(r);
      continue;
    end;
    { We must apply the point transform by Al.  For AC coefficients this
      is an integer division with rounding towards 0.  To do this portably
      in C, we shift after obtaining the absolute value; so the code is
      interwoven with finding the abs value (temp) and output bits (temp2). }

    if (temp < 0) then
    begin
      temp := -temp;		{ temp is abs value of input }
      temp := temp shr Al;	{ apply the point transform }
      { For a negative coef, want temp2 := bitwise complement of abs(coef) }
      temp2 := not temp;
    end
    else
    begin
      temp := temp shr Al;	{ apply the point transform }
      temp2 := temp;
    end;
    { Watch out for case that nonzero coef is zero after point transform }
    if (temp = 0) then
    begin
      Inc(r);
      continue;
    end;

    { Emit any pending EOBRUN }
    if (entropy^.EOBRUN > 0) then
      emit_eobrun(entropy);
    { if run length > 15, must emit special run-length-16 codes ($F0) }
    while (r > 15) do
    begin
      emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
      Dec(r, 16);
    end;

    { Find the number of bits needed for the magnitude of the coefficient }
    nbits := 0;			{ there must be at least one 1 bit }
    repeat
      Inc(nbits);
      temp := temp shr 1;
    until (temp = 0);
      

    { Count/emit Huffman symbol for run length / number of bits }
    emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + nbits);

    { Emit that number of bits of the value, if positive, }
    { or the complement of its magnitude, if negative. }
    emit_bits(entropy, uInt(temp2), nbits);

    r := 0;			{ reset zero run length }
  end;

  if (r > 0) then
  begin			        { If there are trailing zeroes, }
    Inc(entropy^.EOBRUN);	{ count an EOB }
    if (entropy^.EOBRUN = $7FFF) then
      emit_eobrun(entropy);	{ force it out to avoid overflow }
  end;

  cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;

  { Update restart-interval state too }
  if (cinfo^.restart_interval <> 0) then
  begin
    if (entropy^.restarts_to_go = 0) then
    begin
      entropy^.restarts_to_go := cinfo^.restart_interval;
      Inc(entropy^.next_restart_num);
      with entropy^ do
        next_restart_num := next_restart_num and 7;
    end;
    Dec(entropy^.restarts_to_go);
  end;

  encode_mcu_AC_first := TRUE;
end;


{ MCU encoding for DC successive approximation refinement scan.
  Note: we assume such scans can be multi-component, although the spec
  is not very clear on the point. }

{METHODDEF}
function encode_mcu_DC_refine (cinfo : j_compress_ptr;
                              const MCU_data: array of JBLOCKROW) : boolean;
var
  entropy : phuff_entropy_ptr;
  {register} temp : int;
  blkn : int;
  Al : int;
  block : JBLOCK_PTR;
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);
  Al := cinfo^.Al;

  entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;

  { Emit restart marker if needed }
  if (cinfo^.restart_interval <> 0) then
    if (entropy^.restarts_to_go = 0) then
      emit_restart(entropy, entropy^.next_restart_num);

  { Encode the MCU data blocks }
  for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  begin
    block := JBLOCK_PTR(MCU_data[blkn]);

    { We simply emit the Al'th bit of the DC coefficient value. }
    temp := block^[0];
    emit_bits(entropy, uInt(temp shr Al), 1);
  end;

  cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;

  { Update restart-interval state too }
  if (cinfo^.restart_interval <> 0) then
  begin
    if (entropy^.restarts_to_go = 0) then
    begin
      entropy^.restarts_to_go := cinfo^.restart_interval;
      Inc(entropy^.next_restart_num);
      with entropy^ do
        next_restart_num := next_restart_num and 7;
    end;
    Dec(entropy^.restarts_to_go);
  end;

  encode_mcu_DC_refine := TRUE;
end;


{ MCU encoding for AC successive approximation refinement scan. }

{METHODDEF}
function encode_mcu_AC_refine (cinfo : j_compress_ptr;
                               const MCU_data: array of JBLOCKROW) : boolean;

var
  entropy : phuff_entropy_ptr;
  {register} temp : int;
  {register} r, k : int;
  EOB : int;
  BR_buffer : JBytePtr;
  BR : uInt;
  Se : int;
  Al : int;
  block : JBLOCK_PTR;
  absvalues : array[0..DCTSIZE2-1] of int;
begin
  entropy := phuff_entropy_ptr(cinfo^.entropy);
  Se := cinfo^.Se;
  Al := cinfo^.Al;

  entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;

  { Emit restart marker if needed }
  if (cinfo^.restart_interval <> 0) then
    if (entropy^.restarts_to_go = 0) then
      emit_restart(entropy, entropy^.next_restart_num);

  { Encode the MCU data block }
  block := JBLOCK_PTR(MCU_data[0]);

  { It is convenient to make a pre-pass to determine the transformed
    coefficients' absolute values and the EOB position. }

  EOB := 0;
  for k := cinfo^.Ss to Se do
  begin
    temp := block^[jpeg_natural_order[k]];
    { We must apply the point transform by Al.  For AC coefficients this
      is an integer division with rounding towards 0.  To do this portably
      in C, we shift after obtaining the absolute value. }

    if (temp < 0) then
      temp := -temp;		{ temp is abs value of input }
    temp := temp shr Al;		{ apply the point transform }
    absvalues[k] := temp;	{ save abs value for main pass }
    if (temp = 1) then
      EOB := k;			{ EOB := index of last newly-nonzero coef }
  end;

  { Encode the AC coefficients per section G.1.2.3, fig. G.7 }

  r := 0;			{ r := run length of zeros }
  BR := 0;			{ BR := count of buffered bits added now }
  BR_buffer := JBytePtr(@(entropy^.bit_buffer^[entropy^.BE]));
                                { Append bits to buffer }

  for k := cinfo^.Ss to Se do
  begin
    temp := absvalues[k];
    if (temp = 0) then
    begin
      Inc(r);
      continue;
    end;

    { Emit any required ZRLs, but not if they can be folded into EOB }
    while (r > 15) and (k <= EOB) do
    begin
      { emit any pending EOBRUN and the BE correction bits }
      emit_eobrun(entropy);
      { Emit ZRL }
      emit_symbol(entropy, entropy^.ac_tbl_no, $F0);
      Dec(r, 16);
      { Emit buffered correction bits that must be associated with ZRL }
      emit_buffered_bits(entropy, BR_buffer, BR);
      BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
      BR := 0;
    end;

    { If the coef was previously nonzero, it only needs a correction bit.
      NOTE: a straight translation of the spec's figure G.7 would suggest
      that we also need to test r > 15.  But if r > 15, we can only get here
      if k > EOB, which implies that this coefficient is not 1. }
    if (temp > 1) then
    begin
      { The correction bit is the next bit of the absolute value. }
      BR_buffer^[BR] := byte (temp and 1);
      Inc(BR);
      continue;
    end;

    { Emit any pending EOBRUN and the BE correction bits }
    emit_eobrun(entropy);

    { Count/emit Huffman symbol for run length / number of bits }
    emit_symbol(entropy, entropy^.ac_tbl_no, (r shl 4) + 1);

    { Emit output bit for newly-nonzero coef }
    if (block^[jpeg_natural_order[k]] < 0) then
      temp := 0
    else
      temp := 1;
    emit_bits(entropy, uInt(temp), 1);

    { Emit buffered correction bits that must be associated with this code }
    emit_buffered_bits(entropy, BR_buffer, BR);
    BR_buffer := entropy^.bit_buffer; { BE bits are gone now }
    BR := 0;
    r := 0;			{ reset zero run length }
  end;

  if (r > 0) or (BR > 0) then
  begin	                        { If there are trailing zeroes, }
    Inc(entropy^.EOBRUN);       { count an EOB }
    Inc(entropy^.BE, BR);          { concat my correction bits to older ones }
    { We force out the EOB if we risk either:
      1. overflow of the EOB counter;
      2. overflow of the correction bit buffer during the next MCU. }

    if (entropy^.EOBRUN = $7FFF) or
       (entropy^.BE > (MAX_CORR_BITS-DCTSIZE2+1)) then
      emit_eobrun(entropy);
  end;

  cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;

  { Update restart-interval state too }
  if (cinfo^.restart_interval <> 0) then
  begin
    if (entropy^.restarts_to_go = 0) then
    begin
      entropy^.restarts_to_go := cinfo^.restart_interval;
      Inc(entropy^.next_restart_num);
      with entropy^ do
        next_restart_num := next_restart_num and 7;
    end;
    Dec(entropy^.restarts_to_go);
  end;

  encode_mcu_AC_refine := TRUE;
end;


{ Finish up at the end of a Huffman-compressed progressive scan. }

{METHODDEF}
procedure finish_pass_phuff (cinfo : j_compress_ptr);
var
  entropy : phuff_entropy_ptr;
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);

  entropy^.next_output_byte := cinfo^.dest^.next_output_byte;
  entropy^.free_in_buffer := cinfo^.dest^.free_in_buffer;

  { Flush out any buffered data }
  emit_eobrun(entropy);
  flush_bits(entropy);

  cinfo^.dest^.next_output_byte := entropy^.next_output_byte;
  cinfo^.dest^.free_in_buffer := entropy^.free_in_buffer;
end;


{ Finish up a statistics-gathering pass and create the new Huffman tables. }

{METHODDEF}
procedure finish_pass_gather_phuff (cinfo : j_compress_ptr);
var
  entropy : phuff_entropy_ptr;
  is_DC_band : boolean;
  ci, tbl : int;
  compptr : jpeg_component_info_ptr;
  htblptr : ^JHUFF_TBL_PTR;
  did : array[0..NUM_HUFF_TBLS-1] of boolean;
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);

  { Flush out buffered data (all we care about is counting the EOB symbol) }
  emit_eobrun(entropy);

  is_DC_band := (cinfo^.Ss = 0);

  { It's important not to apply jpeg_gen_optimal_table more than once
    per table, because it clobbers the input frequency counts! }

  MEMZERO(@did, SIZEOF(did));

  for ci := 0 to pred(cinfo^.comps_in_scan) do
  begin
    compptr := cinfo^.cur_comp_info[ci];
    if (is_DC_band) then
    begin
      if (cinfo^.Ah <> 0) then     { DC refinement needs no table }
	continue;
      tbl := compptr^.dc_tbl_no;
    end
    else
    begin
      tbl := compptr^.ac_tbl_no;
    end;
    if (not did[tbl]) then
    begin
      if (is_DC_band) then
        htblptr := @(cinfo^.dc_huff_tbl_ptrs[tbl])
      else
        htblptr := @(cinfo^.ac_huff_tbl_ptrs[tbl]);
      if (htblptr^ = NIL) then
        htblptr^ := jpeg_alloc_huff_table(j_common_ptr(cinfo));
      jpeg_gen_optimal_table(cinfo, htblptr^, entropy^.count_ptrs[tbl]^);
      did[tbl] := TRUE;
    end;
  end;
end;


{ Module initialization routine for progressive Huffman entropy encoding. }

{GLOBAL}
procedure jinit_phuff_encoder (cinfo : j_compress_ptr);
var
  entropy : phuff_entropy_ptr;
  i : int;
begin
  entropy := phuff_entropy_ptr(
    cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
				SIZEOF(phuff_entropy_encoder)) );
  cinfo^.entropy := jpeg_entropy_encoder_ptr(entropy);
  entropy^.pub.start_pass := start_pass_phuff;

  { Mark tables unallocated }
  for i := 0 to pred(NUM_HUFF_TBLS) do
  begin
    entropy^.derived_tbls[i] := NIL;
    entropy^.count_ptrs[i] := NIL;
  end;
  entropy^.bit_buffer := NIL;	{ needed only in AC refinement scan }
end;

end.

⌨️ 快捷键说明

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