Debian Codegraph @

pylibdmtx/wrapper.py

raw
1 """Low-level wrapper around libdmtx's interface
2 """
3 from ctypes import (
4 c_double, c_int, c_long, c_size_t, c_ubyte, c_uint, c_ulong,
5 c_ulonglong, c_char_p, Structure, CFUNCTYPE, POINTER
6 )
7 from enum import IntEnum, unique
8 from packaging import version
9
10 from . import dmtx_library
11
12
13 __all__ = [
14 'DmtxPassFail', 'DmtxUndefined', 'DmtxVector2', 'EXTERNAL_DEPENDENCIES',
15 'LIBDMTX', 'c_ubyte_p', 'dmtxImageCreate', 'dmtxImageDestroy',
16 'dmtxDecodeCreate', 'dmtxDecodeDestroy', 'dmtxRegionDestroy',
17 'dmtxMessageDestroy', 'dmtxTimeAdd', 'dmtxMatrix3VMultiplyBy',
18 'dmtxDecodeSetProp', 'DmtxPackOrder', 'DmtxProperty', 'dmtxTimeNow',
19 'dmtxDecodeMatrixRegion', 'dmtxRegionFindNext'
20 ]
21
22 # Globals populated in load_libdmtx
23 LIBDMTX = None
24 """ctypes.CDLL
25 """
26
27 EXTERNAL_DEPENDENCIES = []
28 """List of instances of ctypes.CDLL. Helpful when freezing.
29 """
30
31
32 def load_libdmtx():
33 """Loads the libdmtx shared library.
34
35 Populates the globals LIBDMTX and EXTERNAL_DEPENDENCIES.
36 """
37 global LIBDMTX
38 global EXTERNAL_DEPENDENCIES
39 if not LIBDMTX:
40 LIBDMTX = dmtx_library.load()
41 EXTERNAL_DEPENDENCIES = [LIBDMTX]
42
43 return LIBDMTX
44
45
46 def libdmtx_function(fname, restype, *args):
47 """Returns a foreign function exported by `libdmtx`.
48
49 Args:
50 fname (:obj:`str`): Name of the exported function as string.
51 restype (:obj:): Return type - one of the `ctypes` primitive C data
52 types.
53 *args: Arguments - a sequence of `ctypes` primitive C data types.
54
55 Returns:
56 cddl.CFunctionType: A wrapper around the function.
57 """
58 prototype = CFUNCTYPE(restype, *args)
59 return prototype((fname, load_libdmtx()))
60
61
62 # Types
63 c_ubyte_p = POINTER(c_ubyte)
64 """unsigned char* type
65 """
66
67 # Defines and enums
68 DmtxUndefined = -1
69
70 # Define this function early so that it can be used in the definitions below.
71 _dmtxVersion = libdmtx_function('dmtxVersion', c_char_p)
72
73
74 def dmtxVersion():
75 """Returns the version of the libdmtx libraray
76
77 Returns:
78 str: Version string
79 """
80 return _dmtxVersion().decode()
81
82
83 @unique
84 class DmtxProperty(IntEnum):
85 DmtxPropScheme = 100
86 DmtxPropSizeRequest = 101
87 DmtxPropMarginSize = 102
88 DmtxPropModuleSize = 103
89 DmtxPropFnc1 = 104
90 # Decoding properties
91 DmtxPropEdgeMin = 200
92 DmtxPropEdgeMax = 201
93 DmtxPropScanGap = 202
94 DmtxPropSquareDevn = 203
95 DmtxPropSymbolSize = 204
96 DmtxPropEdgeThresh = 205
97 # Image properties
98 DmtxPropWidth = 300
99 DmtxPropHeight = 301
100 DmtxPropPixelPacking = 302
101 DmtxPropBitsPerPixel = 303
102 DmtxPropBytesPerPixel = 304
103 DmtxPropRowPadBytes = 305
104 DmtxPropRowSizeBytes = 306
105 DmtxPropImageFlip = 307
106 DmtxPropChannelCount = 308
107 # Image modifiers
108 DmtxPropXmin = 400
109 DmtxPropXmax = 401
110 DmtxPropYmin = 402
111 DmtxPropYmax = 403
112 DmtxPropScale = 404
113
114
115 @unique
116 class DmtxPackOrder(IntEnum):
117 DmtxPackCustom = 100
118 DmtxPack1bppK = 200
119 DmtxPack8bppK = 300
120 DmtxPack16bppRGB = 400
121 DmtxPack16bppRGBX = 401
122 DmtxPack16bppXRGB = 402
123 DmtxPack16bppBGR = 403
124 DmtxPack16bppBGRX = 404
125 DmtxPack16bppXBGR = 405
126 DmtxPack16bppYCbCr = 406
127 DmtxPack24bppRGB = 500
128 DmtxPack24bppBGR = 501
129 DmtxPack24bppYCbCr = 502
130 DmtxPack32bppRGBX = 600
131 DmtxPack32bppXRGB = 601
132 DmtxPack32bppBGRX = 602
133 DmtxPack32bppXBGR = 603
134 DmtxPack32bppCMYK = 604
135
136
137 @unique
138 class DmtxFlip(IntEnum):
139 DmtxFlipNone = 0x00
140 DmtxFlipX = 0x01 << 0
141 DmtxFlipY = 0x01 << 1
142
143
144 @unique
145 class DmtxScheme(IntEnum):
146 DmtxSchemeAutoFast = -2
147 DmtxSchemeAutoBest = -1
148 DmtxSchemeAscii = 0
149 DmtxSchemeC40 = 1
150 DmtxSchemeText = 2
151 DmtxSchemeX12 = 3
152 DmtxSchemeEdifact = 4
153 DmtxSchemeBase256 = 5
154
155
156 @unique
157 class DmtxSymbolSize(IntEnum):
158 DmtxSymbolRectAuto = -3
159 DmtxSymbolSquareAuto = -2
160 DmtxSymbolShapeAuto = -1
161 DmtxSymbol10x10 = 0
162 DmtxSymbol12x12 = 1
163 DmtxSymbol14x14 = 2
164 DmtxSymbol16x16 = 3
165 DmtxSymbol18x18 = 4
166 DmtxSymbol20x20 = 5
167 DmtxSymbol22x22 = 6
168 DmtxSymbol24x24 = 7
169 DmtxSymbol26x26 = 8
170 DmtxSymbol32x32 = 9
171 DmtxSymbol36x36 = 10
172 DmtxSymbol40x40 = 11
173 DmtxSymbol44x44 = 12
174 DmtxSymbol48x48 = 13
175 DmtxSymbol52x52 = 14
176 DmtxSymbol64x64 = 15
177 DmtxSymbol72x72 = 16
178 DmtxSymbol80x80 = 17
179 DmtxSymbol88x88 = 18
180 DmtxSymbol96x96 = 19
181 DmtxSymbol104x104 = 20
182 DmtxSymbol120x120 = 21
183 DmtxSymbol132x132 = 22
184 DmtxSymbol144x144 = 23
185 DmtxSymbol8x18 = 24
186 DmtxSymbol8x32 = 25
187 DmtxSymbol12x26 = 26
188 DmtxSymbol12x36 = 27
189 DmtxSymbol16x36 = 28
190 DmtxSymbol16x48 = 29
191
192
193 # Types
194 DmtxPassFail = c_uint
195 DmtxMatrix3 = c_double * 3 * 3
196
197
198 # Structs
199 if version.parse(dmtxVersion()) < version.parse('0.7.5'):
200 class DmtxMessage(Structure):
201 _fields_ = [
202 ('arraySize', c_size_t),
203 ('codeSize', c_size_t),
204 ('outputSize', c_size_t),
205 ('outputIdx', c_int),
206 ('padCount', c_int),
207 ('array', c_ubyte_p),
208 ('code', c_ubyte_p),
209 ('output', c_ubyte_p),
210 ]
211 else:
212 class DmtxMessage(Structure):
213 _fields_ = [
214 ('arraySize', c_size_t),
215 ('codeSize', c_size_t),
216 ('outputSize', c_size_t),
217 ('outputIdx', c_int),
218 ('padCount', c_int),
219 ('fnc1', c_int), # libdmtx 0.7.5 inserts this field
220 ('array', c_ubyte_p),
221 ('code', c_ubyte_p),
222 ('output', c_ubyte_p),
223 ]
224
225
226 class DmtxImage(Structure):
227 _fields_ = [
228 ('width', c_int),
229 ('height', c_int),
230 ('pixelPacking', c_int),
231 ('bitsPerPixel', c_int),
232 ('bytesPerPixel', c_int),
233 ('rowPadBytes', c_int),
234 ('rowSizeBytes', c_int),
235 ('imageFlip', c_int),
236 ('channelCount', c_int),
237 ('channelStart', c_int * 4),
238 ('bitsPerChannel', c_int * 4),
239 ('pxl', c_ubyte_p)
240 ]
241
242
243 class DmtxTime(Structure):
244 _fields_ = [
245 ('sec', c_ulonglong), # Actually a time_t
246 ('usec', c_ulong),
247 ]
248
249
250 class DmtxPixelLoc(Structure):
251 _fields_ = [
252 ('X', c_int),
253 ('Y', c_int),
254 ]
255
256
257 class DmtxVector2(Structure):
258 _fields_ = [
259 ('X', c_double),
260 ('Y', c_double),
261 ]
262
263
264 class DmtxPointFlow(Structure):
265 _fields_ = [
266 ('plane', c_int),
267 ('arrive', c_int),
268 ('depart', c_int),
269 ('mag', c_int),
270 ('loc', DmtxPixelLoc),
271 ]
272
273
274 class DmtxBestLine(Structure):
275 _fields_ = [
276 ('angle', c_int),
277 ('hOffset', c_int),
278 ('mag', c_int),
279 ('stepBeg', c_int),
280 ('stepPos', c_int),
281 ('stepNeg', c_int),
282 ('distSq', c_int),
283 ('devn', c_double),
284 ('locBeg', DmtxPixelLoc),
285 ('locPos', DmtxPixelLoc),
286 ('locNeg', DmtxPixelLoc),
287 ]
288
289
290 class DmtxScanGrid(Structure):
291 _fields_ = [
292 ('minExtent', c_int),
293 ('maxExtent', c_int),
294 ('xOffset', c_int),
295 ('yOffset', c_int),
296 ('xMin', c_int),
297 ('xMax', c_int),
298 ('yMin', c_int),
299 ('yMax', c_int),
300
301 ('total', c_int),
302 ('extent', c_int),
303 ('jumpSize', c_int),
304 ('pixelTotal', c_int),
305 ('startPos', c_int),
306
307 ('pixelCount', c_int),
308 ('xCenter', c_int),
309 ('yCenter', c_int),
310 ]
311
312
313 if version.parse(dmtxVersion()) < version.parse('0.7.5'):
314 class DmtxDecode(Structure):
315 _fields_ = [
316 ('edgeMin', c_int),
317 ('edgeMax', c_int),
318 ('scanGap', c_int),
319 ('squareDevn', c_double),
320 ('sizeIdxExpected', c_int),
321 ('edgeThresh', c_int),
322
323 ('xMin', c_int),
324 ('xMax', c_int),
325 ('yMin', c_int),
326 ('yMax', c_int),
327 ('scale', c_int),
328
329 ('cache', c_ubyte_p),
330 ('image', POINTER(DmtxImage)),
331 ('grid', DmtxScanGrid),
332 ]
333 else:
334 class DmtxDecode(Structure):
335 _fields_ = [
336 ('edgeMin', c_int),
337 ('edgeMax', c_int),
338 ('scanGap', c_int),
339 ('fnc1', c_int), # libdmtx 0.7.5 inserts this field
340 ('squareDevn', c_double),
341 ('sizeIdxExpected', c_int),
342 ('edgeThresh', c_int),
343
344 ('xMin', c_int),
345 ('xMax', c_int),
346 ('yMin', c_int),
347 ('yMax', c_int),
348 ('scale', c_int),
349
350 ('cache', c_ubyte_p),
351 ('image', POINTER(DmtxImage)),
352 ('grid', DmtxScanGrid),
353 ]
354
355
356 class DmtxRegion(Structure):
357 _fields_ = [
358 ('jumpToPos', c_int),
359 ('jumpToNeg', c_int),
360 ('stepsTotal', c_int),
361 ('finalPos', DmtxPixelLoc),
362 ('finalNeg', DmtxPixelLoc),
363 ('boundMin', DmtxPixelLoc),
364 ('boundMax', DmtxPixelLoc),
365 ('flowBegin', DmtxPointFlow),
366
367 ('polarity', c_int),
368 ('stepR', c_int),
369 ('stepT', c_int),
370 ('locR', DmtxPixelLoc),
371 ('locT', DmtxPixelLoc),
372
373 ('leftKnown', c_int),
374 ('leftAngle', c_int),
375 ('leftLoc', DmtxPixelLoc),
376 ('leftLine', DmtxBestLine),
377 ('bottomKnown', c_int),
378 ('bottomAngle', c_int),
379 ('bottomLoc', DmtxPixelLoc),
380 ('bottomLine', DmtxBestLine),
381 ('topKnown', c_int),
382 ('topAngle', c_int),
383 ('topLoc', DmtxPixelLoc),
384 ('rightKnown', c_int),
385 ('rightAngle', c_int),
386 ('rightLoc', DmtxPixelLoc),
387
388 ('onColor', c_int),
389 ('offColor', c_int),
390 ('sizeIdx', c_int),
391 ('symbolRows', c_int),
392 ('symbolCols', c_int),
393 ('mappingRows', c_int),
394 ('mappingCols', c_int),
395
396 ('raw2fit', DmtxMatrix3),
397 ('fit2raw', DmtxMatrix3),
398 ]
399
400
401 if version.parse(dmtxVersion()) < version.parse('0.7.5'):
402 class DmtxEncode(Structure):
403 _fields_ = [
404 ('method', c_int),
405 ('scheme', c_int),
406 ('sizeIdxRequest', c_int),
407 ('marginSize', c_int),
408 ('moduleSize', c_int),
409 ('pixelPacking', c_int),
410 ('imageFlip', c_int),
411 ('rowPadBytes', c_int),
412 ('message', POINTER(DmtxMessage)),
413 ('image', POINTER(DmtxImage)),
414 ('region', DmtxRegion),
415 ('xfrm', DmtxMatrix3),
416 ('rxfrm', DmtxMatrix3),
417 ]
418 else:
419 class DmtxEncode(Structure):
420 _fields_ = [
421 ('method', c_int),
422 ('scheme', c_int),
423 ('sizeIdxRequest', c_int),
424 ('marginSize', c_int),
425 ('moduleSize', c_int),
426 ('pixelPacking', c_int),
427 ('imageFlip', c_int),
428 ('rowPadBytes', c_int),
429 ('fnc1', c_int), # libdmtx 0.7.5 inserts this field
430 ('message', POINTER(DmtxMessage)),
431 ('image', POINTER(DmtxImage)),
432 ('region', DmtxRegion),
433 ('xfrm', DmtxMatrix3),
434 ('rxfrm', DmtxMatrix3),
435 ]
436
437
438 # Function signatures
439
440 dmtxTimeNow = libdmtx_function('dmtxTimeNow', DmtxTime)
441
442 dmtxTimeAdd = libdmtx_function(
443 'dmtxTimeAdd',
444 DmtxTime,
445 DmtxTime, # t
446 c_long # msec
447 )
448
449 dmtxDecodeCreate = libdmtx_function(
450 'dmtxDecodeCreate',
451 POINTER(DmtxDecode),
452 POINTER(DmtxImage), # img
453 c_int, # scale
454 )
455
456 dmtxDecodeDestroy = libdmtx_function(
457 'dmtxDecodeDestroy',
458 DmtxPassFail,
459 POINTER(POINTER(DmtxDecode))
460 )
461
462
463 dmtxDecodeSetProp = libdmtx_function(
464 'dmtxDecodeSetProp',
465 DmtxPassFail,
466 POINTER(DmtxDecode),
467 c_int, # prop
468 c_int # value
469 )
470
471 dmtxImageCreate = libdmtx_function(
472 'dmtxImageCreate',
473 POINTER(DmtxImage),
474 POINTER(c_ubyte), # pxl
475 c_int, # width
476 c_int, # height
477 c_int # pack
478 )
479
480 dmtxImageDestroy = libdmtx_function(
481 'dmtxImageDestroy',
482 DmtxPassFail,
483 POINTER(POINTER(DmtxImage))
484 )
485
486 dmtxRegionFindNext = libdmtx_function(
487 'dmtxRegionFindNext',
488 POINTER(DmtxRegion),
489 POINTER(DmtxDecode),
490 POINTER(DmtxTime) # timeout
491 )
492
493 dmtxDecodeMatrixRegion = libdmtx_function(
494 'dmtxDecodeMatrixRegion',
495 POINTER(DmtxMessage),
496 POINTER(DmtxDecode), # dec
497 POINTER(DmtxRegion), # reg
498 c_int, # fix
499 )
500
501 dmtxMatrix3VMultiplyBy = libdmtx_function(
502 'dmtxMatrix3VMultiplyBy',
503 c_int,
504 POINTER(DmtxVector2),
505 DmtxMatrix3,
506 )
507
508 dmtxMessageDestroy = libdmtx_function(
509 'dmtxMessageDestroy',
510 DmtxPassFail,
511 POINTER(POINTER(DmtxMessage))
512 )
513
514 dmtxRegionDestroy = libdmtx_function(
515 'dmtxRegionDestroy',
516 DmtxPassFail,
517 POINTER(POINTER(DmtxRegion))
518 )
519
520 dmtxImageGetProp = libdmtx_function(
521 'dmtxImageGetProp',
522 c_int,
523 POINTER(DmtxImage),
524 c_int, # prop
525 )
526
527 dmtxEncodeCreate = libdmtx_function(
528 'dmtxEncodeCreate',
529 POINTER(DmtxEncode),
530 )
531
532 dmtxEncodeDestroy = libdmtx_function(
533 'dmtxEncodeDestroy',
534 DmtxPassFail,
535 POINTER(POINTER(DmtxEncode))
536 )
537
538 dmtxEncodeSetProp = libdmtx_function(
539 'dmtxEncodeSetProp',
540 DmtxPassFail,
541 POINTER(DmtxEncode),
542 c_int, # prop
543 c_int # value
544 )
545
546 dmtxEncodeDataMatrix = libdmtx_function(
547 'dmtxEncodeDataMatrix',
548 DmtxPassFail,
549 POINTER(DmtxEncode),
550 c_int,
551 POINTER(c_ubyte)
552 )