| 1 |
{ |
| 2 |
Copyright 2001-2024 Michalis Kamburelis. |
| 3 |
|
| 4 |
This file is part of "Castle Game Engine". |
| 5 |
|
| 6 |
"Castle Game Engine" is free software; see the file COPYING.txt, |
| 7 |
included in this distribution, for details about the copyright. |
| 8 |
|
| 9 |
"Castle Game Engine" is distributed in the hope that it will be useful, |
| 10 |
but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
| 12 |
|
| 13 |
---------------------------------------------------------------------------- |
| 14 |
} |
| 15 |
|
| 16 |
{$ifdef read_interface} |
| 17 |
|
| 18 |
{ Image with high-precision RGB colors encoded as 3 floating-point Single values. } |
| 19 |
TRGBFloatImage = class(TCastleImage) |
| 20 |
private |
| 21 |
function GetPixels: PVector3; |
| 22 |
function GetPixelsArray: PVector3Array; |
| 23 |
{$ifdef FPC} |
| 24 |
procedure FromFpImage(const FPImage: TInternalCastleFpImage); override; |
| 25 |
{$endif} |
| 26 |
protected |
| 27 |
function GetColors(const X, Y, Z: Integer): TCastleColor; override; |
| 28 |
procedure SetColors(const X, Y, Z: Integer; const C: TCastleColor); override; |
| 29 |
public |
| 30 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector3. } |
| 31 |
property Pixels: PVector3 read GetPixels; |
| 32 |
property RGBFloatPixels: PVector3 read GetPixels; {$ifdef FPC} deprecated 'use Pixels'; {$endif} |
| 33 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector3Array. } |
| 34 |
property PixelsArray: PVector3Array read GetPixelsArray; |
| 35 |
|
| 36 |
class function PixelSize: Cardinal; override; |
| 37 |
class function ColorComponentsCount: Cardinal; override; |
| 38 |
|
| 39 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector3; |
| 40 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector3Array; |
| 41 |
|
| 42 |
procedure InvertColors; override; |
| 43 |
|
| 44 |
procedure Clear(const Pixel: TVector4Byte); overload; override; |
| 45 |
function IsClear(const Pixel: TVector4Byte): boolean; overload; override; |
| 46 |
|
| 47 |
procedure Clear(const Pixel: TVector3); reintroduce; overload; |
| 48 |
function IsClear(const Pixel: TVector3): boolean; reintroduce; overload; |
| 49 |
|
| 50 |
{ Converts TRGBFloatImage to TRGBImage. |
| 51 |
Colors in pixels are simply rounded using @link(Vector3Byte). |
| 52 |
So such conversion not only kills the floating-point |
| 53 |
precision in float format but also clamps color components |
| 54 |
to 0..1. } |
| 55 |
function ToRGBImage: TRGBImage; deprecated 'create TRGBImage and use TRGBImage.Assign'; |
| 56 |
{$ifdef FPC} |
| 57 |
function ToFpImage: TInternalCastleFpImage; override; |
| 58 |
{$endif} |
| 59 |
|
| 60 |
{ Every component (red, green, blue) of every pixel |
| 61 |
is multiplied by Scale. } |
| 62 |
procedure ScaleColors(const Scale: Single); |
| 63 |
|
| 64 |
{ Every component (red, green, blue) or every pixel |
| 65 |
is changed to Power(Value, Exp). |
| 66 |
So e.g. Exp = 1/2.2 gives commonly used gamma correction. } |
| 67 |
procedure ExpColors(const Exp: Single); |
| 68 |
|
| 69 |
procedure LerpWith(const Value: Single; SecondImage: TCastleImage); override; |
| 70 |
class procedure MixColors(const OutputColor: Pointer; |
| 71 |
const Weights: TVector4; const AColors: TVector4Pointer); override; |
| 72 |
procedure Assign(const Source: TCastleImage); override; |
| 73 |
end; |
| 74 |
|
| 75 |
{$endif read_interface} |
| 76 |
|
| 77 |
{$ifdef read_implementation} |
| 78 |
|
| 79 |
{ TRGBFloatImage ------------------------------------------------------------- } |
| 80 |
|
| 81 |
function TRGBFloatImage.GetPixels: PVector3; |
| 82 |
begin |
| 83 |
Result := PVector3(RawPixels); |
| 84 |
end; |
| 85 |
|
| 86 |
function TRGBFloatImage.GetPixelsArray: PVector3Array; |
| 87 |
begin |
| 88 |
Result := PVector3Array(RawPixels); |
| 89 |
end; |
| 90 |
|
| 91 |
class function TRGBFloatImage.PixelSize: Cardinal; |
| 92 |
begin |
| 93 |
Result := SizeOf(TVector3); |
| 94 |
end; |
| 95 |
|
| 96 |
class function TRGBFloatImage.ColorComponentsCount: Cardinal; |
| 97 |
begin |
| 98 |
Result := 3; |
| 99 |
end; |
| 100 |
|
| 101 |
function TRGBFloatImage.PixelPtr(const X, Y, Z: Cardinal): PVector3; |
| 102 |
begin |
| 103 |
Result := PVector3(inherited PixelPtr(X, Y, Z)); |
| 104 |
end; |
| 105 |
|
| 106 |
function TRGBFloatImage.RowPtr(const Y, Z: Cardinal): PVector3Array; |
| 107 |
begin |
| 108 |
Result := PVector3Array(inherited RowPtr(Y, Z)); |
| 109 |
end; |
| 110 |
|
| 111 |
function TRGBFloatImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 112 |
var |
| 113 |
Pixel: PVector3; |
| 114 |
begin |
| 115 |
Pixel := PixelPtr(X, Y, Z); |
| 116 |
Result.X := Pixel^.X; |
| 117 |
Result.Y := Pixel^.Y; |
| 118 |
Result.Z := Pixel^.Z; |
| 119 |
Result.W := 1.0; |
| 120 |
end; |
| 121 |
|
| 122 |
procedure TRGBFloatImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 123 |
var |
| 124 |
Pixel: PVector3; |
| 125 |
begin |
| 126 |
Pixel := PixelPtr(X, Y, Z); |
| 127 |
Pixel^.X := C.X; |
| 128 |
Pixel^.Y := C.Y; |
| 129 |
Pixel^.Z := C.Z; |
| 130 |
end; |
| 131 |
|
| 132 |
procedure TRGBFloatImage.Clear(const Pixel: TVector4Byte); |
| 133 |
begin |
| 134 |
Clear(Vector3( |
| 135 |
Pixel.X * 255, |
| 136 |
Pixel.Y * 255, |
| 137 |
Pixel.Z * 255)); |
| 138 |
end; |
| 139 |
|
| 140 |
function TRGBFloatImage.IsClear(const Pixel: TVector4Byte): boolean; |
| 141 |
begin |
| 142 |
Result := IsClear(Vector3( |
| 143 |
Pixel.X * 255, |
| 144 |
Pixel.Y * 255, |
| 145 |
Pixel.Z * 255)); |
| 146 |
end; |
| 147 |
|
| 148 |
procedure TRGBFloatImage.Clear(const Pixel: TVector3); |
| 149 |
var |
| 150 |
P: PVector3; |
| 151 |
I: Cardinal; |
| 152 |
begin |
| 153 |
P := Pixels; |
| 154 |
for I := 1 to Width * Height * Depth do |
| 155 |
begin |
| 156 |
Move(Pixel, P^, SizeOf(TVector3)); |
| 157 |
Inc(P); |
| 158 |
end; |
| 159 |
end; |
| 160 |
|
| 161 |
function TRGBFloatImage.IsClear(const Pixel: TVector3): boolean; |
| 162 |
var |
| 163 |
P: PVector3; |
| 164 |
I: Cardinal; |
| 165 |
begin |
| 166 |
P := Pixels; |
| 167 |
for I := 1 to Width * Height * Depth do |
| 168 |
begin |
| 169 |
if not CompareMem(@Pixel, P, SizeOf(TVector3)) then |
| 170 |
begin |
| 171 |
Result := false; |
| 172 |
Exit; |
| 173 |
end; |
| 174 |
Inc(P); |
| 175 |
end; |
| 176 |
Result := true; |
| 177 |
end; |
| 178 |
|
| 179 |
function TRGBFloatImage.ToRGBImage: TRGBImage; |
| 180 |
begin |
| 181 |
Result := TRGBImage.Create; |
| 182 |
Result.Assign(Self); |
| 183 |
end; |
| 184 |
|
| 185 |
procedure TRGBFloatImage.ScaleColors(const Scale: Single); |
| 186 |
var |
| 187 |
pFloat: PVector3; |
| 188 |
i: Cardinal; |
| 189 |
begin |
| 190 |
PFloat := Pixels; |
| 191 |
for i := 1 to Width * Height * Depth do |
| 192 |
begin |
| 193 |
PFloat^ := PFloat^ * Scale; |
| 194 |
Inc(PFloat); |
| 195 |
end; |
| 196 |
end; |
| 197 |
|
| 198 |
function VectorPowerComponents(const V: TVector3; const Exp: Single): TVector3; |
| 199 |
begin |
| 200 |
Result.X := Power(V.X, Exp); |
| 201 |
Result.Y := Power(V.Y, Exp); |
| 202 |
Result.Z := Power(V.Z, Exp); |
| 203 |
end; |
| 204 |
|
| 205 |
procedure TRGBFloatImage.ExpColors(const Exp: Single); |
| 206 |
var |
| 207 |
pFloat: PVector3; |
| 208 |
i: Cardinal; |
| 209 |
begin |
| 210 |
PFloat := Pixels; |
| 211 |
for i := 1 to Width * Height * Depth do |
| 212 |
begin |
| 213 |
PFloat^ := VectorPowerComponents(PFloat^, Exp); |
| 214 |
Inc(PFloat); |
| 215 |
end; |
| 216 |
end; |
| 217 |
|
| 218 |
procedure TRGBFloatImage.LerpWith(const Value: Single; SecondImage: TCastleImage); |
| 219 |
var |
| 220 |
SelfPtr: PVector3; |
| 221 |
SecondPtr: PVector3; |
| 222 |
I: Cardinal; |
| 223 |
begin |
| 224 |
LerpSimpleCheckConditions(SecondImage); |
| 225 |
|
| 226 |
SelfPtr := Pixels; |
| 227 |
SecondPtr := TRGBFloatImage(SecondImage).Pixels; |
| 228 |
for I := 1 to Width * Height * Depth do |
| 229 |
begin |
| 230 |
SelfPtr^ := Lerp(Value, SelfPtr^, SecondPtr^); |
| 231 |
Inc(SelfPtr); |
| 232 |
Inc(SecondPtr); |
| 233 |
end; |
| 234 |
end; |
| 235 |
|
| 236 |
class procedure TRGBFloatImage.MixColors(const OutputColor: Pointer; |
| 237 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 238 |
var |
| 239 |
OutputCol: PVector3 absolute OutputColor; |
| 240 |
Cols: array [0..3] of PVector3 absolute AColors; |
| 241 |
begin |
| 242 |
OutputCol^.X := |
| 243 |
Weights.X * Cols[0]^.X + |
| 244 |
Weights.Y * Cols[1]^.X + |
| 245 |
Weights.Z * Cols[2]^.X + |
| 246 |
Weights.W * Cols[3]^.X; |
| 247 |
OutputCol^.Y := |
| 248 |
Weights.X * Cols[0]^.Y + |
| 249 |
Weights.Y * Cols[1]^.Y + |
| 250 |
Weights.Z * Cols[2]^.Y + |
| 251 |
Weights.W * Cols[3]^.Y; |
| 252 |
OutputCol^.Z := |
| 253 |
Weights.X * Cols[0]^.Z + |
| 254 |
Weights.Y * Cols[1]^.Z + |
| 255 |
Weights.Z * Cols[2]^.Z + |
| 256 |
Weights.W * Cols[3]^.Z; |
| 257 |
end; |
| 258 |
|
| 259 |
procedure TRGBFloatImage.InvertColors; |
| 260 |
var |
| 261 |
I: Cardinal; |
| 262 |
P: PVector3; |
| 263 |
begin |
| 264 |
P := Pixels; |
| 265 |
for I := 1 to Width * Height * Depth do |
| 266 |
begin |
| 267 |
P^.X := Max(1-P^.X, 0.0); |
| 268 |
P^.Y := Max(1-P^.Y, 0.0); |
| 269 |
P^.Z := Max(1-P^.Z, 0.0); |
| 270 |
Inc(P); |
| 271 |
end; |
| 272 |
end; |
| 273 |
|
| 274 |
{$endif read_implementation} |