-
Notifications
You must be signed in to change notification settings - Fork 17
/
qr_encode.c
94 lines (76 loc) · 2.21 KB
/
qr_encode.c
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
#include <Python.h>
#include <stdlib.h>
#include <qrencode.h>
#if PY_MAJOR_VERSION >= 3
# define BYTES "y"
#else
# define BYTES "s"
#endif
static PyObject *encode(PyObject *self, PyObject *args)
{
char *str;
int i, version, level, hint, case_sensitive, num_pixels;
QRcode *code;
PyObject *ret;
if(!PyArg_ParseTuple(args, BYTES "iiii:_qrencode.encode",
&str, &version, &level, &hint, &case_sensitive))
return NULL;
code = QRcode_encodeString(str, version, level, hint, case_sensitive);
if (!code) {
return Py_BuildValue("");
}
num_pixels = code->width * code->width;
for(i = 0; i < num_pixels; i++)
code->data[i] = 255 - (code->data[i] & 0x1) * 0xFF;
ret = Py_BuildValue("(ii" BYTES "#)", code->version, code->width,
code->data, num_pixels);
QRcode_free(code);
return ret;
};
static PyObject *encode_bytes(PyObject *self, PyObject *args)
{
char *str;
int size, i, version, level, num_pixels;
QRcode *code;
PyObject *ret;
if(!PyArg_ParseTuple(args, BYTES "#ii:_qrencode.encode",
&str, &size, &version, &level))
return NULL;
code = QRcode_encodeData(size, str, version, level);
if (!code) {
return Py_BuildValue("");
}
num_pixels = code->width * code->width;
for(i = 0; i < num_pixels; i++)
code->data[i] = 255 - (code->data[i] & 0x1) * 0xFF;
ret = Py_BuildValue("(ii" BYTES "#)", code->version, code->width,
code->data, num_pixels);
QRcode_free(code);
return ret;
};
static PyMethodDef methods[] =
{
{"encode", encode, METH_VARARGS, "Encodes a string as a QR-code. Returns a tuple of (version, width, data)"},
{"encode_bytes", encode_bytes, METH_VARARGS, "Encodes raw bytes as a QR-code. Returns a tuple of (version, width, data)"},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
static PyModuleDef module = {
PyModuleDef_HEAD_INIT,
"qrencode._qrencode",
NULL,
-1,
methods,
};
PyMODINIT_FUNC
PyInit__qrencode(void)
{
return PyModule_Create(&module);
}
#else
PyMODINIT_FUNC
init_qrencode(void)
{
Py_InitModule("qrencode._qrencode", methods);
}
#endif