-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpixel16.c
More file actions
84 lines (65 loc) · 1.52 KB
/
pixel16.c
File metadata and controls
84 lines (65 loc) · 1.52 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
#include "Python.h"
static void
do_uflip(unsigned short *usdata, int cnt)
{
while (cnt > 0) {
usdata[--cnt] ^= 0x8000;
}
}
static void
do_byteswap(unsigned short *usdata, int cnt)
{
unsigned char t;
while (cnt-- > 0) {
unsigned char *cp = (unsigned char *)&(usdata[cnt]);
t = *cp;
*cp = *(cp+1);
*(cp+1) = t;
}
}
static PyObject *
uflip(PyObject *self, PyObject *args)
{
PyObject *so;
Py_ssize_t len;
char *p;
int ret;
if (!PyArg_ParseTuple(args, "S", &so))
return NULL;
ret = PyString_AsStringAndSize(so, &p, &len);
if (len % 2 == 1) {
PyErr_Format(PyExc_TypeError, "array must contain an even number of bytes");
return NULL;
}
do_uflip((unsigned short *)p, len/2);
Py_INCREF(Py_None);
return Py_None;
}
static PyObject *
byteswap(PyObject *self, PyObject *args)
{
PyObject *so;
Py_ssize_t len;
char *p;
int ret;
if (!PyArg_ParseTuple(args, "S", &so))
return NULL;
ret = PyString_AsStringAndSize(so, &p, &len);
if (len % 2 == 1) {
PyErr_Format(PyExc_TypeError, "array must contain an even number of bytes");
return NULL;
}
do_byteswap((unsigned short *)p, len/2);
Py_INCREF(Py_None);
return Py_None;
}
static PyMethodDef Pixel16Methods[] =
{
{ "uflip", uflip, METH_VARARGS, "flip the sign bit of an array of short integers." },
{ "byteswap", byteswap, METH_VARARGS, "swap the bytes of an array of short integers." },
{ NULL, NULL, 0, NULL }
};
void initpixel16(void)
{
(void)Py_InitModule("pixel16", Pixel16Methods);
}