1 module derelict.vlfeat;
2 
3 import derelict.util.loader;
4 
5 /**
6     generic.h
7 */
8 
9 alias vl_size = size_t;
10 alias vl_type = int;
11 alias vl_uint32 = uint;
12 
13 /**
14     kmeans.h
15 */
16 
17 extern(System)
18 {
19 
20 /** @brief K-means algorithms */
21 
22     enum VlKMeansAlgorithm
23     {
24         VlKMeansLloyd,       /**< Lloyd algorithm */
25         VlKMeansElkan,       /**< Elkan algorithm */
26         VlKMeansANN          /**< Approximate nearest neighbors */
27     }
28 
29     /** @brief K-means initialization algorithms */
30 
31     enum VlKMeansInitialization
32     {
33         VlKMeansRandomSelection,  /**< Randomized selection */
34         VlKMeansPlusPlus          /**< Plus plus raondomized selection */
35     }
36 
37     /** ------------------------------------------------------------------
38     ** @brief K-means quantizer
39     **/
40 
41     struct VlKMeans
42     {
43         vl_type dataType ;                      /**< Data type. */
44         vl_size dimension ;                     /**< Data dimensionality. */
45         vl_size numCenters ;                    /**< Number of centers. */
46         vl_size numTrees ;                      /**< Number of trees in forest when using ANN-kmeans. */
47         vl_size maxNumComparisons ;             /**< Maximum number of comparisons when using ANN-kmeans. */
48 
49         VlKMeansInitialization initialization ; /**< Initalization algorithm. */
50         VlKMeansAlgorithm algorithm ;           /**< Clustring algorithm. */
51         VlVectorComparisonType distance ;       /**< Distance. */
52         vl_size maxNumIterations ;              /**< Maximum number of refinement iterations. */
53         double minEnergyVariation ;             /**< Minimum energy variation. */
54         vl_size numRepetitions ;                /**< Number of clustering repetitions. */
55         int verbosity ;                         /**< Verbosity level. */
56 
57         void * centers ;                        /**< Centers */
58         void * centerDistances ;                /**< Centers inter-distances. */
59 
60         double energy ;                         /**< Current solution energy. */
61         VlFloatVectorComparisonFunction floatVectorComparisonFn ;
62         VlDoubleVectorComparisonFunction doubleVectorComparisonFn ;
63     }
64 }
65 
66 /**
67     mathop.h
68 */
69 
70 extern(System)
71 {
72     alias VlFloatVectorComparisonFunction = float *function(vl_size, const float *, const float *);
73     alias VlDoubleVectorComparisonFunction = double *function(vl_size, const double *, const double *);
74 
75     enum VlVectorComparisonType
76     {
77         VlDistanceL1,        /**< l1 distance (squared intersection metric) */
78         VlDistanceL2,        /**< squared l2 distance */
79         VlDistanceChi2,      /**< squared Chi2 distance */
80         VlDistanceHellinger, /**< squared Hellinger's distance */
81         VlDistanceJS,        /**< squared Jensen-Shannon distance */
82         VlDistanceMahalanobis,     /**< squared mahalanobis distance */
83         VlKernelL1,          /**< intersection kernel */
84         VlKernelL2,          /**< l2 kernel */
85         VlKernelChi2,        /**< Chi2 kernel */
86         VlKernelHellinger,   /**< Hellinger's kernel */
87         VlKernelJS           /**< Jensen-Shannon kernel */
88     }
89 }
90 
91 /**
92     sift.h
93 */
94 
95 /** @brief SIFT filter pixel type */
96 alias vl_sift_pix = float ;
97 
98 /** ------------------------------------------------------------------
99  ** @brief SIFT filter keypoint
100  **
101  ** This structure represent a keypoint as extracted by the SIFT
102  ** filter ::VlSiftFilt.
103  **/
104 
105 extern(System)
106 {
107     struct VlSiftKeypoint
108     {
109         int o ;           /**< o coordinate (octave). */
110 
111         int ix ;          /**< Integer unnormalized x coordinate. */
112         int iy ;          /**< Integer unnormalized y coordinate. */
113         int is_ ;          /**< Integer s coordinate. */
114 
115         float x ;     /**< x coordinate. */
116         float y ;     /**< y coordinate. */
117         float s ;     /**< s coordinate. */
118         float sigma ; /**< scale. */
119     }
120 
121     /** ------------------------------------------------------------------
122     ** @brief SIFT filter
123     **
124     ** This filter implements the SIFT detector and descriptor.
125     **/
126 
127     struct VlSiftFilt
128     {
129         double sigman ;       /**< nominal image smoothing. */
130         double sigma0 ;       /**< smoothing of pyramid base. */
131         double sigmak ;       /**< k-smoothing */
132         double dsigma0 ;      /**< delta-smoothing. */
133 
134         int width ;           /**< image width. */
135         int height ;          /**< image height. */
136         int O ;               /**< number of octaves. */
137         int S ;               /**< number of levels per octave. */
138         int o_min ;           /**< minimum octave index. */
139         int s_min ;           /**< minimum level index. */
140         int s_max ;           /**< maximum level index. */
141         int o_cur ;           /**< current octave. */
142 
143         vl_sift_pix *temp ;   /**< temporary pixel buffer. */
144         vl_sift_pix *octave ; /**< current GSS data. */
145         vl_sift_pix *dog ;    /**< current DoG data. */
146         int octave_width ;    /**< current octave width. */
147         int octave_height ;   /**< current octave height. */
148 
149         vl_sift_pix *gaussFilter ;  /**< current Gaussian filter */
150         double gaussFilterSigma ;   /**< current Gaussian filter std */
151         vl_size gaussFilterWidth ;  /**< current Gaussian filter width */
152 
153         VlSiftKeypoint* keys ;/**< detected keypoints. */
154         int nkeys ;           /**< number of detected keypoints. */
155         int keys_res ;        /**< size of the keys buffer. */
156 
157         double peak_thresh ;  /**< peak threshold. */
158         double edge_thresh ;  /**< edge threshold. */
159         double norm_thresh ;  /**< norm threshold. */
160         double magnif ;       /**< magnification factor. */
161         double windowSize ;   /**< size of Gaussian window (in spatial bins) */
162 
163         vl_sift_pix *grad ;   /**< GSS gradient data. */
164         int grad_o ;          /**< GSS gradient data octave. */
165     }
166 }
167 
168 private
169 {
170     import derelict.util.system;
171 
172     static if(Derelict_OS_Linux)
173     {
174         version(X86_64)
175             enum libNames = "libvl.so";
176         else
177             static assert(0, "Need to implement VLFeat libNames for this arch.");
178     }
179     else
180     {
181         static assert(0, "Need to implement VLFeat libNames for this operating system.");
182     }
183 
184     enum functionTypes = [
185         //kmeans.h
186         ["VlKMeans *", "vl_kmeans_new", "vl_type", "VlVectorComparisonType"],
187         ["VlKMeans *", "vl_kmeans_new_copy", "const(VlKMeans) *"],
188         ["void", "vl_kmeans_delete", "VlKMeans *"],
189         ["void", "vl_kmeans_reset", "VlKMeans *"],
190         ["double", "vl_kmeans_cluster", "VlKMeans *", "const void *", "vl_size", "vl_size", "vl_size"],
191         ["void", "vl_kmeans_quantize", "VlKMeans *", "vl_uint32 *", "void *", "const void *", "vl_size"],
192         ["void", "vl_kmeans_set_centers", "VlKMeans *", "const void *", "vl_size", "vl_size"],
193         ["void", "vl_kmeans_init_centers_with_rand_data", "VlKMeans *", "const void *", "vl_size", "vl_size", "vl_size"],
194         ["void", "vl_kmeans_init_centers_plus_plus", "VlKMeans *", "const void *", "vl_size", "vl_size", "vl_size"],
195         ["double", "vl_kmeans_refine_centers", "VlKMeans *", "const void *", "vl_size"],
196 
197         //sift.h
198         ["VlSiftFilt*", "vl_sift_new", "int", "int", "int", "int", "int"],
199         ["void", "vl_sift_delete", "VlSiftFilt *"],
200         ["int", "vl_sift_process_first_octave", "VlSiftFilt *", "const vl_sift_pix *"],
201         ["int", "vl_sift_process_next_octave", "VlSiftFilt *"],
202         ["void", "vl_sift_detect", "VlSiftFilt *"],
203         ["int", "vl_sift_calc_keypoint_orientations", "VlSiftFilt *", "double[4]", "const VlSiftKeypoint *"],
204         ["void", "vl_sift_calc_keypoint_descriptor", "VlSiftFilt *", "vl_sift_pix *", "const VlSiftKeypoint *", "double"],
205         ["void", "vl_sift_calc_raw_descriptor", "const VlSiftFilt *", "const vl_sift_pix *", "vl_sift_pix *", "int", "int",
206             "double", "double", "double", "double"],
207         ["void", "vl_sift_keypoint_init", "const VlSiftFilt *", "VlSiftKeypoint *", "double", "double", "double"]
208     ];
209 
210     string generateFunctionAliases()
211     {
212         import std.algorithm : joiner;
213         import std.conv : to;
214 
215         string ret;
216 
217         foreach(ft; functionTypes)
218         {
219             ret ~= "alias da_" ~ ft[1] ~ " = " ~ ft[0] ~ " function(" ~ ft[2 .. $].joiner(",").to!string ~ ");";
220         }
221 
222         return ret;
223     }
224 
225     string generateFunctionPointers()
226     {
227         string ret;
228 
229         foreach(ft; functionTypes)
230         {
231             ret ~= "da_" ~ ft[1] ~ " " ~ ft[1] ~ ";";
232         }
233 
234         return ret;
235     }
236 
237     string generateFunctionBinds()
238     {
239         string ret;
240 
241         foreach(ft; functionTypes)
242         {
243             ret ~= "bindFunc(cast(void**)&" ~ ft[1] ~ ", \"" ~ ft[1] ~ "\");";
244         }
245 
246         return ret;
247     }
248 }
249 
250 extern(System) @nogc nothrow
251 {
252     mixin(generateFunctionAliases());
253 }
254 
255 __gshared
256 {
257     mixin(generateFunctionPointers());
258 }
259 
260 class DerelictVLFeatLoader : SharedLibLoader
261 {
262     public
263     {
264         this()
265         {
266             super(libNames);
267         }
268     }
269 
270     protected
271     {
272         override void loadSymbols()
273         {
274             mixin(generateFunctionBinds());
275         }
276     }
277 }
278 
279 __gshared DerelictVLFeatLoader DerelictVLFeat;
280 
281 shared static this()
282 {
283     DerelictVLFeat = new DerelictVLFeatLoader();
284 }
285 
286 unittest
287 {
288     DerelictVLFeat.load();
289 }