/* Created By: Virgil Dupras * Created On: 2010-01-30 * Copyright 2012 Hardcoded Software (http://www.hardcoded.net) * * This software is licensed under the "BSD" 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/bsd_license */ #include "common.h" /* avgdiff/maxdiff has been called with empty lists */ static PyObject *NoBlocksError; /* avgdiff/maxdiff has been called with 2 block lists of different size. */ static PyObject *DifferentBlockCountError; /* Returns a 3 sized tuple containing the mean color of 'image'. * image: a PIL image or crop. */ static PyObject* getblock(PyObject *image) { int i, totr, totg, totb; Py_ssize_t pixel_count; PyObject *ppixels; totr = totg = totb = 0; ppixels = PyObject_CallMethod(image, "getdata", NULL); if (ppixels == NULL) { return NULL; } pixel_count = PySequence_Length(ppixels); for (i=0; i limit*iteration_count) && (iteration_count >= min_iterations)) { return PyLong_FromLong(limit + 1); } } result = sum / count; if (!result && sum) { result = 1; } return PyLong_FromLong(result); } static PyMethodDef BlockMethods[] = { {"getblocks2", block_getblocks2, METH_VARARGS, block_getblocks2_doc}, {"avgdiff", block_avgdiff, METH_VARARGS, block_avgdiff_doc}, {NULL, NULL, 0, NULL} /* Sentinel */ }; static struct PyModuleDef BlockDef = { PyModuleDef_HEAD_INIT, "_block", NULL, -1, BlockMethods, NULL, NULL, NULL, NULL }; PyObject * PyInit__block(void) { PyObject *m = PyModule_Create(&BlockDef); if (m == NULL) { return NULL; } NoBlocksError = PyErr_NewException("_block.NoBlocksError", NULL, NULL); PyModule_AddObject(m, "NoBlocksError", NoBlocksError); DifferentBlockCountError = PyErr_NewException("_block.DifferentBlockCountError", NULL, NULL); PyModule_AddObject(m, "DifferentBlockCountError", DifferentBlockCountError); return m; }