Skip to content

Commit 2c92849

Browse files
authored
Add VTarget.getBlob for memory-backed targets, improve docs (#83)
1 parent 1ddb393 commit 2c92849

23 files changed

+5465
-11
lines changed

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ repositories {
2020
}
2121

2222
dependencies {
23-
implementation("app.photofox.vips-ffm:vips-ffm-core:0.5.11")
23+
implementation("app.photofox.vips-ffm:vips-ffm-core:0.5.12")
2424
}
2525
```
2626
When running your project you must add: `--enable-native-access=ALL-UNNAMED` to your JVM runtime arguments. If you

core/src/main/java/app/photofox/vipsffm/VTarget.java

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package app.photofox.vipsffm;
22

3+
import app.photofox.vipsffm.jextract.VipsTarget;
4+
35
import java.lang.foreign.Arena;
46
import java.lang.foreign.MemorySegment;
57

@@ -8,12 +10,14 @@
810
*/
911
public class VTarget {
1012

13+
final Arena arena;
1114
final MemorySegment address;
1215

13-
VTarget(MemorySegment address) throws VipsError {
16+
VTarget(Arena arena, MemorySegment address) throws VipsError {
1417
if (!VipsValidation.isValidPointer(address)) {
1518
throw new VipsError("invalid pointer used for creation");
1619
}
20+
this.arena = arena;
1721
this.address = address;
1822
}
1923

@@ -51,18 +55,40 @@ public MemorySegment getUnsafeStructAddress() {
5155
return this.address;
5256
}
5357

54-
public static VTarget newToDescriptor(Arena arena, int descriptor) {
58+
/**
59+
* Create a new target pointed at a file descriptor
60+
* This target can be used with (for example) {@link VImage#writeToTarget(VTarget, String, VipsOption...)}
61+
*/
62+
public static VTarget newToDescriptor(Arena arena, int descriptor) throws VipsError {
5563
var pointer = VipsHelper.target_new_to_descriptor(arena, descriptor);
56-
return new VTarget(pointer);
64+
return new VTarget(arena, pointer);
5765
}
5866

59-
public static VTarget newToFile(Arena arena, String filename) {
67+
/**
68+
* Create a new target pointed at an output file
69+
* This target can be used with (for example) {@link VImage#writeToTarget(VTarget, String, VipsOption...)}
70+
*/
71+
public static VTarget newToFile(Arena arena, String filename) throws VipsError {
6072
var pointer = VipsHelper.target_new_to_file(arena, filename);
61-
return new VTarget(pointer);
73+
return new VTarget(arena, pointer);
6274
}
6375

64-
public static VTarget newToMemory(Arena arena) {
76+
/**
77+
* Create a new memory-backed VipsTarget
78+
* This target can be used with (for example) {@link VImage#writeToTarget(VTarget, String, VipsOption...)}
79+
* After writing to this target, you can also retrieve the backing {@link VBlob} with {@link #getBlob()}
80+
*/
81+
public static VTarget newToMemory(Arena arena) throws VipsError {
6582
var pointer = VipsHelper.target_new_to_memory(arena);
66-
return new VTarget(pointer);
83+
return new VTarget(arena, pointer);
84+
}
85+
86+
/**
87+
* Only valid for memory-backed targets (eg via {@link #newToMemory(Arena)})
88+
* Returns the contents of the backing VBlob
89+
*/
90+
public VBlob getBlob() throws VipsError {
91+
var blob = VipsTarget.blob(this.address);
92+
return new VBlob(arena, blob);
6793
}
6894
}

core/src/main/java/app/photofox/vipsffm/VipsInvoker.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ private static void readGValueAndSetOptionValue(
145145
VipsRaw.g_object_get_property(operation, keyCString, gvaluePointer);
146146
var vipsPointer = g_value_get_object(gvaluePointer);
147147
vipsPointer = refGObjectToArenaScope(arena, vipsPointer);
148-
var vipsObject = new VTarget(vipsPointer);
148+
var vipsObject = new VTarget(arena, vipsPointer);
149149
o.setValue(vipsObject);
150150
}
151151
case VipsOption.Blob o -> {
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsConnection {
18+
* VipsObject parent_object;
19+
* int descriptor;
20+
* int tracked_descriptor;
21+
* int close_descriptor;
22+
* char *filename;
23+
* } VipsConnection
24+
* }
25+
*/
26+
public class VipsConnection extends _VipsConnection {
27+
28+
VipsConnection() {
29+
// Should not be called directly
30+
}
31+
}
32+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsConnectionClass {
18+
* VipsObjectClass parent_class;
19+
* } VipsConnectionClass
20+
* }
21+
*/
22+
public class VipsConnectionClass extends _VipsConnectionClass {
23+
24+
VipsConnectionClass() {
25+
// Should not be called directly
26+
}
27+
}
28+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsObject VipsObject
18+
* }
19+
*/
20+
public class VipsObject extends _VipsObject {
21+
22+
VipsObject() {
23+
// Should not be called directly
24+
}
25+
}
26+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsObjectClass VipsObjectClass
18+
* }
19+
*/
20+
public class VipsObjectClass extends _VipsObjectClass {
21+
22+
VipsObjectClass() {
23+
// Should not be called directly
24+
}
25+
}
26+
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef void *(*VipsObjectSetArguments)(VipsObject *, void *, void *)
18+
* }
19+
*/
20+
public class VipsObjectSetArguments {
21+
22+
VipsObjectSetArguments() {
23+
// Should not be called directly
24+
}
25+
26+
/**
27+
* The function pointer signature, expressed as a functional interface
28+
*/
29+
public interface Function {
30+
MemorySegment apply(MemorySegment object, MemorySegment a, MemorySegment b);
31+
}
32+
33+
private static final FunctionDescriptor $DESC = FunctionDescriptor.of(
34+
VipsRaw.C_POINTER,
35+
VipsRaw.C_POINTER,
36+
VipsRaw.C_POINTER,
37+
VipsRaw.C_POINTER
38+
);
39+
40+
/**
41+
* The descriptor of this function pointer
42+
*/
43+
public static FunctionDescriptor descriptor() {
44+
return $DESC;
45+
}
46+
47+
private static final MethodHandle UP$MH = VipsRaw.upcallHandle(VipsObjectSetArguments.Function.class, "apply", $DESC);
48+
49+
/**
50+
* Allocates a new upcall stub, whose implementation is defined by {@code fi}.
51+
* The lifetime of the returned segment is managed by {@code arena}
52+
*/
53+
public static MemorySegment allocate(VipsObjectSetArguments.Function fi, Arena arena) {
54+
return Linker.nativeLinker().upcallStub(UP$MH.bindTo(fi), $DESC, arena);
55+
}
56+
57+
private static final MethodHandle DOWN$MH = Linker.nativeLinker().downcallHandle($DESC);
58+
59+
/**
60+
* Invoke the upcall stub {@code funcPtr}, with given parameters
61+
*/
62+
public static MemorySegment invoke(MemorySegment funcPtr,MemorySegment object, MemorySegment a, MemorySegment b) {
63+
try {
64+
return (MemorySegment) DOWN$MH.invokeExact(funcPtr, object, a, b);
65+
} catch (Throwable ex$) {
66+
throw new AssertionError("should not reach here", ex$);
67+
}
68+
}
69+
}
70+
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsTarget {
18+
* VipsConnection parent_object;
19+
* gboolean memory;
20+
* gboolean ended;
21+
* GString *memory_buffer;
22+
* VipsBlob *blob;
23+
* unsigned char output_buffer[8500];
24+
* int write_point;
25+
* gint64 position;
26+
* gboolean delete_on_close;
27+
* char *delete_on_close_filename;
28+
* } VipsTarget
29+
* }
30+
*/
31+
public class VipsTarget extends _VipsTarget {
32+
33+
VipsTarget() {
34+
// Should not be called directly
35+
}
36+
}
37+
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
// Generated by jextract
2+
3+
package app.photofox.vipsffm.jextract;
4+
5+
import java.lang.invoke.*;
6+
import java.lang.foreign.*;
7+
import java.nio.ByteOrder;
8+
import java.util.*;
9+
import java.util.function.*;
10+
import java.util.stream.*;
11+
12+
import static java.lang.foreign.ValueLayout.*;
13+
import static java.lang.foreign.MemoryLayout.PathElement.*;
14+
15+
/**
16+
* {@snippet lang=c :
17+
* typedef struct _VipsTargetClass {
18+
* VipsConnectionClass parent_class;
19+
* gint64 (*write)(VipsTarget *, const void *, size_t);
20+
* void (*finish)(VipsTarget *);
21+
* gint64 (*read)(VipsTarget *, void *, size_t);
22+
* gint64 (*seek)(VipsTarget *, gint64, int);
23+
* int (*end)(VipsTarget *);
24+
* } VipsTargetClass
25+
* }
26+
*/
27+
public class VipsTargetClass extends _VipsTargetClass {
28+
29+
VipsTargetClass() {
30+
// Should not be called directly
31+
}
32+
}
33+

0 commit comments

Comments
 (0)