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

📄 jdsample.pas

📁 用pascal寫的jpeg codec, 測試過的
💻 PAS
📖 第 1 页 / 共 2 页
字号:
  It's still a box filter. }

{METHODDEF}
procedure h2v2_upsample (cinfo : j_decompress_ptr;
                         compptr : jpeg_component_info_ptr;
	                 input_data : JSAMPARRAY;
                         var output_data_ptr : JSAMPARRAY); far;
var
  output_data : JSAMPARRAY;
  {register} inptr, outptr : JSAMPLE_PTR;
  {register} invalue : JSAMPLE;
  {outend : JSAMPROW;}
  outcount : int;
  inrow, outrow : int;
begin
  output_data := output_data_ptr;

  inrow := 0;
  outrow := 0;
  while (outrow < cinfo^.max_v_samp_factor) do
  begin
    inptr := JSAMPLE_PTR(input_data^[inrow]);
    outptr := JSAMPLE_PTR(output_data^[outrow]);
    {outend := outptr + cinfo^.output_width;}
    outcount := cinfo^.output_width;
    while (outcount > 0) do
    begin
      invalue := inptr^;	{ don't need GETJSAMPLE() here }
      Inc(inptr);
      outptr^ := invalue;
      Inc(outptr);
      outptr^ := invalue;
      Inc(outptr);
      Dec(outcount, 2);
    end;
    jcopy_sample_rows(output_data, outrow, output_data, outrow+1,
		      1, cinfo^.output_width);
    Inc(inrow);
    Inc(outrow, 2);
  end;
end;


{ Fancy processing for the common case of 2:1 horizontal and 1:1 vertical.

  The upsampling algorithm is linear interpolation between pixel centers,
  also known as a "triangle filter".  This is a good compromise between
  speed and visual quality.  The centers of the output pixels are 1/4 and 3/4
  of the way between input pixel centers.

  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_fancy_upsample (cinfo : j_decompress_ptr;
                               compptr : jpeg_component_info_ptr;
                               input_data : JSAMPARRAY;
                               var output_data_ptr : JSAMPARRAY); far;
var
  output_data : JSAMPARRAY;
  {register} pre_inptr, inptr, outptr : JSAMPLE_PTR;
  {register} invalue : int;
  {register} colctr : JDIMENSION;
  inrow : int;
begin
  output_data := output_data_ptr;

  for inrow := 0 to pred(cinfo^.max_v_samp_factor) do
  begin
    inptr := JSAMPLE_PTR(input_data^[inrow]);
    outptr := JSAMPLE_PTR(output_data^[inrow]);
    { Special case for first column }
    pre_inptr := inptr;
    invalue := GETJSAMPLE(inptr^);
    Inc(inptr);
    outptr^ := JSAMPLE (invalue);
    Inc(outptr);
    outptr^ := JSAMPLE ((invalue * 3 + GETJSAMPLE(inptr^) + 2) shr 2);
    Inc(outptr);

    for colctr := pred(compptr^.downsampled_width - 2) downto 0 do
    begin
      { General case: 3/4 * nearer pixel + 1/4 * further pixel }
      invalue := GETJSAMPLE(inptr^) * 3;
      Inc(inptr);
      outptr^ := JSAMPLE ((invalue + GETJSAMPLE(pre_inptr^) + 1) shr 2);
      Inc(pre_inptr);
      Inc(outptr);
      outptr^ := JSAMPLE ((invalue + GETJSAMPLE(inptr^) + 2) shr 2);
      Inc(outptr);
    end;

    { Special case for last column }
    invalue := GETJSAMPLE(inptr^);
    outptr^ := JSAMPLE ((invalue * 3 + GETJSAMPLE(pre_inptr^) + 1) shr 2);
    Inc(outptr);
    outptr^ := JSAMPLE (invalue);
    {Inc(outptr);                        - value never used }
  end;
end;


{ Fancy processing for the common case of 2:1 horizontal and 2:1 vertical.
  Again a triangle filter; see comments for h2v1 case, above.

  It is OK for us to reference the adjacent input rows because we demanded
  context from the main buffer controller (see initialization code). }

{METHODDEF}
procedure h2v2_fancy_upsample (cinfo : j_decompress_ptr;
                               compptr : jpeg_component_info_ptr;
		               input_data : JSAMPARRAY;
                               var output_data_ptr : JSAMPARRAY); far;
var
  output_data : JSAMPARRAY;
  {register} inptr0, inptr1, outptr : JSAMPLE_PTR;
{$ifdef BITS_IN_JSAMPLE_IS_8}
  {register} thiscolsum, lastcolsum, nextcolsum : int;
{$else}
  {register} thiscolsum, lastcolsum, nextcolsum : INT32;
{$endif}
  {register} colctr : JDIMENSION;
  inrow, outrow, v : int;
var
  prev_input_data : JSAMPARRAY;  { Nomssi work around }
begin
  output_data := output_data_ptr;

  outrow := 0;
  inrow := 0;
  while (outrow < cinfo^.max_v_samp_factor) do
  begin
    for v := 0 to pred(2) do
    begin
      { inptr0 points to nearest input row, inptr1 points to next nearest }
      inptr0 := JSAMPLE_PTR(input_data^[inrow]);
      if (v = 0) then         { next nearest is row above }
      begin
	{inptr1 := JSAMPLE_PTR(input_data^[inrow-1]);}
        prev_input_data := input_data;       { work around }
        Dec(JSAMPROW_PTR(prev_input_data));  { negative offsets }
	inptr1 := JSAMPLE_PTR(prev_input_data^[inrow]);
      end
      else                    { next nearest is row below }
	inptr1 := JSAMPLE_PTR(input_data^[inrow+1]);
      outptr := JSAMPLE_PTR(output_data^[outrow]);
      Inc(outrow);

      { Special case for first column }
      thiscolsum := GETJSAMPLE(inptr0^) * 3 + GETJSAMPLE(inptr1^);
      Inc(inptr0);
      Inc(inptr1);
      nextcolsum := GETJSAMPLE(inptr0^) * 3 + GETJSAMPLE(inptr1^);
      Inc(inptr0);
      Inc(inptr1);

      outptr^ := JSAMPLE ((thiscolsum * 4 + 8) shr 4);
      Inc(outptr);
      outptr^ := JSAMPLE ((thiscolsum * 3 + nextcolsum + 7) shr 4);
      Inc(outptr);
      lastcolsum := thiscolsum; thiscolsum := nextcolsum;

      for colctr := pred(compptr^.downsampled_width - 2) downto 0 do
      begin
	{ General case: 3/4 * nearer pixel + 1/4 * further pixel in each }
	{ dimension, thus 9/16, 3/16, 3/16, 1/16 overall }
	nextcolsum := GETJSAMPLE(inptr0^) * 3 + GETJSAMPLE(inptr1^);
        Inc(inptr0);
        Inc(inptr1);
	outptr^ := JSAMPLE ((thiscolsum * 3 + lastcolsum + 8) shr 4);
        Inc(outptr);
	outptr^ := JSAMPLE ((thiscolsum * 3 + nextcolsum + 7) shr 4);
        Inc(outptr);
	lastcolsum := thiscolsum;
        thiscolsum := nextcolsum;
      end;

      { Special case for last column }
      outptr^ := JSAMPLE ((thiscolsum * 3 + lastcolsum + 8) shr 4);
      Inc(outptr);
      outptr^ := JSAMPLE ((thiscolsum * 4 + 7) shr 4);
      {Inc(outptr);                     - value never used }
    end;
    Inc(inrow);
  end;
end;


{ Module initialization routine for upsampling. }

{GLOBAL}
procedure jinit_upsampler (cinfo : j_decompress_ptr);
var
  upsample : my_upsample_ptr;
  ci : int;
  compptr : jpeg_component_info_ptr;
  need_buffer, do_fancy : boolean;
  h_in_group, v_in_group, h_out_group, v_out_group : int;
begin
  upsample := my_upsample_ptr (
    cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
				SIZEOF(my_upsampler)) );
  cinfo^.upsample := jpeg_upsampler_ptr (upsample);
  upsample^.pub.start_pass := start_pass_upsample;
  upsample^.pub.upsample := sep_upsample;
  upsample^.pub.need_context_rows := FALSE; { until we find out differently }

  if (cinfo^.CCIR601_sampling)	then        { this isn't supported }
    ERREXIT(j_common_ptr(cinfo), JERR_CCIR601_NOTIMPL);

  { jdmainct.c doesn't support context rows when min_DCT_scaled_size := 1,
    so don't ask for it. }

  do_fancy := cinfo^.do_fancy_upsampling and (cinfo^.min_DCT_scaled_size > 1);

  { Verify we can handle the sampling factors, select per-component methods,
    and create storage as needed. }

  compptr := cinfo^.comp_info;
  for ci := 0 to pred(cinfo^.num_components) do
  begin
    { Compute size of an "input group" after IDCT scaling.  This many samples
      are to be converted to max_h_samp_factor * max_v_samp_factor pixels. }

    h_in_group := (compptr^.h_samp_factor * compptr^.DCT_scaled_size) div
		 cinfo^.min_DCT_scaled_size;
    v_in_group := (compptr^.v_samp_factor * compptr^.DCT_scaled_size) div
		 cinfo^.min_DCT_scaled_size;
    h_out_group := cinfo^.max_h_samp_factor;
    v_out_group := cinfo^.max_v_samp_factor;
    upsample^.rowgroup_height[ci] := v_in_group; { save for use later }
    need_buffer := TRUE;
    if (not compptr^.component_needed) then
    begin
      { Don't bother to upsample an uninteresting component. }
      upsample^.methods[ci] := noop_upsample;
      need_buffer := FALSE;
    end
    else
      if (h_in_group = h_out_group) and (v_in_group = v_out_group) then
      begin
        { Fullsize components can be processed without any work. }
        upsample^.methods[ci] := fullsize_upsample;
        need_buffer := FALSE;
      end
      else
        if (h_in_group * 2 = h_out_group) and
	         (v_in_group = v_out_group) then
        begin
        { Special cases for 2h1v upsampling }
          if (do_fancy) and (compptr^.downsampled_width > 2) then
	    upsample^.methods[ci] := h2v1_fancy_upsample
          else
	    upsample^.methods[ci] := h2v1_upsample;
        end
        else
          if (h_in_group * 2 = h_out_group) and
	           (v_in_group * 2 = v_out_group) then
          begin
            { Special cases for 2h2v upsampling }
            if (do_fancy) and (compptr^.downsampled_width > 2) then
            begin
	      upsample^.methods[ci] := h2v2_fancy_upsample;
	      upsample^.pub.need_context_rows := TRUE;
            end
            else
	      upsample^.methods[ci] := h2v2_upsample;
          end
          else
            if ((h_out_group mod h_in_group) = 0) and
	             ((v_out_group mod v_in_group) = 0) then
            begin
              { Generic integral-factors upsampling method }
              upsample^.methods[ci] := int_upsample;
              upsample^.h_expand[ci] := UINT8 (h_out_group div h_in_group);
              upsample^.v_expand[ci] := UINT8 (v_out_group div v_in_group);
            end
            else
              ERREXIT(j_common_ptr(cinfo), JERR_FRACT_SAMPLE_NOTIMPL);
    if (need_buffer) then
    begin
      upsample^.color_buf[ci] := cinfo^.mem^.alloc_sarray
	(j_common_ptr(cinfo), JPOOL_IMAGE,
	 JDIMENSION (jround_up( long (cinfo^.output_width),
				long (cinfo^.max_h_samp_factor))),
	 JDIMENSION (cinfo^.max_v_samp_factor));
    end;
    Inc(compptr);
  end;
end;

end.

⌨️ 快捷键说明

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