1 module vips.image;
2 
3 import vips.option;
4 import vips.bindings;
5 
6 import gobject.Value;
7 import gobject.ObjectG;
8 import gobject.c.types : GObject;
9 
10 struct VImage{
11 package:
12     VipsImage* img;
13 public:
14     this(VipsImage* source, bool isOwned = false)
15     {
16         img = source;
17         if(!isOwned){
18             g_object_ref(img);
19         }
20     }
21 
22     ~this()
23     {
24         if(img !is null){
25             g_object_unref(img);
26         }
27     }
28 
29     this(this)
30     {
31         g_object_ref(img);
32     }
33 
34     void saveToFile(string file)
35     {
36         import std.exception : enforce;
37         import std..string : fromStringz, toStringz;
38 
39         auto opName = vips_foreign_find_save(file.toStringz).fromStringz;
40         enforce(opName !is null, "Could not find appropriate VIPS operation to save with for for target file name: " ~ file);
41         auto options = VOption();
42         options
43             .set("in", this)
44             .set("filename", file);
45         baseOp(opName, options);
46     }
47 
48     static VImage fromFile(string file)
49     {
50         import std.exception : enforce;
51         import std..string : fromStringz, toStringz;
52         import std.typecons : scoped;
53         VImage image;
54         auto opName = vips_foreign_find_load(file.toStringz).fromStringz;
55         enforce(opName !is null, "Could not find appropriate VIPS operation to load image with for file: " ~ file);
56         auto options = VOption();
57         options
58             .set("filename", file)
59             .set("out", &image);
60         baseOp(opName, options);
61         return image;
62     }
63 
64     static GType getType()
65     {
66         return vips_image_get_type();
67     }
68 }
69 
70 package void baseOp(const(char)[] name, ref VOption options)
71 {
72     import std..string : fromStringz, toStringz;
73     import std.typecons : Unique;
74     import vips.operation : VOperation;
75 
76     auto op = VOperation(name);
77     op.build(options);
78 }
79 
80 unittest
81 {
82     import std..string : fromStringz;
83     import vips.operations : invert, thumbnail, thumbnail_image, rotate;
84 
85     vips_init("test");
86     scope(exit) vips_shutdown();
87     vips_cache_set_max(0);
88     vips_leak_set(true);
89     VImage image = VImage.fromFile("t.png");
90     foreach(i; 200 .. 300)
91     {
92         auto mid = image.invert().rotate(i % 4 * 90);
93         auto thumb = mid.thumbnail_image(i % 10 * 20 + 200);
94     }
95 }
96