~ubuntu-branches/ubuntu/natty/zbar/natty

1 by Daniel Baumann
Import upstream version 0.7+dfsg
1
/*------------------------------------------------------------------------
2
 *  Copyright 2009 (c) Jeff Brown <spadix@users.sourceforge.net>
3
 *
4
 *  This file is part of the ZBar Bar Code Reader.
5
 *
6
 *  The ZBar Bar Code Reader is free software; you can redistribute it
7
 *  and/or modify it under the terms of the GNU Lesser Public License as
8
 *  published by the Free Software Foundation; either version 2.1 of
9
 *  the License, or (at your option) any later version.
10
 *
11
 *  The ZBar Bar Code Reader is distributed in the hope that it will be
12
 *  useful, but WITHOUT ANY WARRANTY; without even the implied warranty
13
 *  of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 *  GNU Lesser Public License for more details.
15
 *
16
 *  You should have received a copy of the GNU Lesser Public License
17
 *  along with the ZBar Bar Code Reader; if not, write to the Free
18
 *  Software Foundation, Inc., 51 Franklin St, Fifth Floor,
19
 *  Boston, MA  02110-1301  USA
20
 *
21
 *  http://sourceforge.net/projects/zbar
22
 *------------------------------------------------------------------------*/
23
24
#include "zbarmodule.h"
25
26
static char scanner_doc[] = PyDoc_STR(
27
    "low level intensity sample stream scanner.  identifies \"bar\" edges"
28
    "and measures width between them.\n"
29
    "\n"
30
    "FIXME.");
31
32
static zbarScanner*
33
scanner_new (PyTypeObject *type,
34
             PyObject *args,
35
             PyObject *kwds)
36
{
37
    zbarDecoder *decoder = NULL;
38
    static char *kwlist[] = { "decoder", NULL };
39
    if(!PyArg_ParseTupleAndKeywords(args, kwds, "|O!", kwlist,
40
                                    &decoder, zbarDecoder_Type))
41
        return(NULL);
42
43
    zbarScanner *self = (zbarScanner*)type->tp_alloc(type, 0);
44
    if(!self)
45
        return(NULL);
46
47
    zbar_decoder_t *zdcode = NULL;
48
    if(decoder) {
49
        Py_INCREF(decoder);
50
        self->decoder = decoder;
51
        zdcode = decoder->zdcode;
52
    }
53
    self->zscn = zbar_scanner_create(zdcode);
54
    if(!self->zscn) {
55
        Py_DECREF(self);
56
        return(NULL);
57
    }
58
59
    return(self);
60
}
61
62
static int
63
scanner_traverse (zbarScanner *self,
64
                  visitproc visit,
65
                  void *arg)
66
{
67
    Py_VISIT(self->decoder);
68
    return(0);
69
}
70
71
static int
72
scanner_clear (zbarScanner *self)
73
{
74
    Py_CLEAR(self->decoder);
75
    return(0);
76
}
77
78
static void
79
scanner_dealloc (zbarScanner *self)
80
{
81
    scanner_clear(self);
82
    zbar_scanner_destroy(self->zscn);
83
    ((PyObject*)self)->ob_type->tp_free((PyObject*)self);
84
}
85
86
static PyObject*
87
scanner_get_width (zbarScanner *self,
88
                   void *closure)
89
{
90
    unsigned int width = zbar_scanner_get_width(self->zscn);
91
    return(PyInt_FromLong(width));
92
}
93
94
static zbarEnumItem*
95
scanner_get_color (zbarScanner *self,
96
                   void *closure)
97
{
98
    zbar_color_t zcol = zbar_scanner_get_color(self->zscn);
99
    assert(zcol == ZBAR_BAR || zcol == ZBAR_SPACE);
100
    zbarEnumItem *color = color_enum[zcol];
101
    Py_INCREF((PyObject*)color);
102
    return(color);
103
}
104
105
static PyGetSetDef scanner_getset[] = {
106
    { "color",    (getter)scanner_get_color, },
107
    { "width",    (getter)scanner_get_width, },
108
    { NULL, },
109
};
110
111
static PyObject*
112
scanner_reset (zbarScanner *self,
113
               PyObject *args,
114
               PyObject *kwds)
115
{
116
    static char *kwlist[] = { NULL };
117
    if(!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
118
        return(NULL);
119
120
    zbar_scanner_reset(self->zscn);
121
    Py_RETURN_NONE;
122
}
123
124
static PyObject*
125
scanner_new_scan (zbarScanner *self,
126
                  PyObject *args,
127
                  PyObject *kwds)
128
{
129
    static char *kwlist[] = { NULL };
130
    if(!PyArg_ParseTupleAndKeywords(args, kwds, "", kwlist))
131
        return(NULL);
132
133
    zbar_scanner_new_scan(self->zscn);
134
    Py_RETURN_NONE;
135
}
136
137
static zbarEnumItem*
138
scanner_scan_y (zbarScanner *self,
139
                PyObject *args,
140
                PyObject *kwds)
141
{
142
    /* FIXME should accept sequence of values */
143
    int y = 0;
144
    static char *kwlist[] = { "y", NULL };
145
    if(!PyArg_ParseTupleAndKeywords(args, kwds, "i", kwlist, &y))
146
        return(NULL);
147
148
    zbar_symbol_type_t sym = zbar_scan_y(self->zscn, y);
149
    if(PyErr_Occurred())
150
        /* propagate errors during callback */
151
        return(NULL);
152
    if(sym == ZBAR_NONE) {
153
        /* hardcode most common case */
154
        Py_INCREF((PyObject*)symbol_NONE);
155
        return(symbol_NONE);
156
    }
157
    return(zbarSymbol_LookupEnum(sym));
158
}
159
160
static PyMethodDef scanner_methods[] = {
161
    { "reset",        (PyCFunction)scanner_reset,
162
      METH_VARARGS | METH_KEYWORDS, },
163
    { "new_scan",     (PyCFunction)scanner_new_scan,
164
      METH_VARARGS | METH_KEYWORDS, },
165
    { "scan_y",       (PyCFunction)scanner_scan_y,
166
      METH_VARARGS | METH_KEYWORDS, },
167
    { NULL, },
168
};
169
170
PyTypeObject zbarScanner_Type = {
171
    PyObject_HEAD_INIT(NULL)
172
    .tp_name        = "zbar.Scanner",
173
    .tp_doc         = scanner_doc,
174
    .tp_basicsize   = sizeof(zbarScanner),
175
    .tp_flags       = Py_TPFLAGS_DEFAULT | Py_TPFLAGS_BASETYPE |
176
                      Py_TPFLAGS_HAVE_GC,
177
    .tp_new         = (newfunc)scanner_new,
178
    .tp_traverse    = (traverseproc)scanner_traverse,
179
    .tp_clear       = (inquiry)scanner_clear,
180
    .tp_dealloc     = (destructor)scanner_dealloc,
181
    .tp_getset      = scanner_getset,
182
    .tp_methods     = scanner_methods,
183
};