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

📄 jdcoefct.pas

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

{ This file contains the coefficient buffer controller for decompression.
  This controller is the top level of the JPEG decompressor proper.
  The coefficient buffer lies between entropy decoding and inverse-DCT steps.

  In buffered-image mode, this controller is the interface between
  input-oriented processing and output-oriented processing.
  Also, the input side (only) is used when reading a file for transcoding. }

{ Original: jdcoefct.c ; Copyright (C) 1994-1996, Thomas G. Lane. }

interface

uses
  jmorecfg,
  jinclude,
  jdeferr,
  jerror,
  jutils,
  jpeglib;

{$I jconfig.inc}

{GLOBAL}
procedure jinit_d_coef_controller (cinfo : j_decompress_ptr;
                                   need_full_buffer : boolean);


implementation


{ Block smoothing is only applicable for progressive JPEG, so: }
{$ifndef D_PROGRESSIVE_SUPPORTED}
{$undef BLOCK_SMOOTHING_SUPPORTED}
{$endif}

{ Private buffer controller object }

{$ifdef BLOCK_SMOOTHING_SUPPORTED}
const
  SAVED_COEFS = 6;              { we save coef_bits[0..5] }
type
  Latch = array[0..SAVED_COEFS-1] of int;
  Latch_ptr = ^Latch;
{$endif}

type
  my_coef_ptr = ^my_coef_controller;
  my_coef_controller = record
    pub : jpeg_d_coef_controller; { public fields }

    { These variables keep track of the current location of the input side. }
    { cinfo^.input_iMCU_row is also used for this. }
    MCU_ctr : JDIMENSION;               { counts MCUs processed in current row }
    MCU_vert_offset : int;              { counts MCU rows within iMCU row }
    MCU_rows_per_iMCU_row : int;        { number of such rows needed }

    { The output side's location is represented by cinfo^.output_iMCU_row. }

    { In single-pass modes, it's sufficient to buffer just one MCU.
      We allocate a workspace of D_MAX_BLOCKS_IN_MCU coefficient blocks,
      and let the entropy decoder write into that workspace each time.
      (On 80x86, the workspace is FAR even though it's not really very big;
      this is to keep the module interfaces unchanged when a large coefficient
      buffer is necessary.)
      In multi-pass modes, this array points to the current MCU's blocks
      within the virtual arrays; it is used only by the input side. }

    MCU_buffer : array[0..D_MAX_BLOCKS_IN_MCU-1] of JBLOCKROW;

  {$ifdef D_MULTISCAN_FILES_SUPPORTED}
    { In multi-pass modes, we need a virtual block array for each component. }
    whole_image : jvirt_barray_tbl;
  {$endif}

  {$ifdef BLOCK_SMOOTHING_SUPPORTED}
    { When doing block smoothing, we latch coefficient Al values here }
    coef_bits_latch : Latch_Ptr;
  {$endif}
  end;

{ Forward declarations }
{METHODDEF}
function decompress_onepass (cinfo : j_decompress_ptr;
                             output_buf : JSAMPIMAGE) : int; far; forward;
{$ifdef D_MULTISCAN_FILES_SUPPORTED}
{METHODDEF}
function decompress_data (cinfo : j_decompress_ptr;
                          output_buf : JSAMPIMAGE) : int; far; forward;
{$endif}
{$ifdef BLOCK_SMOOTHING_SUPPORTED}
{LOCAL}
function smoothing_ok (cinfo : j_decompress_ptr) : boolean; forward;

{METHODDEF}
function decompress_smooth_data	(cinfo : j_decompress_ptr;
                                 output_buf : JSAMPIMAGE) : int; far; forward;
{$endif}


{LOCAL}
procedure start_iMCU_row (cinfo : j_decompress_ptr);
{ Reset within-iMCU-row counters for a new row (input side) }
var
  coef : my_coef_ptr;
begin
  coef := my_coef_ptr (cinfo^.coef);

  { In an interleaved scan, an MCU row is the same as an iMCU row.
    In a noninterleaved scan, an iMCU row has v_samp_factor MCU rows.
    But at the bottom of the image, process only what's left. }

  if (cinfo^.comps_in_scan > 1) then
  begin
    coef^.MCU_rows_per_iMCU_row := 1;
  end
  else
  begin
    if (cinfo^.input_iMCU_row < (cinfo^.total_iMCU_rows-1)) then
      coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.v_samp_factor
    else
      coef^.MCU_rows_per_iMCU_row := cinfo^.cur_comp_info[0]^.last_row_height;
  end;

  coef^.MCU_ctr := 0;
  coef^.MCU_vert_offset := 0;
end;


{ Initialize for an input processing pass. }

{METHODDEF}
procedure start_input_pass (cinfo : j_decompress_ptr); far;
begin
  cinfo^.input_iMCU_row := 0;
  start_iMCU_row(cinfo);
end;


{ Initialize for an output processing pass. }

{METHODDEF}
procedure start_output_pass (cinfo : j_decompress_ptr); far;
var
  coef : my_coef_ptr;
begin
{$ifdef BLOCK_SMOOTHING_SUPPORTED}
  coef := my_coef_ptr (cinfo^.coef);

  { If multipass, check to see whether to use block smoothing on this pass }
  if (coef^.pub.coef_arrays <> NIL) then
  begin
    if (cinfo^.do_block_smoothing) and smoothing_ok(cinfo) then
      coef^.pub.decompress_data := decompress_smooth_data
    else
      coef^.pub.decompress_data := decompress_data;
  end;
{$endif}
  cinfo^.output_iMCU_row := 0;
end;


{ Decompress and return some data in the single-pass case.
  Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  Input and output must run in lockstep since we have only a one-MCU buffer.
  Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.

  NB: output_buf contains a plane for each component in image.
  For single pass, this is the same as the components in the scan. }


{METHODDEF}
function decompress_onepass (cinfo : j_decompress_ptr;
                             output_buf : JSAMPIMAGE) : int;
var
  coef : my_coef_ptr;
  MCU_col_num : JDIMENSION;     { index of current MCU within row }
  last_MCU_col : JDIMENSION;
  last_iMCU_row : JDIMENSION;
  blkn, ci, xindex, yindex, yoffset, useful_width : int;
  output_ptr : JSAMPARRAY;
  start_col, output_col : JDIMENSION;
  compptr : jpeg_component_info_ptr;
  inverse_DCT : inverse_DCT_method_ptr;
begin
  coef := my_coef_ptr (cinfo^.coef);
  last_MCU_col := cinfo^.MCUs_per_row - 1;
  last_iMCU_row := cinfo^.total_iMCU_rows - 1;

  { Loop to process as much as one whole iMCU row }
  for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  begin
    for MCU_col_num := coef^.MCU_ctr to last_MCU_col do
    begin
      { Try to fetch an MCU.  Entropy decoder expects buffer to be zeroed. }
      jzero_far( coef^.MCU_buffer[0],
		size_t (cinfo^.blocks_in_MCU * SIZEOF(JBLOCK)));
      if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
      begin
	{ Suspension forced; update state counters and exit }
	coef^.MCU_vert_offset := yoffset;
	coef^.MCU_ctr := MCU_col_num;
	decompress_onepass := JPEG_SUSPENDED;
        exit;
      end;
      { Determine where data should go in output_buf and do the IDCT thing.
        We skip dummy blocks at the right and bottom edges (but blkn gets
        incremented past them!).  Note the inner loop relies on having
        allocated the MCU_buffer[] blocks sequentially. }

      blkn := 0;			{ index of current DCT block within MCU }
      for ci := 0 to pred(cinfo^.comps_in_scan) do
      begin
	compptr := cinfo^.cur_comp_info[ci];
	{ Don't bother to IDCT an uninteresting component. }
	if (not compptr^.component_needed) then
        begin
	  Inc(blkn, compptr^.MCU_blocks);
	  continue;
	end;
	inverse_DCT := cinfo^.idct^.inverse_DCT[compptr^.component_index];
        if (MCU_col_num < last_MCU_col) then
          useful_width := compptr^.MCU_width
        else
          useful_width := compptr^.last_col_width;

	output_ptr := JSAMPARRAY(@ output_buf^[ci]^
                                   [yoffset * compptr^.DCT_scaled_size]);
	start_col := MCU_col_num * compptr^.MCU_sample_width;
	for yindex := 0 to pred(compptr^.MCU_height) do
        begin
	  if (cinfo^.input_iMCU_row < last_iMCU_row) or
             (yoffset+yindex < compptr^.last_row_height) then
          begin
	    output_col := start_col;
	    for xindex := 0 to pred(useful_width) do
            begin
	      inverse_DCT (cinfo, compptr,
			   JCOEFPTR(coef^.MCU_buffer[blkn+xindex]),
			   output_ptr, output_col);
	      Inc(output_col, compptr^.DCT_scaled_size);
	    end;
	  end;
	  Inc(blkn, compptr^.MCU_width);
	  Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
	end;
      end;
    end;
    { Completed an MCU row, but perhaps not an iMCU row }
    coef^.MCU_ctr := 0;
  end;
  { Completed the iMCU row, advance counters for next one }
  Inc(cinfo^.output_iMCU_row);

  Inc(cinfo^.input_iMCU_row);
  if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
  begin
    start_iMCU_row(cinfo);
    decompress_onepass := JPEG_ROW_COMPLETED;
    exit;
  end;
  { Completed the scan }
  cinfo^.inputctl^.finish_input_pass (cinfo);
  decompress_onepass := JPEG_SCAN_COMPLETED;
end;

{ Dummy consume-input routine for single-pass operation. }

{METHODDEF}
function dummy_consume_data (cinfo : j_decompress_ptr) : int; far;
begin
  dummy_consume_data := JPEG_SUSPENDED;	{ Always indicate nothing was done }
end;


{$ifdef D_MULTISCAN_FILES_SUPPORTED}

{ Consume input data and store it in the full-image coefficient buffer.
  We read as much as one fully interleaved MCU row ("iMCU" row) per call,
  ie, v_samp_factor block rows for each component in the scan.
  Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.}

{METHODDEF}
function consume_data (cinfo : j_decompress_ptr) : int; far;
var
  coef : my_coef_ptr;
  MCU_col_num : JDIMENSION;     { index of current MCU within row }
  blkn, ci, xindex, yindex, yoffset : int;
  start_col : JDIMENSION;
  buffer : array[0..MAX_COMPS_IN_SCAN-1] of JBLOCKARRAY;
  buffer_ptr : JBLOCKROW;
  compptr : jpeg_component_info_ptr;
begin
  coef := my_coef_ptr (cinfo^.coef);

  { Align the virtual buffers for the components used in this scan. }
  for ci := 0 to pred(cinfo^.comps_in_scan) do
  begin
    compptr := cinfo^.cur_comp_info[ci];
    buffer[ci] := cinfo^.mem^.access_virt_barray
      (j_common_ptr (cinfo), coef^.whole_image[compptr^.component_index],
       cinfo^.input_iMCU_row * compptr^.v_samp_factor,
       JDIMENSION (compptr^.v_samp_factor), TRUE);
    { Note: entropy decoder expects buffer to be zeroed,
      but this is handled automatically by the memory manager
      because we requested a pre-zeroed array. }

  end;

  { Loop to process one whole iMCU row }
  for yoffset := coef^.MCU_vert_offset to pred(coef^.MCU_rows_per_iMCU_row) do
  begin
    for MCU_col_num := coef^.MCU_ctr to pred(cinfo^.MCUs_per_row) do
    begin
      { Construct list of pointers to DCT blocks belonging to this MCU }
      blkn := 0;		{ index of current DCT block within MCU }
      for ci := 0 to pred(cinfo^.comps_in_scan) do
      begin
	compptr := cinfo^.cur_comp_info[ci];
	start_col := MCU_col_num * compptr^.MCU_width;
	for yindex := 0 to pred(compptr^.MCU_height) do
        begin
	  buffer_ptr := JBLOCKROW(@ buffer[ci]^[yindex+yoffset]^[start_col]);
	  for xindex := 0 to pred(compptr^.MCU_width) do
          begin
	    coef^.MCU_buffer[blkn] := buffer_ptr;
            Inc(blkn);
            Inc(JBLOCK_PTR(buffer_ptr));
	  end;
	end;
      end;
      { Try to fetch the MCU. }
      if (not cinfo^.entropy^.decode_mcu (cinfo, coef^.MCU_buffer)) then
      begin
	{ Suspension forced; update state counters and exit }
	coef^.MCU_vert_offset := yoffset;
	coef^.MCU_ctr := MCU_col_num;
	consume_data := JPEG_SUSPENDED;
        exit;
      end;
    end;
    { Completed an MCU row, but perhaps not an iMCU row }
    coef^.MCU_ctr := 0;
  end;
  { Completed the iMCU row, advance counters for next one }
  Inc(cinfo^.input_iMCU_row);
  if (cinfo^.input_iMCU_row < cinfo^.total_iMCU_rows) then
  begin
    start_iMCU_row(cinfo);
    consume_data := JPEG_ROW_COMPLETED;
    exit;
  end;
  { Completed the scan }
  cinfo^.inputctl^.finish_input_pass (cinfo);
  consume_data := JPEG_SCAN_COMPLETED;
end;


{ Decompress and return some data in the multi-pass case.
  Always attempts to emit one fully interleaved MCU row ("iMCU" row).
  Return value is JPEG_ROW_COMPLETED, JPEG_SCAN_COMPLETED, or JPEG_SUSPENDED.

  NB: output_buf contains a plane for each component in image. }

{METHODDEF}
function decompress_data (cinfo : j_decompress_ptr;
                          output_buf : JSAMPIMAGE) : int;
var
  coef : my_coef_ptr;
  last_iMCU_row : JDIMENSION;
  block_num : JDIMENSION;
  ci, block_row, block_rows : int;
  buffer : JBLOCKARRAY;
  buffer_ptr : JBLOCKROW;
  output_ptr : JSAMPARRAY;
  output_col : JDIMENSION;
  compptr : jpeg_component_info_ptr;
  inverse_DCT : inverse_DCT_method_ptr;
begin
  coef := my_coef_ptr (cinfo^.coef);
  last_iMCU_row := cinfo^.total_iMCU_rows - 1;

  { Force some input to be done if we are getting ahead of the input. }
  while (cinfo^.input_scan_number < cinfo^.output_scan_number) or
	 ((cinfo^.input_scan_number = cinfo^.output_scan_number) and
	  (cinfo^.input_iMCU_row <= cinfo^.output_iMCU_row)) do
  begin
    if (cinfo^.inputctl^.consume_input(cinfo) = JPEG_SUSPENDED) then
    begin
      decompress_data := JPEG_SUSPENDED;
      exit;
    end;
  end;

  { OK, output from the virtual arrays. }
  compptr := cinfo^.comp_info;
  for ci := 0 to pred(cinfo^.num_components) do
  begin
    { Don't bother to IDCT an uninteresting component. }
    if (not compptr^.component_needed) then
      continue;
    { Align the virtual buffer for this component. }
    buffer := cinfo^.mem^.access_virt_barray
      (j_common_ptr (cinfo), coef^.whole_image[ci],
       cinfo^.output_iMCU_row * compptr^.v_samp_factor,
       JDIMENSION (compptr^.v_samp_factor), FALSE);
    { Count non-dummy DCT block rows in this iMCU row. }
    if (cinfo^.output_iMCU_row < last_iMCU_row) then
      block_rows := compptr^.v_samp_factor
    else
    begin
      { NB: can't use last_row_height here; it is input-side-dependent! }
      block_rows := int(compptr^.height_in_blocks mod compptr^.v_samp_factor);
      if (block_rows = 0) then
        block_rows := compptr^.v_samp_factor;
    end;
    inverse_DCT := cinfo^.idct^.inverse_DCT[ci];
    output_ptr := output_buf^[ci];
    { Loop over all DCT blocks to be processed. }
    for block_row := 0 to pred(block_rows) do
    begin
      buffer_ptr := buffer^[block_row];
      output_col := 0;
      for block_num := 0 to pred(compptr^.width_in_blocks) do
      begin
	inverse_DCT (cinfo, compptr, JCOEFPTR (buffer_ptr),
			output_ptr, output_col);
	Inc(JBLOCK_PTR(buffer_ptr));
	Inc(output_col, compptr^.DCT_scaled_size);
      end;
      Inc(JSAMPROW_PTR(output_ptr), compptr^.DCT_scaled_size);
    end;
    Inc(compptr);
  end;

  Inc(cinfo^.output_iMCU_row);
  if (cinfo^.output_iMCU_row < cinfo^.total_iMCU_rows) then
  begin
    decompress_data := JPEG_ROW_COMPLETED;
    exit;
  end;
  decompress_data := JPEG_SCAN_COMPLETED;
end;

{$endif} { D_MULTISCAN_FILES_SUPPORTED }


⌨️ 快捷键说明

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