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

📄 jdphuff.pas

📁 用pascal寫的jpeg codec, 測試過的
💻 PAS
📖 第 1 页 / 共 3 页
字号:

  {ASSIGN_STATE(state, entropy^.saved);}
  state := entropy^.saved;

  { Outer loop handles each block in the MCU }

  for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  begin
    block := JBLOCK_PTR(MCU_data[blkn]);
    ci := cinfo^.MCU_membership[blkn];
    compptr := cinfo^.cur_comp_info[ci];
    tbl := entropy^.derived_tbls[compptr^.dc_tbl_no];

    { Decode a single block's worth of coefficients }

    { Section F.2.2.1: decode the DC coefficient difference }
    {HUFF_DECODE(s, br_state, tbl, return FALSE, label1);}
    if (bits_left < HUFF_LOOKAHEAD) then
    begin
      if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
      begin
        decode_mcu_DC_first := FALSE;
        exit;
      end;
      get_buffer := br_state.get_buffer;
      bits_left := br_state.bits_left;
      if (bits_left < HUFF_LOOKAHEAD) then
      begin
        nb := 1;
        goto label1;
      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;
  label1:
      s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
      if (s < 0) then
      begin
        decode_mcu_DC_first := FALSE;
        exit;
      end;
      get_buffer := br_state.get_buffer;
      bits_left := br_state.bits_left;
    end;

    if (s <> 0) then
    begin
      {CHECK_BIT_BUFFER(br_state, s, decode_mcu_DC_first := FALSE);}
      if (bits_left < s) then
      begin
        if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
        begin
          decode_mcu_DC_first := FALSE;
          exit;
        end;
        get_buffer := br_state.get_buffer;
        bits_left := br_state.bits_left;
      end;

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

      {s := HUFF_EXTEND(r, s);}
      if (r < extend_test[s]) then
        s := r + extend_offset[s]
      else
        s := r;
    end;

    { Convert DC difference to actual value, update last_dc_val }
    Inc(s, state.last_dc_val[ci]);
    state.last_dc_val[ci] := s;
    { Scale and output the DC coefficient (assumes jpeg_natural_order[0]=0) }
    block^[0] := JCOEF (s shl Al);
  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;

  {ASSIGN_STATE(entropy^.saved, state);}
  entropy^.saved := state;

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

  decode_mcu_DC_first := TRUE;
end;


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

{METHODDEF}
function decode_mcu_AC_first (cinfo : j_decompress_ptr;
                              var MCU_data : array of JBLOCKROW) : boolean;
label
  label2;
var
  entropy : phuff_entropy_ptr;
  Se : int;
  Al : int;
  {register} s, k, r : int;
  EOBRUN : uInt;
  block : JBLOCK_PTR;
  {BITREAD_STATE_VARS;}
  get_buffer : bit_buf_type ; {register}
  bits_left : int; {register}
  br_state : bitread_working_state;

  tbl : d_derived_tbl_ptr;
var
  nb, look : int; {register}
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);
  Se := cinfo^.Se;
  Al := cinfo^.Al;

  { 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_first := FALSE;
        exit;
      end;
  end;

  { Load up working state.
    We can avoid loading/saving bitread state if in an EOB run. }

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

  { There is always only one block per MCU }

  if (EOBRUN > 0) then       { if it's a band of zeroes... }
    Dec(EOBRUN)              { ...process it now (we do nothing) }
  else
  begin
    {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;

    block := JBLOCK_PTR(MCU_data[0]);
    tbl := entropy^.ac_derived_tbl;

    k := cinfo^.Ss;
    while (k <= Se) do
    begin
      {HUFF_DECODE(s, br_state, tbl, return:= FALSE, label2);}
      if (bits_left < HUFF_LOOKAHEAD) then
      begin
        if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left, 0)) then
        begin
          decode_mcu_AC_first := FALSE;
          exit;
        end;
        get_buffer := br_state.get_buffer;
        bits_left := br_state.bits_left;
        if (bits_left < HUFF_LOOKAHEAD) then
        begin
          nb := 1;
          goto label2;
        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;
    label2:
        s := jpeg_huff_decode(br_state,get_buffer,bits_left,tbl,nb);
        if (s < 0) then
        begin
          decode_mcu_AC_first := FALSE;
          exit;
        end;
        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
        Inc(k, r);
        {CHECK_BIT_BUFFER(br_state, s, return FALSE);}
        if (bits_left < s) then
        begin
          if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,s)) then
          begin
            decode_mcu_AC_first := FALSE;
            exit;
          end;
          get_buffer := br_state.get_buffer;
          bits_left := br_state.bits_left;
        end;

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

        {s := HUFF_EXTEND(r, s);}
        if (r < extend_test[s]) then
          s := r + extend_offset[s]
        else
          s := r;

	{ Scale and output coefficient in natural (dezigzagged) order }
        block^[jpeg_natural_order[k]] := JCOEF (s shl Al);
      end
      else
      begin
        if (r = 15) then
        begin		{ ZRL }
          Inc(k, 15);	{ skip 15 zeroes in band }
        end
        else
        begin		{ EOBr, run length is 2^r + appended bits }
          EOBRUN := 1 shl r;
          if (r <> 0) then
          begin		{ EOBr, r > 0 }
	    {CHECK_BIT_BUFFER(br_state, r, return FALSE);}
            if (bits_left < r) then
            begin
              if (not jpeg_fill_bit_buffer(br_state,get_buffer,bits_left,r)) then
              begin
                decode_mcu_AC_first := FALSE;
                exit;
              end;
              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;
	  Dec(EOBRUN);          { this band is processed at this moment }
	  break;                { force end-of-band }
	end;
      end;
      Inc(k);
    end;

    {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;
  end;

  { Completed MCU, so update state }
  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_first := TRUE;
end;


{ MCU decoding 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 decode_mcu_DC_refine (cinfo : j_decompress_ptr;
                               var MCU_data : array of JBLOCKROW) : boolean;

var
  entropy : phuff_entropy_ptr;
  p1 : int;          { 1 in the bit position being coded }
  blkn : int;
  block : JBLOCK_PTR;
  {BITREAD_STATE_VARS;}
  get_buffer : bit_buf_type ; {register}
  bits_left : int; {register}
  br_state : bitread_working_state;
begin
  entropy := phuff_entropy_ptr (cinfo^.entropy);
  p1 := 1 shl cinfo^.Al;

  { 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_DC_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;

  { Outer loop handles each block in the MCU }

  for blkn := 0 to pred(cinfo^.blocks_in_MCU) do
  begin
    block := JBLOCK_PTR(MCU_data[blkn]);

    { Encoded data is simply the next bit of the two's-complement DC value }
    {CHECK_BIT_BUFFER(br_state, 1, return FALSE);}

⌨️ 快捷键说明

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