| 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 |
{ Grayscale image with an alpha channel. |
| 19 |
Each pixel is two bytes: grayscale + alpha. } |
| 20 |
TGrayscaleAlphaImage = class(TCastleImage) |
| 21 |
private |
| 22 |
function GetPixels: PVector2Byte; |
| 23 |
function GetPixelsArray: PVector2ByteArray; |
| 24 |
{$ifdef FPC} |
| 25 |
procedure FromFpImage(const FPImage: TInternalCastleFpImage); override; |
| 26 |
{$endif} |
| 27 |
protected |
| 28 |
procedure DrawFromCore(Source: TCastleImage; |
| 29 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 30 |
const Mode: TDrawMode); override; |
| 31 |
function GetColors(const X, Y, Z: Integer): TCastleColor; override; |
| 32 |
procedure SetColors(const X, Y, Z: Integer; const C: TCastleColor); override; |
| 33 |
public |
| 34 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector2Byte. } |
| 35 |
property Pixels: PVector2Byte read GetPixels; |
| 36 |
property GrayscaleAlphaPixels: PVector2Byte read GetPixels; {$ifdef FPC}deprecated 'use Pixels'; {$endif} |
| 37 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PVector2ByteArray. } |
| 38 |
property PixelsArray: PVector2ByteArray read GetPixelsArray; |
| 39 |
|
| 40 |
class function PixelSize: Cardinal; override; |
| 41 |
class function ColorComponentsCount: Cardinal; override; |
| 42 |
|
| 43 |
{ Remove alpha channel. } |
| 44 |
function ToGrayscaleImage: TGrayscaleImage; deprecated 'create TGrayscaleImage and use TGrayscaleImage.Assign'; |
| 45 |
|
| 46 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector2Byte; |
| 47 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector2ByteArray; |
| 48 |
|
| 49 |
procedure InvertColors; override; |
| 50 |
|
| 51 |
procedure Clear(const Pixel: TVector4Byte); override; |
| 52 |
function IsClear(const Pixel: TVector4Byte): boolean; override; |
| 53 |
|
| 54 |
procedure Clear(const Pixel: TVector2Byte); reintroduce; overload; |
| 55 |
function IsClear(const Pixel: TVector2Byte): boolean; reintroduce; overload; |
| 56 |
|
| 57 |
function HasAlpha: boolean; override; |
| 58 |
|
| 59 |
function AlphaChannel( |
| 60 |
const AlphaTolerance: Byte): TAlphaChannel; override; |
| 61 |
|
| 62 |
{$ifdef FPC} |
| 63 |
function ToFpImage: TInternalCastleFpImage; override; |
| 64 |
{$endif} |
| 65 |
|
| 66 |
procedure LerpWith(const Value: Single; SecondImage: TCastleImage); override; |
| 67 |
class procedure MixColors(const OutputColor: Pointer; |
| 68 |
const Weights: TVector4; const AColors: TVector4Pointer); override; |
| 69 |
|
| 70 |
procedure Assign(const Source: TCastleImage); override; |
| 71 |
|
| 72 |
// TODO: this should be implemented, just like for TRGBAlphaImage |
| 73 |
//procedure AlphaBleed(const ProgressTitle: string = ''); override; |
| 74 |
//function MakeAlphaBleed(const ProgressTitle: string = ''): TCastleImage; override; |
| 75 |
public |
| 76 |
procedure FillEllipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 77 |
const aColor: TCastleColor); override; |
| 78 |
procedure Ellipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 79 |
const aWidth: single; const aColor: TCastleColor); override; |
| 80 |
procedure FillRectangle(const x1, y1, x2, y2: single; |
| 81 |
const aColor: TCastleColor); override; |
| 82 |
procedure Rectangle(const x1, y1, x2, y2: single; |
| 83 |
const aWidth: single; const aColor: TCastleColor); override; |
| 84 |
procedure Line(const x1, y1, x2, y2: single; |
| 85 |
const aWidth: single; const aColor: TCastleColor); override; |
| 86 |
end; |
| 87 |
|
| 88 |
{$endif read_interface} |
| 89 |
|
| 90 |
{$ifdef read_implementation} |
| 91 |
|
| 92 |
{ TGrayscaleAlphaImage ------------------------------------------------------------ } |
| 93 |
|
| 94 |
function TGrayscaleAlphaImage.GetPixels: PVector2Byte; |
| 95 |
begin |
| 96 |
Result := PVector2Byte(RawPixels); |
| 97 |
end; |
| 98 |
|
| 99 |
function TGrayscaleAlphaImage.GetPixelsArray: PVector2ByteArray; |
| 100 |
begin |
| 101 |
Result := PVector2ByteArray(RawPixels); |
| 102 |
end; |
| 103 |
|
| 104 |
class function TGrayscaleAlphaImage.PixelSize: Cardinal; |
| 105 |
begin |
| 106 |
Result := 2; |
| 107 |
end; |
| 108 |
|
| 109 |
class function TGrayscaleAlphaImage.ColorComponentsCount: Cardinal; |
| 110 |
begin |
| 111 |
Result := 2; |
| 112 |
end; |
| 113 |
|
| 114 |
function TGrayscaleAlphaImage.PixelPtr(const X, Y, Z: Cardinal): PVector2Byte; |
| 115 |
begin |
| 116 |
Result := PVector2Byte(inherited PixelPtr(X, Y, Z)); |
| 117 |
end; |
| 118 |
|
| 119 |
function TGrayscaleAlphaImage.RowPtr(const Y, Z: Cardinal): PVector2ByteArray; |
| 120 |
begin |
| 121 |
Result := PVector2ByteArray(inherited RowPtr(Y, Z)); |
| 122 |
end; |
| 123 |
|
| 124 |
procedure TGrayscaleAlphaImage.Clear(const Pixel: TVector4Byte); |
| 125 |
begin |
| 126 |
Clear(Vector2Byte(GrayscaleValue(Pixel), Pixel.W)); |
| 127 |
end; |
| 128 |
|
| 129 |
function TGrayscaleAlphaImage.IsClear(const Pixel: TVector4Byte): boolean; |
| 130 |
begin |
| 131 |
Result := IsClear(Vector2Byte(GrayscaleValue(Pixel), Pixel.W)); |
| 132 |
end; |
| 133 |
|
| 134 |
procedure TGrayscaleAlphaImage.Clear(const Pixel: TVector2Byte); |
| 135 |
var |
| 136 |
P: PVector2Byte; |
| 137 |
I: Cardinal; |
| 138 |
begin |
| 139 |
P := Pixels; |
| 140 |
for I := 1 to Width * Height * Depth do |
| 141 |
begin |
| 142 |
Move(Pixel, P^, SizeOf(Pixel)); |
| 143 |
Inc(P); |
| 144 |
end; |
| 145 |
end; |
| 146 |
|
| 147 |
function TGrayscaleAlphaImage.IsClear(const Pixel: TVector2Byte): boolean; |
| 148 |
var |
| 149 |
P: PVector2Byte; |
| 150 |
I: Cardinal; |
| 151 |
begin |
| 152 |
P := Pixels; |
| 153 |
for I := 1 to Width * Height * Depth do |
| 154 |
begin |
| 155 |
if not CompareMem(@Pixel, P, SizeOf(Pixel)) then |
| 156 |
begin |
| 157 |
Result := false; |
| 158 |
Exit; |
| 159 |
end; |
| 160 |
Inc(P); |
| 161 |
end; |
| 162 |
Result := true; |
| 163 |
end; |
| 164 |
|
| 165 |
function TGrayscaleAlphaImage.HasAlpha: boolean; |
| 166 |
begin |
| 167 |
Result := true; |
| 168 |
end; |
| 169 |
|
| 170 |
function TGrayscaleAlphaImage.AlphaChannel( |
| 171 |
const AlphaTolerance: Byte): TAlphaChannel; |
| 172 |
var |
| 173 |
PtrAlpha: PVector2Byte; |
| 174 |
I: Cardinal; |
| 175 |
begin |
| 176 |
PtrAlpha := Pixels; |
| 177 |
|
| 178 |
for I := 1 to Width * Height * Depth do |
| 179 |
begin |
| 180 |
if (PtrAlpha^.Y > AlphaTolerance) and |
| 181 |
(PtrAlpha^.Y < 255 - AlphaTolerance) then |
| 182 |
Exit(acBlending); |
| 183 |
Inc(PtrAlpha); |
| 184 |
end; |
| 185 |
|
| 186 |
Result := acTest; |
| 187 |
end; |
| 188 |
|
| 189 |
procedure TGrayscaleAlphaImage.LerpWith(const Value: Single; SecondImage: TCastleImage); |
| 190 |
var |
| 191 |
SelfPtr: PVector2Byte; |
| 192 |
SecondPtr: PVector2Byte; |
| 193 |
I: Cardinal; |
| 194 |
begin |
| 195 |
LerpSimpleCheckConditions(SecondImage); |
| 196 |
|
| 197 |
SelfPtr := Pixels; |
| 198 |
SecondPtr := TGrayscaleAlphaImage(SecondImage).Pixels; |
| 199 |
for I := 1 to Width * Height * Depth do |
| 200 |
begin |
| 201 |
SelfPtr^ := Lerp(Value, SelfPtr^, SecondPtr^); |
| 202 |
Inc(SelfPtr); |
| 203 |
Inc(SecondPtr); |
| 204 |
end; |
| 205 |
end; |
| 206 |
|
| 207 |
class procedure TGrayscaleAlphaImage.MixColors(const OutputColor: Pointer; |
| 208 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 209 |
var |
| 210 |
OutputCol: PVector2Byte absolute OutputColor; |
| 211 |
Cols: array [0..3] of PVector2Byte absolute AColors; |
| 212 |
begin |
| 213 |
{$I norqcheckbegin.inc} |
| 214 |
OutputCol^.X := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 215 |
Weights.X * Cols[0]^.X + |
| 216 |
Weights.Y * Cols[1]^.X + |
| 217 |
Weights.Z * Cols[2]^.X + |
| 218 |
Weights.W * Cols[3]^.X) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 219 |
OutputCol^.Y := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 220 |
Weights.X * Cols[0]^.Y + |
| 221 |
Weights.Y * Cols[1]^.Y + |
| 222 |
Weights.Z * Cols[2]^.Y + |
| 223 |
Weights.W * Cols[3]^.Y) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 224 |
{$I norqcheckend.inc} |
| 225 |
end; |
| 226 |
|
| 227 |
procedure TGrayscaleAlphaImage.InvertColors; |
| 228 |
var |
| 229 |
I: Cardinal; |
| 230 |
P: PVector2Byte; |
| 231 |
begin |
| 232 |
P := Pixels; |
| 233 |
for I := 1 to Width * Height * Depth do |
| 234 |
begin |
| 235 |
P^.X := High(Byte) - P^.X; |
| 236 |
Inc(P); |
| 237 |
end; |
| 238 |
end; |
| 239 |
|
| 240 |
function TGrayscaleAlphaImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 241 |
var |
| 242 |
Pixel: PVector2Byte; |
| 243 |
begin |
| 244 |
Pixel := PixelPtr(X, Y, Z); |
| 245 |
Result.X := Pixel^.X / 255; |
| 246 |
Result.Y := Pixel^.X / 255; |
| 247 |
Result.Z := Pixel^.X / 255; |
| 248 |
Result.W := Pixel^.Y / 255; |
| 249 |
end; |
| 250 |
|
| 251 |
procedure TGrayscaleAlphaImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 252 |
var |
| 253 |
Pixel: PVector2Byte; |
| 254 |
begin |
| 255 |
Pixel := PixelPtr(X, Y, Z); |
| 256 |
Pixel^.X := Clamped(Round(GrayscaleValue(C) * 255), Low(Byte), High(Byte)); |
| 257 |
Pixel^.Y := Clamped(Round(C.W * 255), Low(Byte), High(Byte)); |
| 258 |
end; |
| 259 |
|
| 260 |
function TGrayscaleAlphaImage.ToGrayscaleImage: TGrayscaleImage; |
| 261 |
begin |
| 262 |
Result := TGrayscaleImage.Create; |
| 263 |
Result.Assign(Self); |
| 264 |
end; |
| 265 |
|
| 266 |
{$endif read_implementation} |