/* Created By: Virgil Dupras * Created On: 2010-01-31 * Copyright 2010 Hardcoded Software (http://www.hardcoded.net) * * This software is licensed under the "HS" License as described in the "LICENSE" file, * which should be included with this package. The terms are also available at * http://www.hardcoded.net/licenses/hs_license **/ #define PY_SSIZE_T_CLEAN #include "Python.h" /* It seems like MS VC defines min/max already */ #ifndef _MSC_VER static int max(int a, int b) { return b > a ? b : a; } static int min(int a, int b) { return b < a ? b : a; } #endif static PyObject* getblock(PyObject *image, int width, int height) { int pixel_count, red, green, blue, bytes_per_line; PyObject *pred, *pgreen, *pblue; PyObject *result; red = green = blue = 0; pixel_count = width * height; if (pixel_count) { PyObject *sipptr, *pi; char *s; int i; pi = PyObject_CallMethod(image, "bytesPerLine", NULL); bytes_per_line = PyLong_AsLong(pi); Py_DECREF(pi); sipptr = PyObject_CallMethod(image, "bits", NULL); /* int(sipptr) returns the address of the pointer */ pi = PyObject_CallMethod(sipptr, "__int__", NULL); Py_DECREF(sipptr); s = (char *)PyLong_AsLong(pi); Py_DECREF(pi); /* Qt aligns all its lines on 32bit, which means that if the number of bytes per * line for image is not divisible by 4, there's going to be crap inserted in "s" * We have to take this into account when calculating offsets **/ for (i=0; i [(int r, int g, int b), ...] * * Compute blocks out of `image`. Note the use of min/max when compes the time of computing widths * and heights and positions. This is to cover the case where the width or height of the image is * smaller than `block_count_per_side`. In these cases, blocks will be, of course, 1 pixel big. But * also, because all compared block lists are required to be of the same size, any block that has * no pixel to be assigned to will simply be assigned the last pixel. This is why we have * min(..., height-block_height-1) and stuff like that. **/ static PyObject* block_getblocks(PyObject *self, PyObject *args) { int block_count_per_side, width, height, block_width, block_height, ih; PyObject *image; PyObject *pi; PyObject *result; if (!PyArg_ParseTuple(args, "Oi", &image, &block_count_per_side)) { return NULL; } pi = PyObject_CallMethod(image, "width", NULL); width = PyLong_AsLong(pi); Py_DECREF(pi); pi = PyObject_CallMethod(image, "height", NULL); height = PyLong_AsLong(pi); Py_DECREF(pi); if (!(width && height)) { return PyList_New(0); } block_width = max(width / block_count_per_side, 1); block_height = max(height / block_count_per_side, 1); result = PyList_New(block_count_per_side * block_count_per_side); if (result == NULL) { return NULL; } for (ih=0; ih