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

📄 jcsample.pas

📁 DELPHI版的JPEG文件解码源程序
💻 PAS
📖 第 1 页 / 共 2 页
字号:
Unit JcSample;

{ This file contains downsampling routines.

  Downsampling input data is counted in "row groups".  A row group
  is defined to be max_v_samp_factor pixel rows of each component,
  from which the downsampler produces v_samp_factor sample rows.
  A single row group is processed in each call to the downsampler module.

  The downsampler is responsible for edge-expansion of its output data
  to fill an integral number of DCT blocks horizontally.  The source buffer
  may be modified if it is helpful for this purpose (the source buffer is
  allocated wide enough to correspond to the desired output width).
  The caller (the prep controller) is responsible for vertical padding.

  The downsampler may request "context rows" by setting need_context_rows
  during startup.  In this case, the input arrays will contain at least
  one row group's worth of pixels above and below the passed-in data;
  the caller will create dummy rows at image top and bottom by replicating
  the first or last real pixel row.

  An excellent reference for image resampling is
    Digital Image Warping, George Wolberg, 1990.
    Pub. by IEEE Computer Society Press, Los Alamitos, CA. ISBN 0-8186-8944-7.

  The downsampling algorithm used here is a simple average of the source
  pixels covered by the output pixel.  The hi-falutin sampling literature
  refers to this as a "box filter".  In general the characteristics of a box
  filter are not very good, but for the specific cases we normally use (1:1
  and 2:1 ratios) the box is equivalent to a "triangle filter" which is not
  nearly so bad.  If you intend to use other sampling ratios, you'd be well
  advised to improve this code.

  A simple input-smoothing capability is provided.  This is mainly intended
  for cleaning up color-dithered GIF input files (if you find it inadequate,
  we suggest using an external filtering program such as pnmconvol).  When
  enabled, each input pixel P is replaced by a weighted sum of itself and its
  eight neighbors.  P's weight is 1-8*SF and each neighbor's weight is SF,
  where SF := (smoothing_factor / 1024).
  Currently, smoothing is only supported for 2h2v sampling factors. }

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


interface

{$I jconfig.inc}

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


{ Module initialization routine for downsampling.
  Note that we must select a routine for each component. }

{GLOBAL}
procedure jinit_downsampler (cinfo : j_compress_ptr);

implementation

{ Pointer to routine to downsample a single component }
type
  downsample1_ptr = procedure(cinfo : j_compress_ptr;
                              compptr : jpeg_component_info_ptr;
		              input_data : JSAMPARRAY;
                              output_data : JSAMPARRAY);

{ Private subobject }

type
  my_downsample_ptr = ^my_downsampler;
  my_downsampler = record
    pub : jpeg_downsampler;	{ public fields }

    { Downsampling method pointers, one per component }
    methods : array[0..MAX_COMPONENTS-1] of downsample1_ptr;
  end;

{ Initialize for a downsampling pass. }

{METHODDEF}
procedure start_pass_downsample (cinfo : j_compress_ptr); far;
begin
  { no work for now }
end;


{ Expand a component horizontally from width input_cols to width output_cols,
  by duplicating the rightmost samples. }

{LOCAL}
procedure expand_right_edge (image_data : JSAMPARRAY;
                             num_rows : int;
		             input_cols : JDIMENSION;
                             output_cols : JDIMENSION);
var
  {register} ptr : JSAMPLE_PTR;
  {register} pixval : JSAMPLE;
  {register} count : int;
  row : int;
  numcols : int;
begin
  numcols := int (output_cols - input_cols);

  if (numcols > 0) then
  begin
    for row := 0 to pred(num_rows) do
    begin
      ptr := JSAMPLE_PTR(@(image_data^[row]^[input_cols-1]));
      pixval := ptr^;		{ don't need GETJSAMPLE() here }
      for count := pred(numcols) downto 0 do
      begin
        Inc(ptr);
	ptr^ := pixval;
      end;
    end;
  end;
end;


{ Do downsampling for a whole row group (all components).

  In this version we simply downsample each component independently. }

{METHODDEF}
procedure sep_downsample (cinfo : j_compress_ptr;
		          input_buf : JSAMPIMAGE;
                          in_row_index : JDIMENSION;
		          output_buf : JSAMPIMAGE;
                          out_row_group_index : JDIMENSION); far;
var
  downsample : my_downsample_ptr;
  ci : int;
  compptr : jpeg_component_info_ptr;
  in_ptr, out_ptr : JSAMPARRAY;
begin
  downsample := my_downsample_ptr (cinfo^.downsample);

  compptr := jpeg_component_info_ptr(cinfo^.comp_info);
  for ci := 0 to pred(cinfo^.num_components) do
  begin
    in_ptr := JSAMPARRAY(@ input_buf^[ci]^[in_row_index]);
    out_ptr := JSAMPARRAY(@ output_buf^[ci]^
                     [out_row_group_index * compptr^.v_samp_factor]);
    downsample^.methods[ci] (cinfo, compptr, in_ptr, out_ptr);
    Inc(compptr);
  end;
end;


{ Downsample pixel values of a single component.
  One row group is processed per call.
  This version handles arbitrary integral sampling ratios, without smoothing.
  Note that this version is not actually used for customary sampling ratios. }

{METHODDEF}
procedure int_downsample (cinfo : j_compress_ptr;
                          compptr : jpeg_component_info_ptr;
		          input_data : JSAMPARRAY;
                          output_data : JSAMPARRAY); far;
var
  inrow, outrow, h_expand, v_expand, numpix, numpix2, h, v : int;
  outcol, outcol_h :  JDIMENSION;	{ outcol_h = outcol*h_expand }
  output_cols : JDIMENSION;
  inptr,
  outptr : JSAMPLE_PTR;
  outvalue : INT32;
begin
  output_cols := compptr^.width_in_blocks * DCTSIZE;

  h_expand := cinfo^.max_h_samp_factor div compptr^.h_samp_factor;
  v_expand := cinfo^.max_v_samp_factor div compptr^.v_samp_factor;
  numpix := h_expand * v_expand;
  numpix2 := numpix div 2;

  { Expand input data enough to let all the output samples be generated
    by the standard loop.  Special-casing padded output would be more
    efficient. }

  expand_right_edge(input_data, cinfo^.max_v_samp_factor,
		    cinfo^.image_width, output_cols * h_expand);

  inrow := 0;
  for outrow := 0 to pred(compptr^.v_samp_factor) do
  begin
    outptr := JSAMPLE_PTR(output_data^[outrow]);
    outcol_h := 0;
    for outcol := 0 to pred(output_cols) do
    begin
      outvalue := 0;
      for v := 0 to pred(v_expand) do
      begin
	inptr := @(input_data^[inrow+v]^[outcol_h]);
	for h := 0 to pred(h_expand) do
        begin
	  Inc(outvalue, INT32 (GETJSAMPLE(inptr^)) );
          Inc(inptr);
	end;
      end;
      outptr^ := JSAMPLE ((outvalue + numpix2) div numpix);
      Inc(outptr);
      Inc(outcol_h, h_expand);
    end;
    Inc(inrow, v_expand);
  end;
end;


{ Downsample pixel values of a single component.
  This version handles the special case of a full-size component,
  without smoothing. }

{METHODDEF}
procedure fullsize_downsample (cinfo : j_compress_ptr;
                               compptr : jpeg_component_info_ptr;
		               input_data : JSAMPARRAY;
                               output_data : JSAMPARRAY); far;
begin
  { Copy the data }
  jcopy_sample_rows(input_data, 0, output_data, 0,
		    cinfo^.max_v_samp_factor, cinfo^.image_width);
  { Edge-expand }
  expand_right_edge(output_data, cinfo^.max_v_samp_factor,
		    cinfo^.image_width, compptr^.width_in_blocks * DCTSIZE);
end;


{ Downsample pixel values of a single component.
  This version handles the common case of 2:1 horizontal and 1:1 vertical,
  without smoothing.

  A note about the "bias" calculations: when rounding fractional values to
  integer, we do not want to always round 0.5 up to the next integer.
  If we did that, we'd introduce a noticeable bias towards larger values.
  Instead, this code is arranged so that 0.5 will be rounded up or down at
  alternate pixel locations (a simple ordered dither pattern). }

{METHODDEF}
procedure h2v1_downsample (cinfo : j_compress_ptr;
                           compptr : jpeg_component_info_ptr;
		           input_data : JSAMPARRAY;
                           output_data : JSAMPARRAY); far;
var
  outrow : int;
  outcol : JDIMENSION;
  output_cols : JDIMENSION;
  {register} inptr, outptr : JSAMPLE_PTR;
  {register} bias : int;
begin
  output_cols := compptr^.width_in_blocks * DCTSIZE;

  { Expand input data enough to let all the output samples be generated
    by the standard loop.  Special-casing padded output would be more
    efficient. }

  expand_right_edge(input_data, cinfo^.max_v_samp_factor,
		    cinfo^.image_width, output_cols * 2);

  for outrow := 0 to pred(compptr^.v_samp_factor) do
  begin
    outptr := JSAMPLE_PTR(output_data^[outrow]);
    inptr := JSAMPLE_PTR(input_data^[outrow]);
    bias := 0;		     { bias := 0,1,0,1,... for successive samples }
    for outcol := 0 to pred(output_cols) do
    begin
      outptr^ := JSAMPLE ((GETJSAMPLE(inptr^) +
                           GETJSAMPLE(JSAMPROW(inptr)^[1]) + bias) shr 1);
      Inc(outptr);
      bias := bias xor 1;    { 0=>1, 1=>0 }
      Inc(inptr, 2);
    end;
  end;
end;


{ Downsample pixel values of a single component.
  This version handles the standard case of 2:1 horizontal and 2:1 vertical,
  without smoothing. }

{METHODDEF}
procedure h2v2_downsample (cinfo : j_compress_ptr;
                           compptr : jpeg_component_info_ptr;
                           input_data : JSAMPARRAY;
                           output_data : JSAMPARRAY); far;
var
  inrow, outrow : int;
  outcol : JDIMENSION;
  output_cols : JDIMENSION;
  {register} inptr0, inptr1, outptr : JSAMPLE_PTR;
  {register} bias : int;
begin
  output_cols := compptr^.width_in_blocks * DCTSIZE;

  { Expand input data enough to let all the output samples be generated
    by the standard loop.  Special-casing padded output would be more
    efficient. }

  expand_right_edge(input_data, cinfo^.max_v_samp_factor,
		    cinfo^.image_width, output_cols * 2);

  inrow := 0;
  for outrow := 0 to pred(compptr^.v_samp_factor) do
  begin
    outptr := JSAMPLE_PTR(output_data^[outrow]);
    inptr0 := JSAMPLE_PTR(input_data^[inrow]);
    inptr1 := JSAMPLE_PTR(input_data^[inrow+1]);
    bias := 1;			{ bias := 1,2,1,2,... for successive samples }
    for outcol := 0 to pred(output_cols) do
    begin
      outptr^ := JSAMPLE ((GETJSAMPLE(inptr0^) +
                           GETJSAMPLE(JSAMPROW(inptr0)^[1]) +

⌨️ 快捷键说明

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