2010-01-30 15:29:18 +00:00
|
|
|
/* Created By: Virgil Dupras
|
|
|
|
* Created On: 2010-01-30
|
2014-04-19 16:19:11 +00:00
|
|
|
* Copyright 2014 Hardcoded Software (http://www.hardcoded.net)
|
2010-01-31 10:23:23 +00:00
|
|
|
*
|
2023-01-12 06:14:17 +00:00
|
|
|
* 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
|
2010-01-30 15:29:18 +00:00
|
|
|
*/
|
2010-02-04 12:13:08 +00:00
|
|
|
|
|
|
|
#include "common.h"
|
2010-01-30 15:50:49 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
static PyObject *cache_bytes_to_colors(PyObject *self, PyObject *args) {
|
|
|
|
char *y;
|
|
|
|
Py_ssize_t char_count, i, color_count;
|
|
|
|
PyObject *result;
|
|
|
|
unsigned long r, g, b;
|
|
|
|
Py_ssize_t ci;
|
|
|
|
PyObject *color_tuple;
|
2022-09-27 09:34:57 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
if (!PyArg_ParseTuple(args, "y#", &y, &char_count)) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-10 04:58:08 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
color_count = char_count / 3;
|
|
|
|
result = PyList_New(color_count);
|
|
|
|
if (result == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2023-01-10 04:58:08 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
for (i = 0; i < color_count; i++) {
|
|
|
|
ci = i * 3;
|
|
|
|
r = (unsigned char)y[ci];
|
|
|
|
g = (unsigned char)y[ci + 1];
|
|
|
|
b = (unsigned char)y[ci + 2];
|
2023-01-10 04:58:08 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
color_tuple = inttuple(3, r, g, b);
|
|
|
|
if (color_tuple == NULL) {
|
|
|
|
Py_DECREF(result);
|
|
|
|
return NULL;
|
2010-01-30 15:29:18 +00:00
|
|
|
}
|
2023-01-12 06:14:17 +00:00
|
|
|
PyList_SET_ITEM(result, i, color_tuple);
|
|
|
|
}
|
2023-01-10 04:58:08 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
return result;
|
2010-01-30 15:29:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
static PyMethodDef CacheMethods[] = {
|
2023-01-12 06:14:17 +00:00
|
|
|
{"bytes_to_colors", cache_bytes_to_colors, METH_VARARGS,
|
|
|
|
"Transform the bytes 's' into a list of 3 sized tuples."},
|
|
|
|
{NULL, NULL, 0, NULL} /* Sentinel */
|
2010-01-30 15:29:18 +00:00
|
|
|
};
|
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
static struct PyModuleDef CacheDef = {PyModuleDef_HEAD_INIT,
|
|
|
|
"_cache",
|
|
|
|
NULL,
|
|
|
|
-1,
|
|
|
|
CacheMethods,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL,
|
|
|
|
NULL};
|
2010-08-11 14:39:06 +00:00
|
|
|
|
2023-01-12 06:14:17 +00:00
|
|
|
PyObject *PyInit__cache(void) {
|
|
|
|
PyObject *m = PyModule_Create(&CacheDef);
|
|
|
|
if (m == NULL) {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
return m;
|
2023-01-10 04:58:08 +00:00
|
|
|
}
|