2010-01-31 10:24:51 +00:00
|
|
|
/* Created By: Virgil Dupras
|
|
|
|
* Created On: 2010-01-31
|
2013-04-28 14:35:51 +00:00
|
|
|
* Copyright 2013 Hardcoded Software (http://www.hardcoded.net)
|
2010-01-31 10:24:51 +00:00
|
|
|
*
|
2010-09-30 10:17:41 +00:00
|
|
|
* This software is licensed under the "BSD" License as described in the "LICENSE" file,
|
2010-01-31 10:24:51 +00:00
|
|
|
* which should be included with this package. The terms are also available at
|
2010-09-30 10:17:41 +00:00
|
|
|
* http://www.hardcoded.net/licenses/bsd_license
|
2010-02-03 14:44:15 +00:00
|
|
|
**/
|
2010-01-31 10:24:51 +00:00
|
|
|
|
|
|
|
#define PY_SSIZE_T_CLEAN
|
|
|
|
#include "Python.h"
|
|
|
|
|
2010-01-31 11:05:13 +00:00
|
|
|
/* It seems like MS VC defines min/max already */
|
|
|
|
#ifndef _MSC_VER
|
2010-01-31 10:33:26 +00:00
|
|
|
static int
|
2010-01-31 10:24:51 +00:00
|
|
|
max(int a, int b)
|
|
|
|
{
|
|
|
|
return b > a ? b : a;
|
|
|
|
}
|
|
|
|
|
2010-01-31 10:33:26 +00:00
|
|
|
static int
|
2010-01-31 10:24:51 +00:00
|
|
|
min(int a, int b)
|
|
|
|
{
|
|
|
|
return b < a ? b : a;
|
|
|
|
}
|
2010-01-31 11:05:13 +00:00
|
|
|
#endif
|
2010-01-31 10:24:51 +00:00
|
|
|
|
2010-01-31 10:33:26 +00:00
|
|
|
static PyObject*
|
2010-02-03 14:44:15 +00:00
|
|
|
getblock(PyObject *image, int width, int height)
|
2010-01-31 10:24:51 +00:00
|
|
|
{
|
2010-02-03 14:44:15 +00:00
|
|
|
int pixel_count, red, green, blue, bytes_per_line;
|
|
|
|
PyObject *pred, *pgreen, *pblue;
|
2010-01-31 10:24:51 +00:00
|
|
|
PyObject *result;
|
|
|
|
|
|
|
|
red = green = blue = 0;
|
|
|
|
pixel_count = width * height;
|
|
|
|
if (pixel_count) {
|
2012-06-06 21:18:53 +00:00
|
|
|
PyObject *sipptr, *bits_capsule, *pi;
|
2010-01-31 10:24:51 +00:00
|
|
|
char *s;
|
|
|
|
int i;
|
|
|
|
|
|
|
|
pi = PyObject_CallMethod(image, "bytesPerLine", NULL);
|
2010-08-17 07:30:25 +00:00
|
|
|
bytes_per_line = PyLong_AsLong(pi);
|
2010-01-31 10:24:51 +00:00
|
|
|
Py_DECREF(pi);
|
|
|
|
|
|
|
|
sipptr = PyObject_CallMethod(image, "bits", NULL);
|
2012-06-06 21:18:53 +00:00
|
|
|
bits_capsule = PyObject_CallMethod(sipptr, "ascapsule", NULL);
|
2010-01-31 10:24:51 +00:00
|
|
|
Py_DECREF(sipptr);
|
2012-06-06 21:18:53 +00:00
|
|
|
s = (char *)PyCapsule_GetPointer(bits_capsule, NULL);
|
|
|
|
Py_DECREF(bits_capsule);
|
2010-01-31 10:24:51 +00:00
|
|
|
/* 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<height; i++) {
|
|
|
|
int j;
|
|
|
|
for (j=0; j<width; j++) {
|
|
|
|
int offset;
|
|
|
|
unsigned char r, g, b;
|
|
|
|
|
|
|
|
offset = i * bytes_per_line + j * 3;
|
|
|
|
r = s[offset];
|
|
|
|
g = s[offset + 1];
|
|
|
|
b = s[offset + 2];
|
|
|
|
red += r;
|
|
|
|
green += g;
|
|
|
|
blue += b;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
red /= pixel_count;
|
|
|
|
green /= pixel_count;
|
|
|
|
blue /= pixel_count;
|
|
|
|
}
|
|
|
|
|
2010-08-17 07:30:25 +00:00
|
|
|
pred = PyLong_FromLong(red);
|
|
|
|
pgreen = PyLong_FromLong(green);
|
|
|
|
pblue = PyLong_FromLong(blue);
|
2010-01-31 10:24:51 +00:00
|
|
|
result = PyTuple_Pack(3, pred, pgreen, pblue);
|
|
|
|
Py_DECREF(pred);
|
|
|
|
Py_DECREF(pgreen);
|
|
|
|
Py_DECREF(pblue);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2010-02-03 14:44:15 +00:00
|
|
|
/* block_getblocks(QImage image, int block_count_per_side) -> [(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.
|
|
|
|
**/
|
2010-01-31 10:24:51 +00:00
|
|
|
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);
|
2010-08-17 07:30:25 +00:00
|
|
|
width = PyLong_AsLong(pi);
|
2010-01-31 10:24:51 +00:00
|
|
|
Py_DECREF(pi);
|
|
|
|
pi = PyObject_CallMethod(image, "height", NULL);
|
2010-08-17 07:30:25 +00:00
|
|
|
height = PyLong_AsLong(pi);
|
2010-01-31 10:24:51 +00:00
|
|
|
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<block_count_per_side; ih++) {
|
|
|
|
int top, iw;
|
|
|
|
top = min(ih*block_height, height-block_height-1);
|
|
|
|
for (iw=0; iw<block_count_per_side; iw++) {
|
|
|
|
int left;
|
|
|
|
PyObject *pcrop;
|
|
|
|
PyObject *pblock;
|
|
|
|
|
|
|
|
left = min(iw*block_width, width-block_width-1);
|
|
|
|
pcrop = PyObject_CallMethod(image, "copy", "iiii", left, top, block_width, block_height);
|
|
|
|
if (pcrop == NULL) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
2010-02-03 14:44:15 +00:00
|
|
|
pblock = getblock(pcrop, block_width, block_height);
|
2010-01-31 10:24:51 +00:00
|
|
|
Py_DECREF(pcrop);
|
|
|
|
if (pblock == NULL) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
PyList_SET_ITEM(result, ih*block_count_per_side+iw, pblock);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef BlockMethods[] = {
|
|
|
|
{"getblocks", block_getblocks, METH_VARARGS, ""},
|
|
|
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
|
|
|
};
|
|
|
|
|
2010-08-17 07:30:25 +00:00
|
|
|
static struct PyModuleDef BlockDef = {
|
|
|
|
PyModuleDef_HEAD_INIT,
|
2010-08-21 15:04:23 +00:00
|
|
|
"_block_qt",
|
2010-08-17 07:30:25 +00:00
|
|
|
NULL,
|
|
|
|
-1,
|
|
|
|
BlockMethods,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL
|
|
|
|
};
|
|
|
|
|
|
|
|
PyObject *
|
2010-08-21 15:04:23 +00:00
|
|
|
PyInit__block_qt(void)
|
2010-01-31 10:24:51 +00:00
|
|
|
{
|
2010-08-17 07:30:25 +00:00
|
|
|
PyObject *m = PyModule_Create(&BlockDef);
|
2010-01-31 10:24:51 +00:00
|
|
|
if (m == NULL) {
|
2010-08-17 07:30:25 +00:00
|
|
|
return NULL;
|
2010-01-31 10:24:51 +00:00
|
|
|
}
|
2010-08-17 07:30:25 +00:00
|
|
|
return m;
|
2010-01-31 10:24:51 +00:00
|
|
|
}
|