
interface

uses
  dtypes,
  dstrnum,
  dcolors,
  dstream,
  dstream_op,
  dbitstream,
  dlzw_decompressor,
  dtexture2d,
  dtiff_reader;

type
TTexture2dFormatTiff = object(TTexture2dFormatNew)
  function ReadHeaderAndCheckSupported(Stream: PStream; var Header: TTiffHeader): Boolean;
  function CheckFormat(Stream: PStream): Boolean; override;
  function Load(Stream: PStream; var Texture: TTexture2d): Boolean; override;
  function Load(Stream: PStream; var Texture: TTexture2dRgba_): Boolean; virtual;
end;

implementation

function TTexture2dFormatTiff.ReadHeaderAndCheckSupported(Stream: PStream; var Header: TTiffHeader): Boolean;
const
  TIFF_MARKER_INTEL:    array[0..3] of Byte = (Ord('I'), Ord('I'), 42,  0);
  TIFF_MARKER_MOTOROLA: array[0..3] of Byte = (Ord('M'), Ord('M'),  0, 42);
begin
  Stream.Read(@Header, SizeOf(TUint32) + SizeOf(TUint32));
  with Header do
    if Marker = TUint32(TIFF_MARKER_INTEL) then begin
      Header.LittleEndian := True;
      Result := True;
    end else if Marker = TUint32(TIFF_MARKER_MOTOROLA) then begin
      Header.LittleEndian := False;
      Result := True;
    end else
      Result := False;
end;

function TTexture2dFormatTiff.CheckFormat(Stream: PStream): Boolean;
var
  Header: TTiffHeader;
begin
  Result := ReadHeaderAndCheckSupported(Stream, Header);
end;

function TTexture2dFormatTiff.Load(Stream: PStream; var Texture: TTexture2dRgba_): Boolean; virtual;
var
  Header: TTiffHeader;
begin
  Result := False;

  if not ReadHeaderAndCheckSupported(Stream, Header) then
    Exit;

  // TODO
end;

var
  Format: TTexture2dFormatTiff;
initialization
  Format.Init;
  RegistTexture2dFormat(@Format);
end.
