| 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. Color is a simple Byte value. } |
| 19 |
TGrayscaleImage = class(TCastleImage) |
| 20 |
private |
| 21 |
FTreatAsAlpha: boolean; |
| 22 |
FColorWhenTreatedAsAlpha: TVector3Byte; |
| 23 |
FGrayscaleColorWhenTreatedAsAlpha: Byte; |
| 24 |
function GetPixels: PByte; |
| 25 |
function GetPixelsArray: PByteArray; |
| 26 |
procedure SetColorWhenTreatedAsAlpha(const Value: TVector3Byte); |
| 27 |
{$ifdef FPC} |
| 28 |
procedure FromFpImage(const FPImage: TInternalCastleFpImage); override; |
| 29 |
{$endif} |
| 30 |
protected |
| 31 |
procedure DrawFromCore(Source: TCastleImage; |
| 32 |
X, Y, SourceX, SourceY, SourceWidth, SourceHeight: Integer; |
| 33 |
const Mode: TDrawMode); override; |
| 34 |
function GetColors(const X, Y, Z: Integer): TCastleColor; override; |
| 35 |
procedure SetColors(const X, Y, Z: Integer; const C: TCastleColor); override; |
| 36 |
procedure AssignProperties(const Source: TCastleImage); override; |
| 37 |
public |
| 38 |
constructor Create; overload; override; |
| 39 |
|
| 40 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PByte. } |
| 41 |
property Pixels: PByte read GetPixels; |
| 42 |
property GrayscalePixels: PByte read GetPixels; {$ifdef FPC} deprecated 'use Pixels'; {$endif} |
| 43 |
{ Pointer to pixels. Same as RawPixels, only typecasted to PByteArray. } |
| 44 |
property PixelsArray: PByteArray read GetPixelsArray; |
| 45 |
|
| 46 |
class function PixelSize: Cardinal; override; |
| 47 |
class function ColorComponentsCount: Cardinal; override; |
| 48 |
|
| 49 |
function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PByte; |
| 50 |
function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PByteArray; |
| 51 |
|
| 52 |
procedure InvertColors; override; |
| 53 |
|
| 54 |
procedure Clear(const Pixel: TVector4Byte); override; |
| 55 |
function IsClear(const Pixel: TVector4Byte): boolean; override; |
| 56 |
|
| 57 |
procedure Clear(const Pixel: Byte); reintroduce; overload; |
| 58 |
function IsClear(const Pixel: Byte): boolean; reintroduce; overload; |
| 59 |
|
| 60 |
{ Every pixels value is halved (divided by 2). |
| 61 |
This is done by simple bitshift, so you can be sure that all |
| 62 |
components are < 2^7 after this. } |
| 63 |
procedure HalfColors; |
| 64 |
|
| 65 |
{ Add alpha channel. |
| 66 |
|
| 67 |
If TreatAsAlpha = @false: add constant opaque alpha. |
| 68 |
|
| 69 |
If TreatAsAlpha = @true: output alpha will be derived from source grayscale, |
| 70 |
output luminance will be derived from ColorWhenTreatedAsAlpha. } |
| 71 |
function ToGrayscaleAlphaImage: TGrayscaleAlphaImage; deprecated 'create TGrayscaleAlphaImage and use TGrayscaleAlphaImage.Assign'; |
| 72 |
|
| 73 |
{$ifdef FPC} |
| 74 |
function ToFpImage: TInternalCastleFpImage; override; |
| 75 |
{$endif} |
| 76 |
|
| 77 |
procedure LerpWith(const Value: Single; SecondImage: TCastleImage); override; |
| 78 |
class procedure MixColors(const OutputColor: Pointer; |
| 79 |
const Weights: TVector4; const AColors: TVector4Pointer); override; |
| 80 |
|
| 81 |
{ Should we treat grayscale image as pure alpha channel (without any color |
| 82 |
information) when using this as a texture. |
| 83 |
|
| 84 |
This property is meaningful for some operations: |
| 85 |
|
| 86 |
@orderedList( |
| 87 |
@item( |
| 88 |
When creating OpenGL texture from this image. |
| 89 |
If @true, then the grayscale pixel data will be loaded as alpha channel contents. |
| 90 |
When the texture is read by shaders, the RGB is (1,1,1) and alpha comes from the image. |
| 91 |
|
| 92 |
Note the we don't pass ColorWhenTreatedAsAlpha to OpenGL, |
| 93 |
as we don't have this functionality (e.g. https://www.khronos.org/opengl/wiki/Texture#Swizzle_mask |
| 94 |
cannot express an arbitrary but constant color on some channels.) |
| 95 |
) |
| 96 |
|
| 97 |
@item( |
| 98 |
When using @link(DrawFrom) / @link(DrawTo) methods or being assigned to something using @link(Assign). |
| 99 |
If @true, this image is drawn like an RGBA image, |
| 100 |
with constant RGB color ColorWhenTreatedAsAlpha, and alpha channel |
| 101 |
taken from contents of this image.) |
| 102 |
|
| 103 |
@item( |
| 104 |
It is also the only way for TGrayscaleImage to return AlphaChannel <> acNone.) |
| 105 |
|
| 106 |
) |
| 107 |
} |
| 108 |
property TreatAsAlpha: boolean |
| 109 |
read FTreatAsAlpha write FTreatAsAlpha default false; |
| 110 |
|
| 111 |
{ Used for drawing/assigning when TreatAsAlpha is @true, and we need the base |
| 112 |
(not alpha) color for some equation. |
| 113 |
By default white (255, 255, 255). } |
| 114 |
property ColorWhenTreatedAsAlpha: TVector3Byte |
| 115 |
read FColorWhenTreatedAsAlpha write SetColorWhenTreatedAsAlpha; |
| 116 |
|
| 117 |
{ Automatically derived from ColorWhenTreatedAsAlpha by averaging RGB components |
| 118 |
to calculate grayscale luminance. |
| 119 |
By default 255. } |
| 120 |
property GrayscaleColorWhenTreatedAsAlpha: Byte |
| 121 |
read FGrayscaleColorWhenTreatedAsAlpha; |
| 122 |
|
| 123 |
function AlphaChannel( |
| 124 |
const AlphaTolerance: Byte): TAlphaChannel; override; |
| 125 |
|
| 126 |
procedure Assign(const Source: TCastleImage); override; |
| 127 |
public |
| 128 |
procedure FillEllipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 129 |
const aColor: TCastleColor); override; |
| 130 |
procedure Ellipse(const x, y: single; const aRadiusX, aRadiusY: single; |
| 131 |
const aWidth: single; const aColor: TCastleColor); override; |
| 132 |
procedure FillRectangle(const x1, y1, x2, y2: single; |
| 133 |
const aColor: TCastleColor); override; |
| 134 |
procedure Rectangle(const x1, y1, x2, y2: single; |
| 135 |
const aWidth: single; const aColor: TCastleColor); override; |
| 136 |
procedure Line(const x1, y1, x2, y2: single; |
| 137 |
const aWidth: single; const aColor: TCastleColor); override; |
| 138 |
end; |
| 139 |
|
| 140 |
{$endif read_interface} |
| 141 |
|
| 142 |
{$ifdef read_implementation} |
| 143 |
|
| 144 |
{ TGrayscaleImage ------------------------------------------------------------ } |
| 145 |
|
| 146 |
constructor TGrayscaleImage.Create; |
| 147 |
begin |
| 148 |
inherited; |
| 149 |
FColorWhenTreatedAsAlpha := Vector3Byte(255, 255, 255); |
| 150 |
FGrayscaleColorWhenTreatedAsAlpha := GrayscaleValue(FColorWhenTreatedAsAlpha); |
| 151 |
end; |
| 152 |
|
| 153 |
function TGrayscaleImage.GetPixels: PByte; |
| 154 |
begin |
| 155 |
Result := PByte(RawPixels); |
| 156 |
end; |
| 157 |
|
| 158 |
function TGrayscaleImage.GetPixelsArray: PByteArray; |
| 159 |
begin |
| 160 |
Result := PByteArray(RawPixels); |
| 161 |
end; |
| 162 |
|
| 163 |
class function TGrayscaleImage.PixelSize: Cardinal; |
| 164 |
begin |
| 165 |
Result := 1; |
| 166 |
end; |
| 167 |
|
| 168 |
class function TGrayscaleImage.ColorComponentsCount: Cardinal; |
| 169 |
begin |
| 170 |
Result := 1; |
| 171 |
end; |
| 172 |
|
| 173 |
function TGrayscaleImage.PixelPtr(const X, Y, Z: Cardinal): PByte; |
| 174 |
begin |
| 175 |
Result := PByte(inherited PixelPtr(X, Y, Z)); |
| 176 |
end; |
| 177 |
|
| 178 |
function TGrayscaleImage.RowPtr(const Y, Z: Cardinal): PByteArray; |
| 179 |
begin |
| 180 |
Result := PByteArray(inherited RowPtr(Y, Z)); |
| 181 |
end; |
| 182 |
|
| 183 |
procedure TGrayscaleImage.Clear(const Pixel: TVector4Byte); |
| 184 |
begin |
| 185 |
Clear(GrayscaleValue(Pixel)); |
| 186 |
end; |
| 187 |
|
| 188 |
function TGrayscaleImage.IsClear(const Pixel: TVector4Byte): boolean; |
| 189 |
begin |
| 190 |
Result := IsClear(GrayscaleValue(Pixel)); |
| 191 |
end; |
| 192 |
|
| 193 |
procedure TGrayscaleImage.Clear(const Pixel: Byte); |
| 194 |
begin |
| 195 |
FillChar(RawPixels^, Size, Pixel); |
| 196 |
end; |
| 197 |
|
| 198 |
function TGrayscaleImage.IsClear(const Pixel: Byte): boolean; |
| 199 |
begin |
| 200 |
Result := IsMemCharFilled(RawPixels^, Size, AnsiChar(Pixel)); |
| 201 |
end; |
| 202 |
|
| 203 |
procedure TGrayscaleImage.HalfColors; |
| 204 |
var |
| 205 |
P: PByte; |
| 206 |
I: Cardinal; |
| 207 |
begin |
| 208 |
P := Pixels; |
| 209 |
for I := 1 to Width * Height * Depth do |
| 210 |
begin |
| 211 |
P^ := P^ shr 1; |
| 212 |
Inc(P); |
| 213 |
end; |
| 214 |
end; |
| 215 |
|
| 216 |
procedure TGrayscaleImage.LerpWith(const Value: Single; SecondImage: TCastleImage); |
| 217 |
var |
| 218 |
SelfPtr: PByte; |
| 219 |
SecondPtr: PByte; |
| 220 |
I: Cardinal; |
| 221 |
begin |
| 222 |
LerpSimpleCheckConditions(SecondImage); |
| 223 |
|
| 224 |
SelfPtr := Pixels; |
| 225 |
SecondPtr := TGrayscaleImage(SecondImage).Pixels; |
| 226 |
for I := 1 to Width * Height * Depth do |
| 227 |
begin |
| 228 |
SelfPtr^ := Clamped(Round(Lerp(Value, SelfPtr^, SecondPtr^)), 0, High(Byte)); |
| 229 |
Inc(SelfPtr); |
| 230 |
Inc(SecondPtr); |
| 231 |
end; |
| 232 |
end; |
| 233 |
|
| 234 |
class procedure TGrayscaleImage.MixColors(const OutputColor: Pointer; |
| 235 |
const Weights: TVector4; const AColors: TVector4Pointer); |
| 236 |
var |
| 237 |
OutputCol: PByte absolute OutputColor; |
| 238 |
Cols: array [0..3] of PByte absolute AColors; |
| 239 |
begin |
| 240 |
{$I norqcheckbegin.inc} |
| 241 |
OutputCol^ := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round( |
| 242 |
Weights.X * Cols[0]^ + |
| 243 |
Weights.Y * Cols[1]^ + |
| 244 |
Weights.Z * Cols[2]^ + |
| 245 |
Weights.W * Cols[3]^) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif}; |
| 246 |
{$I norqcheckend.inc} |
| 247 |
end; |
| 248 |
|
| 249 |
function TGrayscaleImage.ToGrayscaleAlphaImage: TGrayscaleAlphaImage; |
| 250 |
begin |
| 251 |
Result := TGrayscaleAlphaImage.Create; |
| 252 |
Result.Assign(Self); |
| 253 |
end; |
| 254 |
|
| 255 |
function TGrayscaleImage.AlphaChannel( |
| 256 |
const AlphaTolerance: Byte): TAlphaChannel; |
| 257 |
var |
| 258 |
PtrAlpha: PByte; |
| 259 |
I: Cardinal; |
| 260 |
begin |
| 261 |
if not TreatAsAlpha then |
| 262 |
Exit(inherited AlphaChannel(AlphaTolerance)); |
| 263 |
|
| 264 |
PtrAlpha := Pixels; |
| 265 |
|
| 266 |
for I := 1 to Width * Height * Depth do |
| 267 |
begin |
| 268 |
if (PtrAlpha^ > AlphaTolerance) and |
| 269 |
(PtrAlpha^ < 255 - AlphaTolerance) then |
| 270 |
Exit(acBlending); |
| 271 |
Inc(PtrAlpha); |
| 272 |
end; |
| 273 |
|
| 274 |
Result := acTest; |
| 275 |
end; |
| 276 |
|
| 277 |
procedure TGrayscaleImage.InvertColors; |
| 278 |
var |
| 279 |
I: Cardinal; |
| 280 |
P: PByte; |
| 281 |
begin |
| 282 |
P := Pixels; |
| 283 |
for I := 1 to Width * Height * Depth do |
| 284 |
begin |
| 285 |
P^ := High(Byte)-P^; |
| 286 |
Inc(P); |
| 287 |
end; |
| 288 |
end; |
| 289 |
|
| 290 |
function TGrayscaleImage.GetColors(const X, Y, Z: Integer): TCastleColor; |
| 291 |
var |
| 292 |
Pixel: PByte; |
| 293 |
begin |
| 294 |
Pixel := PixelPtr(X, Y, Z); |
| 295 |
Result.X := Pixel^ / 255; |
| 296 |
Result.Y := Pixel^ / 255; |
| 297 |
Result.Z := Pixel^ / 255; |
| 298 |
Result.W := 1.0; |
| 299 |
end; |
| 300 |
|
| 301 |
procedure TGrayscaleImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor); |
| 302 |
var |
| 303 |
Pixel: PByte; |
| 304 |
begin |
| 305 |
Pixel := PixelPtr(X, Y, Z); |
| 306 |
Pixel^ := Clamped(Round(GrayscaleValue(C) * 255), Low(Byte), High(Byte)); |
| 307 |
end; |
| 308 |
|
| 309 |
procedure TGrayscaleImage.SetColorWhenTreatedAsAlpha(const Value: TVector3Byte); |
| 310 |
begin |
| 311 |
FColorWhenTreatedAsAlpha := Value; |
| 312 |
FGrayscaleColorWhenTreatedAsAlpha := GrayscaleValue(Value); |
| 313 |
end; |
| 314 |
|
| 315 |
procedure TGrayscaleImage.AssignProperties(const Source: TCastleImage); |
| 316 |
begin |
| 317 |
inherited; |
| 318 |
|
| 319 |
{ Copying these properties between TGrayscaleImage->TGrayscaleImage |
| 320 |
is important, e.g. when old OpenGLs need to resize font texture |
| 321 |
(to be power of 2) we have to copy the TreatAsAlpha value, |
| 322 |
to render font properly with alpha blending. |
| 323 |
} |
| 324 |
if Source is TGrayscaleImage then |
| 325 |
begin |
| 326 |
FTreatAsAlpha := TGrayscaleImage(Source).TreatAsAlpha; |
| 327 |
FColorWhenTreatedAsAlpha := TGrayscaleImage(Source).ColorWhenTreatedAsAlpha; |
| 328 |
FGrayscaleColorWhenTreatedAsAlpha := TGrayscaleImage(Source).GrayscaleColorWhenTreatedAsAlpha; |
| 329 |
end; |
| 330 |
end; |
| 331 |
|
| 332 |
{$endif read_implementation} |