-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathzxing.xs
360 lines (315 loc) · 8.22 KB
/
zxing.xs
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
#define ZX_USE_UTF8
#include "ReadBarcode.h"
#include "GTIN.h"
#include "ZXVersion.h"
#include <optional>
#include <memory>
#define PERL_NO_GET_CONTEXT
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
#include "imext.h"
#include "imperl.h"
using namespace ZXing;
// typemap support
using std_string = std::string;
#define string_to_SV(str, flags) string_to_SVx(aTHX_ (str), (flags))
static inline SV *
string_to_SVx(pTHX_ const std::string &str, U32 flags) {
SV *sv = newSVpvn_flags(str.data(), str.size(), flags);
// in theory at least, the decoded strings are UTF-8
// in C++20 that would mean std::u8string but zxing doesn't seem to use that yet
sv_utf8_decode(sv);
return sv;
}
struct decoder {
DecodeHints hints;
std::string error;
};
static decoder *
dec_new() {
decoder *dec = new decoder();
dec->hints.setFormats(BarcodeFormats::all());
#if ZXING_VERSION_MAJOR >= 2
dec->hints.setTextMode(TextMode::HRI);
#endif
dec->hints.setEanAddOnSymbol(EanAddOnSymbol::Read);
return dec;
}
static void
dec_DESTROY(decoder *dec) {
delete dec;
}
#define dec_formats(dec) dec_formatsx(aTHX_ (dec))
static std::string
dec_formatsx(pTHX_ decoder *dec) {
return ToString(dec->hints.formats());
}
static bool
dec_set_formats(decoder *dec, const char *formats) {
try {
dec->hints.setFormats(BarcodeFormatsFromString(formats));
return true;
}
catch (std::exception &e) {
dec->error = e.what();
return false;
}
}
static inline std::string
dec_error(decoder *dec) {
return dec->error;
}
static std::vector<std::string>
dec_avail_formats() {
std::vector<std::string> formats;
for (auto f : BarcodeFormats::all()) {
formats.emplace_back(ToString(f));
}
return formats;
}
static std::unique_ptr<uint8_t[]>
get_image_data(i_img *im, ImageFormat &format) {
int channels = im->channels < 3 ? 1 : 3;
size_t row_size = im->xsize * channels;
auto data{std::make_unique<uint8_t[]>(im->ysize * row_size)};
auto datap = data.get();
for (i_img_dim y = 0; y < im->ysize; ++y) {
i_gsamp(im, 0, im->xsize, y, datap, nullptr, channels);
datap += row_size;
}
format = channels == 1 ? ImageFormat::Lum : ImageFormat::RGB;
return data;
}
static std::optional<Results>
dec_decode(decoder *dec, i_img *im) {
ImageFormat format;
auto imdata = get_image_data(im, format);
ImageView image(imdata.get(), im->xsize, im->ysize, format);
return ReadBarcodes(image, dec->hints);
}
static inline std_string
res_text(Result *res) {
return res->text();
}
#define Q_(x) #x
#define Q(x) Q_(x)
#define zx_version() \
Q(ZXING_VERSION_MAJOR) "." Q(ZXING_VERSION_MINOR) "." Q(ZXING_VERSION_PATCH)
typedef decoder *Imager__zxing__Decoder;
typedef Result *Imager__zxing__Decoder__Result;
enum bool_options {
bo_try_harder = 1,
bo_try_downscale,
bo_pure,
bo_try_code39_extended_mode,
bo_validate_code39_checksum,
bo_validate_itf_checksum,
bo_return_codabar_start_end,
bo_return_errors,
bo_try_rotate,
bo_try_invert
};
DEFINE_IMAGER_CALLBACKS;
MODULE = Imager::zxing PACKAGE = Imager::zxing PREFIX=zx_
const char *
zx_version(...)
C_ARGS:
MODULE = Imager::zxing PACKAGE = Imager::zxing::Decoder PREFIX=dec_
Imager::zxing::Decoder
dec_new(cls)
C_ARGS:
void
dec_DESTROY(Imager::zxing::Decoder dec)
std_string
dec_formats(Imager::zxing::Decoder dec)
bool
dec_set_formats(Imager::zxing::Decoder dec, const char *formats)
void
dec_decode(Imager::zxing::Decoder dec, Imager im)
PPCODE:
auto results = dec_decode(dec, im);
if (results) {
EXTEND(SP, results->size());
for (auto &&r : *results) {
auto pr = new Result(r);
SV *sv_r = sv_newmortal();
sv_setref_pv(sv_r, "Imager::zxing::Decoder::Result", pr);
PUSHs(sv_r);
}
}
else {
XSRETURN_EMPTY;
}
std_string
dec_error(Imager::zxing::Decoder dec)
void
dec_set_try_harder(Imager::zxing::Decoder dec, bool val)
ALIAS:
set_try_harder = bo_try_harder
set_try_downscale = bo_try_downscale
set_pure = bo_pure
set_try_code39_extended_mode = bo_try_code39_extended_mode
set_validate_code39_checksum = bo_validate_code39_checksum
set_validate_itf_checksum = bo_validate_itf_checksum
set_return_codabar_start_end = bo_return_codabar_start_end
set_return_errors = bo_return_errors
set_try_rotate = bo_try_rotate
set_try_invert = bo_try_invert
CODE:
switch (static_cast<bool_options>(ix)) {
case bo_try_harder:
dec->hints.setTryHarder(val);
break;
case bo_try_downscale:
dec->hints.setTryDownscale(val);
break;
case bo_pure:
dec->hints.setIsPure(val);
break;
case bo_try_code39_extended_mode:
dec->hints.setTryCode39ExtendedMode(val);
break;
case bo_validate_code39_checksum:
dec->hints.setValidateCode39CheckSum(val);
break;
case bo_validate_itf_checksum:
dec->hints.setValidateITFCheckSum(val);
break;
case bo_return_codabar_start_end:
dec->hints.setReturnCodabarStartEnd(val);
break;
case bo_return_errors:
dec->hints.setReturnErrors(val);
break;
case bo_try_rotate:
dec->hints.setTryRotate(val);
break;
case bo_try_invert:
#if ZXING_VERSION_MAJOR >= 2
dec->hints.setTryInvert(val);
#else
Perl_croak(aTHX_ "set_try_invert requires zxing-cpp 2.0.0 or later");
#endif
break;
}
bool
dec_try_harder(Imager::zxing::Decoder dec)
ALIAS:
try_harder = bo_try_harder
try_downscale = bo_try_downscale
pure = bo_pure
try_code39_extended_mode = bo_try_code39_extended_mode
validate_code39_checksum = bo_validate_code39_checksum
validate_itf_checksum = bo_validate_itf_checksum
return_codabar_start_end = bo_return_codabar_start_end
return_errors = bo_return_errors
try_rotate = bo_try_rotate
try_invert = bo_try_invert
CODE:
switch (static_cast<bool_options>(ix)) {
case bo_try_harder:
RETVAL = dec->hints.tryHarder();
break;
case bo_try_downscale:
RETVAL = dec->hints.tryDownscale();
break;
case bo_pure:
RETVAL = dec->hints.isPure();
break;
case bo_try_code39_extended_mode:
RETVAL = dec->hints.tryCode39ExtendedMode();
break;
case bo_validate_code39_checksum:
RETVAL = dec->hints.validateCode39CheckSum();
break;
case bo_validate_itf_checksum:
RETVAL = dec->hints.validateITFCheckSum();
break;
case bo_return_codabar_start_end:
RETVAL = dec->hints.returnCodabarStartEnd();
break;
case bo_return_errors:
RETVAL = dec->hints.returnErrors();
break;
case bo_try_rotate:
RETVAL = dec->hints.tryRotate();
break;
case bo_try_invert:
#if ZXING_VERSION_MAJOR >= 2
RETVAL = dec->hints.tryInvert();
#else
Perl_croak(aTHX_ "try_invert requires zxing-cpp 2.0.0 or later");
#endif
break;
}
OUTPUT: RETVAL
void
dec_avail_formats(cls)
PPCODE:
auto v = dec_avail_formats();
EXTEND(SP, v.size());
for (auto f : v) {
PUSHs(string_to_SV(f, SVs_TEMP));
}
MODULE = Imager::zxing PACKAGE = Imager::zxing::Decoder::Result PREFIX = res_
std_string
res_text(Imager::zxing::Decoder::Result res)
bool
res_is_valid(Imager::zxing::Decoder::Result res)
ALIAS:
is_valid = 1
is_mirrored = 2
is_inverted = 3
CODE:
switch (ix) {
case 1:
RETVAL = res->isValid();
break;
case 2:
RETVAL = res->isMirrored();
break;
case 3:
#if ZXING_MAJOR_VERSION >= 2
RETVAL = res->isInverted();
#else
RETVAL = false;
#endif
break;
}
OUTPUT: RETVAL
std_string
res_format(Imager::zxing::Decoder::Result res)
ALIAS:
format = 1
content_type = 2
CODE:
switch (ix) {
case 1:
RETVAL = ToString(res->format());
break;
case 2:
RETVAL = ToString(res->contentType());
break;
}
OUTPUT: RETVAL
void
res_position(Imager::zxing::Decoder::Result res)
PPCODE:
auto pos = res->position();
EXTEND(SP, 8);
for (auto &f : pos) {
PUSHs(sv_2mortal(newSViv(f.x)));
PUSHs(sv_2mortal(newSViv(f.y)));
}
int
res_orientation(Imager::zxing::Decoder::Result res)
CODE:
RETVAL = res->orientation();
OUTPUT: RETVAL
void
res_DESTROY(Imager::zxing::Decoder::Result res)
PPCODE:
delete res;
BOOT:
PERL_INITIALIZE_IMAGER_CALLBACKS;