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

📄 wrjpgcom.pas

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

{ wrjpgcom.c

  Copyright (C) 1994-1997, Thomas G. Lane.
  This file is part of the Independent JPEG Group's software.
  For conditions of distribution and use, see the accompanying README file.

  This file contains a very simple stand-alone application that inserts
  user-supplied text as a COM (comment) marker in a JFIF file.
  This may be useful as an example of the minimum logic needed to parse
  JPEG markers. }

uses
  jmorecfg,
  jinclude,
  cdjpeg,
  strings,
  fcache;

const
  EXIT_FAILURE = 1;             { define Halt() codes if not provided }
  EXIT_SUCCESS = 0;

{ Reduce this value if your malloc() can't allocate blocks up to 64K.
  On DOS, compiling in large model is usually a better solution. }

const
  MAX_COM_LENGTH = Long(32000);  { must be <= 65533 in any case }


{ These macros are used to read the input file and write the output file.
  To reuse this code in another application, you might need to change these. }

var
  infile : file;                { input JPEG file }

{ Return next input byte, or EOF if no more }

var
  outfile : file;               { output JPEG file }

{ Emit an output byte }
function NEXTBYTE : byte;
begin
  NEXTBYTE := fc_getc(var fc : Cache);
{ Read a byte at the current buffer read-index, increment the buffer
  read-index }
end;

procedure PUTBYTE(c : int);
begin
  outfile.Write(c, 1);
end;

{ Error exit handler }
procedure ERREXIT(msg : string);
begin
  WriteLn(msg);
  Halt(EXIT_FAILURE);
end;


{ Read one byte, testing for EOF }
function read_1_byte : int;
var
  c : byte;
begin
  c := NEXTBYTE;
  if (c = int(EOF)) then
    ERREXIT('Premature EOF in JPEG file');
  read_1_byte := c;
end;

{ Read 2 bytes, convert to uint }
{ All 2-byte quantities in JPEG markers are MSB first }
function read_2_bytes : uint;
var
  c1, c2 : int;
begin
  c1 := NEXTBYTE;
  if (c1 = int(EOF)) then
    ERREXIT('Premature EOF in JPEG file');
  c2 := NEXTBYTE;
  if (c2 = int(EOF)) then
    ERREXIT('Premature EOF in JPEG file');
  read_2_bytes := ((uint(c1)) shl 8) + (uint(c2));
end;


{ Routines to write data to output file }

procedure write_1_byte (c : int);
begin
  PUTBYTE(c);
end;

procedure write_2_bytes (val : uint);
begin
  PUTBYTE((val shr 8) and $FF);
  PUTBYTE(val and $FF);
end;

procedure write_marker (marker : int);
begin
  PUTBYTE($FF);
  PUTBYTE(marker);
end;

procedure copy_rest_of_file;
var
  c : int;
begin
  repeat
    c := NEXTBYTE;
    if (c <> int(EOF)) then
      PUTBYTE(c);
  until (c = int(EOF));
end;


{ JPEG markers consist of one or more $FF bytes, followed by a marker
  code byte (which is not an FF).  Here are the marker codes of interest
  in this program.  (See jdmarker.c for a more complete list.) }
const
  M_SOF0   = $C0;               { Start Of Frame N }
  M_SOF1   = $C1;               { N indicates which compression process }
  M_SOF2   = $C2;               { Only SOF0-SOF2 are now in common use }
  M_SOF3   = $C3;
  M_SOF5   = $C5;               { NB: codes C4 and CC are NOT SOF markers }
  M_SOF6   = $C6;
  M_SOF7   = $C7;
  M_SOF9   = $C9;
  M_SOF10  = $CA;
  M_SOF11  = $CB;
  M_SOF13  = $CD;
  M_SOF14  = $CE;
  M_SOF15  = $CF;
  M_SOI    = $D8;               { Start Of Image (beginning of datastream) }
  M_EOI    = $D9;               { End Of Image (end of datastream) }
  M_SOS    = $DA;               { Start Of Scan (begins compressed data) }
  M_COM    = $FE;               { COMment }


{ Find the next JPEG marker and return its marker code.
  We expect at least one FF byte, possibly more if the compressor used FFs
  to pad the file.  (Padding FFs will NOT be replicated in the output file.)
  There could also be non-FF garbage between markers.  The treatment of such
  garbage is unspecified; we choose to skip over it but emit a warning msg.
  NB: this routine must not be used after seeing SOS marker, since it will
  not deal correctly with FF/00 sequences in the compressed image data... }

function next_marker : int;
var
  c : int;
  discarded_bytes : int;
begin
  discarded_bytes := 0;

  { Find $FF byte; count and skip any non-FFs. }
  c := read_1_byte;
  while (c <> $FF) do
  begin
    Inc(discarded_bytes);
    c := read_1_byte;
  end;
  { Get marker code byte, swallowing any duplicate FF bytes.  Extra FFs
    are legal as pad bytes, so don't count them in discarded_bytes.  }
  repeat
    c := read_1_byte;
  until (c <> $FF);

  if (discarded_bytes <> 0) then
  begin
    WriteLn('Warning: garbage data found in JPEG file');
  end;

  next_marker := c;
end;


{ Read the initial marker, which should be SOI.
  For a JFIF file, the first two bytes of the file should be literally
  $FF M_SOI.  To be more general, we could use next_marker, but if the
  input file weren't actually JPEG at all, next_marker might read the whole
  file and then return a misleading error message... }

function first_marker : int;
var
  c1, c2 : int;
begin
  c1 := NEXTBYTE;
  c2 := NEXTBYTE;
  if (c1 <> $FF) or (c2 <> M_SOI) then
    ERREXIT('Not a JPEG file');
  first_marker := c2;
end;


{ Most types of marker are followed by a variable-length parameter segment.
  This routine skips over the parameters for any marker we don't otherwise
  want to process.
  Note that we MUST skip the parameter segment explicitly in order not to
  be fooled by $FF bytes that might appear within the parameter segment;
  such bytes do NOT introduce new markers. }

procedure copy_variable;
{ Copy an unknown or uninteresting variable-length marker }
var
  length : uint;
begin
  { Get the marker parameter length count }
  length := read_2_bytes;
  write_2_bytes(length);
  { Length includes itself, so must be at least 2 }
  if (length < 2) then
    ERREXIT('Erroneous JPEG marker length');
  Dec(length, 2);
  { Skip over the remaining bytes }
  while (length > 0) do
  begin
    write_1_byte(read_1_byte);
    Dec(length);
  end;
end;

procedure skip_variable;
{ Skip over an unknown or uninteresting variable-length marker }
var
  length : uint;
begin
  { Get the marker parameter length count }
  length := read_2_bytes;
  { Length includes itself, so must be at least 2 }
  if (length < 2) then
    ERREXIT('Erroneous JPEG marker length');
  Dec(length, 2);
  { Skip over the remaining bytes }
  while (length > 0) do
  begin
    read_1_byte;
    Dec(length);
  end;
end;


{ Parse the marker stream until SOFn or EOI is seen;
  copy data to output, but discard COM markers unless keep_COM is true. }

function scan_JPEG_header (keep_COM : boolean) : int;
var
  marker : int;
begin
  { Expect SOI at start of file }
  if (first_marker <> M_SOI) then
    ERREXIT('Expected SOI marker first');
  write_marker(M_SOI);

  { Scan miscellaneous markers until we reach SOFn. }
  while TRUE do
  begin
    marker := next_marker;
    case marker of
      { Note that marker codes $C4, $C8, $CC are not, and must not be,
        treated as SOFn.  C4 in particular is actually DHT. }
    M_SOF0,		{ Baseline }
    M_SOF1,		{ Extended sequential, Huffman }
    M_SOF2,		{ Progressive, Huffman }
    M_SOF3,		{ Lossless, Huffman }
    M_SOF5,		{ Differential sequential, Huffman }
    M_SOF6,		{ Differential progressive, Huffman }
    M_SOF7,		{ Differential lossless, Huffman }
    M_SOF9,		{ Extended sequential, arithmetic }
    M_SOF10,		{ Progressive, arithmetic }
    M_SOF11,		{ Lossless, arithmetic }
    M_SOF13,		{ Differential sequential, arithmetic }
    M_SOF14,		{ Differential progressive, arithmetic }
    M_SOF15:		{ Differential lossless, arithmetic }
      begin
        scan_JPEG_header := marker;
        exit;
      end;

    M_SOS:			{ should not see compressed data before SOF }
      ERREXIT('SOS without prior SOFn');

    M_EOI:			{ in case it's a tables-only JPEG stream }
      begin
        scan_JPEG_header := marker;
        exit;
      end;

    M_COM:			{ Existing COM: conditionally discard }
      if (keep_COM) then
      begin

⌨️ 快捷键说明

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