Debian Codegraph @

src/images/castleimages_vcl_imaging.inc

raw
1 {%MainUnit castleimages.pas}
2 {
3 Copyright 2021-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 { Handle images format using Delphi's Vcl.Imaging:
18 https://docwiki.embarcadero.com/Libraries/Sydney/en/Vcl.Imaging }
19
20 {$ifndef FPC}
21 {$ifndef USE_VAMPYRE_IMAGING}
22
23 function LoadPNG_PngImage(const Stream: TStream;
24 const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
25 var
26 PngImage: TPngImage;
27 ImageClass: TCastleImageClass;
28 W, H: Cardinal;
29
30 procedure CopyRGB;
31 var
32 X, Y: Integer;
33 PngScanline: PVector3Byte;
34 OutImage: PVector3Byte;
35 begin
36 for Y := 0 to H - 1 do
37 begin
38 PngScanline := PVector3Byte(PngImage.Scanline[H - 1 - Y]);
39 OutImage := PVector3Byte(TCastleImage(Result).RowPtr(Y));
40 Move(PngScanline^, OutImage^, W * 3);
41 // convert BGR -> RGB
42 for X := 0 to W - 1 do
43 begin
44 SwapValues(OutImage^.X, OutImage^.Z);
45 Inc(OutImage);
46 end;
47 end;
48 end;
49
50 procedure CopyRGBAlpha;
51 var
52 X, Y: Integer;
53 PngScanline: PVector3Byte;
54 PngAlphaScanline: PByte;
55 OutImage: PVector4Byte;
56 begin
57 for Y := 0 to H - 1 do
58 begin
59 PngScanline := PVector3Byte(PngImage.Scanline[H - 1 - Y]);
60 PngAlphaScanline := PByte(PngImage.AlphaScanline[H - 1 - Y]);
61 OutImage := PVector4Byte(TCastleImage(Result).RowPtr(Y));
62 for X := 0 to W - 1 do
63 begin
64 // convert BGR -> RGB by the way
65 OutImage^.X := PngScanline^[2];
66 OutImage^.Y := PngScanline^[1];
67 OutImage^.Z := PngScanline^[0];
68 OutImage^.W := PngAlphaScanline^;
69 Inc(PngScanline);
70 Inc(PngAlphaScanline);
71 Inc(OutImage);
72 end;
73 end;
74 end;
75
76 begin
77 PngImage := TPngImage.Create;
78 try
79 PngImage.LoadFromStream(Stream);
80
81 if PngImage.Header.BitDepth <> 8 then
82 raise EImageLoadError.Create('Loading PNG using PngImage: supports only 8-bit PNG images');
83
84 case PngImage.Header.ColorType of
85 PNG_COLOR_TYPE_GRAY : ImageClass := TGrayscaleImage;
86 PNG_COLOR_TYPE_RGB : ImageClass := TRGBImage;
87 PNG_COLOR_TYPE_RGB_ALPHA : ImageClass := TRGBAlphaImage;
88 PNG_COLOR_TYPE_GRAY_ALPHA: ImageClass := TGrayscaleAlphaImage;
89 else
90 // possible in case of PNG_COLOR_TYPE_PALETTE
91 raise EImageLoadError.Create('Loading PNG using PngImage: does not support PNG images with palette');
92 end;
93
94 // TODO: what happens in case of transparency by tRNS
95
96 W := PngImage.Header.Width;
97 H := PngImage.Header.Height;
98 Result := ImageClass.Create(W, H);
99 try
100 case PngImage.Header.ColorType of
101 PNG_COLOR_TYPE_RGB: CopyRgb;
102 PNG_COLOR_TYPE_RGB_ALPHA: CopyRGBAlpha;
103 // TODO: PNG_COLOR_TYPE_GRAY
104 // TODO: PNG_COLOR_TYPE_GRAY_ALPHA
105 else raise EImageLoadError.CreateFmt('Loading PNG using PngImage: Color type %d not implemented', [
106 PngImage.Header.ColorType
107 ]);
108 end;
109 except FreeAndNil(Result); raise end;
110 finally FreeAndNil(PngImage) end;
111 end;
112
113 {$endif}
114 {$endif}
115