/* * 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 * * See the README file for information on usage and redistribution. */ #include "Python.h" #include "libImaging/Imaging.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_idxtype != 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_idximage8; 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