forked from whitphx/lear-gist-python
-
Notifications
You must be signed in to change notification settings - Fork 0
/
gistmodule.c
148 lines (113 loc) · 3.01 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
#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)
{
int nblocks=4;
int n_scale=3;
int orientations_per_scale[50]={8,8,4};
PyArrayObject *image, *descriptor;
if (!PyArg_ParseTuple(args, "O", &image))
{
return NULL;
}
if (PyArray_TYPE(image) != NPY_UINT8) {
PyErr_SetString(PyExc_TypeError, "type of image must be uint8");
return NULL;
}
if (PyArray_NDIM(image) != 3) {
PyErr_SetString(PyExc_TypeError, "dimensions of image must be 3.");
return NULL;
}
npy_intp *dims_image = PyArray_DIMS(image);
const int w = (int) *(dims_image+1);
const int h = (int) *(dims_image);
// 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);
int descsize=0;
/* compute descriptor size */
for(int i=0;i<n_scale;i++)
descsize+=nblocks*nblocks*orientations_per_scale[i];
descsize*=3; /* color */
// Allocate output
npy_intp dim_desc[1] = {descsize};
descriptor = (PyArrayObject *) PyArray_SimpleNew(1, dim_desc, NPY_FLOAT);
// Set val
for (int i=0 ; i<descsize ; ++i) {
*(float *)PyArray_GETPTR1(descriptor, i) = desc[i];
}
// Release memory
color_image_delete(im);
free(desc);
return PyArray_Return(descriptor);
}
static PyMethodDef gist_methods[] = {
{"extract", gist_extract, METH_VARARGS, ""},
{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
}