File size: 14,543 Bytes
d4035c1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/** @file    generic.c
 ** @author  Andrea Vedaldi
 ** @brief   Generic - Definition
 **/

/* AUTORIGHTS
Copyright (C) 2007-09 Andrea Vedaldi and Brian Fulkerson

This file is part of VLFeat, available in the terms of the GNU
General Public License version 2.
*/

/**
 @mainpage VLFeat -- Vision Lab Features Library

 @version __VLFEAT_VERSION__
 @author Andrea Vedaldi  (vedaldi@cs.ucla.edu)
 @author Brian Fulkerson (bfulkers@cs.ucla.edu)

 @par Copyright © 2007-09 Andrea Vedaldi and Brian Fulkerson

 <em>VLFeat C library contains implementations of common computer
 vision algorithms, with a special focus on visual features for
 matching image regions. Applications include structure from motion
 and object and category detection and recognition.

 We strive to make the library free of clutter, portable (VLFeat is
 largely C-89 compatible), and self- documented. Different parts of
 the library are weakly interdependent, simplifying understanding and
 extraction of code.</em>

 @section main-contents Contents

 - @ref design          "Design Concepts"
   - @ref design-objects     "Objects"
   - @ref design-resources   "Memory and resource management"
   - @ref design-threads     "Multi-threading"
   - @ref design-portability "Portability"
 - @ref main-glossary "Glossary"
 - Support functionalities
   - @ref host.h      "Platform abstraction"
   - @ref generic.h   "Errors, memory, logging, and others"
   - @ref random.h    "Random number generator"
   - @ref mathop.h    "Math operations"
   - @ref heap.h      "Heap (priority queue)"
   - @ref stringop.h  "String operations"
   - @ref imop.h      "Image operations"
   - @ref pgm.h       "PGM reading and writing"
   - @ref rodrigues.h "Rodrigues formula"
   - @ref mexutils.h  "MATLAB MEX helper functions"
 - Algorithms
   - @ref sift.h     "Scale Invariant Feature Transform (SIFT)"
   - @ref dsift.h    "Dense SIFT (DSIFT)"
   - @ref mser.h     "Maximally Stable Extremal Regions (MSER)"
   - @ref ikmeans.h  "Integer K-means (IKM)"
   - @ref hikmeans.h "Hierarchical Integer K-means (HIKM)"
   - @ref aib.h      "Agglomerative Information Bottleneck (AIB)"
   - @ref quickshift.h "Quick shift image segmentation"
   - @ref kdtree.h   "KDTree (fast and approximate nearest neighbors)"

 @section design VLFeat Design Concepts

 VLFeat follows a simple but rigorous design that makes it portable
 and simple to use in conjunction with high level language such as
 MATLAB. This section illustrates and motivates the aspects of the
 design that are relevant to the users of the library. Most of the
 features discussed in here are implemented in the @ref generic.h
 module.

 @subsection design-objects Objects

 Most of VLFeat functionalities are implemented as opaque data
 structures, to which we refer as "objects". Typically, you create an
 object by means of a constructor function and dispose of it by means
 of a destructor function. The data member of the object should not be
 accessed directly, but by means of appropriate accessor functions
 (typically containing the @c _get and _set keywords in their names).

 @subsection design-resources Memory and Resource Management

 Resource management in VLFeat is minimal. In most cases, you can
 assume that VLFeat does not provide any resource management
 functionality at all. Objects or memory blocks allocated by the
 library but owned by the client must be explicitly disposed. The
 following rule helps identifying such blocks and objects:

 <b> The client owns a memory block or object if, and only if, it is
   returned by a library call containing the keywords @c _new or @c
   _copy, or by the allocator functions ::vl_malloc, ::vl_calloc,
   ::vl_realloc.</b>

 More in detail, the following rules apply:

 - Memory is the only managed resource. Other resources used by the
   library (e.g. files) are either passed to the library by the
   client, or acquired and released within the scope of a library
   function call.
 - The memory manager can be customized through ::vl_set_alloc_func
   (which sets the implementations of ::vl_malloc, ::vl_realloc,
   ::vl_calloc and ::vl_free). The library allocates memory only through
   these functions.
 - The memory manager is global to all threads.
 - At any moment, there is only one memory manager in
   existence. ::vl_set_alloc_func can be used only before any other
   function is invoked, or right before or after the previous memory
   manager has relinquished all memory.
 - Such rules apply both to the library client and to the library
   implementation. The only exception regards the memory allocated in
   the global library state, which uses the native memory manager.

  These rules make it possible to undo all the work done by the
  library at any given instant. Disposing the memory allocated by the
  custom memory manager essentially "resets" the library (except for
  its global state). This functionality is used extensively in the
  implementation of MATLAB MEX functions that access the library to
  support abrupt interruption.

   @note The rules guarantee that all memory allocated by the library
     at any given time is allocated by the same memory manager, except
     for the global state that should survive the "reset"
     operation. In order to support multiple memory managers, one
     should keep track of the allocator of each object (or memory
     block). Moreover, partial de-allocation of a pool of objects is
     dangerous, as such objects may be referred by other objects that
     are not being de-allocated, corrupting their state. A proper
     solution to the latter problem is the retain/release mechanism
     implemented, for instance, by Apple Core Foundation or Cocoa.

  @subsection design-threads Multi-threading

  The library is currently <em>not</em> thread safe, but this support
  will be added in a future release.

  The library is almost entirely reentrant. The only thread-sensitive
  operations are on the global library state and are limited to:

  - Global library configuration (e.g. ::vl_set_alloc_func).
  - Random number generator state (@ref random.h).
  - Error handling (e.g. ::vl_err_no).

  @subsection design-portability Portability features

  Platform dependent details are isolated in the @ref generic.h
  library module. These include:

  - Atomic types (e.g. ::vl_int32).
  - Special syntaxes for the declaration of symbols exported by the library
    and inline functions (e.g. ::VL_EXPORT).
  - Host-dependent conversion of data endianess
    (e.g. ::vl_swap_host_big_endianness_8()).

  VLFeat uses processor specific features (e.g. Intel SSE) if those
  are available at compile time.

  <!-- @see http://www.macresearch.org/how_to_properly_use_sse3_and_ssse3_and_future_intel_vector_extensions_0  -->

  @section main-glossary Glossary

  - <b>Column-major.</b> A <em>M x N </em> matrix <em>A</em> is
  stacked with column-major order as the sequence \f$(A_{11}, A_{21},
  \dots, A_{12}, \dots)\f$. More in general, when stacking a multi
  dimensional array this indicates that the first index is the one
  varying most quickly, with the other followed in the natural order.
  - <b>Opaque structure.</b> A structure is opaque if the user is not supposed
  to access its member directly, but through appropriate interface functions.
  Opaque structures are commonly used to define objects.
  - <b>Row-major.</b> A <em>M x N </em> matrix <em>A</em> is
  stacked with row-major order as the sequence \f$(A_{11}, A_{12},
  \dots, A_{21}, \dots)\f$. More in general, when stacking a multi
  dimensional array this indicates that the last index is the one
  varying most quickly, with the other followed in reverse order.
  - <b>Feature frame.</b> A <em>feature frame</em> is the geometrical
  description of a visual features. For instance, the frame of
  a @ref sift.h "SIFT feature" is oriented disk and the frame of
  @ref mser.h "MSER feature" is either a compact and connected set or
  a disk.
  - <b>Feature descriptor.</b> A <em>feature descriptor</em> is a quantity
  (usually a vector) which describes compactly the appearance of an
  image region (usually corresponding to a feature frame).
**/

/**
 @file generic.h

 This module provides the following functionalities:

 - @ref generic-preproc
 - @ref generic-error
 - @ref generic-memory
 - @ref generic-logging
 - @ref generic-time

  @section generic-preproc C preprocessor

  VLFeat provides a few C preprocessor macros of general
  utility. These include stringification (::VL_STRINGIFY,
  ::VL_XSTRINGIFY) and concatenation (::VL_CAT, ::VL_XCAT) of symbols.

  @section generic-error Error handling

  Some VLFeat functions signal errors as the standard C library. Such
  functions return 0 when they succeed and -1 when they fail, and set
  the global variable ::vl_err_no with a code identifying the error
  occurred. This variable is never set on success and should be
  examined right after an error had occurred.

  @section generic-memory Memory allocation

  VLFeat uses the ::vl_malloc(), ::vl_realloc(), ::vl_calloc() and
  ::vl_free() functions to allocate memory. Normally these functions
  are mapped to the underlying standard C library implementations. However
  ::vl_set_alloc_func() can be used to map them to other implementations.
  For instance, in MATALB MEX files these functions are mapped to
  the MATLAB equivalent which has a garbage collection mechanism to cope
  with interruptions during execution.

  @section generic-logging Logging

  VLFeat uses the macros ::VL_PRINT and ::VL_PRINTF to print progress or
  debug informations. These functions are normally mapped to the @c
  printf function of the underlying standard C library. However
  ::vl_set_printf_func() can be used to map it to a different
  implementation. For instance, in MATLAB MEX files this function is
  mapped to @c mexPrintf. Setting the function to @c NULL disables
  logging.

  @section generic-time Measruing time

  VLFeat provides ::vl_tic() and ::vl_toc() as an easy way of
  measuring elapsed time.

**/

#include "generic.h"

#include <assert.h>
#include <stdlib.h>
#include <stdio.h>
#include <math.h>

#ifdef VL_OS_WIN
#include <Windows.h>
#endif

VL_EXPORT int vl_err_no = 0 ;
VL_EXPORT char vl_err_msg [VL_ERR_MSG_LEN + 1] = "" ;

/** ------------------------------------------------------------------
 ** @brief Get version string
 ** @return library version string
 **/

VL_EXPORT
char const * vl_get_version_string ()
{
  return VL_VERSION_STRING ;
}

/** ------------------------------------------------------------------
 ** @brief Print information about the library
 ** @return library version string
 **/

VL_EXPORT
void vl_print_info ()
{
  VL_PRINTF ("VLFeat version %s\n", vl_get_version_string()) ;
  vl_print_host_info () ;
}

/** @internal@brief A printf that does not do anything */
static int
do_nothing_printf (char const* format, ...)
{
  return 0 ;
}

/** @internal@brief Customizable @c malloc function pointer */
void *(*vl_malloc_func)  (size_t)          = &malloc ;

/** @internal@brief Customizable @c realloc function pointer */
void *(*vl_realloc_func) (void*,size_t)    = &realloc ;

/** @internal@brief Customizable @c calloc function pointer */
void *(*vl_calloc_func)  (size_t, size_t) = &calloc ;

/** @internal@brief Customizable @c free function pointer */
void  (*vl_free_func)    (void*)            = &free ;

/** @internal@brief Customizable @c printf function pointer */
int   (*vl_printf_func)  (char const *, ...)=  printf ; /* &do_nothing_printf ;*/

/** --------------------------------------------------------------- */

/** @fn ::vl_malloc(size_t)
 ** @brief Call customizable @c malloc function
 ** @param n number of bytes to allocate.
 **
 ** The function calls the user customizable @c malloc.
 **
 ** @return result of @c malloc
 **/

/** @fn ::vl_realloc(void*,size_t)
 ** @brief Call customizable @c resize function
 **
 ** @param ptr buffer to reallocate.
 ** @param n   number of bytes to allocate.
 **
 ** The function calls the user-customizable @c realloc.
 **
 ** @return result of the user-customizable @c realloc.
 **/

/** @fn ::vl_calloc(size_t,size_t)
 ** @brief Call customizable @c calloc function
 **
 ** @param n    size of each element in byte.
 ** @param size size of the array to allocate (number of elements).
 **
 ** The function calls the user-customizable @c calloc.
 **
 ** @return result of the user-customizable @c calloc.
 **/

/** @fn ::vl_free(void*)
 ** @brief Call customizable @c free function
 **
 ** @param ptr buffer to free.
 **
 ** The function calls the user customizable @c free.
 **/

/** ------------------------------------------------------------------
 ** @brief Set memory allocation functions
 ** @param malloc_func  pointer to @c malloc.
 ** @param realloc_func pointer to @c realloc.
 ** @param calloc_func  pointer to @c calloc.
 ** @param free_func    pointer to @c free.
 **/

VL_EXPORT
void vl_set_alloc_func (void *(*malloc_func)  (size_t),
                        void *(*realloc_func) (void*, size_t),
                        void *(*calloc_func)  (size_t, size_t),
                        void  (*free_func)    (void*))
{
  vl_malloc_func  = malloc_func ;
  vl_realloc_func = realloc_func ;
  vl_calloc_func  = calloc_func ;
  vl_free_func    = free_func ;
}

VL_EXPORT
void
vl_set_printf_func (printf_func_t printf_func)
{
  vl_printf_func  = printf_func ? printf_func : do_nothing_printf ;
}

#ifdef VL_OS_WIN
LARGE_INTEGER tic_freq ;
LARGE_INTEGER tic_mark ;
#else
clock_t tic_mark ; /**< @internal Store clock time for ::vl_tic() */
#endif

/** ------------------------------------------------------------------
 ** @brief Set time reference
 **/

void vl_tic()
{
#ifdef VL_OS_WIN
  QueryPerformanceFrequency(&tic_freq) ;
  QueryPerformanceCounter(&tic_mark) ;
#else
  tic_mark = clock() ;
#endif
}

/** ------------------------------------------------------------------
 ** @brief Get time since reference
 **
 ** Returns the processor time elapsed since ::vl_tic() was called.
 **
 ** @remark On UNIX, this function uses the @c clock() system call.
 ** On Windows, it uses the @c QueryPerformanceCounter() system call,
 ** which is more accurate than @c clock() on this platform.
 **
 ** @return time in seconds.
 **/

double vl_toc()
{
#ifdef VL_OS_WIN
  LARGE_INTEGER toc_mark ;
  QueryPerformanceCounter(&toc_mark) ;
  return (double) (toc_mark.QuadPart - tic_mark.QuadPart) / tic_freq.QuadPart ;
#else
  return (double) (clock() - tic_mark) / CLOCKS_PER_SEC ;
#endif
}