Debian Codegraph @

src/images/castleimages_png.inc

raw
1 {%MainUnit castleimages.pas}
2 {
3 Copyright 2019-2023 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 PNG format. }
18
19 var
20 WarningNoLibPngDone: Boolean;
21
22 { Lack of LibPng is important to report, as it causes PNG loading to be much slower.
23
24 Note that this warning shall be done only if you load PNG,
25 lack of LibPng is harmless if your application doesn't load PNG (and in this case
26 our build tool would not copy libpng DLL into the project on Windows,
27 so CastlePngInitialized will rightly fail). }
28 procedure WarningNoLibPng;
29 begin
30 if not WarningNoLibPngDone then
31 begin
32 WarningNoLibPngDone := true;
33
34 {$if defined(CASTLE_PNG_DYNAMIC) or defined(CASTLE_PNG_STATIC)}
35 { Do not make any warning when InternalDisableDynamicLibraries,
36 build tool deliberately uses this (see tools/build-tool/code/tooldisabledynamiclibraries.pas)
37 and it is harmless, there's no problem with build tool not using faster LibPng. }
38 if not InternalDisableDynamicLibraries then
39 begin
40 if not CastleZLibInitialized then
41 WritelnWarning('Initializing dynamic Zlib library failed.' + NL +
42 ' Note that it may also cause failures to initialize libraries that require Zlib, like LibPng.'
43 {$ifdef MSWINDOWS} +
44 NL +
45 ' Make sure you have copied the required DLL files alongside the EXE file, with the correct CPU architecture (32-bit vs 64-bit).' + NL +
46 ' We advise to build your applications using Castle Game Engine editor or (command-line) build tool, that will automatically place the required DLL files.'
47 {$endif}
48 );
49
50 if not CastlePngInitialized then
51 WritelnWarning('Initializing dynamic LibPng library failed.' + NL +
52 ' Loading PNG will use a slower approach.'
53 {$ifdef MSWINDOWS} +
54 NL +
55 ' Make sure you have copied the required DLL files alongside the EXE file, with the correct CPU architecture (32-bit vs 64-bit).' + NL +
56 ' We advise to build your applications using Castle Game Engine editor or (command-line) build tool, that will automatically place the required DLL files.'
57 {$endif}
58 );
59 end;
60 {$else}
61 WritelnWarning('Initializing LibPng failed, LibPng support not compiled-in.' + NL +
62 ' Loading PNG will use a slower approach.');
63 {$endif}
64 end;
65 end;
66
67 function LoadPNG(const Stream: TStream;
68 const AllowedImageClasses: array of TEncodedImageClass): TEncodedImage;
69 begin
70 // use LibPng, this is the preferred (fastest) option
71 {$if defined(CASTLE_PNG_DYNAMIC) or defined(CASTLE_PNG_STATIC)}
72 if CastlePngInitialized then
73 begin
74 // Necessary on Android, where InitializePNGUsingLibpng in "initialization" was too early.
75 InitializePNGUsingLibpng;
76 Result := LoadPNG_LibPng(Stream, AllowedImageClasses);
77 end else
78 {$endif}
79 begin
80 WarningNoLibPng;
81 {$if defined(USE_VAMPYRE_IMAGING)} // fallback LibPng -> Vampyre
82 Result := Load_VampyreImaging(Stream, AllowedImageClasses);
83 {$elseif defined(FPC)} // fallback LibPng -> FpImage (FPC only)
84 Result := Load_FpImage(Stream, AllowedImageClasses, TFPReaderPNG.Create, ifPNG)
85 {$else} // fallback LibPng -> PngImage (Delphi only)
86 Result := LoadPNG_PngImage(Stream, AllowedImageClasses);
87 {$endif}
88 end;
89 end;
90
91 procedure SavePNG(const Img: TEncodedImage; const Stream: TStream);
92
93 {$ifdef FPC}
94 procedure SavePNG_FPImage(const Img: TEncodedImage; const Stream: TStream);
95 var
96 Writer: TFPWriterPNG;
97 begin
98 Writer := TFPWriterPNG.Create;
99 { without this, Writer.UseAlpha is always false and TFPWriterPNG never
100 stores alpha channel of png image. }
101 Writer.UseAlpha := Img.HasAlpha;
102 { By default, we produce 16-bit PNG. Which usually is useless
103 (as our inputs is 8-bit), and makes "nvcompress" on macOS fail
104 on PNG files produced by our "downscaling". }
105 Writer.WordSized := false;
106 Save_FpImage(Img, Stream, Writer);
107 end;
108 {$endif}
109
110 begin
111 {$if defined(CASTLE_PNG_DYNAMIC) or defined(CASTLE_PNG_STATIC)}
112 if CastlePngInitialized then
113 begin
114 // Necessary on Android, where InitializePNGUsingLibpng in "initialization" was too early.
115 InitializePNGUsingLibpng;
116 SavePNG_LibPng(Img, Stream);
117 end else
118 {$endif}
119 begin
120 {$if defined(USE_VAMPYRE_IMAGING)} // fallback LibPng -> Vampyre
121 Save_VampyreImaging(Img, Stream, 'png');
122 {$elseif defined(FPC)} // fallback LibPng -> FpImage
123 SavePNG_FPImage(Img, Stream);
124 {$else}
125 raise EImageSaveError.Create('Cannot save PNG: LibPNG not initialized, and Vampyre and FpImage not available at compile-time');
126 {$endif}
127 end;
128 end;