aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/Pillow/py2/_imagingmorph.c
blob: fc8f246cc86f890ff5a87f15b9896a35ed86d09f (plain) (blame)
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
/*
 * The Python Imaging Library
 *
 * A binary morphology add-on for the Python Imaging Library
 *
 * History:
 *   2014-06-04 Initial version.
 *
 * Copyright (c) 2014 Dov Grobgeld <dov.grobgeld@gmail.com>
 *
 * See the README file for information on usage and redistribution.
 */

#include "Python.h"
#include "Imaging.h"
#include "py3.h"

#define LUT_SIZE (1<<9)

/* Apply a morphologic LUT to a binary image. Outputs a
   a new binary image.

   Expected parameters:

      1. a LUT - a 512 byte size lookup table.
      2. an input Imaging image id.
      3. an output Imaging image id

   Returns number of changed pixels.
*/
static PyObject*
apply(PyObject *self, PyObject* args)
{
    const char *lut;
    PyObject *py_lut;
    Py_ssize_t lut_len, i0, i1;
    Imaging imgin, imgout;
    int width, height;
    int row_idx, col_idx;
    UINT8 **inrows, **outrows;
    int num_changed_pixels = 0;

    if (!PyArg_ParseTuple(args, "Onn", &py_lut, &i0, &i1)) {
        PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
        return NULL;
    }

    if (!PyBytes_Check(py_lut)) {
        PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
        return NULL;
    }

    lut_len = PyBytes_Size(py_lut);

    if (lut_len < LUT_SIZE) {
        PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
        return NULL;
    }

    lut = PyBytes_AsString(py_lut);

    imgin = (Imaging) i0;
    imgout = (Imaging) i1;
    width = imgin->xsize;
    height = imgin->ysize;

    if (imgin->type != IMAGING_TYPE_UINT8 ||
        imgin->bands != 1) {
        PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
        return NULL;
    }
    if (imgout->type != IMAGING_TYPE_UINT8 ||
        imgout->bands != 1) {
        PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
        return NULL;
    }

    inrows = imgin->image8;
    outrows = imgout->image8;

    for (row_idx=0; row_idx < height; row_idx++) {
        UINT8 *outrow = outrows[row_idx];
        UINT8 *inrow = inrows[row_idx];
        UINT8 *prow, *nrow; /* Previous and next row */

        /* zero boundary conditions. TBD support other modes */
        outrow[0] = outrow[width-1] = 0;
        if (row_idx==0 || row_idx == height-1) {
            for(col_idx=0; col_idx<width; col_idx++)
                outrow[col_idx] = 0;
            continue;
        }

        prow = inrows[row_idx-1];
        nrow = inrows[row_idx+1];

        for (col_idx=1; col_idx<width-1; col_idx++) {
            int cim = col_idx-1;
            int cip = col_idx+1;
            unsigned char b0 = prow[cim] &1;
            unsigned char b1 = prow[col_idx]&1;
            unsigned char b2 = prow[cip]&1;

            unsigned char b3 = inrow[cim]&1;
            unsigned char b4 = inrow[col_idx]&1;
            unsigned char b5 = inrow[cip]&1;

            unsigned char b6 = nrow[cim]&1;
            unsigned char b7 = nrow[col_idx]&1;
            unsigned char b8 = nrow[cip]&1;

            int lut_idx = (b0
                           |(b1 << 1)
                           |(b2 << 2)
                           |(b3 << 3)
                           |(b4 << 4)
                           |(b5 << 5)
                           |(b6 << 6)
                           |(b7 << 7)
                           |(b8 << 8));
            outrow[col_idx] = 255*(lut[lut_idx]&1);
            num_changed_pixels += ((b4&1)!=(outrow[col_idx]&1));
        }
    }
    return Py_BuildValue("i",num_changed_pixels);
}

/* Match a morphologic LUT to a binary image and return a list
   of the coordinates of all matching pixels.

   Expected parameters:

      1. a LUT - a 512 byte size lookup table.
      2. an input Imaging image id.

   Returns list of matching pixels.
*/
static PyObject*
match(PyObject *self, PyObject* args)
{
    const char *lut;
    PyObject *py_lut;
    Py_ssize_t lut_len, i0;
    Imaging imgin;
    int width, height;
    int row_idx, col_idx;
    UINT8 **inrows;
    PyObject *ret = PyList_New(0);

    if (!PyArg_ParseTuple(args, "On", &py_lut, &i0)) {
        PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");
        return NULL;
    }

    if (!PyBytes_Check(py_lut)) {
        PyErr_SetString(PyExc_RuntimeError, "The morphology LUT is not a bytes object");
        return NULL;
    }

    lut_len = PyBytes_Size(py_lut);

    if (lut_len < LUT_SIZE) {
        PyErr_SetString(PyExc_RuntimeError, "The morphology LUT has the wrong size");
        return NULL;
    }

    lut = PyBytes_AsString(py_lut);
    imgin = (Imaging) i0;

    if (imgin->type != IMAGING_TYPE_UINT8 ||
        imgin->bands != 1) {
        PyErr_SetString(PyExc_RuntimeError, "Unsupported image type");
        return NULL;
    }

    inrows = imgin->image8;
    width = imgin->xsize;
    height = imgin->ysize;

    for (row_idx=1; row_idx < height-1; row_idx++) {
        UINT8 *inrow = inrows[row_idx];
        UINT8 *prow, *nrow;

        prow = inrows[row_idx-1];
        nrow = inrows[row_idx+1];

        for (col_idx=1; col_idx<width-1; col_idx++) {
            int cim = col_idx-1;
            int cip = col_idx+1;
            unsigned char b0 = prow[cim] &1;
            unsigned char b1 = prow[col_idx]&1;
            unsigned char b2 = prow[cip]&1;

            unsigned char b3 = inrow[cim]&1;
            unsigned char b4 = inrow[col_idx]&1;
            unsigned char b5 = inrow[cip]&1;

            unsigned char b6 = nrow[cim]&1;
            unsigned char b7 = nrow[col_idx]&1;
            unsigned char b8 = nrow[cip]&1;

            int lut_idx = (b0
                           |(b1 << 1)
                           |(b2 << 2)
                           |(b3 << 3)
                           |(b4 << 4)
                           |(b5 << 5)
                           |(b6 << 6)
                           |(b7 << 7)
                           |(b8 << 8));
            if (lut[lut_idx]) {
                PyObject *coordObj = Py_BuildValue("(nn)",col_idx,row_idx);
                PyList_Append(ret, coordObj);
            }
        }
    }

    return ret;
}

/* Return a list of the coordinates of all turned on pixels in an image.
   May be used to extract features after a sequence of MorphOps were applied.
   This is faster than match as only 1x1 lookup is made.
*/
static PyObject*
get_on_pixels(PyObject *self, PyObject* args)
{
    Py_ssize_t i0;
    Imaging img;
    UINT8 **rows;
    int row_idx, col_idx;
    int width, height;
    PyObject *ret = PyList_New(0);

    if (!PyArg_ParseTuple(args, "n", &i0)) {
        PyErr_SetString(PyExc_RuntimeError, "Argument parsing problem");

        return NULL;
    }
    img = (Imaging) i0;
    rows = img->image8;
    width = img->xsize;
    height = img->ysize;

    for (row_idx=0; row_idx < height; row_idx++) {
        UINT8 *row = rows[row_idx];
        for (col_idx=0; col_idx<width; col_idx++) {
            if (row[col_idx]) {
                PyObject *coordObj = Py_BuildValue("(nn)",col_idx,row_idx);
                PyList_Append(ret, coordObj);
            }
        }
    }
    return ret;
}


static int
setup_module(PyObject* m)
{
    PyObject* d = PyModule_GetDict(m);

    PyDict_SetItemString(d, "__version", PyUnicode_FromString("0.1"));

    return 0;
}

static PyMethodDef functions[] = {
    /* Functions */
    {"apply", (PyCFunction)apply, METH_VARARGS, NULL},
    {"get_on_pixels", (PyCFunction)get_on_pixels, METH_VARARGS, NULL},
    {"match", (PyCFunction)match, METH_VARARGS, NULL},
    {NULL, NULL, 0, NULL}
};

#if PY_VERSION_HEX >= 0x03000000
PyMODINIT_FUNC
PyInit__imagingmorph(void) {
    PyObject* m;

    static PyModuleDef module_def = {
        PyModuleDef_HEAD_INIT,
        "_imagingmorph",         /* m_name */
        "A module for doing image morphology",               /* m_doc */
        -1,                 /* m_size */
        functions,          /* m_methods */
    };

    m = PyModule_Create(&module_def);

    if (setup_module(m) < 0)
        return NULL;

    return m;
}
#else
PyMODINIT_FUNC
init_imagingmorph(void)
{
    PyObject* m = Py_InitModule("_imagingmorph", functions);
    setup_module(m);
}
#endif