TARGA & Texture Units

Added TARGA and Texture units to represent & parse TARGA into and provide a standard texture format for use when drawing.
This commit is contained in:
Kieron Morris 2022-02-06 13:25:04 +00:00
parent 4e991c3e6f
commit d7a54d858d
3 changed files with 105 additions and 0 deletions

View File

@ -28,24 +28,28 @@ type
R : uint8; R : uint8;
A : uint8; A : uint8;
end; end;
PRGB32 = ^TRGB32;
TRGB24 = bitpacked record TRGB24 = bitpacked record
B : uint8; B : uint8;
G : uint8; G : uint8;
R : uint8; R : uint8;
end; end;
PRGB23 = ^TRGB24;
TRGB16 = bitpacked record TRGB16 = bitpacked record
B : UBit5; B : UBit5;
G : UBit6; G : UBit6;
R : UBit5; R : UBit5;
end; end;
PRGB16 = ^TRGB16;
TRGB8 = bitpacked record TRGB8 = bitpacked record
B : UBit2; B : UBit2;
G : UBit4; G : UBit4;
R : UBit2; R : UBit2;
end; end;
PRGB8 = ^TRGB8;
const const
black : TRGB32 = (B: 000; G: 000; R: 000; A: 000); black : TRGB32 = (B: 000; G: 000; R: 000; A: 000);

67
src/include/targa.pas Normal file
View File

@ -0,0 +1,67 @@
unit targa;
interface
uses
lmemorymanager, texture;
type
TTARGAColor = packed record
b, g, r, a: uint8;
end;
PTARGAColor = ^TTARGAColor;
TTARGAHeader = packed record
Magic: uint8;
ColorMapType: uint8;
ImageType: uint8;
ColorMapOrigin: uint16;
ColorMapLength: uint16;
ColorMapDepth: uint8;
XOrigin: uint16;
YOrigin: uint16;
Width: uint16;
Height: uint16;
PixelDepth: uint8;
ImageDescriptor: uint8;
Data : PTARGAColor;
end;
PTARGAHeader = ^TTARGAHeader;
Function Parse(buffer : puint8; len : uint32) : PTexture;
implementation
Function Parse(buffer : puint8; len : uint32) : PTexture;
var
header : PTARGAHeader;
i, j : uint32;
data : PTARGAColor;
tex : PTexture;
begin
if (len < sizeof(TTARGAHeader)) then exit;
header := PTARGAHeader(buffer);
if (header^.Magic <> $0) or (header^.ImageType <> 2) or (header^.PixelDepth <> 32) then exit;
//Create a new texture
tex:= texture.newTexture(header^.Width, header^.Height);
tex^.Width:= header^.Width;
tex^.Height:= header^.Height;
//Copy the data
data := PTARGAColor(header^.Data);
for i := 0 to header^.Height - 1 do begin
for j := 0 to header^.Width - 1 do begin
tex^.Pixels[i * header^.Width + j].r := data^.r;
tex^.Pixels[i * header^.Width + j].g := data^.g;
tex^.Pixels[i * header^.Width + j].b := data^.b;
tex^.Pixels[i * header^.Width + j].a := data^.a;
end;
end;
Parse := tex;
end;
end.

34
src/include/texture.pas Normal file
View File

@ -0,0 +1,34 @@
unit texture;
interface
uses
lmemorymanager, color;
type
TTexture = packed record
Width : uint32;
Height : uint32;
Size : uint32;
Pixels : PRGB32;
end;
PTexture = ^TTexture;
Function newTexture(width, height: uint32): PTexture;
implementation
Function newTexture(width, height: uint32): PTexture;
var
texture: PTexture;
begin
texture:= PTexture(kalloc(sizeof(TTexture)));
texture^.Pixels:= PRGB32(kalloc(width * height * sizeof(TRGB32)));
texture^.Width:= width;
texture^.Height:= height;
texture^.Size:= width * height;
newTexture:= texture;
end;
end.