Debian Codegraph @

src/images/castleimages_vampyre_imaging.inc

raw
1 {%MainUnit castleimages.pas}
2 {
3 Copyright 2021-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 { Handle images format using Vampyre Imaging:
18 https://github.com/galfar/imaginglib
19 https://imaginglib.sourceforge.io/ }
20
21 {$ifdef USE_VAMPYRE_IMAGING}
22
23 function Load_VampyreImaging(const Stream: TStream;
24 const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
25 var
26 Img: TSingleImage;
27
28 { Copy row-by-row data from Img to Target, assuming exactly the same pixel format,
29 and the same order within rows.
30 Flips rows (as TCastleImage wants rows from bottom to top). }
31 procedure CopyRowsFlip(const Target: TCastleImage);
32 var
33 I: Integer;
34 begin
35 for I := 0 to Img.Height - 1 do
36 Move(Img.ScanLine[I]^, Target.RowPtr(Img.Height - 1 - I)^, Target.PixelSize * Img.Width);
37 end;
38
39 { Copy whole data from Img to Target, assuming exactly the same memory format. }
40 procedure CopyWhole(const Target: TEncodedImage);
41 begin
42 if Target.Size <> Img.ImageDataPointer^.Size then
43 raise EImageLoadError.CreateFmt('Reading image from Vampyre Imaging failed, expected size %d but got size %d, for format %s', [
44 Target.Size,
45 Img.ImageDataPointer^.Size,
46 GetFormatName(Img.Format)
47 ]);
48 Move(Img.ImageDataPointer^.Bits^, Target.RawPixels^, Target.Size);
49 end;
50
51 procedure ChangeVampyreFormat(const NewFormat: TImageFormat);
52 begin
53 WritelnLog('Converting Vampyre Imaging format %s -> %s', [
54 GetFormatName(Img.Format),
55 GetFormatName(NewFormat)
56 ]);
57 Img.Format := NewFormat; // calls ConvertImage and actually changes content
58 end;
59
60 { Convert Result from one TCastleImage to another. }
61 procedure ChangeResultClass(const NewClass: TCastleImageClass);
62 var
63 NewResult: TCastleImage;
64 begin
65 Assert(Result <> nil);
66 Assert(Result is TCastleImage);
67 NewResult := NewClass.Create(Result.Width, Result.Height, Result.Depth);
68 NewResult.Assign(Result as TCastleImage);
69 FreeAndNil(Result);
70 Result := NewResult;
71 end;
72
73 begin
74 Img := TSingleImage.CreateFromStream(Stream);
75 try
76 // WritelnLog('Loaded image from Vampyre Imaging, format %s, size %dx%d', [
77 // GetFormatName(Img.Format),
78 // Img.Width,
79 // Img.Height
80 // ]);
81 // happens e.g. when reading TIFF, but TIFF dynamic library not available
82 if Img.Format = ifUnknown then
83 raise EImageLoadError.Create('Reading image using Vampyre Imaging failed, unknown or unsupported format');
84 case Img.Format of
85 ifGray8: Result := TGrayscaleImage.Create(Img.Width, Img.Height);
86 ifA8Gray8: Result := TGrayscaleAlphaImage.Create(Img.Width, Img.Height);
87 ifA8R8G8B8:
88 begin
89 Result := TRGBAlphaImage.Create(Img.Width, Img.Height);
90 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
91 end;
92 ifR8G8B8:
93 begin
94 Result := TRGBImage.Create(Img.Width, Img.Height);
95 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
96 end;
97 // TODO: tcDxt1_RGBA or tcDxt1_RGB
98 ifDXT1: Result := TGPUCompressedImage.Create(Img.Width, Img.Height, 1, tcDxt1_RGBA);
99 ifDXT3: Result := TGPUCompressedImage.Create(Img.Width, Img.Height, 1, tcDxt3);
100 ifDXT5: Result := TGPUCompressedImage.Create(Img.Width, Img.Height, 1, tcDxt5);
101
102 // example: RGBE/HDR images
103 ifR32G32B32F:
104 begin
105 Result := TRGBFloatImage.Create(Img.Width, Img.Height);
106 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
107 end;
108 ifB32G32R32F: Result := TRGBFloatImage.Create(Img.Width, Img.Height);
109
110 ifA32R32G32B32F:
111 begin
112 Result := TRGBAlphaFloatImage.Create(Img.Width, Img.Height);
113 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
114 end;
115 ifA32B32G32R32F: Result := TRGBAlphaFloatImage.Create(Img.Width, Img.Height);
116
117 { Example of ifR16G16B16:
118 16-bit PNGs from https://www.motionforgepictures.com/height-maps/ .
119 Convert these to TRGBFloatImage to keep precision beyond 8 bits,
120 useful for terrains. }
121 ifR16G16B16, ifB16G16R16:
122 begin
123 ChangeVampyreFormat(ifB32G32R32F);
124 Result := TRGBFloatImage.Create(Img.Width, Img.Height);
125 end;
126
127 ifA16R16G16B16, ifA16B16G16R16:
128 begin
129 ChangeVampyreFormat(ifA32B32G32R32F);
130 Result := TRGBAlphaFloatImage.Create(Img.Width, Img.Height);
131 end;
132
133 { A few grayscale formats that have precision beyond 8-bit,
134 very useful for terrains. }
135 ifR32F: Result := TGrayscaleFloatImage.Create(Img.Width, Img.Height);
136 ifR16F, ifGray16, ifGray32:
137 begin
138 ChangeVampyreFormat(ifR32F);
139 Result := TGrayscaleFloatImage.Create(Img.Width, Img.Height);
140 end;
141
142 ifA16Gray16:
143 begin
144 { Vampyre doesn't have a format with 2 floats.
145 Convert to 4 floats, which is the closest, and then we'll
146 convert to 2 floats using TGrayscaleAlphaFloatImage.Assign. }
147 ChangeVampyreFormat(ifA32B32G32R32F);
148 Result := TRGBAlphaFloatImage.Create(Img.Width, Img.Height);
149 ChangeResultClass(TGrayscaleAlphaFloatImage);
150 end;
151
152 else
153 begin
154 { Fallback to read everything through TRGBAlphaImage,
155 which has most usual information (8-bit, RGBA).
156
157 Testcase: vampyre_imaginglib/src/Demos/Data/Tigers.tga
158 Testcase: vampyre_imaginglib/src/Demos/Data/Tigers.tif }
159 ChangeVampyreFormat(ifA8R8G8B8);
160 Result := TRGBAlphaImage.Create(Img.Width, Img.Height);
161 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
162 end;
163 end;
164
165 if Result is TCastleImage then
166 CopyRowsFlip(TCastleImage(Result))
167 else
168 CopyWhole(Result);
169 finally FreeAndNil(Img) end;
170
171 { TODO: honor AllowedImageClasses, to fix the loaded data type
172 using Vampyre transformations, *before* LoadEncodedImage
173 sees it. LoadEncodedImage will also fix the loaded data type, but it is slower. }
174 end;
175
176 procedure Save_VampyreImaging(const CastleImage: TEncodedImage; const Stream: TStream; const Ext: String);
177 var
178 Img: TSingleImage;
179
180 { Copy row-by-row data from Source to Img, assuming exactly the same pixel format,
181 and the same order within rows.
182 Flips rows (as TCastleImage wants rows from bottom to top). }
183 procedure CopyRowsFlip(const Source: TCastleImage);
184 var
185 I: Integer;
186 begin
187 for I := 0 to Img.Height - 1 do
188 Move(Source.RowPtr(Img.Height - 1 - I)^, Img.ScanLine[I]^, Source.PixelSize * Img.Width);
189 end;
190
191 { Copy whole data from Img to Source, assuming exactly the same memory format. }
192 procedure CopyWhole(const Source: TEncodedImage);
193 begin
194 if Source.Size <> Img.ImageDataPointer^.Size then
195 raise EImageSaveError.CreateFmt('Saving image to Vampyre Imaging failed, expected size %d but got size %d, for format %s', [
196 Source.Size,
197 Img.ImageDataPointer^.Size,
198 GetFormatName(Img.Format)
199 ]);
200 Move(Source.RawPixels^, Img.ImageDataPointer^.Bits^, Source.Size);
201 end;
202
203 var
204 Format: TImageFormat;
205 begin
206 { calculate Format }
207 if CastleImage is TGrayscaleImage then
208 Format := ifGray8
209 else
210 if CastleImage is TGrayscaleAlphaImage then
211 Format := ifA8Gray8
212 else
213 if CastleImage is TRGBImage then
214 Format := ifR8G8B8
215 else
216 if CastleImage is TRGBAlphaImage then
217 Format := ifA8R8G8B8
218 else
219 if CastleImage is TGPUCompressedImage then
220 begin
221 case TGPUCompressedImage(CastleImage).Compression of
222 tcDxt1_RGB, tcDxt1_RGBA: Format := ifDXT1;
223 tcDxt3: Format := ifDXT3;
224 tcDxt5: Format := ifDXT5;
225 else raise EImageSaveError.CreateFmt('Cannot save image with GPU compression %s using Vampyre Imaging', [
226 TextureCompressionToString(TGPUCompressedImage(CastleImage).Compression)
227 ]);
228 end;
229 end else
230 if CastleImage is TGrayscaleFloatImage then
231 Format := ifR32F
232 else
233 // TODO: No ifR32G32F in Vampyre Imaging, we need to convert this to be able to save it
234 // if CastleImage is TGrayscaleAlphaFloatImage then
235 // Format := ifR32G32F
236 // else
237 if CastleImage is TRGBFloatImage then
238 Format := ifR32G32B32F
239 else
240 if CastleImage is TRGBAlphaFloatImage then
241 Format := ifA32R32G32B32F
242 else
243 begin
244 raise EImageSaveError.CreateFmt('Cannot save image with format %s using Vampyre Imaging', [
245 CastleImage.ClassName
246 ]);
247 end;
248
249 Img := TSingleImage.CreateFromParams(CastleImage.Width, CastleImage.Height, Format);
250 try
251 if CastleImage is TCastleImage then
252 CopyRowsFlip(TCastleImage(CastleImage))
253 else
254 CopyWhole(CastleImage);
255 if Format in [ifR8G8B8, ifA8R8G8B8, ifR32G32B32F, ifA32R32G32B32F] then
256 SwapChannels(Img.ImageDataPointer^, ChannelRed, ChannelBlue);
257 if not Img.SaveToStream(Ext, Stream) then
258 raise EImageSaveError.CreateFmt('Cannot save image to file format %s using Vampyre Imaging', [
259 Ext
260 ]);
261 finally FreeAndNil(Img) end;
262 end;
263
264 {$endif}