Debian Codegraph @

src/images/castleimages_fpimage.inc

raw
1 {%MainUnit castleimages.pas}
2 {
3 Copyright 2001-2022 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 { Image oparations using FPImage.
18
19 FPImage docs:
20 http://lazarus-ccr.sourceforge.net/docs/fcl/fpimage/index.html
21 http://wiki.freepascal.org/fcl-image
22
23 Also http://free-pascal-general.1045716.n5.nabble.com/Why-is-FPImage-dog-slow-in-reading-jpeg-s-td4439450.html
24 is informative, explains UsePalette stuff.
25 However, for some formats palette is needed --- e.g. reading XPM
26 reader segfaults when UsePalette = false, tested on
27 /usr/share/pixmaps/EasyTAG_icon.xpm
28 /usr/share/pixmaps/flamerobin.xpm
29 }
30
31 {$ifdef FPC} // FpImage is only available in FPC
32
33 { TInternalCastleFpImage ----------------------------------------------------- }
34
35 function CreateFpImage(const Width, Height: Integer): TInternalCastleFpImage;
36 begin
37 Result := TInternalCastleFpImage.Create(Width, Height);
38 //Result := TFPMemoryImage.Create(Width, Height);
39 //Result := CreateFPCompactImg(GetFPCompactImgDesc(false, 8, true), Width, Height);
40 end;
41
42 function TInternalCastleFpImage.GetColors8Bit(const x, y: integer): TFPCompactImgRGBA8BitValue;
43 begin
44 Result := FData[X + Y * Width];
45 end;
46
47 procedure TInternalCastleFpImage.SetColors8Bit(const x, y: integer; const Value: TFPCompactImgRGBA8BitValue);
48 begin
49 FData[X + Y * Width] := Value;
50 end;
51
52 { TInternalCastleFpImage -> TEncodedImage --------------------------------------------- }
53
54 procedure TEncodedImage.FromFpImage(const FPImage: TInternalCastleFpImage);
55 begin
56 NotImplemented('FromFpImage');
57 end;
58
59 procedure TRGBImage.FromFpImage(const FPImage: TInternalCastleFpImage);
60 var
61 X, Y: Integer;
62 ResultPixels: PVector3Byte;
63 Color: TFPCompactImgRGBA8BitValue;
64 begin
65 SetSize(FPImage.Width, FPImage.Height);
66
67 ResultPixels := Pixels;
68 for Y := FPImage.Height - 1 downto 0 do
69 for X := 0 to FPImage.Width - 1 do
70 begin
71 Color := FPImage.Colors8Bit[X, Y];
72 ResultPixels^.X := Color.R;
73 ResultPixels^.Y := Color.G;
74 ResultPixels^.Z := Color.B;
75 Inc(ResultPixels);
76 end;
77 end;
78
79 procedure TRGBAlphaImage.FromFpImage(const FPImage: TInternalCastleFpImage);
80 var
81 Y: Integer;
82 RowSize: Integer;
83 SourceRow: PByte;
84 DestRow: PByte;
85 begin
86 SetSize(FPImage.Width, FPImage.Height);
87
88 RowSize := FPImage.Width * 4;
89 { prepare pointers }
90 SourceRow := PByte(FPImage.FData) + RowSize * FPImage.Height;
91 DestRow := FRawPixels;
92
93 { copy lines upside down }
94 for Y := FPImage.Height - 1 downto 0 do
95 begin
96 Dec(SourceRow, RowSize);
97 Move(SourceRow^, DestRow^, RowSize);
98 Inc(DestRow, RowSize);
99 end;
100 end;
101
102 procedure TGrayscaleImage.FromFpImage(const FPImage: TInternalCastleFpImage);
103 var
104 X, Y: Integer;
105 ResultPixels: PByte;
106 Color: TFPCompactImgRGBA8BitValue;
107 begin
108 SetSize(FPImage.Width, FPImage.Height);
109
110 ResultPixels := Pixels;
111 for Y := FPImage.Height - 1 downto 0 do
112 for X := 0 to FPImage.Width - 1 do
113 begin
114 Color := FPImage.Colors8Bit[X, Y];
115 ResultPixels^ := GrayscaleValue(Vector3Byte(
116 Color.R,
117 Color.G,
118 Color.B));
119 Inc(ResultPixels);
120 end;
121 end;
122
123 procedure TGrayscaleAlphaImage.FromFpImage(const FPImage: TInternalCastleFpImage);
124 var
125 X, Y: Integer;
126 ResultPixels: PVector2Byte;
127 Color: TFPCompactImgRGBA8BitValue;
128 begin
129 SetSize(FPImage.Width, FPImage.Height);
130
131 ResultPixels := Pixels;
132 for Y := FPImage.Height - 1 downto 0 do
133 for X := 0 to FPImage.Width - 1 do
134 begin
135 Color := FPImage.Colors8Bit[X, Y];
136 ResultPixels^.X := GrayscaleValue(Vector3Byte(
137 Color.R,
138 Color.G,
139 Color.B));
140 ResultPixels^.Y := Color.A;
141 Inc(ResultPixels);
142 end;
143 end;
144
145 procedure TRGBFloatImage.FromFpImage(const FPImage: TInternalCastleFpImage);
146 var
147 X, Y: Integer;
148 ResultPixels: PVector3;
149 Color: TFPCompactImgRGBA8BitValue;
150 begin
151 SetSize(FPImage.Width, FPImage.Height);
152
153 ResultPixels := Pixels;
154 for Y := FPImage.Height - 1 downto 0 do
155 for X := 0 to FPImage.Width - 1 do
156 begin
157 Color := FPImage.Colors8Bit[X, Y];
158 ResultPixels^.X := Color.R / 255;
159 ResultPixels^.Y := Color.G / 255;
160 ResultPixels^.Z := Color.B / 255;
161 Inc(ResultPixels);
162 end;
163 end;
164
165 { Convert TInternalCastleFpImage to the best TCastleImage class. }
166 function CreateFromFpImage(const FPImage: TInternalCastleFpImage;
167 const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
168
169 { FPImage doesn't provide us any information does the image contain useful
170 alpha channel, and is it grayscale.
171 So just iterate over pixels, and check it ourselves. }
172 procedure Detect(out Alpha, Grayscale: boolean);
173 var
174 X, Y: Integer;
175 Col: TFPCompactImgRGBA8BitValue;
176 begin
177 Alpha := false;
178 Grayscale := true;
179 for Y := FPImage.Height - 1 downto 0 do
180 for X := 0 to FPImage.Width - 1 do
181 begin
182 Col := FPImage.Colors8Bit[X, Y];
183 if Col.A <> $FF then
184 begin
185 Alpha := true;
186 if not Grayscale then Exit; // early exit for RGBA images
187 end;
188 if (Col.R <> Col.G) or
189 (Col.R <> Col.B) then
190 begin
191 Grayscale := false;
192 if Alpha then Exit; // early exit for RGBA images
193 end;
194 end;
195 end;
196
197 var
198 Alpha, Grayscale: boolean;
199 ImageClass: TEncodedImageClass;
200 begin
201 if High(AllowedImageClasses) = 0 then
202 begin
203 { if AllowedImageClasses forces a specific class, then just create this image class }
204 ImageClass := AllowedImageClasses[0];
205 end else
206 begin
207 { detect Alpha and Grayscale looking at image contents, to determine ImageClass }
208 Detect(Alpha, Grayscale);
209 if not Grayscale then
210 begin
211 if Alpha then
212 ImageClass := TRGBAlphaImage
213 else
214 ImageClass := TRGBImage;
215 end else
216 begin
217 if Alpha then
218 ImageClass := TGrayscaleAlphaImage
219 else
220 ImageClass := TGrayscaleImage;
221 end;
222
223 { if the optimal ImageClass (for these image contents) is not allowed,
224 try to find another class that works, and looses least information. }
225 if not ClassAllowed(ImageClass, AllowedImageClasses) then
226 begin
227 if ClassAllowed(TRGBAlphaImage, AllowedImageClasses) then
228 ImageClass := TRGBAlphaImage
229 else
230 if ClassAllowed(TRGBImage, AllowedImageClasses) then
231 ImageClass := TRGBImage
232 else
233 if ClassAllowed(TGrayscaleImage, AllowedImageClasses) then
234 ImageClass := TGrayscaleImage
235 else
236 if ClassAllowed(TGrayscaleAlphaImage, AllowedImageClasses) then
237 ImageClass := TGrayscaleAlphaImage
238 else
239 raise Exception.Create('Cannot load FPImage to any of formats allowed by LoadImage AllowedImageClasses');
240 end;
241 end;
242
243 Result := ImageClass.Create;
244 try
245 Result.FromFpImage(FPImage);
246 except FreeAndNil(Result); raise end;
247 end;
248
249 function Load_FpImage(const Stream: TStream;
250 const AllowedImageClasses: array of TEncodedImageClass;
251 const Reader: TFPCustomImageReader;
252 const ImageFormat: TImageFormat): TEncodedImage;
253 var
254 FPImage: TInternalCastleFpImage;
255 DetectedClass: TEncodedImageClass;
256 begin
257 { Unless exactly 1 image classes is allowed,
258 try to detect best class using format-specific DetectClass function.
259 This may avoid the need for time-consuming Detect(...) call in CreateFromFpImage.
260 Note that we don't limit this detection only to AllowedImageClasses = [] case,
261 as LoadImage is often called with TextureImageClasses or TextureImageClassesAll. }
262 DetectedClass := nil;
263 if (High(AllowedImageClasses) <> 0) and
264 Assigned(ImageFormatInfos[ImageFormat].DetectClass) then
265 begin
266 DetectedClass := ImageFormatInfos[ImageFormat].DetectClass(Stream);
267 if (DetectedClass <> nil) and
268 (not ClassAllowed(DetectedClass, AllowedImageClasses)) then
269 begin
270 DetectedClass := nil; // do not use
271 end;
272 // for debug:
273 // if DetectedClass <> nil then
274 // WritelnLog('Will use detected image class %s (format %s)', [
275 // DetectedClass.ClassName,
276 // ImageFormatInfos[ImageFormat].FormatName
277 // ]);
278 end;
279
280 FPImage := CreateFpImage(0, 0);
281 try
282 FPImage.UsePalette := false;
283 try
284 FPImage.LoadFromStream(Stream, Reader);
285 except
286 { FPImage.LoadFromStream may raise any kind of Exception (for example,
287 in case of invalid JPEG header it just raises generic Exception class).
288 Catch it and convert to our EImageLoadError. }
289 on E: Exception do
290 raise EImageLoadError.Create(E.Message)
291 end;
292
293 if DetectedClass <> nil then
294 Result := CreateFromFpImage(FPImage, [DetectedClass])
295 else
296 Result := CreateFromFpImage(FPImage, AllowedImageClasses);
297 finally
298 FreeAndNil(FPImage);
299 Reader.Free;
300 end;
301 end;
302
303 { TEncodedImage -> TInternalCastleFpImage --------------------------------------------- }
304
305 function TEncodedImage.ToFpImage: TInternalCastleFpImage;
306 begin
307 raise EImageSaveError.Create('Cannot convert to FPImage the images of class ' + ClassName);
308 Result := nil; // avoid warning
309 end;
310
311 function TGrayscaleImage.ToFpImage: TInternalCastleFpImage;
312 var
313 X, Y: Integer;
314 P: PByte;
315 Color: TFPCompactImgRGBA8BitValue;
316 begin
317 Result := CreateFpImage(Width, Height);
318 try
319 Result.UsePalette := false;
320 P := Pixels;
321 for Y := Result.Height - 1 downto 0 do
322 for X := 0 to Result.Width - 1 do
323 begin
324 Color.R := P^;
325 Color.G := Color.R;
326 Color.B := Color.R;
327 Color.A := $FF;
328 Result.Colors8Bit[X, Y] := Color;
329 Inc(P);
330 end;
331 except FreeAndNil(Result); raise end;
332 end;
333
334 function TGrayscaleAlphaImage.ToFpImage: TInternalCastleFpImage;
335 var
336 X, Y: Integer;
337 P: PVector2Byte;
338 Color: TFPCompactImgRGBA8BitValue;
339 begin
340 Result := CreateFpImage(Width, Height);
341 try
342 Result.UsePalette := false;
343 P := Pixels;
344 for Y := Result.Height - 1 downto 0 do
345 for X := 0 to Result.Width - 1 do
346 begin
347 Color.R := P^.X;
348 Color.G := Color.R;
349 Color.B := Color.R;
350 Color.A := P^.Y;
351 Result.Colors8Bit[X, Y] := Color;
352 Inc(P);
353 end;
354 except FreeAndNil(Result); raise end;
355 end;
356
357 function TRGBImage.ToFpImage: TInternalCastleFpImage;
358 var
359 X, Y: Integer;
360 P: PVector3Byte;
361 Color: TFPCompactImgRGBA8BitValue;
362 begin
363 Result := CreateFpImage(Width, Height);
364 try
365 Result.UsePalette := false;
366 P := Pixels;
367 for Y := Result.Height - 1 downto 0 do
368 for X := 0 to Result.Width - 1 do
369 begin
370 Color.R := P^.X;
371 Color.G := P^.Y;
372 Color.B := P^.Z;
373 Color.A := $FF;
374 Result.Colors8Bit[X, Y] := Color;
375 Inc(P);
376 end;
377 except FreeAndNil(Result); raise end;
378 end;
379
380 function TRGBAlphaImage.ToFpImage: TInternalCastleFpImage;
381 var
382 X, Y: Integer;
383 P: PVector4Byte;
384 Color: TFPCompactImgRGBA8BitValue;
385 begin
386 Result := CreateFpImage(Width, Height);
387 try
388 Result.UsePalette := false;
389 P := Pixels;
390 for Y := Result.Height - 1 downto 0 do
391 for X := 0 to Result.Width - 1 do
392 begin
393 Color.R := P^.X;
394 Color.G := P^.Y;
395 Color.B := P^.Z;
396 Color.A := P^.W;
397 Result.Colors8Bit[X, Y] := Color;
398 Inc(P);
399 end;
400 except FreeAndNil(Result); raise end;
401 end;
402
403 function TRGBFloatImage.ToFpImage: TInternalCastleFpImage;
404 var
405 X, Y: Integer;
406 P: PVector3;
407 Col: TVector3Byte;
408 Color: TFPCompactImgRGBA8BitValue;
409 begin
410 Result := CreateFpImage(Width, Height);
411 try
412 Result.UsePalette := false;
413 P := Pixels;
414 for Y := Result.Height - 1 downto 0 do
415 for X := 0 to Result.Width - 1 do
416 begin
417 Col := Vector3Byte(P^);
418 Color.R := Col.X;
419 Color.G := Col.Y;
420 Color.B := Col.Z;
421 Color.A := $FF;
422 Result.Colors8Bit[X, Y] := Color;
423 Inc(P);
424 end;
425 except FreeAndNil(Result); raise end;
426 end;
427
428 procedure Save_FpImage(Img: TEncodedImage; Stream: TStream;
429 Writer: TFPCustomImageWriter);
430 var
431 FPImage: TInternalCastleFpImage;
432 begin
433 FPImage := nil;
434 try
435 {$warnings off} // using deprecated in deprecated
436 FPImage := Img.ToFpImage;
437 {$warnings on}
438 try
439 FPImage.SaveToStream(Stream, Writer);
440 { FPImage.SaveToStream may raise any kind of Exception.
441 Catch it and convert to our EImageSaveError. }
442 except on E: Exception do raise EImageSaveError.Create(E.Message) end;
443 finally
444 FreeAndNil(FPImage);
445 FreeAndNil(Writer);
446 end;
447 end;
448
449 { resizing ------------------------------------------------------------------- }
450
451 function TCastleImage.MakeResizedToFpImage(ResizeWidth, ResizeHeight: Cardinal;
452 const Interpolation: TResizeInterpolation): TInternalCastleFpImage;
453
454 function CreateInterpolation(const I: TResizeInterpolation): TFPBaseInterpolation;
455 begin
456 case I of
457 riNearest : Result := TBoxInterpolation.Create;
458 riBilinear : Result := TBilineairInterpolation.Create;
459 riMitchel : Result := TMitchelInterpolation.Create;
460 riBlackman : Result := TBlackmanInterpolation.Create;
461 riBlackmanSinc : Result := TBlackmanSincInterpolation.Create;
462 riBlackmanBessel : Result := TBlackmanBesselInterpolation.Create;
463 riGaussian : Result := TGaussianInterpolation.Create;
464 riHermite : Result := THermiteInterpolation.Create;
465 riLanczos : Result := TLanczosInterpolation.Create;
466 riQuadratic : Result := TQuadraticInterpolation.Create;
467 riCubic : Result := TCubicInterpolation.Create;
468 riCatrom : Result := TCatromInterpolation.Create;
469 riHanning : Result := THanningInterpolation.Create;
470 riHamming : Result := THammingInterpolation.Create;
471 {$ifndef COMPILER_CASE_ANALYSIS}
472 else raise EInternalError.Create('CreateInterpolation:I?');
473 {$endif}
474 end;
475 end;
476
477 var
478 Interpolator: TFPBaseInterpolation;
479 FPImageSource: TInternalCastleFpImage;
480 CanvasDestination: TFPImageCanvas;
481 begin
482 if ResizeWidth = 0 then ResizeWidth := Width;
483 if ResizeHeight = 0 then ResizeHeight := Height;
484
485 {$warnings off} // using deprecated in deprecated
486 FPImageSource := ToFpImage;
487 {$warnings on}
488 try
489 Interpolator := CreateInterpolation(Interpolation);
490 try
491 Result := CreateFpImage(ResizeWidth, ResizeHeight);
492 try
493 {$warnings off} // contains some abstract methods (FCL bug?)
494 CanvasDestination := TFPImageCanvas.Create(Result);
495 {$warnings on}
496 try
497 CanvasDestination.Interpolation := Interpolator;
498 CanvasDestination.StretchDraw(0, 0, ResizeWidth, ResizeHeight, FPImageSource);
499 finally FreeAndNil(CanvasDestination) end;
500 except FreeAndNil(Result); raise end;
501 finally FreeAndNil(Interpolator) end;
502 finally FreeAndNil(FPImageSource) end;
503 end;
504
505 {$endif FPC}