unit DXT;

interface

uses Images, Windows;

type
  TDXTHeader = packed record
    DataSize            : cardinal;
    InternalFormat      : cardinal;
    Width               : cardinal;
    Height              : cardinal;
    Depth               : cardinal;
    BytesPerPel         : cardinal;
    Reserved            : array[0..9] of byte;
  end;

function LoadDXT(filename: string): TCompressedImage;
procedure SaveDXT(filename: string; img: TCompressedImage);

implementation

function LoadDXT(filename: string): TCompressedImage;
var
  f: File;
  hdr: TDXTHeader;
begin
  Assign(f, filename);
  Reset(f, 1);

  BlockRead(f, hdr, SizeOf(TDXTHeader));

  Seek(f, 16);

  result := TCompressedImage.Create(hdr.Width, hdr.Height, hdr.Depth,
                             hdr.BytesPerPel * 8, hdr.DataSize);
  result.InternalFormat := hdr.InternalFormat;

  BlockRead(f, result.Data^, hdr.DataSize);

  Close(f);
end;

procedure SaveDXT;
var
  f: File;
  hdr: TDXTHeader;
  i: cardinal;
begin
  Assign(f, filename);
  ReWrite(f, 1);

  hdr.DataSize          := img.DataSize;
  hdr.InternalFormat    := img.InternalFormat;
  hdr.Width             := img.Width;
  hdr.Height            := img.Height;
  hdr.Depth             := img.Depth;
  hdr.BytesPerPel       := img.BytesPerPel;
  for i := 0 to 9 do hdr.Reserved[i] := 0;

  BlockWrite(f, hdr, SizeOf(TDXTHeader));

  BlockWrite(f, img.Data^, img.DataSize);

  Close(f);
end;

end.
