| 1 |
{%MainUnit castleimages.pas} |
| 2 |
{ |
| 3 |
Copyright 2024-2024 Michalis Kamburelis. |
| 4 |
|
| 5 |
This file is part of "Castle Game Engine". |
| 6 |
|
| 7 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 8 |
included in this distribution, for details about the copyright. |
| 9 |
|
| 10 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 11 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 13 |
|
| 14 |
---------------------------------------------------------------------------- |
| 15 |
} |
| 16 |
|
| 17 |
{$ifdef read_interface} |
| 18 |
|
| 19 |
{ Image with RGBA colors using Single (4 floating-point numbers for each pixel). |
| 20 |
For high-precision RGBA images. |
| 21 |
Compared to TRGBAlphaImage, each pixel is a floating-point number, |
| 22 |
which means it is more precise and can also be above 1.0 value |
| 23 |
(though they are still in 0..1 range when reading from normal image formats). } |
| 24 |
TRGBAlphaFloatImage = class(TCastleImage) |
| 25 |
private |
| 26 |
function GetPixels: PVector4; |
| 27 |
function GetPixelsArray: PVector4Array; |
| 28 |
protected |
| 29 |
function GetColors(const X, Y, Z: Integer): TCastleColor; override; |
| 30 |
procedure SetColors(const X, Y, Z: Integer; const C: TCastleColor); override; |
| 31 |
public |
| 32 |
class function PixelSize: Cardinal; override; |
| 33 |
class function ColorComponentsCount: Cardinal; override; |
| 34 |
procedure Assign(const Source: TCastleImage); override; |
| 35 |
procedure InvertColors; override; |
| 36 |
class procedure MixColors(const OutputColor: Pointer; |
| 37 |
const Weights: TVector4; const AColors: TVector4Pointer); override; |
| 38 |
|
| 39 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector4. } |
| 40 |
property Pixels: PVector4 read GetPixels; |
| 41 |
|
| 42 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector4Array. } |
| 43 |
property PixelsArray: PVector4Array read GetPixelsArray; |
| 44 |
|
| 45 |
{ Pointer to given pixel. } |
| 46 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector4; |
| 47 |
|
| 48 |
{ Pointer to given row. } |
| 49 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector4Array; |
| 50 |
end; |
| 51 |
|
| 52 |
{$endif read_interface} |
| 53 |
|
| 54 |
{$ifdef read_implementation} |
| 55 |
|
| 56 |
{ TRGBAlphaFloatImage ------------------------------------------------------------ } |
| 57 |
|
| 58 |
function TRGBAlphaFloatImage.GetPixels: PVector4; |
| 59 |
begin |
| 60 |
Result := PVector4(RawPixels); |
| 61 |
end; |
| 62 |
|
| 63 |
function TRGBAlphaFloatImage.GetPixelsArray: PVector4Array; |
| 64 |
begin |
| 65 |
Result := PVector4Array(RawPixels); |
| 66 |
end; |
| 67 |
|
| 68 |
class function TRGBAlphaFloatImage.PixelSize: Cardinal; |
| 69 |
begin |
| 70 |
Result := SizeOf(TVector4); |
| 71 |
end; |
| 72 |
|
| 73 |
class function TRGBAlphaFloatImage.ColorComponentsCount: Cardinal; |
| 74 |
begin |
| 75 |
Result := 4; |
| 76 |
end; |
| 77 |
|
| 78 |
function TRGBAlphaFloatImage.PixelPtr(const X, Y, Z: Cardinal): PVector4; |
| 79 |
begin |
| 80 |
Result := PVector4(inherited PixelPtr(X, Y, Z)); |
| 81 |
end; |
| 82 |
|
| 83 |
function TRGBAlphaFloatImage.RowPtr(const Y, Z: Cardinal): PVector4Array; |
| 84 |
begin |
| 85 |
Result := PVector4Array(inherited RowPtr(Y, Z)); |
| 86 |
end; |
| 87 |
|
| 88 |
procedure TRGBAlphaFloatImage.InvertColors; |
| 89 |
var |
| 90 |
I: Cardinal; |
| 91 |
P: PVector4; |
| 92 |
begin |
| 93 |
P := Pixels; |
| 94 |
for I := 1 to Width * Height * Depth do |
| 95 |
begin |
| 96 |
P^.X := Max(1.0 - P^.X, 0.0); // make sure result is >= 0, even when input > 1.0 |
| 97 |
P^.Y := Max(1.0 - P^.Y, 0.0); |
| 98 |
P^.Z := Max(1.0 - P^.Z, 0.0); |
| 99 |
// P^.W := Max(1.0 - P^.W, 0.0); // don't touch alpha |
| 100 |
Inc(P); |
| 101 |
end; |
| 102 |
end; |
| 103 |
|
| 104 |
function TRGBAlphaFloatImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 105 |
var |
| 106 |
Pixel: PVector4; |
| 107 |
begin |
| 108 |
Pixel := PixelPtr(X, Y, Z); |
| 109 |
Result := Pixel^; |
| 110 |
end; |
| 111 |
|
| 112 |
procedure TRGBAlphaFloatImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 113 |
var |
| 114 |
Pixel: PVector4; |
| 115 |
begin |
| 116 |
Pixel := PixelPtr(X, Y, Z); |
| 117 |
Pixel^ := C; |
| 118 |
end; |
| 119 |
|
| 120 |
class procedure TRGBAlphaFloatImage.MixColors(const OutputColor: Pointer; |
| 121 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 122 |
var |
| 123 |
OutputCol: PVector4 absolute OutputColor; |
| 124 |
Cols: array [0..3] of PVector4 absolute AColors; |
| 125 |
begin |
| 126 |
OutputCol^.X := |
| 127 |
Weights.X * Cols[0]^.X + |
| 128 |
Weights.Y * Cols[1]^.X + |
| 129 |
Weights.Z * Cols[2]^.X + |
| 130 |
Weights.W * Cols[3]^.X; |
| 131 |
OutputCol^.Y := |
| 132 |
Weights.X * Cols[0]^.Y + |
| 133 |
Weights.Y * Cols[1]^.Y + |
| 134 |
Weights.Z * Cols[2]^.Y + |
| 135 |
Weights.W * Cols[3]^.Y; |
| 136 |
OutputCol^.Z := |
| 137 |
Weights.X * Cols[0]^.Z + |
| 138 |
Weights.Y * Cols[1]^.Z + |
| 139 |
Weights.Z * Cols[2]^.Z + |
| 140 |
Weights.W * Cols[3]^.Z; |
| 141 |
OutputCol^.W := |
| 142 |
Weights.X * Cols[0]^.W + |
| 143 |
Weights.Y * Cols[1]^.W + |
| 144 |
Weights.Z * Cols[2]^.W + |
| 145 |
Weights.W * Cols[3]^.W; |
| 146 |
end; |
| 147 |
|
| 148 |
{$endif read_implementation} |