File: wrapper.py

package info (click to toggle)
python-pylibdmtx 0.1.9-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, sid
  • size: 224 kB
  • sloc: python: 1,120; makefile: 11
file content (594 lines) | stat: -rw-r--r-- 14,300 bytes parent folder | download | duplicates (2)
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
"""Low-level wrapper around libdmtx's interface
"""
from ctypes import (
    cdll, c_double, c_int, c_long, c_size_t, c_ubyte, c_uint, c_ulong,
    c_ulonglong, c_char_p, Structure, CFUNCTYPE, POINTER
)
from enum import IntEnum, unique
from distutils.version import LooseVersion

from . import dmtx_library


__all__ = [
    'DmtxPassFail', 'DmtxUndefined', 'DmtxVector2', 'EXTERNAL_DEPENDENCIES',
    'LIBDMTX', 'c_ubyte_p', 'dmtxImageCreate', 'dmtxImageDestroy',
    'dmtxDecodeCreate', 'dmtxDecodeDestroy', 'dmtxRegionDestroy',
    'dmtxMessageDestroy', 'dmtxTimeAdd', 'dmtxMatrix3VMultiplyBy',
    'dmtxDecodeSetProp', 'DmtxPackOrder', 'DmtxProperty', 'dmtxTimeNow',
    'dmtxDecodeMatrixRegion', 'dmtxRegionFindNext'
]

# Globals populated in load_libdmtx
LIBDMTX = None
"""ctypes.CDLL
"""

EXTERNAL_DEPENDENCIES = []
"""List of instances of ctypes.CDLL. Helpful when freezing.
"""

def load_libdmtx():
    """Loads the libdmtx shared library.

    Populates the globals LIBDMTX and EXTERNAL_DEPENDENCIES.
    """
    global LIBDMTX
    global EXTERNAL_DEPENDENCIES
    if not LIBDMTX:
        LIBDMTX = dmtx_library.load()
        EXTERNAL_DEPENDENCIES = [LIBDMTX]

    return LIBDMTX


def libdmtx_function(fname, restype, *args):
    """Returns a foreign function exported by `libdmtx`.

    Args:
        fname (:obj:`str`): Name of the exported function as string.
        restype (:obj:): Return type - one of the `ctypes` primitive C data
        types.
        *args: Arguments - a sequence of `ctypes` primitive C data types.

    Returns:
        cddl.CFunctionType: A wrapper around the function.
    """
    prototype = CFUNCTYPE(restype, *args)
    return prototype((fname, load_libdmtx()))


# Types
c_ubyte_p = POINTER(c_ubyte)
"""unsigned char* type
"""

# Defines and enums
DmtxUndefined = -1

# Define this function early so that it can be used in the definitions below.
_dmtxVersion = libdmtx_function('dmtxVersion', c_char_p)

def dmtxVersion():
    return _dmtxVersion().decode()


@unique
class DmtxProperty(IntEnum):
    DmtxPropScheme = 100
    DmtxPropSizeRequest = 101
    DmtxPropMarginSize = 102
    DmtxPropModuleSize = 103
    DmtxPropFnc1 = 104
    # Decoding properties
    DmtxPropEdgeMin = 200
    DmtxPropEdgeMax = 201
    DmtxPropScanGap = 202
    DmtxPropSquareDevn = 203
    DmtxPropSymbolSize = 204
    DmtxPropEdgeThresh = 205
    # Image properties
    DmtxPropWidth = 300
    DmtxPropHeight = 301
    DmtxPropPixelPacking = 302
    DmtxPropBitsPerPixel = 303
    DmtxPropBytesPerPixel = 304
    DmtxPropRowPadBytes = 305
    DmtxPropRowSizeBytes = 306
    DmtxPropImageFlip = 307
    DmtxPropChannelCount = 308
    # Image modifiers
    DmtxPropXmin = 400
    DmtxPropXmax = 401
    DmtxPropYmin = 402
    DmtxPropYmax = 403
    DmtxPropScale = 404


@unique
class DmtxPackOrder(IntEnum):
    DmtxPackCustom = 100
    DmtxPack1bppK = 200
    DmtxPack8bppK = 300
    DmtxPack16bppRGB = 400
    DmtxPack16bppRGBX = 401
    DmtxPack16bppXRGB = 402
    DmtxPack16bppBGR = 403
    DmtxPack16bppBGRX = 404
    DmtxPack16bppXBGR = 405
    DmtxPack16bppYCbCr = 406
    DmtxPack24bppRGB = 500
    DmtxPack24bppBGR = 501
    DmtxPack24bppYCbCr = 502
    DmtxPack32bppRGBX = 600
    DmtxPack32bppXRGB = 601
    DmtxPack32bppBGRX = 602
    DmtxPack32bppXBGR = 603
    DmtxPack32bppCMYK = 604


@unique
class DmtxFlip(IntEnum):
    DmtxFlipNone = 0x00
    DmtxFlipX = 0x01 << 0
    DmtxFlipY = 0x01 << 1


@unique
class DmtxScheme(IntEnum):
    DmtxSchemeAutoFast = -2
    DmtxSchemeAutoBest = -1
    DmtxSchemeAscii = 0
    DmtxSchemeC40 = 1
    DmtxSchemeText = 2
    DmtxSchemeX12 = 3
    DmtxSchemeEdifact = 4
    DmtxSchemeBase256 = 5


@unique
class DmtxSymbolSize(IntEnum):
    DmtxSymbolRectAuto = -3
    DmtxSymbolSquareAuto = -2
    DmtxSymbolShapeAuto = -1
    DmtxSymbol10x10 = 0
    DmtxSymbol12x12 = 1
    DmtxSymbol14x14 = 2
    DmtxSymbol16x16 = 3
    DmtxSymbol18x18 = 4
    DmtxSymbol20x20 = 5
    DmtxSymbol22x22 = 6
    DmtxSymbol24x24 = 7
    DmtxSymbol26x26 = 8
    DmtxSymbol32x32 = 9
    DmtxSymbol36x36 = 10
    DmtxSymbol40x40 = 11
    DmtxSymbol44x44 = 12
    DmtxSymbol48x48 = 13
    DmtxSymbol52x52 = 14
    DmtxSymbol64x64 = 15
    DmtxSymbol72x72 = 16
    DmtxSymbol80x80 = 17
    DmtxSymbol88x88 = 18
    DmtxSymbol96x96 = 19
    DmtxSymbol104x104 = 20
    DmtxSymbol120x120 = 21
    DmtxSymbol132x132 = 22
    DmtxSymbol144x144 = 23
    DmtxSymbol8x18 = 24
    DmtxSymbol8x32 = 25
    DmtxSymbol12x26 = 26
    DmtxSymbol12x36 = 27
    DmtxSymbol16x36 = 28
    DmtxSymbol16x48 = 29


@unique
class DmtxScheme(IntEnum):
    DmtxSchemeAutoFast = -2
    DmtxSchemeAutoBest = -1
    DmtxSchemeAscii = 0
    DmtxSchemeC40 = 1
    DmtxSchemeText = 2
    DmtxSchemeX12 = 3
    DmtxSchemeEdifact = 4
    DmtxSchemeBase256 = 5


@unique
class DmtxSymbolSize(IntEnum):
    DmtxSymbolRectAuto = -3
    DmtxSymbolSquareAuto = -2
    DmtxSymbolShapeAuto = -1
    DmtxSymbol10x10 = 0
    DmtxSymbol12x12 = 1
    DmtxSymbol14x14 = 2
    DmtxSymbol16x16 = 3
    DmtxSymbol18x18 = 4
    DmtxSymbol20x20 = 5
    DmtxSymbol22x22 = 6
    DmtxSymbol24x24 = 7
    DmtxSymbol26x26 = 8
    DmtxSymbol32x32 = 9
    DmtxSymbol36x36 = 10
    DmtxSymbol40x40 = 11
    DmtxSymbol44x44 = 12
    DmtxSymbol48x48 = 13
    DmtxSymbol52x52 = 14
    DmtxSymbol64x64 = 15
    DmtxSymbol72x72 = 16
    DmtxSymbol80x80 = 17
    DmtxSymbol88x88 = 18
    DmtxSymbol96x96 = 19
    DmtxSymbol104x104 = 20
    DmtxSymbol120x120 = 21
    DmtxSymbol132x132 = 22
    DmtxSymbol144x144 = 23
    DmtxSymbol8x18 = 24
    DmtxSymbol8x32 = 25
    DmtxSymbol12x26 = 26
    DmtxSymbol12x36 = 27
    DmtxSymbol16x36 = 28
    DmtxSymbol16x48 = 29


# Types
DmtxPassFail = c_uint
DmtxMatrix3 = c_double * 3 * 3


# Structs
if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
    class DmtxMessage(Structure):
        _fields_ = [
            ('arraySize', c_size_t),
            ('codeSize', c_size_t),
            ('outputSize', c_size_t),
            ('outputIdx', c_int),
            ('padCount', c_int),
            ('array', c_ubyte_p),
            ('code', c_ubyte_p),
            ('output', c_ubyte_p),
        ]
else:
    class DmtxMessage(Structure):
        _fields_ = [
            ('arraySize', c_size_t),
            ('codeSize', c_size_t),
            ('outputSize', c_size_t),
            ('outputIdx', c_int),
            ('padCount', c_int),
            ('fnc1', c_int),           # libdmtx 0.7.5 inserts this field
            ('array', c_ubyte_p),
            ('code', c_ubyte_p),
            ('output', c_ubyte_p),
        ]


class DmtxImage(Structure):
    _fields_ = [
        ('width', c_int),
        ('height', c_int),
        ('pixelPacking', c_int),
        ('bitsPerPixel', c_int),
        ('bytesPerPixel', c_int),
        ('rowPadBytes', c_int),
        ('rowSizeBytes', c_int),
        ('imageFlip', c_int),
        ('channelCount', c_int),
        ('channelStart', c_int * 4),
        ('bitsPerChannel', c_int * 4),
        ('pxl', c_ubyte_p)
    ]


class DmtxTime(Structure):
    _fields_ = [
        ('sec', c_ulonglong),      # Actually a time_t
        ('usec', c_ulong),
    ]


class DmtxPixelLoc(Structure):
    _fields_ = [
        ('X', c_int),
        ('Y', c_int),
    ]


class DmtxVector2(Structure):
    _fields_ = [
        ('X', c_double),
        ('Y', c_double),
    ]


class DmtxPointFlow(Structure):
    _fields_ = [
        ('plane', c_int),
        ('arrive', c_int),
        ('depart', c_int),
        ('mag', c_int),
        ('loc', DmtxPixelLoc),
    ]


class DmtxBestLine(Structure):
    _fields_ = [
        ('angle', c_int),
        ('hOffset', c_int),
        ('mag', c_int),
        ('stepBeg', c_int),
        ('stepPos', c_int),
        ('stepNeg', c_int),
        ('distSq', c_int),
        ('devn', c_double),
        ('locBeg', DmtxPixelLoc),
        ('locPos', DmtxPixelLoc),
        ('locNeg', DmtxPixelLoc),
    ]


class DmtxScanGrid(Structure):
    _fields_ = [
        ('minExtent', c_int),
        ('maxExtent', c_int),
        ('xOffset', c_int),
        ('yOffset', c_int),
        ('xMin', c_int),
        ('xMax', c_int),
        ('yMin', c_int),
        ('yMax', c_int),

        ('total', c_int),
        ('extent', c_int),
        ('jumpSize', c_int),
        ('pixelTotal', c_int),
        ('startPos', c_int),

        ('pixelCount', c_int),
        ('xCenter', c_int),
        ('yCenter', c_int),
    ]


if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
    class DmtxDecode(Structure):
        _fields_ = [
            ('edgeMin', c_int),
            ('edgeMax', c_int),
            ('scanGap', c_int),
            ('squareDevn', c_double),
            ('sizeIdxExpected', c_int),
            ('edgeThresh', c_int),

            ('xMin', c_int),
            ('xMax', c_int),
            ('yMin', c_int),
            ('yMax', c_int),
            ('scale', c_int),

            ('cache', c_ubyte_p),
            ('image', POINTER(DmtxImage)),
            ('grid', DmtxScanGrid),
        ]
else:
    class DmtxDecode(Structure):
        _fields_ = [
            ('edgeMin', c_int),
            ('edgeMax', c_int),
            ('scanGap', c_int),
            ('fnc1', c_int),           # libdmtx 0.7.5 inserts this field
            ('squareDevn', c_double),
            ('sizeIdxExpected', c_int),
            ('edgeThresh', c_int),

            ('xMin', c_int),
            ('xMax', c_int),
            ('yMin', c_int),
            ('yMax', c_int),
            ('scale', c_int),

            ('cache', c_ubyte_p),
            ('image', POINTER(DmtxImage)),
            ('grid', DmtxScanGrid),
        ]


class DmtxRegion(Structure):
    _fields_ = [
        ('jumpToPos', c_int),
        ('jumpToNeg', c_int),
        ('stepsTotal', c_int),
        ('finalPos', DmtxPixelLoc),
        ('finalNeg', DmtxPixelLoc),
        ('boundMin', DmtxPixelLoc),
        ('boundMax', DmtxPixelLoc),
        ('flowBegin', DmtxPointFlow),

        ('polarity', c_int),
        ('stepR', c_int),
        ('stepT', c_int),
        ('locR', DmtxPixelLoc),
        ('locT', DmtxPixelLoc),

        ('leftKnown', c_int),
        ('leftAngle', c_int),
        ('leftLoc', DmtxPixelLoc),
        ('leftLine', DmtxBestLine),
        ('bottomKnown', c_int),
        ('bottomAngle', c_int),
        ('bottomLoc', DmtxPixelLoc),
        ('bottomLine', DmtxBestLine),
        ('topKnown', c_int),
        ('topAngle', c_int),
        ('topLoc', DmtxPixelLoc),
        ('rightKnown', c_int),
        ('rightAngle', c_int),
        ('rightLoc', DmtxPixelLoc),

        ('onColor', c_int),
        ('offColor', c_int),
        ('sizeIdx', c_int),
        ('symbolRows', c_int),
        ('symbolCols', c_int),
        ('mappingRows', c_int),
        ('mappingCols', c_int),

        ('raw2fit', DmtxMatrix3),
        ('fit2raw', DmtxMatrix3),
    ]


if LooseVersion(dmtxVersion()) < LooseVersion('0.7.5'):
    class DmtxEncode(Structure):
        _fields_ = [
            ('method', c_int),
            ('scheme', c_int),
            ('sizeIdxRequest', c_int),
            ('marginSize', c_int),
            ('moduleSize', c_int),
            ('pixelPacking', c_int),
            ('imageFlip', c_int),
            ('rowPadBytes', c_int),
            ('message', POINTER(DmtxMessage)),
            ('image', POINTER(DmtxImage)),
            ('region', DmtxRegion),
            ('xfrm', DmtxMatrix3),
            ('rxfrm', DmtxMatrix3),
        ]
else:
    class DmtxEncode(Structure):
        _fields_ = [
            ('method', c_int),
            ('scheme', c_int),
            ('sizeIdxRequest', c_int),
            ('marginSize', c_int),
            ('moduleSize', c_int),
            ('pixelPacking', c_int),
            ('imageFlip', c_int),
            ('rowPadBytes', c_int),
            ('fnc1', c_int),            # libdmtx 0.7.5 inserts this field
            ('message', POINTER(DmtxMessage)),
            ('image', POINTER(DmtxImage)),
            ('region', DmtxRegion),
            ('xfrm', DmtxMatrix3),
            ('rxfrm', DmtxMatrix3),
        ]


# Function signatures

dmtxTimeNow = libdmtx_function('dmtxTimeNow', DmtxTime)

dmtxTimeAdd = libdmtx_function(
    'dmtxTimeAdd',
    DmtxTime,
    DmtxTime,    # t
    c_long       # msec
)

dmtxDecodeCreate = libdmtx_function(
    'dmtxDecodeCreate',
    POINTER(DmtxDecode),
    POINTER(DmtxImage),    # img
    c_int,      # scale
)

dmtxDecodeDestroy = libdmtx_function(
    'dmtxDecodeDestroy',
    DmtxPassFail,
    POINTER(POINTER(DmtxDecode))
)


dmtxDecodeSetProp = libdmtx_function(
    'dmtxDecodeSetProp',
    DmtxPassFail,
    POINTER(DmtxDecode),
    c_int,    # prop
    c_int     # value
)

dmtxImageCreate = libdmtx_function(
    'dmtxImageCreate',
    POINTER(DmtxImage),
    POINTER(c_ubyte),    # pxl
    c_int,     # width
    c_int,     # height
    c_int      # pack
)

dmtxImageDestroy = libdmtx_function(
    'dmtxImageDestroy',
    DmtxPassFail,
    POINTER(POINTER(DmtxImage))
)

dmtxRegionFindNext = libdmtx_function(
    'dmtxRegionFindNext',
    POINTER(DmtxRegion),
    POINTER(DmtxDecode),
    POINTER(DmtxTime)    # timeout
)

dmtxDecodeMatrixRegion = libdmtx_function(
    'dmtxDecodeMatrixRegion',
    POINTER(DmtxMessage),
    POINTER(DmtxDecode),    # dec
    POINTER(DmtxRegion),    # reg
    c_int,                  # fix
)

dmtxMatrix3VMultiplyBy = libdmtx_function(
    'dmtxMatrix3VMultiplyBy',
    c_int,
    POINTER(DmtxVector2),
    DmtxMatrix3,
)

dmtxMessageDestroy = libdmtx_function(
    'dmtxMessageDestroy',
    DmtxPassFail,
    POINTER(POINTER(DmtxMessage))
)

dmtxRegionDestroy = libdmtx_function(
    'dmtxRegionDestroy',
    DmtxPassFail,
    POINTER(POINTER(DmtxRegion))
)

dmtxImageGetProp = libdmtx_function(
    'dmtxImageGetProp',
    c_int,
    POINTER(DmtxImage),
    c_int,  # prop
)

dmtxEncodeCreate = libdmtx_function(
    'dmtxEncodeCreate',
    POINTER(DmtxEncode),
)

dmtxEncodeDestroy = libdmtx_function(
    'dmtxEncodeDestroy',
    DmtxPassFail,
    POINTER(POINTER(DmtxEncode))
)

dmtxEncodeSetProp = libdmtx_function(
    'dmtxEncodeSetProp',
    DmtxPassFail,
    POINTER(DmtxEncode),
    c_int,    # prop
    c_int     # value
)

dmtxEncodeDataMatrix = libdmtx_function(
    'dmtxEncodeDataMatrix',
    DmtxPassFail,
    POINTER(DmtxEncode),
    c_int,
    POINTER(c_ubyte)
)