Debian Codegraph @

src/images/castleimages_class_grayscale_float.inc

raw
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 Single (floating-point number) for each pixel.
20 Ideal for high-precision height maps.
21 Compared to TGrayscaleImage, each pixel is a floating-point number,
22 which means it is more precise and can also be above 1.0 value
23 (though they are still in 0..1 range when reading from normal image formats). }
24 TGrayscaleFloatImage = class(TCastleImage)
25 private
26 function GetPixels: PSingle;
27 function GetPixelsArray: PSingleArray;
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 PSingle. }
40 property Pixels: PSingle read GetPixels;
41
42 { Pointer to pixels. Same as RawPixels, only typecasted to PSingleArray. }
43 property PixelsArray: PSingleArray read GetPixelsArray;
44
45 { Pointer to given pixel. }
46 function PixelPtr(const X, Y: Cardinal; const Z: Cardinal = 0): PSingle;
47
48 { Pointer to given row. }
49 function RowPtr(const Y: Cardinal; const Z: Cardinal = 0): PSingleArray;
50 end;
51
52 {$endif read_interface}
53
54 {$ifdef read_implementation}
55
56 { TGrayscaleFloatImage ------------------------------------------------------------ }
57
58 function TGrayscaleFloatImage.GetPixels: PSingle;
59 begin
60 Result := PSingle(RawPixels);
61 end;
62
63 function TGrayscaleFloatImage.GetPixelsArray: PSingleArray;
64 begin
65 Result := PSingleArray(RawPixels);
66 end;
67
68 class function TGrayscaleFloatImage.PixelSize: Cardinal;
69 begin
70 Result := SizeOf(Single);
71 end;
72
73 class function TGrayscaleFloatImage.ColorComponentsCount: Cardinal;
74 begin
75 Result := 1;
76 end;
77
78 function TGrayscaleFloatImage.PixelPtr(const X, Y, Z: Cardinal): PSingle;
79 begin
80 Result := PSingle(inherited PixelPtr(X, Y, Z));
81 end;
82
83 function TGrayscaleFloatImage.RowPtr(const Y, Z: Cardinal): PSingleArray;
84 begin
85 Result := PSingleArray(inherited RowPtr(Y, Z));
86 end;
87
88 procedure TGrayscaleFloatImage.InvertColors;
89 var
90 I: Cardinal;
91 P: PSingle;
92 begin
93 P := Pixels;
94 for I := 1 to Width * Height * Depth do
95 begin
96 P^ := Max(1.0 - P^, 0.0); // make sure result is >= 0, even when input > 1.0
97 Inc(P);
98 end;
99 end;
100
101 function TGrayscaleFloatImage.GetColors(const X, Y, Z: Integer): TCastleColor;
102 var
103 Pixel: PSingle;
104 begin
105 Pixel := PixelPtr(X, Y, Z);
106 Result.X := Pixel^;
107 Result.Y := Pixel^;
108 Result.Z := Pixel^;
109 Result.W := 1.0;
110 end;
111
112 procedure TGrayscaleFloatImage.SetColors(const X, Y, Z: Integer; const C: TCastleColor);
113 var
114 Pixel: PSingle;
115 begin
116 Pixel := PixelPtr(X, Y, Z);
117 Pixel^ := GrayscaleValue(C);
118 end;
119
120 class procedure TGrayscaleFloatImage.MixColors(const OutputColor: Pointer;
121 const Weights: TVector4; const AColors: TVector4Pointer);
122 var
123 OutputCol: PSingle absolute OutputColor;
124 Cols: array [0..3] of PSingle absolute AColors;
125 begin
126 OutputCol^ :=
127 Weights.X * Cols[0]^ +
128 Weights.Y * Cols[1]^ +
129 Weights.Z * Cols[2]^ +
130 Weights.W * Cols[3]^;
131 end;
132
133 {$endif read_implementation}