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

📄 jccolor.pas

📁 DELPHI版的JPEG文件解码源程序
💻 PAS
📖 第 1 页 / 共 2 页
字号:
var
  cconvert : my_cconvert_ptr;
  {register} r, g, b : int;
  {register} ctab : INT32_FIELD_PTR;
  {register} inptr : JSAMPROW;
  {register} outptr0, outptr1, outptr2, outptr3 : JSAMPROW;
  {register} col : JDIMENSION;
  num_cols : JDIMENSION;
begin
  cconvert := my_cconvert_ptr (cinfo^.cconvert);
  ctab := cconvert^.rgb_ycc_tab;
  num_cols := cinfo^.image_width;

  while (num_rows > 0) do
  begin
    Dec(num_rows);
    inptr := input_buf^[0];
    Inc(JSAMPROW_PTR(input_buf));
    outptr0 := output_buf^[0]^[output_row];
    outptr1 := output_buf^[1]^[output_row];
    outptr2 := output_buf^[2]^[output_row];
    outptr3 := output_buf^[3]^[output_row];
    Inc(output_row);
    for col := 0 to pred(num_cols) do
    begin
      r := MAXJSAMPLE - GETJSAMPLE(inptr^[0]);
      g := MAXJSAMPLE - GETJSAMPLE(inptr^[1]);
      b := MAXJSAMPLE - GETJSAMPLE(inptr^[2]);
      { K passes through as-is }
      outptr3^[col] := inptr^[3];	{ don't need GETJSAMPLE here }
      Inc(JSAMPLE_PTR(inptr), 4);
      { If the inputs are 0..MAXJSAMPLE, the outputs of these equations
        must be too; we do not need an explicit range-limiting operation.
        Hence the value being shifted is never negative, and we don't
        need the general RIGHT_SHIFT macro. }

      { Y }
      outptr0^[col] := JSAMPLE (
		((ctab^[r+R_Y_OFF] + ctab^[g+G_Y_OFF] + ctab^[b+B_Y_OFF])
		 shr SCALEBITS) );
      { Cb }
      outptr1^[col] := JSAMPLE(
		((ctab^[r+R_CB_OFF] + ctab^[g+G_CB_OFF] + ctab^[b+B_CB_OFF])
		 shr SCALEBITS) );
      { Cr }
      outptr2^[col] := JSAMPLE (
		((ctab^[r+R_CR_OFF] + ctab^[g+G_CR_OFF] + ctab^[b+B_CR_OFF])
		 shr SCALEBITS) );
    end;
  end;
end;


{ Convert some rows of samples to the JPEG colorspace.
  This version handles grayscale output with no conversion.
  The source can be either plain grayscale or YCbCr (since Y = gray). }

{METHODDEF}
procedure grayscale_convert (cinfo : j_compress_ptr;
                            input_buf : JSAMPARRAY;
                            output_buf : JSAMPIMAGE;
                            output_row : JDIMENSION;
                            num_rows: int); far;
var
  {register} inptr : JSAMPROW;
  {register} outptr : JSAMPROW;
  {register} col : JDIMENSION;
  num_cols :JDIMENSION;
  instride : int;
begin
  num_cols := cinfo^.image_width;
  instride := cinfo^.input_components;

  while (num_rows > 0) do
  begin
    Dec(num_rows);
    inptr := input_buf^[0];
    Inc(JSAMPROW_PTR(input_buf));
    outptr := output_buf^[0]^[output_row];
    Inc(output_row);
    for col := 0 to pred(num_cols) do
    begin
      outptr^[col] := inptr^[0];	{ don't need GETJSAMPLE() here }
      Inc(JSAMPLE_PTR(inptr), instride);
    end;
  end;
end;


{ Convert some rows of samples to the JPEG colorspace.
  This version handles multi-component colorspaces without conversion.
  We assume input_components = num_components. }

{METHODDEF}
procedure null_convert (cinfo : j_compress_ptr;
	                input_buf : JSAMPARRAY;
                        output_buf : JSAMPIMAGE;
                        output_row : JDIMENSION;
                        num_rows : int); far;
var
  {register} inptr : JSAMPROW;
  {register} outptr : JSAMPROW;
  {register} col : JDIMENSION;
  {register} ci : int;
  nc : int;
  num_cols : JDIMENSION;
begin
  nc := cinfo^.num_components;
  num_cols := cinfo^.image_width;

  while (num_rows > 0) do
  begin
    Dec(num_rows);
    { It seems fastest to make a separate pass for each component. }
    for ci := 0 to pred(nc) do
    begin
      inptr := input_buf^[0];
      outptr := output_buf^[ci]^[output_row];
      for col := 0 to pred(num_cols) do
      begin
	outptr^[col] := inptr^[ci]; { don't need GETJSAMPLE() here }
	Inc(JSAMPLE_PTR(inptr), nc);
      end;
    end;
    Inc(JSAMPROW_PTR(input_buf));
    Inc(output_row);
  end;
end;


{ Empty method for start_pass. }

{METHODDEF}
procedure null_method (cinfo : j_compress_ptr); far;
begin
  { no work needed }
end;


{ Module initialization routine for input colorspace conversion. }

{GLOBAL}
procedure jinit_color_converter (cinfo : j_compress_ptr);
var
  cconvert : my_cconvert_ptr;
begin
  cconvert := my_cconvert_ptr(
    cinfo^.mem^.alloc_small (j_common_ptr(cinfo), JPOOL_IMAGE,
				SIZEOF(my_color_converter)) );
  cinfo^.cconvert := jpeg_color_converter_ptr(cconvert);
  { set start_pass to null method until we find out differently }
  cconvert^.pub.start_pass := null_method;

  { Make sure input_components agrees with in_color_space }
  case (cinfo^.in_color_space) of
  JCS_GRAYSCALE:
    if (cinfo^.input_components <> 1) then
      ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);

{$ifdef RGB_PIXELSIZE <> 3}
  JCS_RGB:
    if (cinfo^.input_components <> RGB_PIXELSIZE) then
      ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
{$else} { share code with YCbCr }
  JCS_RGB,
{$endif}
  JCS_YCbCr:
    if (cinfo^.input_components <> 3) then
      ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);

  JCS_CMYK,
  JCS_YCCK:
    if (cinfo^.input_components <> 4) then
      ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);

  else			{ JCS_UNKNOWN can be anything }
    if (cinfo^.input_components < 1) then
      ERREXIT(j_common_ptr(cinfo), JERR_BAD_IN_COLORSPACE);
  end;

  { Check num_components, set conversion method based on requested space }
  case (cinfo^.jpeg_color_space) of
  JCS_GRAYSCALE:
    begin
      if (cinfo^.num_components <> 1) then
        ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
      if (cinfo^.in_color_space = JCS_GRAYSCALE) then
        cconvert^.pub.color_convert := grayscale_convert
      else
        if (cinfo^.in_color_space = JCS_RGB) then
        begin
          cconvert^.pub.start_pass := rgb_ycc_start;
          cconvert^.pub.color_convert := rgb_gray_convert;
        end
        else
          if (cinfo^.in_color_space = JCS_YCbCr) then
            cconvert^.pub.color_convert := grayscale_convert
          else
            ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
    end;

  JCS_RGB:
    begin
      if (cinfo^.num_components <> 3) then
        ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
        if (cinfo^.in_color_space = JCS_RGB) and (RGB_PIXELSIZE = 3) then
          cconvert^.pub.color_convert := null_convert
        else
          ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
    end;

  JCS_YCbCr:
    begin
      if (cinfo^.num_components <> 3) then
        ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
      if (cinfo^.in_color_space = JCS_RGB) then
      begin
        cconvert^.pub.start_pass := rgb_ycc_start;
        cconvert^.pub.color_convert := rgb_ycc_convert;
      end
      else
        if (cinfo^.in_color_space = JCS_YCbCr) then
          cconvert^.pub.color_convert := null_convert
        else
          ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
    end;

  JCS_CMYK:
    begin
      if (cinfo^.num_components <> 4) then
        ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
      if (cinfo^.in_color_space = JCS_CMYK) then
        cconvert^.pub.color_convert := null_convert
      else
        ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
    end;

  JCS_YCCK:
    begin
      if (cinfo^.num_components <> 4) then
        ERREXIT(j_common_ptr(cinfo), JERR_BAD_J_COLORSPACE);
      if (cinfo^.in_color_space = JCS_CMYK) then
      begin
        cconvert^.pub.start_pass := rgb_ycc_start;
        cconvert^.pub.color_convert := cmyk_ycck_convert;
      end
      else
        if (cinfo^.in_color_space = JCS_YCCK) then
          cconvert^.pub.color_convert := null_convert
        else
          ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
    end;

  else		{ allow null conversion of JCS_UNKNOWN }
    begin
      if (cinfo^.jpeg_color_space <> cinfo^.in_color_space) or
	 (cinfo^.num_components <> cinfo^.input_components) then
        ERREXIT(j_common_ptr(cinfo), JERR_CONVERSION_NOTIMPL);
      cconvert^.pub.color_convert := null_convert;
    end;
  end;
end;

end.

⌨️ 快捷键说明

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