-
-
Notifications
You must be signed in to change notification settings - Fork 14
/
gistmodule.c
204 lines (166 loc) · 5.24 KB
/
gistmodule.c
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
#include <Python.h>
#include <numpy/arrayobject.h>
#include <numpy/arrayscalars.h>
#include "lear_gist-1.2/gist.h"
struct module_state {
PyObject *error;
};
#if PY_MAJOR_VERSION >= 3
#define GETSTATE(m) ((struct module_state*)PyModule_GetState(m))
#else
#define GETSTATE(m) (&_state)
static struct module_state _state;
#endif
static PyObject* gist_extract(PyObject *self, PyObject *args, PyObject *keywds)
{
PyArrayObject *image; // Python object to contain the given `image` argument.
PyObject* pyobj_orientations_per_scale = NULL; // Python Object to contain the given `orientations_per_scale` argument.
int nblocks=4; // An variable to contain the given `nblocks` argument, whose default value is 4
// n_scale and orientations_per_scale are parsed from pyobj_orientations_per_scale below.
int n_scale;
int *orientations_per_scale = NULL;
// Parse the given arguments
static char *kwlist[] = {"", "nblocks", "orientations_per_scale", NULL};
if (!PyArg_ParseTupleAndKeywords(args, keywds, "O!|iO", kwlist,
&PyArray_Type, &image, &nblocks, &pyobj_orientations_per_scale))
{
return NULL;
}
// Check validity of image argument
if (PyArray_TYPE(image) != NPY_UINT8) {
PyErr_SetString(PyExc_ValueError, "type of image must be uint8");
return NULL;
}
if (PyArray_NDIM(image) != 3) {
PyErr_SetString(PyExc_ValueError, "dimensions of image must be 3.");
return NULL;
}
// Parse orientations_per_scale argument
// Ref: https://www.oreilly.com/library/view/python-cookbook/0596001673/ch16s03.html
if (pyobj_orientations_per_scale != NULL) {
pyobj_orientations_per_scale = PySequence_Fast(pyobj_orientations_per_scale, "orientations_per_scale must be iterable");
if (!pyobj_orientations_per_scale) {
return NULL;
}
n_scale = PySequence_Fast_GET_SIZE(pyobj_orientations_per_scale);
orientations_per_scale = malloc(n_scale * sizeof(int));
if (!orientations_per_scale) {
Py_DECREF(pyobj_orientations_per_scale);
return PyErr_NoMemory();
}
for (int i = 0; i < n_scale; ++i) {
PyObject *long_item;
PyObject *item = PySequence_Fast_GET_ITEM(pyobj_orientations_per_scale, i);
if (!item) {
Py_DECREF(pyobj_orientations_per_scale);
free(orientations_per_scale);
return NULL;
}
long_item = PyNumber_Long(item);
if (!long_item) {
Py_DECREF(pyobj_orientations_per_scale);
free(orientations_per_scale);
PyErr_SetString(PyExc_TypeError, "all items of orientations_per_scale must be int");
return NULL;
}
orientations_per_scale[i] = (int) PyLong_AsLong(long_item); // XXX: Down cast
Py_DECREF(long_item);
}
Py_DECREF(pyobj_orientations_per_scale);
} else {
// Default values of n_scale and orientations_per_scale
n_scale = 3;
orientations_per_scale = malloc(n_scale * sizeof(int));
orientations_per_scale[0] = 8;
orientations_per_scale[1] = 8;
orientations_per_scale[2] = 4;
}
npy_intp *dims_image = PyArray_DIMS(image);
const int w = (int) *(dims_image+1);
const int h = (int) *(dims_image);
const int channels = (int) *(dims_image+2);
if (w == 0 || h == 0) {
PyErr_SetString(PyExc_ValueError, "invalid image size.");
return NULL;
}
if (channels != 3) {
PyErr_SetString(PyExc_ValueError, "invalid color channels.");
return NULL;
}
// Read image to color_image_t structure
color_image_t *im=color_image_new(w,h);
for (int y=0, i=0 ; y<h ; ++y) {
for (int x=0 ; x<w ; ++x, ++i) {
im->c1[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 0);
im->c2[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 1);
im->c3[i] = *(unsigned char *)PyArray_GETPTR3(image, y, x, 2);
}
}
// Extract descriptor
float *desc=color_gist_scaletab(im, nblocks, n_scale, orientations_per_scale);
/* compute descriptor size */
int descsize=0;
for(int i=0;i<n_scale;i++)
descsize+=nblocks*nblocks*orientations_per_scale[i];
descsize*=3; /* color */
// Create output
npy_intp dim_desc[1] = {descsize};
PyObject *descriptor = PyArray_SimpleNewFromData(1, dim_desc, NPY_FLOAT, desc);
PyArray_ENABLEFLAGS((PyArrayObject *)descriptor, NPY_ARRAY_OWNDATA);
// Release memory
color_image_delete(im);
free(orientations_per_scale);
return descriptor;
//return PyArray_Return(descriptor);
}
static PyMethodDef gist_methods[] = {
{"extract", gist_extract, METH_VARARGS | METH_KEYWORDS, "Extracts Lear's GIST descriptor"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static int gist_traverse(PyObject *m, visitproc visit, void *arg) {
Py_VISIT(GETSTATE(m)->error);
return 0;
}
static int gist_clear(PyObject *m) {
Py_CLEAR(GETSTATE(m)->error);
return 0;
}
static struct PyModuleDef moduledef = {
PyModuleDef_HEAD_INIT,
"gist",
NULL,
sizeof(struct module_state),
gist_methods,
NULL,
gist_traverse,
gist_clear,
NULL
};
#define INITERROR return NULL
PyMODINIT_FUNC
PyInit_gist(void)
#else
#define INITERROR return
PyMODINIT_FUNC
initgist(void)
#endif
{
#if PY_MAJOR_VERSION >= 3
PyObject *module = PyModule_Create(&moduledef);
#else
PyObject *module = Py_InitModule("gist", gist_methods);
#endif
if (module == NULL)
INITERROR;
struct module_state *st = GETSTATE(module);
st->error = PyErr_NewException("gist.Error", NULL, NULL);
if (st->error == NULL) {
Py_DECREF(module);
INITERROR;
}
import_array();
#if PY_MAJOR_VERSION >= 3
return module;
#endif
}