forked from openslide/openslide-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path_convert.c
More file actions
122 lines (109 loc) · 3.19 KB
/
_convert.c
File metadata and controls
122 lines (109 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
* openslide-python - Python bindings for the OpenSlide library
*
* Copyright (c) 2015 Carnegie Mellon University
*
* This library is free software; you can redistribute it and/or modify it
* under the terms of version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful, but
* WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public
* License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this library. If not, see <https://www.gnu.org/licenses/>.
*/
#include <Python.h>
typedef unsigned char u8;
static void
argb2rgba(PY_UINT32_T *buf, Py_ssize_t len)
{
Py_ssize_t cur;
for (cur = 0; cur < len; cur++) {
PY_UINT32_T val = buf[cur];
u8 a = val >> 24;
switch (a) {
case 0:
break;
case 255:
val = (val << 8) | a;
#ifndef WORDS_BIGENDIAN
// compiler should optimize this to bswap
val = (((val & 0x000000ff) << 24) |
((val & 0x0000ff00) << 8) |
((val & 0x00ff0000) >> 8) |
((val & 0xff000000) >> 24));
#endif
buf[cur] = val;
break;
default:
; // label cannot point to a variable declaration
u8 r = 255 * ((val >> 16) & 0xff) / a;
u8 g = 255 * ((val >> 8) & 0xff) / a;
u8 b = 255 * ((val >> 0) & 0xff) / a;
#ifdef WORDS_BIGENDIAN
val = r << 24 | g << 16 | b << 8 | a;
#else
val = a << 24 | b << 16 | g << 8 | r;
#endif
buf[cur] = val;
break;
}
}
}
// Takes one argument: a contiguous buffer object. Modifies it in place.
static PyObject *
_convert_argb2rgba(PyObject *self, PyObject *args)
{
PyObject *ret = NULL;
Py_buffer view;
if (!PyArg_ParseTuple(args, "s*", &view))
return NULL;
if (!PyBuffer_IsContiguous(&view, 'A')) {
PyErr_SetString(PyExc_ValueError, "Argument is not contiguous");
goto DONE;
}
if (view.readonly) {
PyErr_SetString(PyExc_ValueError, "Argument is not writable");
goto DONE;
}
if (view.len % 4) {
PyErr_SetString(PyExc_ValueError, "Argument has invalid size");
goto DONE;
}
if (view.itemsize != 4) {
PyErr_SetString(PyExc_ValueError, "Argument has invalid item size");
goto DONE;
}
Py_BEGIN_ALLOW_THREADS
argb2rgba(view.buf, view.len / 4);
Py_END_ALLOW_THREADS
Py_IncRef(Py_None);
ret = Py_None;
DONE:
PyBuffer_Release(&view);
return ret;
}
static PyMethodDef _convert_methods[] = {
{"argb2rgba", _convert_argb2rgba, METH_VARARGS,
"Convert aRGB to RGBA in place."},
{NULL, NULL, 0, NULL}
};
static PyModuleDef_Slot _convert_slots[] = {
{0, NULL}
};
static struct PyModuleDef _convert_module = {
PyModuleDef_HEAD_INIT,
"_convert",
NULL,
0,
_convert_methods,
_convert_slots,
};
PyMODINIT_FUNC
PyInit__convert(void)
{
return PyModuleDef_Init(&_convert_module);
}