11package app .photofox .vipsffm ;
22
33import java .lang .foreign .Arena ;
4+ import java .util .Locale ;
5+ import java .util .Optional ;
6+ import java .util .concurrent .atomic .AtomicBoolean ;
47
58/// Helper class for running Vips commands with an appropriate arena
69///
710/// Blocks untrusted operations by default
811public class Vips {
912
13+ private static final AtomicBoolean HAS_INITIALISED = new AtomicBoolean (false );
14+
15+ static {
16+ autoInit ();
17+ }
18+
19+ /// Note that you do not usually need to call [Vips#init] yourself, as initialisation is performed
20+ /// automatically when vips-ffm classes are used
1021 public static void init () {
11- init ( false , false );
22+ initOnce ( );
1223 }
1324
25+ /// @deprecated Please migrate to using [Vips#allowUntrustedOperations] and [Vips#enableLeakDetection], and note
26+ /// that calling [Vips#init] is no longer required
27+ @ Deprecated (forRemoval = true )
1428 public static void init (boolean allowUntrusted , boolean detectLeaks ) {
15- var arena = Arena .global ();
16- VipsHelper .init (arena , allowUntrusted );
17- VipsHelper .leak_set (detectLeaks );
29+ initOnce ();
30+ if (allowUntrusted ) {
31+ Vips .allowUntrustedOperations ();
32+ }
33+ if (detectLeaks ) {
34+ Vips .enableLeakDetection ();
35+ }
36+ }
37+
38+ // Intentionally package-private
39+ static void autoInit () {
40+ if (HAS_INITIALISED .get ()) {
41+ // Optimistically skip reading system property
42+ return ;
43+ }
44+ var shouldAutoInitString = Optional .ofNullable (System .getProperty ("vipsffm.autoinit" ))
45+ .orElse ("true" )
46+ .toLowerCase (Locale .ENGLISH );
47+ var shouldAutoInit = Boolean .parseBoolean (shouldAutoInitString );
48+ if (!shouldAutoInit ) {
49+ return ;
50+ }
51+
52+ initOnce ();
53+ }
54+
55+ private static void initOnce () {
56+ if (HAS_INITIALISED .get ()) {
57+ return ;
58+ }
59+ VipsHelper .init (Arena .global ());
60+ HAS_INITIALISED .set (true );
1861 }
1962
2063 /// Provides a scoped arena to provide a memory boundary for running libvips operations
@@ -30,6 +73,14 @@ public static void shutdown() {
3073 VipsHelper .shutdown ();
3174 }
3275
76+ /// Enables leak detection
77+ ///
78+ /// Calling [Vips#shutdown] will make libvips print out any leaks, and what classes hold memory references
79+ /// causing these leaks
80+ public static void enableLeakDetection () {
81+ VipsHelper .leak_set (true );
82+ }
83+
3384 /// Permits untrusted operations, such as loading PDFs
3485 ///
3586 /// vips-ffm blocks these by default - see the [libvips docs](https://www.libvips.org/API/8.17/func.block_untrusted_set.html)
0 commit comments