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

📄 jdphuff.pas

📁 用pascal寫的jpeg codec, 測試過的
💻 PAS
📖 第 1 页 / 共 3 页
字号:
    if (bits_left < 1) then
    begin
      if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
      begin
        decode_mcu_DC_refine := FALSE;
        exit;
      end;
      get_buffer := br_state.get_buffer;
      bits_left := br_state.bits_left;
    end;

    {if (GET_BITS(1)) then}
    Dec(bits_left);
    if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) ) <> 0 then
      block^[0] := block^[0] or p1;
    { Note: since we use OR, repeating the assignment later is safe }
  end;

  { Completed MCU, so update state }
  {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  cinfo^.src^.next_input_byte := br_state.next_input_byte;
  cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  cinfo^.unread_marker := br_state.unread_marker;
  entropy^.bitstate.get_buffer := get_buffer;
  entropy^.bitstate.bits_left := bits_left;

  { Account for restart interval (no-op if not using restarts) }
  Dec(entropy^.restarts_to_go);

  decode_mcu_DC_refine := TRUE;
end;


{ MCU decoding for AC successive approximation refinement scan. }

{METHODDEF}
function decode_mcu_AC_refine (cinfo : j_decompress_ptr;
                               var MCU_data : array of JBLOCKROW) : boolean;
label
  undoit, label3;
var
  entropy : phuff_entropy_ptr;
  Se : int;
  p1 : int;     { 1 in the bit position being coded }
  m1 : int;     { -1 in the bit position being coded }
  {register} s, k, r : int;
  EOBRUN : uInt;
  block : JBLOCK_PTR;
  thiscoef : JCOEF_PTR;
  {BITREAD_STATE_VARS;}
  get_buffer : bit_buf_type ; {register}
  bits_left : int; {register}
  br_state : bitread_working_state;

  tbl : d_derived_tbl_ptr;
  num_newnz : int;
  newnz_pos : array[0..DCTSIZE2-1] of int;
var
  pos : int;
var
  nb, look : int; {register}
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);
  Se := cinfo^.Se;
  p1 := 1 shl cinfo^.Al;	{ 1 in the bit position being coded }
  m1 := (-1) shl cinfo^.Al;	{ -1 in the bit position being coded }

  { Process restart marker if needed; may have to suspend }
  if (cinfo^.restart_interval <> 0) then
  begin
    if (entropy^.restarts_to_go = 0) then
      if (not process_restart(cinfo)) then
      begin
	decode_mcu_AC_refine := FALSE;
        exit;
      end;
  end;

  { Load up working state }
  {BITREAD_LOAD_STATE(cinfo,entropy^.bitstate);}
  br_state.cinfo := cinfo;
  br_state.next_input_byte := cinfo^.src^.next_input_byte;
  br_state.bytes_in_buffer := cinfo^.src^.bytes_in_buffer;
  br_state.unread_marker := cinfo^.unread_marker;
  get_buffer := entropy^.bitstate.get_buffer;
  bits_left := entropy^.bitstate.bits_left;
  br_state.printed_eod_ptr := @entropy^.bitstate.printed_eod;

  EOBRUN := entropy^.saved.EOBRUN; { only part of saved state we care about }

  { There is always only one block per MCU }
  block := JBLOCK_PTR(MCU_data[0]);
  tbl := entropy^.ac_derived_tbl;

  { If we are forced to suspend, we must undo the assignments to any newly
    nonzero coefficients in the block, because otherwise we'd get confused
    next time about which coefficients were already nonzero.
    But we need not undo addition of bits to already-nonzero coefficients;
    instead, we can test the current bit position to see if we already did it.}

  num_newnz := 0;

  { initialize coefficient loop counter to start of band }
  k := cinfo^.Ss;

  if (EOBRUN = 0) then
  begin
    while (k <= Se) do
    begin
      {HUFF_DECODE(s, br_state, tbl, goto undoit, label3);}
      if (bits_left < HUFF_LOOKAHEAD) then
      begin
        if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
          goto undoit;
        get_buffer := br_state.get_buffer;
        bits_left := br_state.bits_left;
        if (bits_left < HUFF_LOOKAHEAD) then
        begin
          nb := 1;
          goto label3;
        end;
      end;
      {look := PEEK_BITS(HUFF_LOOKAHEAD);}
      look := int(get_buffer shr (bits_left -  HUFF_LOOKAHEAD)) and
                     pred(1 shl HUFF_LOOKAHEAD);

      nb := tbl^.look_nbits[look];
      if (nb <> 0) then
      begin
        {DROP_BITS(nb);}
        Dec(bits_left, nb);

        s := tbl^.look_sym[look];
      end
      else
      begin
        nb := HUFF_LOOKAHEAD+1;
    label3:
        s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
        if (s < 0) then
          goto undoit;
        get_buffer := br_state.get_buffer;
        bits_left := br_state.bits_left;
      end;

      r := s shr 4;
      s := s and 15;
      if (s <> 0) then
      begin
	if (s <> 1) then	{ size of new coef should always be 1 }
	  WARNMS(j_common_ptr(cinfo), JWRN_HUFF_BAD_CODE);
        {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
        if (bits_left < 1) then
        begin
          if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
            goto undoit;
          get_buffer := br_state.get_buffer;
          bits_left := br_state.bits_left;
        end;

        {if (GET_BITS(1)) then}
        Dec(bits_left);
        if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
	  s := p1		{ newly nonzero coef is positive }
	else
	  s := m1;		{ newly nonzero coef is negative }
      end
      else
      begin
	if (r <> 15) then
        begin
	  EOBRUN := 1 shl r;	{ EOBr, run length is 2^r + appended bits }
	  if (r <> 0) then
          begin
	    {CHECK_BIT_BUFFER(br_state, r, goto undoit);}
            if (bits_left < r) then
            begin
              if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
                goto undoit;
              get_buffer := br_state.get_buffer;
              bits_left := br_state.bits_left;
            end;

	    {r := GET_BITS(r);}
            Dec(bits_left, r);
            r := (int(get_buffer shr bits_left)) and ( pred(1 shl r) );

	    Inc(EOBRUN, r);
	  end;
	  break;		{ rest of block is handled by EOB logic }
	end;
	{ note s := 0 for processing ZRL }
      end;
      { Advance over already-nonzero coefs and r still-zero coefs,
        appending correction bits to the nonzeroes.  A correction bit is 1
        if the absolute value of the coefficient must be increased. }

      repeat
	thiscoef :=@(block^[jpeg_natural_order[k]]);
	if (thiscoef^ <> 0) then
        begin
	  {CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
          if (bits_left < 1) then
          begin
            if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
              goto undoit;
            get_buffer := br_state.get_buffer;
            bits_left := br_state.bits_left;
          end;

	  {if (GET_BITS(1)) then}
          Dec(bits_left);
          if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
          begin
	    if ((thiscoef^ and p1) = 0) then
            begin { do nothing if already changed it }
	      if (thiscoef^ >= 0) then
		Inc(thiscoef^, p1)
	      else
		Inc(thiscoef^, m1);
	    end;
	  end;
	end
        else
        begin
          Dec(r);
	  if (r < 0) then
	    break;		{ reached target zero coefficient }
	end;
	Inc(k);
      until (k > Se);
      if (s <> 0) then
      begin
	pos := jpeg_natural_order[k];
	{ Output newly nonzero coefficient }
	block^[pos] := JCOEF (s);
	{ Remember its position in case we have to suspend }
	newnz_pos[num_newnz] := pos;
        Inc(num_newnz);
      end;
      Inc(k);
    end;
  end;

  if (EOBRUN > 0) then
  begin
    { Scan any remaining coefficient positions after the end-of-band
      (the last newly nonzero coefficient, if any).  Append a correction
      bit to each already-nonzero coefficient.  A correction bit is 1
      if the absolute value of the coefficient must be increased. }

    while (k <= Se) do
    begin
      thiscoef := @(block^[jpeg_natural_order[k]]);
      if (thiscoef^ <> 0) then
      begin
	{CHECK_BIT_BUFFER(br_state, 1, goto undoit);}
        if (bits_left < 1) then
        begin
          if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,1)) then
            goto undoit;
          get_buffer := br_state.get_buffer;
          bits_left := br_state.bits_left;
        end;

	{if (GET_BITS(1)) then}
        Dec(bits_left);
        if (int(get_buffer shr bits_left)) and ( pred(1 shl 1) )<>0 then
        begin
	  if ((thiscoef^ and p1) = 0) then
          begin { do nothing if already changed it }
	    if (thiscoef^ >= 0) then
	      Inc(thiscoef^, p1)
	    else
	      Inc(thiscoef^, m1);
	  end;
	end;
      end;
      Inc(k);
    end;
    { Count one block completed in EOB run }
    Dec(EOBRUN);
  end;

  { Completed MCU, so update state }
  {BITREAD_SAVE_STATE(cinfo,entropy^.bitstate);}
  cinfo^.src^.next_input_byte := br_state.next_input_byte;
  cinfo^.src^.bytes_in_buffer := br_state.bytes_in_buffer;
  cinfo^.unread_marker := br_state.unread_marker;
  entropy^.bitstate.get_buffer := get_buffer;
  entropy^.bitstate.bits_left := bits_left;

  entropy^.saved.EOBRUN := EOBRUN; { only part of saved state we care about }

  { Account for restart interval (no-op if not using restarts) }
  Dec(entropy^.restarts_to_go);

  decode_mcu_AC_refine := TRUE;
  exit;

undoit:
  { Re-zero any output coefficients that we made newly nonzero }
  while (num_newnz > 0) do
  begin
    Dec(num_newnz);
    block^[newnz_pos[num_newnz]] := 0;
  end;

  decode_mcu_AC_refine := FALSE;
end;


{ Module initialization routine for progressive Huffman entropy decoding. }

{GLOBAL}
procedure jinit_phuff_decoder (cinfo : j_decompress_ptr);
var
  entropy : phuff_entropy_ptr;
  coef_bit_ptr : int_ptr;
  ci, i : int;
begin
  entropy := phuff_entropy_ptr(
    cinfo^.mem^.alloc_small (j_common_ptr (cinfo), JPOOL_IMAGE,
				SIZEOF(phuff_entropy_decoder)) );
  cinfo^.entropy := jpeg_entropy_decoder_ptr (entropy);
  entropy^.pub.start_pass := start_pass_phuff_decoder;

  { Mark derived tables unallocated }
  for i := 0 to pred(NUM_HUFF_TBLS) do
  begin
    entropy^.derived_tbls[i] := NIL;
  end;

  { Create progression status table }
  cinfo^.coef_bits := coef_bits_ptrrow (
     cinfo^.mem^.alloc_small ( j_common_ptr (cinfo), JPOOL_IMAGE,
				cinfo^.num_components*DCTSIZE2*SIZEOF(int)) );
  coef_bit_ptr := @cinfo^.coef_bits^[0][0];
  for ci := 0 to pred(cinfo^.num_components) do
    for i := 0 to pred(DCTSIZE2) do
    begin
      coef_bit_ptr^ := -1;
      Inc(coef_bit_ptr);
    end;
end;

end.

⌨️ 快捷键说明

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