File: qr_encode.c

package info (click to toggle)
python-qrencode 1.2-5
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, sid
  • size: 52 kB
  • sloc: ansic: 54; python: 54; makefile: 4
file content (67 lines) | stat: -rw-r--r-- 1,425 bytes parent folder | download | duplicates (3)
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
#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 PyMethodDef methods[] =
{
    {"encode", encode, METH_VARARGS, "Encodes a string 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