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

📄 rdtarga.pas

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

{ rdtarga.c ;  Copyright (C) 1991-1996, Thomas G. Lane.

  These routines may need modification for non-Unix environments or
  specialized applications.  As they stand, they assume input from
  an ordinary stdio stream.  They further assume that reading begins
  at the start of the file; start_input may need work if the
  user interface has already read some data (e.g., to determine that
  the file is indeed Targa format).

  Based on code contributed by Lee Daniel Crocker. }

interface

{$I jconfig.inc}

uses
  jmorecfg,
  jpeglib,
  jinclude,
  jdeferr,
  jerror,
  cdjpeg;		{ Common decls for cjpeg/djpeg applications }


{ The module selection routine for Targa format input. }

{GLOBAL}
function jinit_read_targa (cinfo : j_compress_ptr) : cjpeg_source_ptr;

implementation

{ Macros to deal with unsigned chars as efficiently as compiler allows }

type
  U_CHAR = byte;
  UCH = int;
(*type
{$ifdef CHAR_IS_UNSIGNED}
  UCH = int;
{$else}
  UCH = int (x and $FF);
{$endif}
*)
{ Private version of data source object }

type
  tga_source_ptr = ^tga_source_struct;
  tga_source_struct = record
    pub : cjpeg_source_struct; { public fields }

    cinfo : j_compress_ptr;             { back link saves passing separate parm }

    colormap : JSAMPARRAY;              { Targa colormap (converted to my format) }

    whole_image : jvirt_sarray_ptr;     { Needed if funny input row order }
    current_row : JDIMENSION;           { Current logical row number to read }

    { Pointer to routine to extract next Targa pixel from input file }
    read_pixel : procedure (sinfo : tga_source_ptr);

    { Result of read_pixel is delivered here: }
    tga_pixel : array[0..4-1] of U_CHAR;

    pixel_size : int;                   { Bytes per Targa pixel (1 to 4) }

    { State info for reading RLE-coded pixels; both counts must be init to 0 }
    block_count : int;          { # of pixels remaining in RLE block }
    dup_pixel_count : int;      { # of times to duplicate previous pixel }

    { This saves the correct pixel-row-expansion method for preload_image }
    get_pixel_rows : function(cinfo : j_compress_ptr;
                              sinfo : cjpeg_source_ptr) : JDIMENSION;
  end;

{ For expanding 5-bit pixel values to 8-bit with best rounding }

const
  c5to8bits : array[0..32-1] of UINT8 =
               (  0,   8,  16,  25,  33,  41,  49,  58,
                 66,  74,  82,  90,  99, 107, 115, 123,
                132, 140, 148, 156, 165, 173, 181, 189,
                197, 206, 214, 222, 230, 239, 247, 255);

function getc(f : fileptr) : byte;
begin
  getc := 0;
end;
const
  EOF = byte(26);  { ^Z }

{LOCAL}
function read_byte (sinfo : tga_source_ptr) : int;
{ Read next byte from Targa file }
var
  {register} infile : FILEptr;
  {register} c : int;
begin
  infile := sinfo^.pub.input_file;
  c := getc(infile);
  if (c = EOF) then
    ERREXIT(j_common_ptr(sinfo^.cinfo), JERR_INPUT_EOF);
  read_byte := c;
end;


{LOCAL}
procedure read_colormap (sinfo : tga_source_ptr;
                         cmaplen : int;
                         mapentrysize : int);
{ Read the colormap from a Targa file }
var
  i : int;
begin
  { Presently only handles 24-bit BGR format }
  if (mapentrysize <> 24) then
    ERREXIT(j_common_ptr(sinfo^.cinfo), JERR_TGA_BADCMAP);

  for i := 0 to pred(cmaplen) do
  begin
    sinfo^.colormap^[2]^[i] := JSAMPLE (read_byte(sinfo));
    sinfo^.colormap^[1]^[i] := JSAMPLE (read_byte(sinfo));
    sinfo^.colormap^[0]^[i] := JSAMPLE (read_byte(sinfo));
  end;
end;


{ read_pixel methods: get a single pixel from Targa file into tga_pixel[] }

{METHODDEF}
procedure read_non_rle_pixel (sinfo : tga_source_ptr); far;
{ Read one Targa pixel from the input file; no RLE expansion }
var
  {register} infile : FILEptr;
  {register} i : int;
begin
  infile := sinfo^.pub.input_file;
  for i := 0 to pred(sinfo^.pixel_size) do
  begin
    sinfo^.tga_pixel[i] := U_CHAR (getc(infile));
  end;
end;


{METHODDEF}
procedure read_rle_pixel (sinfo : tga_source_ptr); far;
{ Read one Targa pixel from the input file, expanding RLE data as needed }
var
  {register} infile : FILEptr;
  {register} i : int;
begin
  infile := sinfo^.pub.input_file;

  { Duplicate previously read pixel? }
  if (sinfo^.dup_pixel_count > 0) then
  begin
    Dec(sinfo^.dup_pixel_count);
    exit;
  end;

  { Time to read RLE block header? }
  Dec(sinfo^.block_count);
  if (sinfo^.block_count < 0) then
  begin { decrement pixels remaining in block }
    i := read_byte(sinfo);
    if (i and $80) <> 0 then
    begin		{ Start of duplicate-pixel block? }
      sinfo^.dup_pixel_count := i and $7F; { number of dups after this one }
      sinfo^.block_count := 0;	{ then read new block header }
    end
    else
    begin
      sinfo^.block_count := i and $7F; { number of pixels after this one }
    end;
  end;

  { Read next pixel }
  for i := 0 to pred(sinfo^.pixel_size) do
  begin
    sinfo^.tga_pixel[i] := U_CHAR (getc(infile));
  end;
end;


{ Read one row of pixels.

  We provide several different versions depending on input file format. }

{METHODDEF}
function get_8bit_gray_row (cinfo : j_compress_ptr;
                            sinfo : cjpeg_source_ptr) : JDIMENSION; far;
{ This version is for reading 8-bit grayscale pixels }
var
  source : tga_source_ptr;
  {register} ptr : JSAMPLE_PTR;
  {register} col : JDIMENSION;
begin
  source := tga_source_ptr (sinfo);
  ptr := JSAMPLE_PTR(source^.pub.buffer^[0]);
  for col := pred(cinfo^.image_width) downto 0 do
  begin
    source^.read_pixel (source); { Load next pixel into tga_pixel }
    ptr^ := JSAMPLE (UCH(source^.tga_pixel[0]));
    Inc(ptr);
  end;
  get_8bit_gray_row  := 1;
end;

{METHODDEF}
function get_8bit_row (cinfo : j_compress_ptr;
                       sinfo : cjpeg_source_ptr) : JDIMENSION; far;
{ This version is for reading 8-bit colormap indexes }
var
  source : tga_source_ptr;
  {register} t : int;
  {register} ptr : JSAMPLE_PTR;
  {register} col : JDIMENSION;
  {register} colormap : JSAMPARRAY;
begin
  source := tga_source_ptr (sinfo);
  colormap := source^.colormap;

  ptr := JSAMPLE_PTR(source^.pub.buffer^[0]);
  for col := pred(cinfo^.image_width) downto 0 do
  begin
    source^.read_pixel (source); { Load next pixel into tga_pixel }
    t := UCH(source^.tga_pixel[0]);
    ptr^ := colormap^[0]^[t];
    Inc(ptr);
    ptr^ := colormap^[1]^[t];
    Inc(ptr);
    ptr^ := colormap^[2]^[t];
    Inc(ptr);
  end;
  get_8bit_row  := 1;
end;

{METHODDEF}
function get_16bit_row (cinfo : j_compress_ptr;
                        sinfo : cjpeg_source_ptr) : JDIMENSION; far;
{ This version is for reading 16-bit pixels }
var
  source : tga_source_ptr;

  {register} t : int;
  {register} ptr : JSAMPROW;
  {register} col : JDIMENSION;
begin
  source := tga_source_ptr (sinfo);

  ptr := source^.pub.buffer^[0];
  for col := pred(cinfo^.image_width) downto 0 do
  begin
    source^.read_pixel (source); { Load next pixel into tga_pixel }
    t := UCH(source^.tga_pixel[0]);
    Inc(t, UCH(source^.tga_pixel[1]) shr 8);
    { We expand 5 bit data to 8 bit sample width.
      The format of the 16-bit (LSB first) input word is
          xRRRRRGGGGGBBBBB
     }
    ptr^[2] := JSAMPLE (c5to8bits[t and $1F]);
    t := t shr 5;
    ptr^[1] := JSAMPLE (c5to8bits[t and $1F]);
    t := t shr 5;
    ptr^[0] := JSAMPLE (c5to8bits[t and $1F]);
    Inc(JSAMPLE_PTR(ptr), 3);
  end;
  get_16bit_row  :=1;
end;

{METHODDEF}
function get_24bit_row (cinfo : j_compress_ptr;
                        sinfo : cjpeg_source_ptr) : JDIMENSION; far;
{ This version is for reading 24-bit pixels }
var
  source : tga_source_ptr;

  {register} ptr : JSAMPLE_PTR;
  {register} col : JDIMENSION;

⌨️ 快捷键说明

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