| 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 pixel represented as a TVector3Byte (red, green, blue). } |
| 19 |
TRGBImage = class(TCastleImage) |
| 20 |
private |
| 21 |
function GetPixels: PVector3Byte; |
| 22 |
function GetPixelsArray: PVector3ByteArray; |
| 23 |
{$ifdef FPC} |
| 24 |
procedure FromFpImage(const FPImage: TInternalCastleFpImage); override; |
| 25 |
{$endif FPC} |
| 26 |
protected |
| 27 |
procedure DrawFromCore(Source: TCastleImage; |
| 28 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 29 |
const Mode: TDrawMode); override; |
| 30 |
function GetColors(const X, Y, Z: Integer): TCastleColor; override; |
| 31 |
procedure SetColors(const X, Y, Z: Integer; const C: TCastleColor); override; |
| 32 |
public |
| 33 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector3Byte. } |
| 34 |
property Pixels: PVector3Byte read GetPixels; |
| 35 |
property RGBPixels: PVector3Byte read GetPixels; {$ifdef FPC} deprecated 'use Pixels'; {$endif} |
| 36 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector3ByteArray. } |
| 37 |
property PixelsArray: PVector3ByteArray read GetPixelsArray; |
| 38 |
|
| 39 |
class function PixelSize: Cardinal; override; |
| 40 |
class function ColorComponentsCount: Cardinal; override; |
| 41 |
|
| 42 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector3Byte; |
| 43 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector3ByteArray; |
| 44 |
|
| 45 |
procedure InvertColors; override; |
| 46 |
|
| 47 |
procedure Clear(const Pixel: TVector4Byte); override; |
| 48 |
function IsClear(const Pixel: TVector4Byte): boolean; override; |
| 49 |
|
| 50 |
procedure TransformRGB(const Matrix: TMatrix3); override; |
| 51 |
procedure ModulateRGB(const ColorModulator: TColorModulatorByteFunc); override; |
| 52 |
|
| 53 |
{ Create a new TRGBAlphaImage instance with RGB contents copied from this |
| 54 |
image, and alpha fully opaque. } |
| 55 |
function ToRGBAlphaImage: TRGBAlphaImage; deprecated 'create TRGBAlphaImage and use TRGBAlphaImage.Assign'; |
| 56 |
|
| 57 |
{ Convert image to an TRGBFloatImage format. |
| 58 |
|
| 59 |
Generally this conversion is safe, since Single can express |
| 60 |
more-or-less precisely (and definitely uniquely) all values that Byte can express. |
| 61 |
(Yeah, we have autotest for this statement.) } |
| 62 |
function ToRGBFloat: TRGBFloatImage; deprecated 'create TRGBFloatImage and use TRGBFloatImage.Assign'; |
| 63 |
|
| 64 |
function ToGrayscale: TGrayscaleImage; deprecated 'create TGrayscaleImage and use TGrayscaleImage.Assign'; |
| 65 |
|
| 66 |
{$ifdef FPC} |
| 67 |
function ToFpImage: TInternalCastleFpImage; override; |
| 68 |
{$endif FPC} |
| 69 |
|
| 70 |
{ Draw horizontal line. Must be y1 <= y2, else it is NOOP. } |
| 71 |
procedure HorizontalLine(const x1, x2, y: Integer; |
| 72 |
const Color: TVector3Byte); overload; |
| 73 |
procedure HorizontalLine(const x1, x2, y: Integer; |
| 74 |
const Color: TCastleColor); overload; |
| 75 |
|
| 76 |
{ Draw vertical line. Must be x1 <= x2, else it is NOOP. } |
| 77 |
procedure VerticalLine(const x, y1, y2: Integer; |
| 78 |
const Color: TVector3Byte); overload; |
| 79 |
procedure VerticalLine(const x, y1, y2: Integer; |
| 80 |
const Color: TCastleColor); overload; |
| 81 |
|
| 82 |
{ Create image by merging two images according to a (third) mask image. |
| 83 |
This is a very special constructor. |
| 84 |
It creates image with the same size as MapImage. |
| 85 |
It also resizes ReplaceWhiteImage, ReplaceBlackImage |
| 86 |
to the size of MapImage. |
| 87 |
|
| 88 |
Then it inits color of each pixel of our image with |
| 89 |
combined colors of two pixels on the same coordinates from |
| 90 |
ReplaceWhiteImage, ReplaceBlackImage, something like |
| 91 |
|
| 92 |
@preformatted( |
| 93 |
Pixel[x, y] := ReplaceWhiteImage[x, y] * S + |
| 94 |
ReplaceBlackImage[x, y] * (S-1); |
| 95 |
) |
| 96 |
|
| 97 |
where S = average of red, gree, blue of color MapImage[x, y]. |
| 98 |
|
| 99 |
This means that final image will look like ReplaceWhiteImage |
| 100 |
in the areas where MapImage is white, and it will look like |
| 101 |
ReplaceBlackImage in the areas where MapImage is black. } |
| 102 |
constructor CreateCombined(const MapImage: TRGBImage; |
| 103 |
var ReplaceWhiteImage, ReplaceBlackImage: TRGBImage); |
| 104 |
|
| 105 |
procedure LerpWith(const Value: Single; SecondImage: TCastleImage); override; |
| 106 |
class procedure MixColors(const OutputColor: Pointer; |
| 107 |
const Weights: TVector4; const AColors: TVector4Pointer); override; |
| 108 |
|
| 109 |
procedure Assign(const Source: TCastleImage); override; |
| 110 |
|
| 111 |
public |
| 112 |
procedure FillEllipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 113 |
const aColor: TCastleColor); override; |
| 114 |
procedure Ellipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 115 |
const aWidth: single; const aColor: TCastleColor); override; |
| 116 |
procedure FillRectangle(const x1, y1, x2, y2: single; |
| 117 |
const aColor: TCastleColor); override; |
| 118 |
procedure Rectangle(const x1, y1, x2, y2: single; |
| 119 |
const aWidth: single; const aColor: TCastleColor); override; |
| 120 |
procedure Line(const x1, y1, x2, y2: single; |
| 121 |
const aWidth: single; const aColor: TCastleColor); override; |
| 122 |
end; |
| 123 |
|
| 124 |
{$endif read_interface} |
| 125 |
|
| 126 |
{$ifdef read_implementation} |
| 127 |
|
| 128 |
{ TRGBImage ------------------------------------------------------------ } |
| 129 |
|
| 130 |
constructor TRGBImage.CreateCombined(const MapImage: TRGBImage; |
| 131 |
var ReplaceWhiteImage, ReplaceBlackImage: TRGBImage); |
| 132 |
var |
| 133 |
Map, White, Black, Res: PVector3Byte; |
| 134 |
s: single; |
| 135 |
i: integer; |
| 136 |
begin |
| 137 |
Create(MapImage.Width, MapImage.Height); |
| 138 |
|
| 139 |
ReplaceWhiteImage.Resize(MapImage.Width, MapImage.Height); |
| 140 |
ReplaceBlackImage.Resize(MapImage.Width, MapImage.Height); |
| 141 |
|
| 142 |
Map := MapImage.Pixels; |
| 143 |
White := ReplaceWhiteImage.Pixels; |
| 144 |
Black := ReplaceBlackImage.Pixels; |
| 145 |
Res := Pixels; |
| 146 |
|
| 147 |
for i := 1 to Width * Height * Depth do |
| 148 |
begin |
| 149 |
s := (Map^.X + Map^.Y + Map^.Z) / 255 / 3; |
| 150 |
Res^.X := Round(s * White^.X + (1-s) * Black^.X); |
| 151 |
Res^.Y := Round(s * White^.Y + (1-s) * Black^.Y); |
| 152 |
Res^.Z := Round(s * White^.Z + (1-s) * Black^.Z); |
| 153 |
Inc(Map); |
| 154 |
Inc(White); |
| 155 |
Inc(Black); |
| 156 |
Inc(Res); |
| 157 |
end; |
| 158 |
end; |
| 159 |
|
| 160 |
function TRGBImage.GetPixels: PVector3Byte; |
| 161 |
begin |
| 162 |
Result := PVector3Byte(RawPixels); |
| 163 |
end; |
| 164 |
|
| 165 |
function TRGBImage.GetPixelsArray: PVector3ByteArray; |
| 166 |
begin |
| 167 |
Result := PVector3ByteArray(RawPixels); |
| 168 |
end; |
| 169 |
|
| 170 |
class function TRGBImage.PixelSize: Cardinal; |
| 171 |
begin |
| 172 |
Result := 3; |
| 173 |
end; |
| 174 |
|
| 175 |
class function TRGBImage.ColorComponentsCount: Cardinal; |
| 176 |
begin |
| 177 |
Result := 3; |
| 178 |
end; |
| 179 |
|
| 180 |
function TRGBImage.PixelPtr(const X, Y, Z: Cardinal): PVector3Byte; |
| 181 |
begin |
| 182 |
Result := PVector3Byte(inherited PixelPtr(X, Y, Z)); |
| 183 |
end; |
| 184 |
|
| 185 |
function TRGBImage.RowPtr(const Y, Z: Cardinal): PVector3ByteArray; |
| 186 |
begin |
| 187 |
Result := PVector3ByteArray(inherited RowPtr(Y, Z)); |
| 188 |
end; |
| 189 |
|
| 190 |
procedure TRGBImage.InvertColors; |
| 191 |
var |
| 192 |
i: Cardinal; |
| 193 |
prgb: PVector3Byte; |
| 194 |
begin |
| 195 |
prgb := Pixels; |
| 196 |
for i := 1 to Width * Height * Depth do |
| 197 |
begin |
| 198 |
prgb^.X := High(byte) - prgb^.X; |
| 199 |
prgb^.Y := High(byte) - prgb^.Y; |
| 200 |
prgb^.Z := High(byte) - prgb^.Z; |
| 201 |
Inc(prgb); |
| 202 |
end; |
| 203 |
end; |
| 204 |
|
| 205 |
function TRGBImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 206 |
var |
| 207 |
Pixel: PVector3Byte; |
| 208 |
begin |
| 209 |
Pixel := PixelPtr(X, Y, Z); |
| 210 |
Result.X := Pixel^.X / 255; |
| 211 |
Result.Y := Pixel^.Y / 255; |
| 212 |
Result.Z := Pixel^.Z / 255; |
| 213 |
Result.W := 1.0; |
| 214 |
end; |
| 215 |
|
| 216 |
procedure TRGBImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 217 |
var |
| 218 |
Pixel: PVector3Byte; |
| 219 |
begin |
| 220 |
Pixel := PixelPtr(X, Y, Z); |
| 221 |
Pixel^.X := Clamped(Round(C.X * 255), Low(Byte), High(Byte)); |
| 222 |
Pixel^.Y := Clamped(Round(C.Y * 255), Low(Byte), High(Byte)); |
| 223 |
Pixel^.Z := Clamped(Round(C.Z * 255), Low(Byte), High(Byte)); |
| 224 |
end; |
| 225 |
|
| 226 |
procedure TRGBImage.Clear(const Pixel: TVector4Byte); |
| 227 |
var |
| 228 |
P: PVector3Byte; |
| 229 |
I: Cardinal; |
| 230 |
begin |
| 231 |
P := Pixels; |
| 232 |
for I := 1 to Width * Height * Depth do |
| 233 |
begin |
| 234 |
Move(Pixel, P^, SizeOf(TVector3Byte)); |
| 235 |
Inc(P); |
| 236 |
end; |
| 237 |
end; |
| 238 |
|
| 239 |
function TRGBImage.IsClear(const Pixel: TVector4Byte): boolean; |
| 240 |
var |
| 241 |
P: PVector3Byte; |
| 242 |
I: Cardinal; |
| 243 |
begin |
| 244 |
P := Pixels; |
| 245 |
for I := 1 to Width * Height * Depth do |
| 246 |
begin |
| 247 |
if not CompareMem(@Pixel, P, SizeOf(TVector3Byte)) then |
| 248 |
begin |
| 249 |
Result := false; |
| 250 |
Exit; |
| 251 |
end; |
| 252 |
Inc(P); |
| 253 |
end; |
| 254 |
Result := true; |
| 255 |
end; |
| 256 |
|
| 257 |
procedure TRGBImage.TransformRGB(const Matrix: TMatrix3); |
| 258 |
type PPixel = PVector3Byte; |
| 259 |
{$I castleimages_transformrgb_implement.inc} |
| 260 |
|
| 261 |
procedure TRGBImage.ModulateRGB(const ColorModulator: TColorModulatorByteFunc); |
| 262 |
type PPixel = PVector3Byte; |
| 263 |
{$I castleimages_modulatergb_implement.inc} |
| 264 |
|
| 265 |
function TRGBImage.ToRGBAlphaImage: TRGBAlphaImage; |
| 266 |
begin |
| 267 |
Result := TRGBAlphaImage.Create; |
| 268 |
Result.Assign(Self); |
| 269 |
end; |
| 270 |
|
| 271 |
function TRGBImage.ToRGBFloat: TRGBFloatImage; |
| 272 |
begin |
| 273 |
Result := TRGBFloatImage.Create; |
| 274 |
Result.Assign(Self); |
| 275 |
end; |
| 276 |
|
| 277 |
function TRGBImage.ToGrayscale: TGrayscaleImage; |
| 278 |
begin |
| 279 |
Result := TGrayscaleImage.Create; |
| 280 |
Result.Assign(Self); |
| 281 |
end; |
| 282 |
|
| 283 |
procedure TRGBImage.HorizontalLine(const x1, x2, y: Integer; |
| 284 |
const Color: TVector3Byte); |
| 285 |
var |
| 286 |
P: PVector3Byte; |
| 287 |
i: Integer; |
| 288 |
begin |
| 289 |
P := PixelPtr(x1, y); |
| 290 |
for i := 0 to x2 - x1 do begin P^ := Color; Inc(P) end; |
| 291 |
end; |
| 292 |
|
| 293 |
procedure TRGBImage.HorizontalLine(const X1, X2, Y: Integer; |
| 294 |
const Color: TCastleColor); |
| 295 |
begin |
| 296 |
HorizontalLine(X1, X2, Y, Vector3Byte(Color.XYZ)); |
| 297 |
end; |
| 298 |
|
| 299 |
procedure TRGBImage.VerticalLine(const x, y1, y2: Integer; |
| 300 |
const Color: TVector3Byte); |
| 301 |
var P: PVector3Byte; |
| 302 |
i: Integer; |
| 303 |
begin |
| 304 |
P := PixelPtr(x, y1); |
| 305 |
for i := 0 to y2 - y1 do |
| 306 |
begin |
| 307 |
P^ := Color; |
| 308 |
P := PointerAdd(P, SizeOf(TVector3Byte) * Width); |
| 309 |
end; |
| 310 |
end; |
| 311 |
|
| 312 |
procedure TRGBImage.VerticalLine(const x, y1, y2: Integer; |
| 313 |
const Color: TCastleColor); |
| 314 |
begin |
| 315 |
VerticalLine(X, Y1, Y2, Vector3Byte(Color.XYZ)); |
| 316 |
end; |
| 317 |
|
| 318 |
procedure TRGBImage.LerpWith(const Value: Single; SecondImage: TCastleImage); |
| 319 |
var |
| 320 |
SelfPtr: PVector3Byte; |
| 321 |
SecondPtr: PVector3Byte; |
| 322 |
I: Cardinal; |
| 323 |
begin |
| 324 |
LerpSimpleCheckConditions(SecondImage); |
| 325 |
|
| 326 |
SelfPtr := Pixels; |
| 327 |
SecondPtr := TRGBImage(SecondImage).Pixels; |
| 328 |
for I := 1 to Width * Height * Depth do |
| 329 |
begin |
| 330 |
SelfPtr^ := Lerp(Value, SelfPtr^, SecondPtr^); |
| 331 |
Inc(SelfPtr); |
| 332 |
Inc(SecondPtr); |
| 333 |
end; |
| 334 |
end; |
| 335 |
|
| 336 |
{ $define FAST_UNSAFE_MIX_COLORS} |
| 337 |
|
| 338 |
class procedure TRGBImage.MixColors(const OutputColor: Pointer; |
| 339 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 340 |
var |
| 341 |
OutputCol: PVector3Byte absolute OutputColor; |
| 342 |
Cols: array [0..3] of PVector3Byte absolute AColors; |
| 343 |
begin |
| 344 |
{$I norqcheckbegin.inc} |
| 345 |
OutputCol^.X := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 346 |
Weights.X * Cols[0]^.X + |
| 347 |
Weights.Y * Cols[1]^.X + |
| 348 |
Weights.Z * Cols[2]^.X + |
| 349 |
Weights.W * Cols[3]^.X) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 350 |
OutputCol^.Y := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 351 |
Weights.X * Cols[0]^.Y + |
| 352 |
Weights.Y * Cols[1]^.Y + |
| 353 |
Weights.Z * Cols[2]^.Y + |
| 354 |
Weights.W * Cols[3]^.Y) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 355 |
OutputCol^.Z := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 356 |
Weights.X * Cols[0]^.Z + |
| 357 |
Weights.Y * Cols[1]^.Z + |
| 358 |
Weights.Z * Cols[2]^.Z + |
| 359 |
Weights.W * Cols[3]^.Z) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 360 |
{$I norqcheckend.inc} |
| 361 |
end; |
| 362 |
|
| 363 |
{$endif read_implementation} |