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

📄 jdmaster.pas

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

{ This file contains master control logic for the JPEG decompressor.
  These routines are concerned with selecting the modules to be executed
  and with determining the number of passes and the work to be done in each
  pass. }

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

interface

{$I jconfig.inc}

uses
  jmorecfg,
  jinclude,
  jutils,
  jerror,
  jdeferr,
  jdcolor, jdsample, jdpostct, jddctmgr, jdphuff, jdhuff, jdcoefct, jdmainct,
{$ifdef QUANT_1PASS_SUPPORTED}
  jquant1,
{$endif}
{$ifdef QUANT_2PASS_SUPPORTED}
  jquant2,
{$endif}
{$ifdef UPSAMPLE_MERGING_SUPPORTED}
  jdmerge,
{$endif}
  jpeglib;


{ Compute output image dimensions and related values.
  NOTE: this is exported for possible use by application.
  Hence it mustn't do anything that can't be done twice.
  Also note that it may be called before the master module is initialized! }

{GLOBAL}
procedure jpeg_calc_output_dimensions (cinfo : j_decompress_ptr);
{ Do computations that are needed before master selection phase }


{$ifdef D_MULTISCAN_FILES_SUPPORTED}

{GLOBAL}
procedure jpeg_new_colormap (cinfo : j_decompress_ptr);

{$endif}

{ Initialize master decompression control and select active modules.
  This is performed at the start of jpeg_start_decompress. }

{GLOBAL}
procedure jinit_master_decompress (cinfo : j_decompress_ptr);

implementation

{ Private state }

type
  my_master_ptr = ^my_decomp_master;
  my_decomp_master = record
    pub : jpeg_decomp_master; { public fields }

    pass_number : int;		{ # of passes completed }

    using_merged_upsample : boolean; { TRUE if using merged upsample/cconvert }

    { Saved references to initialized quantizer modules,
      in case we need to switch modes. }

    quantizer_1pass : jpeg_color_quantizer_ptr;
    quantizer_2pass : jpeg_color_quantizer_ptr;
  end;

{ Determine whether merged upsample/color conversion should be used.
  CRUCIAL: this must match the actual capabilities of jdmerge.c! }

{LOCAL}
function use_merged_upsample (cinfo : j_decompress_ptr) : boolean;
var
  compptr : jpeg_component_info_list_ptr;  { Nomssi }
begin
  compptr := jpeg_component_info_list_ptr(cinfo^.comp_info);

{$ifdef UPSAMPLE_MERGING_SUPPORTED}
  { Merging is the equivalent of plain box-filter upsampling }
  if (cinfo^.do_fancy_upsampling) or (cinfo^.CCIR601_sampling) then
  begin
    use_merged_upsample := FALSE;
    exit;
  end;
  { jdmerge.c only supports YCC=>RGB color conversion }
  if (cinfo^.jpeg_color_space <> JCS_YCbCr) or (cinfo^.num_components <> 3)
  or (cinfo^.out_color_space <> JCS_RGB)
  or (cinfo^.out_color_components <> RGB_PIXELSIZE) then
  begin
    use_merged_upsample := FALSE;
    exit;
  end;

  { and it only handles 2h1v or 2h2v sampling ratios }
  if (compptr^[0].h_samp_factor <> 2) or
     (compptr^[1].h_samp_factor <> 1) or
     (compptr^[2].h_samp_factor <> 1) or
     (compptr^[0].v_samp_factor >  2) or
     (compptr^[1].v_samp_factor <> 1) or
     (compptr^[2].v_samp_factor <> 1) then
  begin
    use_merged_upsample := FALSE;
    exit;
  end;
  { furthermore, it doesn't work if we've scaled the IDCTs differently }
  if (compptr^[0].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) or
     (compptr^[1].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) or
     (compptr^[2].DCT_scaled_size <> cinfo^.min_DCT_scaled_size) then
  begin
    use_merged_upsample := FALSE;
    exit;
  end;
  { ??? also need to test for upsample-time rescaling, when & if supported }
  use_merged_upsample := TRUE;			{ by golly, it'll work... }
{$else}
  use_merged_upsample := FALSE;
{$endif}
end;


{ Compute output image dimensions and related values.
  NOTE: this is exported for possible use by application.
  Hence it mustn't do anything that can't be done twice.
  Also note that it may be called before the master module is initialized! }

{GLOBAL}
procedure jpeg_calc_output_dimensions (cinfo : j_decompress_ptr);
{ Do computations that are needed before master selection phase }
var
  ci : int;
  compptr : jpeg_component_info_ptr;
var
  ssize : int;
begin
  { Prevent application from calling me at wrong times }
  if (cinfo^.global_state <> DSTATE_READY) then
    ERREXIT1(j_common_ptr(cinfo), JERR_BAD_STATE, cinfo^.global_state);

{$ifdef IDCT_SCALING_SUPPORTED}

  { Compute actual output image dimensions and DCT scaling choices. }
  if (cinfo^.scale_num * 8 <= cinfo^.scale_denom) then
  begin
    { Provide 1/8 scaling }
    cinfo^.output_width := JDIMENSION (
      jdiv_round_up( long(cinfo^.image_width), long(8)) );
    cinfo^.output_height := JDIMENSION (
      jdiv_round_up( long(cinfo^.image_height), long(8)) );
    cinfo^.min_DCT_scaled_size := 1;
  end
  else
    if (cinfo^.scale_num * 4 <= cinfo^.scale_denom) then
    begin
      { Provide 1/4 scaling }
      cinfo^.output_width := JDIMENSION (
        jdiv_round_up( long (cinfo^.image_width), long(4)) );
      cinfo^.output_height := JDIMENSION (
        jdiv_round_up( long (cinfo^.image_height), long(4)) );
      cinfo^.min_DCT_scaled_size := 2;
    end
    else
      if (cinfo^.scale_num * 2 <= cinfo^.scale_denom) then
      begin
        { Provide 1/2 scaling }
        cinfo^.output_width := JDIMENSION (
          jdiv_round_up( long(cinfo^.image_width), long(2)) );
        cinfo^.output_height := JDIMENSION (
          jdiv_round_up( long(cinfo^.image_height), long(2)) );
        cinfo^.min_DCT_scaled_size := 4;
      end
      else
      begin
        { Provide 1/1 scaling }
        cinfo^.output_width := cinfo^.image_width;
        cinfo^.output_height := cinfo^.image_height;
        cinfo^.min_DCT_scaled_size := DCTSIZE;
      end;
  { In selecting the actual DCT scaling for each component, we try to
    scale up the chroma components via IDCT scaling rather than upsampling.
    This saves time if the upsampler gets to use 1:1 scaling.
    Note this code assumes that the supported DCT scalings are powers of 2. }

  compptr := cinfo^.comp_info;
  for ci := 0 to pred(cinfo^.num_components) do
  begin
    ssize := cinfo^.min_DCT_scaled_size;
    while (ssize < DCTSIZE) and
	  ((compptr^.h_samp_factor * ssize * 2 <=
	    cinfo^.max_h_samp_factor * cinfo^.min_DCT_scaled_size) and
	   (compptr^.v_samp_factor * ssize * 2 <=
	    cinfo^.max_v_samp_factor * cinfo^.min_DCT_scaled_size)) do
    begin
      ssize := ssize * 2;
    end;
    compptr^.DCT_scaled_size := ssize;
    Inc(compptr);
  end;

  { Recompute downsampled dimensions of components;
    application needs to know these if using raw downsampled data. }

  compptr := cinfo^.comp_info;
  for ci := 0 to pred(cinfo^.num_components) do
  begin
    { Size in samples, after IDCT scaling }
    compptr^.downsampled_width := JDIMENSION (
      jdiv_round_up(long (cinfo^.image_width) *
		    long (compptr^.h_samp_factor * compptr^.DCT_scaled_size),
		    long (cinfo^.max_h_samp_factor * DCTSIZE)) );
    compptr^.downsampled_height := JDIMENSION (
      jdiv_round_up(long (cinfo^.image_height) *
		    long (compptr^.v_samp_factor * compptr^.DCT_scaled_size),
		    long (cinfo^.max_v_samp_factor * DCTSIZE)) );
    Inc(compptr);
  end;

{$else} { !IDCT_SCALING_SUPPORTED }

  { Hardwire it to "no scaling" }
  cinfo^.output_width := cinfo^.image_width;
  cinfo^.output_height := cinfo^.image_height;
  { jdinput.c has already initialized DCT_scaled_size to DCTSIZE,
    and has computed unscaled downsampled_width and downsampled_height. }

{$endif} { IDCT_SCALING_SUPPORTED }

  { Report number of components in selected colorspace. }
  { Probably this should be in the color conversion module... }
  case (cinfo^.out_color_space) of
  JCS_GRAYSCALE:
    cinfo^.out_color_components := 1;
{$ifndef RGB_PIXELSIZE_IS_3}
  JCS_RGB:
    cinfo^.out_color_components := RGB_PIXELSIZE;
{$else}
  JCS_RGB,
{$endif} { else share code with YCbCr }
  JCS_YCbCr:
    cinfo^.out_color_components := 3;
  JCS_CMYK,
  JCS_YCCK:
    cinfo^.out_color_components := 4;
  else			{ else must be same colorspace as in file }
    cinfo^.out_color_components := cinfo^.num_components;
  end;
  if (cinfo^.quantize_colors) then
    cinfo^.output_components := 1
  else
    cinfo^.output_components := cinfo^.out_color_components;

  { See if upsampler will want to emit more than one row at a time }
  if (use_merged_upsample(cinfo)) then
    cinfo^.rec_outbuf_height := cinfo^.max_v_samp_factor
  else
    cinfo^.rec_outbuf_height := 1;
end;


{ Several decompression processes need to range-limit values to the range
  0..MAXJSAMPLE; the input value may fall somewhat outside this range
  due to noise introduced by quantization, roundoff error, etc.  These
  processes are inner loops and need to be as fast as possible.  On most
  machines, particularly CPUs with pipelines or instruction prefetch,
  a (subscript-check-less) C table lookup
 		x := sample_range_limit[x];
  is faster than explicit tests
 		if (x < 0)  x := 0;
 		else if (x > MAXJSAMPLE)  x := MAXJSAMPLE;
  These processes all use a common table prepared by the routine below.

  For most steps we can mathematically guarantee that the initial value
  of x is within MAXJSAMPLE+1 of the legal range, so a table running from
  -(MAXJSAMPLE+1) to 2*MAXJSAMPLE+1 is sufficient.  But for the initial
  limiting step (just after the IDCT), a wildly out-of-range value is
  possible if the input data is corrupt.  To avoid any chance of indexing
  off the end of memory and getting a bad-pointer trap, we perform the
  post-IDCT limiting thus:
 		x := range_limit[x & MASK];
  where MASK is 2 bits wider than legal sample data, ie 10 bits for 8-bit
  samples.  Under normal circumstances this is more than enough range and
  a correct output will be generated; with bogus input data the mask will
  cause wraparound, and we will safely generate a bogus-but-in-range output.
  For the post-IDCT step, we want to convert the data from signed to unsigned
  representation by adding CENTERJSAMPLE at the same time that we limit it.
  So the post-IDCT limiting table ends up looking like this:
    CENTERJSAMPLE,CENTERJSAMPLE+1,...,MAXJSAMPLE,
    MAXJSAMPLE (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
    0          (repeat 2*(MAXJSAMPLE+1)-CENTERJSAMPLE times),
    0,1,...,CENTERJSAMPLE-1
  Negative inputs select values from the upper half of the table after
  masking.

  We can save some space by overlapping the start of the post-IDCT table
  with the simpler range limiting table.  The post-IDCT table begins at
  sample_range_limit + CENTERJSAMPLE.

  Note that the table is allocated in near data space on PCs; it's small
  enough and used often enough to justify this. }

{LOCAL}
procedure prepare_range_limit_table (cinfo : j_decompress_ptr);
{ Allocate and fill in the sample_range_limit table }
var
  table : range_limit_table_ptr;
  idct_table : JSAMPROW;
  i : int;
begin
  table := range_limit_table_ptr (
    cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
		(5 * (MAXJSAMPLE+1) + CENTERJSAMPLE) * SIZEOF(JSAMPLE)) );

  { First segment of "simple" table: limit[x] := 0 for x < 0 }
  MEMZERO(table, (MAXJSAMPLE+1) * SIZEOF(JSAMPLE));

  cinfo^.sample_range_limit := (table);
  { allow negative subscripts of simple table }
  { is noop, handled via type definition (Nomssi) }
  { Main part of "simple" table: limit[x] := x }
  for i := 0 to MAXJSAMPLE do
    table^[i] := JSAMPLE (i);
  idct_table := JSAMPROW(@ table^[CENTERJSAMPLE]);
                        { Point to where post-IDCT table starts }
  { End of simple table, rest of first half of post-IDCT table }
  for i := CENTERJSAMPLE to pred(2*(MAXJSAMPLE+1)) do
    idct_table^[i] := MAXJSAMPLE;
  { Second half of post-IDCT table }
  MEMZERO(@(idct_table^[2 * (MAXJSAMPLE+1)]),
	  (2 * (MAXJSAMPLE+1) - CENTERJSAMPLE) * SIZEOF(JSAMPLE));
  MEMCOPY(@(idct_table^[(4 * (MAXJSAMPLE+1) - CENTERJSAMPLE)]),
	  @cinfo^.sample_range_limit^[0], CENTERJSAMPLE * SIZEOF(JSAMPLE));

⌨️ 快捷键说明

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