Debian Codegraph @

src/images/castleimages_class_rgb_alpha.inc

raw
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 each pixel stored as 4 bytes: RGB (color) and alpha (opacity). }
19 TRGBAlphaImage = class(TCastleImage)
20 private
21 FPremultipliedAlpha: boolean;
22 function GetPixels: PVector4Byte;
23 function GetPixelsArray: PVector4ByteArray;
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 PVector4Byte. }
35 property Pixels: PVector4Byte read GetPixels;
36 {$ifdef FPC}property AlphaPixels: PVector4Byte read GetPixels; deprecated 'use Pixels';{$endif}
37 { Pointer to pixels. Same as RawPixels, only typecasted to PVector4ByteArray. }
38 property PixelsArray: PVector4ByteArray read GetPixelsArray;
39
40 class function PixelSize: Cardinal; override;
41 class function ColorComponentsCount: Cardinal; override;
42
43 function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PVector4Byte;
44 function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PVector4ByteArray;
45
46 procedure InvertColors; override;
47
48 procedure Clear(const Pixel: TVector4Byte); override;
49 function IsClear(const Pixel: TVector4Byte): boolean; override;
50
51 { Set alpha channel on every pixel to the same given value. }
52 procedure ClearAlpha(const Alpha: Byte);
53
54 procedure TransformRGB(const Matrix: TMatrix3); override;
55 procedure ModulateRGB(const ColorModulator: TColorModulatorByteFunc); override;
56
57 { Set alpha of every pixel to either AlphaOnColor
58 (when color of pixel is equal to AlphaColor with Tolerance,
59 see @link(EqualRGB)) or AlphaOnNoColor. }
60 procedure AlphaDecide(const AlphaColor: TVector3Byte;
61 Tolerance: Byte; AlphaOnColor: Byte; AlphaOnNoColor: Byte);
62
63 { Copy RGB contents from one image, and alpha contents from the other.
64 RGB channels are copied from the RGB image,
65 alpha channel is copied from the Grayscale image. Given RGB and Grayscale
66 images must have the same size, and this is the resulting
67 size of this image after Compose call. }
68 procedure Compose(RGB: TRGBImage; AGrayscale: TGrayscaleImage);
69
70 function HasAlpha: boolean; override;
71
72 function AlphaChannel(
73 const AlphaTolerance: Byte): TAlphaChannel; override;
74
75 procedure LerpWith(const Value: Single; SecondImage: TCastleImage); override;
76 class procedure MixColors(const OutputColor: Pointer;
77 const Weights: TVector4; const AColors: TVector4Pointer); override;
78
79 procedure Assign(const Source: TCastleImage); override;
80
81 { Remove alpha channel. }
82 function ToRGBImage: TRGBImage; deprecated 'create TRGBImage and use TRGBImage.Assign';
83
84 { Flatten to grayscale. }
85 function ToGrayscaleAlphaImage: TGrayscaleAlphaImage; deprecated 'create TGrayscaleAlphaImage and use TGrayscaleAlphaImage.Assign';
86
87 { Flatten to grayscale and remove alpha channel. }
88 function ToGrayscaleImage: TGrayscaleImage; deprecated 'create TGrayscaleImage and use TGrayscaleImage.Assign';
89 {$ifdef FPC}
90 function ToFpImage: TInternalCastleFpImage; override;
91 {$endif}
92
93 { Premultiply the RGB channel with alpha, to make it faster
94 to use this image as source for TCastleImage.DrawTo and
95 TCastleImage.DrawFrom operations. Changes @link(PremultipliedAlpha)
96 from @false to @true. Unless @link(PremultipliedAlpha) was
97 already @true, in which case this method does nothing --- this way
98 it is safe to call this many times, we will not repeat multiplying.
99
100 @italic(The image with premultiplied alpha can only be used
101 with a subset of image routines that actually support premultiplied alpha.)
102 Right now, these are only TCastleImage.DrawTo and
103 TCastleImage.DrawFrom. Image with PremultipliedAlpha can be used
104 as a source for drawing, and the results will be the same as without
105 premultiplying, but faster. }
106 procedure PremultiplyAlpha;
107 property PremultipliedAlpha: boolean read FPremultipliedAlpha;
108
109 procedure AlphaBleed(const ProgressTitle: string = ''); override;
110 function MakeAlphaBleed(const ProgressTitle: string = ''): TCastleImage; override;
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 { TRGBAlphaImage ------------------------------------------------------------ }
129
130 function TRGBAlphaImage.GetPixels: PVector4Byte;
131 begin
132 Result := PVector4Byte(RawPixels);
133 end;
134
135 function TRGBAlphaImage.GetPixelsArray: PVector4ByteArray;
136 begin
137 Result := PVector4ByteArray(RawPixels);
138 end;
139
140 class function TRGBAlphaImage.PixelSize: Cardinal;
141 begin
142 Result := 4;
143 end;
144
145 class function TRGBAlphaImage.ColorComponentsCount: Cardinal;
146 begin
147 Result := 4;
148 end;
149
150 function TRGBAlphaImage.PixelPtr(const X, Y, Z: Cardinal): PVector4Byte;
151 begin
152 Result := PVector4Byte(inherited PixelPtr(X, Y, Z));
153 end;
154
155 function TRGBAlphaImage.RowPtr(const Y, Z: Cardinal): PVector4ByteArray;
156 begin
157 Result := PVector4ByteArray(inherited RowPtr(Y, Z));
158 end;
159
160 procedure TRGBAlphaImage.InvertColors;
161 var
162 i: Cardinal;
163 palpha: PVector4Byte;
164 begin
165 palpha := Pixels;
166 for i := 1 to Width * Height * Depth do
167 begin
168 palpha^.X := High(byte) - palpha^.X;
169 palpha^.Y := High(byte) - palpha^.Y;
170 palpha^.Z := High(byte) - palpha^.Z;
171 Inc(palpha);
172 end;
173 end;
174
175 function TRGBAlphaImage.GetColors(const X, Y, Z: Integer): TCastleColor;
176 var
177 Pixel: PVector4Byte;
178 begin
179 Pixel := PixelPtr(X, Y, Z);
180 Result.X := Pixel^.X / 255;
181 Result.Y := Pixel^.Y / 255;
182 Result.Z := Pixel^.Z / 255;
183 Result.W := Pixel^.W / 255;
184 end;
185
186 procedure TRGBAlphaImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor);
187 var
188 Pixel: PVector4Byte;
189 begin
190 Pixel := PixelPtr(X, Y, Z);
191 Pixel^.X := Clamped(Round(C.X * 255), Low(Byte), High(Byte));
192 Pixel^.Y := Clamped(Round(C.Y * 255), Low(Byte), High(Byte));
193 Pixel^.Z := Clamped(Round(C.Z * 255), Low(Byte), High(Byte));
194 Pixel^.W := Clamped(Round(C.W * 255), Low(Byte), High(Byte));
195 end;
196
197 procedure TRGBAlphaImage.Clear(const Pixel: TVector4Byte);
198 begin
199 FillDWord(RawPixels^, Width*Height, UInt32(Pixel));
200 end;
201
202 procedure TRGBAlphaImage.ClearAlpha(const Alpha: Byte);
203 var
204 i: Cardinal;
205 palpha: PVector4Byte;
206 begin
207 palpha := Pixels;
208 for i := 1 to Width * Height * Depth do
209 begin
210 palpha^.W := Alpha;
211 Inc(palpha);
212 end;
213 end;
214
215 function TRGBAlphaImage.IsClear(const Pixel: TVector4Byte): boolean;
216 begin
217 Result := IsMemDWordFilled(RawPixels^, Width * Height * Depth, UInt32(Pixel));
218 end;
219
220 procedure TRGBAlphaImage.TransformRGB(const Matrix: TMatrix3);
221 type PPixel = PVector4Byte;
222 {$I castleimages_transformrgb_implement.inc}
223
224 procedure TRGBAlphaImage.ModulateRGB(const ColorModulator: TColorModulatorByteFunc);
225 type PPixel = PVector4Byte;
226 {$I castleimages_modulatergb_implement.inc}
227
228 procedure TRGBAlphaImage.AlphaDecide(const AlphaColor: TVector3Byte;
229 Tolerance: Byte; AlphaOnColor: Byte; AlphaOnNoColor: Byte);
230 var
231 pa: PVector4Byte;
232 i: Cardinal;
233 begin
234 pa := Pixels;
235 for i := 1 to Width * Height * Depth do
236 begin
237 if EqualRGB(AlphaColor, PVector3Byte(pa)^, Tolerance) then
238 pa^.W := AlphaOnColor else
239 pa^.W := AlphaOnNoColor;
240 Inc(pa);
241 end;
242 end;
243
244 procedure TRGBAlphaImage.Compose(RGB: TRGBImage; AGrayscale: TGrayscaleImage);
245 var
246 PtrAlpha: PVector4Byte;
247 PtrRGB: PVector3Byte;
248 PtrGrayscale: PByte;
249 I: Cardinal;
250 begin
251 Check( (RGB.Width = AGrayscale.Width) and
252 (RGB.Height = AGrayscale.Height) and
253 (RGB.Depth = AGrayscale.Depth),
254 'For TRGBAlphaImage.Compose, RGB and alpha images must have the same sizes');
255
256 SetSize(RGB);
257
258 PtrAlpha := Pixels;
259 PtrRGB := RGB.Pixels;
260 PtrGrayscale := AGrayscale.Pixels;
261
262 for I := 1 to Width * Height * Depth do
263 begin
264 System.Move(PtrRGB^, PtrAlpha^, SizeOf(TVector3Byte));
265 PtrAlpha^.W := PtrGrayscale^;
266
267 Inc(PtrAlpha);
268 Inc(PtrRGB);
269 Inc(PtrGrayscale);
270 end;
271 end;
272
273 function TRGBAlphaImage.HasAlpha: boolean;
274 begin
275 Result := true;
276 end;
277
278 function TRGBAlphaImage.AlphaChannel(
279 const AlphaTolerance: Byte): TAlphaChannel;
280 var
281 PtrAlpha: PVector4Byte;
282 I: Cardinal;
283 begin
284 PtrAlpha := Pixels;
285
286 for I := 1 to Width * Height * Depth do
287 begin
288 if (PtrAlpha^.W > AlphaTolerance) and
289 (PtrAlpha^.W < 255 - AlphaTolerance) then
290 Exit(acBlending);
291 Inc(PtrAlpha);
292 end;
293
294 Result := acTest;
295 end;
296
297 procedure TRGBAlphaImage.LerpWith(const Value: Single; SecondImage: TCastleImage);
298 var
299 SelfPtr: PVector4Byte;
300 SecondPtr: PVector4Byte;
301 I: Cardinal;
302 begin
303 LerpSimpleCheckConditions(SecondImage);
304
305 SelfPtr := Pixels;
306 SecondPtr := TRGBAlphaImage(SecondImage).Pixels;
307 for I := 1 to Width * Height * Depth do
308 begin
309 SelfPtr^ := Lerp(Value, SelfPtr^, SecondPtr^);
310 Inc(SelfPtr);
311 Inc(SecondPtr);
312 end;
313 end;
314
315 class procedure TRGBAlphaImage.MixColors(const OutputColor: Pointer;
316 const Weights: TVector4; const AColors: TVector4Pointer);
317 var
318 OutputCol: PVector4Byte absolute OutputColor;
319 Cols: array [0..3] of PVector4Byte absolute AColors;
320 begin
321 {$I norqcheckbegin.inc}
322 OutputCol^.X := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round(
323 Weights.X * Cols[0]^.X +
324 Weights.Y * Cols[1]^.X +
325 Weights.Z * Cols[2]^.X +
326 Weights.W * Cols[3]^.X) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif};
327 OutputCol^.Y := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round(
328 Weights.X * Cols[0]^.Y +
329 Weights.Y * Cols[1]^.Y +
330 Weights.Z * Cols[2]^.Y +
331 Weights.W * Cols[3]^.Y) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif};
332 OutputCol^.Z := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round(
333 Weights.X * Cols[0]^.Z +
334 Weights.Y * Cols[1]^.Z +
335 Weights.Z * Cols[2]^.Z +
336 Weights.W * Cols[3]^.Z) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif};
337 OutputCol^.W := {$ifndef FAST_UNSAFE_MIX_COLORS} Clamped( {$endif} Round(
338 Weights.X * Cols[0]^.W +
339 Weights.Y * Cols[1]^.W +
340 Weights.Z * Cols[2]^.W +
341 Weights.W * Cols[3]^.W) {$ifndef FAST_UNSAFE_MIX_COLORS} , 0, High(Byte)) {$endif};
342 {$I norqcheckend.inc}
343 end;
344
345 function TRGBAlphaImage.ToRGBImage: TRGBImage;
346 begin
347 Result := TRGBImage.Create;
348 Result.Assign(Self);
349 end;
350
351 function TRGBAlphaImage.ToGrayscaleImage: TGrayscaleImage;
352 begin
353 Result := TGrayscaleImage.Create;
354 Result.Assign(Self);
355 end;
356
357 function TRGBAlphaImage.ToGrayscaleAlphaImage: TGrayscaleAlphaImage;
358 begin
359 Result := TGrayscaleAlphaImage.Create;
360 Result.Assign(Self);
361 end;
362
363 procedure TRGBAlphaImage.PremultiplyAlpha;
364 var
365 P: PVector4Byte;
366 I: Integer;
367 begin
368 if not FPremultipliedAlpha then
369 begin
370 FPremultipliedAlpha := true;
371 P := Pixels;
372 for I := 1 to Width * Height * Depth do
373 begin
374 P^.X := Clamped(Round(P^.X * P^.W / 255), 0, 255);
375 P^.Y := Clamped(Round(P^.Y * P^.W / 255), 0, 255);
376 P^.Z := Clamped(Round(P^.Z * P^.W / 255), 0, 255);
377 Inc(P);
378 end;
379 end;
380 end;
381
382 procedure TRGBAlphaImage.AlphaBleed(const ProgressTitle: string);
383 var
384 Copy: TCastleImage;
385 begin
386 Copy := MakeAlphaBleed(ProgressTitle);
387 try
388 Assert(Size = Copy.Size);
389 Move(Copy.RawPixels^, RawPixels^, Size);
390 finally FreeAndNil(Copy) end;
391 end;
392
393 function TRGBAlphaImage.MakeAlphaBleed(const ProgressTitle: string): TCastleImage;
394
395 function FindNearestNonTransparentPixel(X, Y, Z: Integer): PVector4Byte;
396
397 function TryPixelOpaque(const DX, DY: Integer;
398 var SomePixelWithinImage: boolean): PVector4Byte;
399 var
400 NX, NY, GX, GY: Integer;
401 begin
402 NX := X + DX;
403 NY := Y + DY;
404 GX := Clamped(NX, 0, Width - 1);
405 GY := Clamped(NY, 0, Height - 1);
406 { does not search in Z.
407 This is faster for 2D images.
408 Also this way we do not look if we're outside Z range below,
409 which would stop the search too quickly for 2D images. }
410 if (NX = GX) and (NY = GY) then
411 SomePixelWithinImage := true;
412 Result := PixelPtr(GX, GY, Z);
413 if Result^.W <> High(Byte) then
414 Result := nil; // nope, tried pixel is not opaque
415 end;
416
417 var
418 Distance: Cardinal;
419 SomePixelWithinImage: boolean;
420 begin
421 Distance := 1;
422 Result := nil;
423 repeat
424 SomePixelWithinImage := false;
425 Result := TryPixelOpaque(-Distance, 0, SomePixelWithinImage); if Result <> nil then Exit;
426 Result := TryPixelOpaque( Distance, 0, SomePixelWithinImage); if Result <> nil then Exit;
427 Result := TryPixelOpaque( 0, -Distance, SomePixelWithinImage); if Result <> nil then Exit;
428 Result := TryPixelOpaque( 0, Distance, SomePixelWithinImage); if Result <> nil then Exit;
429 Result := TryPixelOpaque(-Distance, -Distance, SomePixelWithinImage); if Result <> nil then Exit;
430 Result := TryPixelOpaque(-Distance, Distance, SomePixelWithinImage); if Result <> nil then Exit;
431 Result := TryPixelOpaque( Distance, -Distance, SomePixelWithinImage); if Result <> nil then Exit;
432 Result := TryPixelOpaque( Distance, Distance, SomePixelWithinImage); if Result <> nil then Exit;
433 Inc(Distance);
434 until not SomePixelWithinImage;
435 end;
436
437 var
438 P, NewP: PVector4Byte;
439 X, Y, Z: Integer;
440 begin
441 Result := MakeCopy;
442 for X := 0 to Width - 1 do
443 for Y := 0 to Height - 1 do
444 for Z := 0 to Depth - 1 do
445 begin
446 P := Result.PixelPtr(X, Y, Z);
447 if P^.W <> High(Byte) then
448 begin
449 NewP := FindNearestNonTransparentPixel(X, Y, Z);
450 if NewP <> nil then
451 Move(NewP^, P^, SizeOf(TVector3Byte));
452 end;
453 end;
454 end;
455
456 {$endif read_implementation}