| 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 each pixel represented as 2 floating-point numbers (Single): |
| 20 |
grayscale (luminance) and alpha (opacity). |
| 21 |
|
| 22 |
Compared to TGrayscaleAlphaImage, each pixel is a floating-point number, |
| 23 |
which means it is more precise and can also be above 1.0 value. } |
| 24 |
TGrayscaleAlphaFloatImage = class(TCastleImage) |
| 25 |
private |
| 26 |
function GetPixels: PVector2; |
| 27 |
function GetPixelsArray: PVector2Array; |
| 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 PVector2. } |
| 40 |
property Pixels: PVector2 read GetPixels; |
| 41 |
|
| 42 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector2Array. } |
| 43 |
property PixelsArray: PVector2Array read GetPixelsArray; |
| 44 |
|
| 45 |
{ Pointer to given pixel. } |
| 46 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector2; |
| 47 |
|
| 48 |
{ Pointer to given row. } |
| 49 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector2Array; |
| 50 |
end; |
| 51 |
|
| 52 |
{$endif read_interface} |
| 53 |
|
| 54 |
{$ifdef read_implementation} |
| 55 |
|
| 56 |
{ TGrayscaleAlphaFloatImage ------------------------------------------------------------ } |
| 57 |
|
| 58 |
function TGrayscaleAlphaFloatImage.GetPixels: PVector2; |
| 59 |
begin |
| 60 |
Result := PVector2(RawPixels); |
| 61 |
end; |
| 62 |
|
| 63 |
function TGrayscaleAlphaFloatImage.GetPixelsArray: PVector2Array; |
| 64 |
begin |
| 65 |
Result := PVector2Array(RawPixels); |
| 66 |
end; |
| 67 |
|
| 68 |
class function TGrayscaleAlphaFloatImage.PixelSize: Cardinal; |
| 69 |
begin |
| 70 |
Result := SizeOf(TVector2); |
| 71 |
end; |
| 72 |
|
| 73 |
class function TGrayscaleAlphaFloatImage.ColorComponentsCount: Cardinal; |
| 74 |
begin |
| 75 |
Result := 2; |
| 76 |
end; |
| 77 |
|
| 78 |
function TGrayscaleAlphaFloatImage.PixelPtr(const X, Y, Z: Cardinal): PVector2; |
| 79 |
begin |
| 80 |
Result := PVector2(inherited PixelPtr(X, Y, Z)); |
| 81 |
end; |
| 82 |
|
| 83 |
function TGrayscaleAlphaFloatImage.RowPtr(const Y, Z: Cardinal): PVector2Array; |
| 84 |
begin |
| 85 |
Result := PVector2Array(inherited RowPtr(Y, Z)); |
| 86 |
end; |
| 87 |
|
| 88 |
procedure TGrayscaleAlphaFloatImage.InvertColors; |
| 89 |
var |
| 90 |
I: Cardinal; |
| 91 |
P: PVector2; |
| 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 |
// do not touch alpha in P.Y |
| 98 |
Inc(P); |
| 99 |
end; |
| 100 |
end; |
| 101 |
|
| 102 |
function TGrayscaleAlphaFloatImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 103 |
var |
| 104 |
Pixel: PVector2; |
| 105 |
begin |
| 106 |
Pixel := PixelPtr(X, Y, Z); |
| 107 |
Result.X := Pixel^.X; |
| 108 |
Result.Y := Pixel^.X; |
| 109 |
Result.Z := Pixel^.X; |
| 110 |
Result.W := Pixel^.Y; |
| 111 |
end; |
| 112 |
|
| 113 |
procedure TGrayscaleAlphaFloatImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 114 |
var |
| 115 |
Pixel: PVector2; |
| 116 |
begin |
| 117 |
Pixel := PixelPtr(X, Y, Z); |
| 118 |
Pixel^.X := GrayscaleValue(C); |
| 119 |
Pixel^.Y := C.W; |
| 120 |
end; |
| 121 |
|
| 122 |
class procedure TGrayscaleAlphaFloatImage.MixColors(const OutputColor: Pointer; |
| 123 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 124 |
var |
| 125 |
OutputCol: PVector2 absolute OutputColor; |
| 126 |
Cols: array [0..3] of PVector2 absolute AColors; |
| 127 |
begin |
| 128 |
OutputCol^.X := |
| 129 |
Weights.X * Cols[0]^.X + |
| 130 |
Weights.Y * Cols[1]^.X + |
| 131 |
Weights.Z * Cols[2]^.X + |
| 132 |
Weights.W * Cols[3]^.X; |
| 133 |
OutputCol^.Y := |
| 134 |
Weights.X * Cols[0]^.Y + |
| 135 |
Weights.Y * Cols[1]^.Y + |
| 136 |
Weights.Z * Cols[2]^.Y + |
| 137 |
Weights.W * Cols[3]^.Y; |
| 138 |
end; |
| 139 |
|
| 140 |
{$endif read_implementation} |