| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | /* GATE PROJECT LICENSE: | ||
| 2 | +----------------------------------------------------------------------------+ | ||
| 3 | | Copyright(c) 2018-2026, Stefan Meislinger | | ||
| 4 | | All rights reserved. | | ||
| 5 | | | | ||
| 6 | | Redistribution and use in source and binary forms, with or without | | ||
| 7 | | modification, are permitted provided that the following conditions are met:| | ||
| 8 | | | | ||
| 9 | | 1. Redistributions of source code must retain the above copyright notice, | | ||
| 10 | | this list of conditions and the following disclaimer. | | ||
| 11 | | 2. Redistributions in binary form must reproduce the above copyright | | ||
| 12 | | notice, this list of conditions and the following disclaimer in the | | ||
| 13 | | documentation and/or other materials provided with the distribution. | | ||
| 14 | | | | ||
| 15 | | THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"| | ||
| 16 | | AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | | ||
| 17 | | IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | | ||
| 18 | | ARE DISCLAIMED.IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE | | ||
| 19 | | LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | | ||
| 20 | | CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | | ||
| 21 | | SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | | ||
| 22 | | INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | | ||
| 23 | | CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | | ||
| 24 | | ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | | ||
| 25 | | THE POSSIBILITY OF SUCH DAMAGE. | | ||
| 26 | +----------------------------------------------------------------------------+ | ||
| 27 | */ | ||
| 28 | |||
| 29 | #include "gate/tech/barcodes.h" | ||
| 30 | #include "gate/results.h" | ||
| 31 | #include "gate/debugging.h" | ||
| 32 | #include "gate/blobs.h" | ||
| 33 | #include "gate/graphics/fonts.h" | ||
| 34 | #include "gate/graphics/drawing.h" | ||
| 35 | |||
| 36 | |||
| 37 | #if !defined(GATE_EXTLIB_DMTX) | ||
| 38 | |||
| 39 | gate_result_t gate_tech_dmtx_scan(gate_rasterimage_t const* input, gate_enumint_t flags, gate_string_t* output) | ||
| 40 | { | ||
| 41 | (void)input; | ||
| 42 | (void)flags; | ||
| 43 | (void)output; | ||
| 44 | return GATE_RESULT_NOTSUPPORTED; | ||
| 45 | } | ||
| 46 | |||
| 47 | gate_result_t gate_tech_dmtx_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | ||
| 48 | { | ||
| 49 | (void)input; | ||
| 50 | (void)flags; | ||
| 51 | (void)output; | ||
| 52 | return GATE_RESULT_NOTSUPPORTED; | ||
| 53 | } | ||
| 54 | |||
| 55 | |||
| 56 | #else /* GATE_EXTLIB_DMTX */ | ||
| 57 | |||
| 58 | #include <dmtx.h> | ||
| 59 | |||
| 60 | 3 | gate_result_t gate_tech_dmtx_scan(gate_rasterimage_t const* input, gate_enumint_t flags, gate_string_t* output) | |
| 61 | { | ||
| 62 | 3 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 63 | 3 | DmtxImage* dmtx_image = NULL; | |
| 64 | 3 | DmtxDecode* decoder = NULL; | |
| 65 | 3 | DmtxRegion* region = NULL; | |
| 66 | 3 | DmtxMessage* message = NULL; | |
| 67 | 3 | gate_blob_t blob = GATE_INIT_EMPTY; | |
| 68 | |||
| 69 | GATE_UNUSED_ARG(flags); | ||
| 70 | |||
| 71 | do | ||
| 72 | { | ||
| 73 | 3 | const int width = (int)gate_rasterimage_width(input); | |
| 74 | 3 | const int height = (int)gate_rasterimage_height(input); | |
| 75 | |||
| 76 |
2/3✓ Branch 1 taken 1 times.
✓ Branch 2 taken 2 times.
✗ Branch 3 not taken.
|
3 | switch (gate_rasterimage_format(input)) |
| 77 | { | ||
| 78 | 1 | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
| 79 | { | ||
| 80 | 1 | dmtx_image = dmtxImageCreate((unsigned char*)gate_rasterimage_get_line_ptr(input, 0), width, height, DmtxPack32bppRGBX); | |
| 81 | 1 | dmtxImageSetProp(dmtx_image, DmtxPropRowPadBytes, 0); | |
| 82 | 1 | dmtxImageSetProp(dmtx_image, DmtxPropImageFlip, DmtxFlipNone); | |
| 83 | 1 | break; | |
| 84 | } | ||
| 85 | 2 | case GATE_IMAGE_PIXELFORMAT_GRAY8: | |
| 86 | { | ||
| 87 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 1 times.
|
2 | if (input->line_padding == 0) |
| 88 | { | ||
| 89 | 1 | dmtx_image = dmtxImageCreate((unsigned char*)gate_rasterimage_get_line_ptr(input, 0), width, height, DmtxPack8bppK); | |
| 90 | } | ||
| 91 | else | ||
| 92 | { | ||
| 93 | 1 | gate_uint8_t* ptr_blob = NULL; | |
| 94 | int y; | ||
| 95 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_blob_create(&blob, NULL, width * height)) |
| 96 | { | ||
| 97 | ✗ | break; | |
| 98 | } | ||
| 99 | 1 | ptr_blob = gate_blob_databuffer(&blob); | |
| 100 | |||
| 101 |
2/2✓ Branch 0 taken 122 times.
✓ Branch 1 taken 1 times.
|
123 | for (y = 0; y < height; ++y) |
| 102 | { | ||
| 103 | 122 | gate_mem_copy(ptr_blob + y * width, gate_rasterimage_get_line_ptr(input, y), (gate_size_t)width); | |
| 104 | } | ||
| 105 | 1 | dmtx_image = dmtxImageCreate(ptr_blob, width, height, DmtxPack8bppK); | |
| 106 | } | ||
| 107 | 2 | dmtxImageSetProp(dmtx_image, DmtxPropRowPadBytes, 0); | |
| 108 | 2 | dmtxImageSetProp(dmtx_image, DmtxPropImageFlip, DmtxFlipNone); | |
| 109 | 2 | break; | |
| 110 | } | ||
| 111 | } | ||
| 112 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (NULL == dmtx_image) |
| 113 | { | ||
| 114 | ✗ | ret = GATE_RESULT_FAILED; | |
| 115 | ✗ | break; | |
| 116 | } | ||
| 117 | |||
| 118 | 3 | decoder = dmtxDecodeCreate(dmtx_image, 1); | |
| 119 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (NULL == decoder) |
| 120 | { | ||
| 121 | ✗ | ret = GATE_RESULT_FAILED; | |
| 122 | ✗ | break; | |
| 123 | } | ||
| 124 | |||
| 125 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | while (NULL != (region = dmtxRegionFindNext(decoder, NULL))) |
| 126 | { | ||
| 127 | 3 | message = dmtxDecodeMatrixRegion(decoder, region, DmtxUndefined); | |
| 128 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | if (NULL != message) |
| 129 | { | ||
| 130 | /* region could be decoded -> something was found -> success */ | ||
| 131 | 3 | break; | |
| 132 | } | ||
| 133 | /* could not decode region, destroy it and try to find another one*/ | ||
| 134 | ✗ | dmtxRegionDestroy(®ion); | |
| 135 | } | ||
| 136 | |||
| 137 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (NULL == message) |
| 138 | { | ||
| 139 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 140 | ✗ | break; | |
| 141 | } | ||
| 142 | |||
| 143 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == gate_string_create(output, (char const*)message->output, (gate_size_t)message->outputIdx)) |
| 144 | { | ||
| 145 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 146 | ✗ | break; | |
| 147 | } | ||
| 148 | |||
| 149 | 3 | ret = GATE_RESULT_OK; | |
| 150 | |||
| 151 | } while (0); | ||
| 152 | |||
| 153 | 3 | dmtxMessageDestroy(&message); | |
| 154 | 3 | dmtxRegionDestroy(®ion); | |
| 155 | 3 | dmtxDecodeDestroy(&decoder); | |
| 156 | 3 | dmtxImageDestroy(&dmtx_image); | |
| 157 | 3 | gate_blob_release(&blob); | |
| 158 | |||
| 159 | 3 | return ret; | |
| 160 | } | ||
| 161 | |||
| 162 | 258 | static void draw_dmtx_dot(gate_rasterimage_t* image, unsigned x, unsigned y, unsigned strength, unsigned border) | |
| 163 | { | ||
| 164 | static const gate_color_t black = GATE_COLOR_BLACK; | ||
| 165 | unsigned a; | ||
| 166 |
2/2✓ Branch 0 taken 1032 times.
✓ Branch 1 taken 258 times.
|
1290 | for (a = 0; a != strength; ++a) |
| 167 | { | ||
| 168 | unsigned b; | ||
| 169 |
2/2✓ Branch 0 taken 4128 times.
✓ Branch 1 taken 1032 times.
|
5160 | for (b = 0; b != strength; ++b) |
| 170 | { | ||
| 171 | 4128 | gate_rasterimage_set_pixel(image, border + x * strength + b, border + y * strength + a, &black); | |
| 172 | } | ||
| 173 | } | ||
| 174 | 258 | } | |
| 175 | |||
| 176 | 1 | gate_result_t gate_tech_dmtx_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | |
| 177 | { | ||
| 178 | static const gate_color_t white = GATE_COLOR_WHITE; | ||
| 179 | 1 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 180 | DmtxEncode* encoder; | ||
| 181 | |||
| 182 | GATE_UNUSED_ARG(flags); | ||
| 183 | |||
| 184 | do | ||
| 185 | { | ||
| 186 | unsigned int result; | ||
| 187 | int width, height; | ||
| 188 | int row, col; | ||
| 189 | 1 | const unsigned pixel_strength = 4; | |
| 190 | 1 | const unsigned border = pixel_strength * 4; | |
| 191 | |||
| 192 | 1 | encoder = dmtxEncodeCreate(); | |
| 193 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (NULL == encoder) |
| 194 | { | ||
| 195 | ✗ | ret = GATE_RESULT_FAILED; | |
| 196 | ✗ | break; | |
| 197 | } | ||
| 198 | |||
| 199 | //DmtxPass | DmtxFail | ||
| 200 | 1 | result = dmtxEncodeDataMatrix(encoder, (int)gate_string_length(input), (unsigned char*)gate_string_ptr(input, 0)); | |
| 201 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (DmtxFail == result) |
| 202 | { | ||
| 203 | ✗ | ret = GATE_RESULT_EXECUTIONFAILED; | |
| 204 | ✗ | break; | |
| 205 | } | ||
| 206 | |||
| 207 | 1 | width = encoder->region.symbolCols * (int)pixel_strength + (int)border * 2; | |
| 208 | 1 | height = encoder->region.symbolRows * (int)pixel_strength + (int)border * 2; | |
| 209 | |||
| 210 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_rasterimage_create(output, GATE_IMAGE_PIXELFORMAT_GRAY8, width, height, NULL)) |
| 211 | { | ||
| 212 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 213 | ✗ | break; | |
| 214 | } | ||
| 215 | 1 | gate_rasterimage_clear(output, &white); | |
| 216 | |||
| 217 |
2/2✓ Branch 0 taken 22 times.
✓ Branch 1 taken 1 times.
|
23 | for (row = 0; row < encoder->region.symbolRows; ++row) |
| 218 | { | ||
| 219 | 22 | int rowinverse = encoder->region.symbolRows - row - 1; | |
| 220 | 22 | gate_uint8_t* ptr_gray = (gate_uint8_t*)gate_rasterimage_get_line_ptr(output, (unsigned)row + border); | |
| 221 | 22 | ptr_gray += border; | |
| 222 |
2/2✓ Branch 0 taken 484 times.
✓ Branch 1 taken 22 times.
|
506 | for (col = 0; col < encoder->region.symbolCols; ++col) |
| 223 | { | ||
| 224 | 484 | int module_status = dmtxSymbolModuleStatus(encoder->message, encoder->region.sizeIdx, rowinverse, col); | |
| 225 |
2/2✓ Branch 0 taken 258 times.
✓ Branch 1 taken 226 times.
|
484 | if (module_status & DmtxModuleOn) |
| 226 | { | ||
| 227 | 258 | draw_dmtx_dot(output, col, row, pixel_strength, border); | |
| 228 | } | ||
| 229 | } | ||
| 230 | } | ||
| 231 | 1 | ret = GATE_RESULT_OK; | |
| 232 | } while (0); | ||
| 233 | |||
| 234 | 1 | dmtxEncodeDestroy(&encoder); | |
| 235 | 1 | return ret; | |
| 236 | } | ||
| 237 | |||
| 238 | #endif /* GATE_EXTLIB_DMTX */ | ||
| 239 | |||
| 240 | |||
| 241 | |||
| 242 | #if !defined(GATE_EXTLIB_QUIRC) | ||
| 243 | |||
| 244 | gate_result_t gate_tech_qrcode_scan(gate_rasterimage_t const* input, gate_enumint_t flags, gate_string_t* output) | ||
| 245 | { | ||
| 246 | return GATE_RESULT_NOTSUPPORTED; | ||
| 247 | } | ||
| 248 | |||
| 249 | #else /* GATE_EXTLIB_QUIRC */ | ||
| 250 | |||
| 251 | #include <quirc.h> | ||
| 252 | |||
| 253 | 2 | gate_result_t gate_tech_qrcode_scan(gate_rasterimage_t const* input, gate_enumint_t flags, gate_string_t* output) | |
| 254 | { | ||
| 255 | 2 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 256 | 2 | struct quirc* qr = NULL; | |
| 257 | 2 | gate_strbuilder_t builder = GATE_INIT_EMPTY; | |
| 258 | |||
| 259 | GATE_UNUSED_ARG(flags); | ||
| 260 | |||
| 261 | do | ||
| 262 | { | ||
| 263 | int result; | ||
| 264 | int width, height; | ||
| 265 | int x, y; | ||
| 266 | uint8_t* gray_image; | ||
| 267 | uint8_t* dst_pixel; | ||
| 268 | int num_codes, code_index; | ||
| 269 | gate_size_t success_count, error_count; | ||
| 270 | gate_enumint_t format; | ||
| 271 | |||
| 272 | 2 | gate_strbuilder_create(&builder, 192); | |
| 273 | |||
| 274 | 2 | qr = quirc_new(); | |
| 275 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (!qr) |
| 276 | { | ||
| 277 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 278 | ✗ | break; | |
| 279 | } | ||
| 280 | |||
| 281 | 2 | width = gate_rasterimage_width(input); | |
| 282 | 2 | height = gate_rasterimage_height(input); | |
| 283 | 2 | format = gate_rasterimage_format(input); | |
| 284 | 2 | result = quirc_resize(qr, width, height); | |
| 285 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (result < 0) |
| 286 | { | ||
| 287 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 288 | ✗ | break; | |
| 289 | } | ||
| 290 | |||
| 291 | 2 | gray_image = quirc_begin(qr, &width, &height); | |
| 292 | 2 | dst_pixel = gray_image; | |
| 293 | |||
| 294 |
1/3✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
✗ Branch 2 not taken.
|
2 | switch (format) |
| 295 | { | ||
| 296 | 2 | case GATE_IMAGE_PIXELFORMAT_GRAY8: | |
| 297 | { | ||
| 298 |
2/2✓ Branch 0 taken 66 times.
✓ Branch 1 taken 2 times.
|
68 | for (y = 0; y != height; ++y) |
| 299 | { | ||
| 300 | 66 | gate_uint8_t const* src_byte = (gate_uint8_t const*)gate_rasterimage_get_line_ptr(input, y); | |
| 301 |
2/2✓ Branch 0 taken 2178 times.
✓ Branch 1 taken 66 times.
|
2244 | for (x = 0; x != width; ++x) |
| 302 | { | ||
| 303 | 2178 | *dst_pixel = *src_byte; | |
| 304 | 2178 | ++src_byte; | |
| 305 | 2178 | ++dst_pixel; | |
| 306 | } | ||
| 307 | } | ||
| 308 | 2 | break; | |
| 309 | } | ||
| 310 | ✗ | case GATE_IMAGE_PIXELFORMAT_RGBA: | |
| 311 | { | ||
| 312 | ✗ | for (y = 0; y != height; ++y) | |
| 313 | { | ||
| 314 | ✗ | gate_color_t const* src_pixel = (gate_color_t const*)gate_rasterimage_get_line_ptr(input, y); | |
| 315 | ✗ | for (x = 0; x != width; ++x) | |
| 316 | { | ||
| 317 | ✗ | *dst_pixel = (gate_uint8_t)((0 | |
| 318 | ✗ | + ((int)src_pixel->r * 54) | |
| 319 | ✗ | + ((int)src_pixel->g * 182) | |
| 320 | ✗ | + ((int)src_pixel->b * 19)) | |
| 321 | ✗ | / 255); | |
| 322 | ✗ | ++src_pixel; | |
| 323 | ✗ | ++dst_pixel; | |
| 324 | } | ||
| 325 | } | ||
| 326 | ✗ | break; | |
| 327 | } | ||
| 328 | ✗ | default: | |
| 329 | { | ||
| 330 | ✗ | for (y = 0; y != height; ++y) | |
| 331 | { | ||
| 332 | ✗ | for (x = 0; x != width; ++x) | |
| 333 | { | ||
| 334 | gate_color_t pixel; | ||
| 335 | ✗ | gate_rasterimage_get_pixel(input, x, y, &pixel); | |
| 336 | ✗ | *dst_pixel = (gate_uint8_t)((0 | |
| 337 | ✗ | + ((int)pixel.r * 54) | |
| 338 | ✗ | + ((int)pixel.g * 182) | |
| 339 | ✗ | + ((int)pixel.b * 19)) | |
| 340 | ✗ | / 255); | |
| 341 | ✗ | ++dst_pixel; | |
| 342 | } | ||
| 343 | } | ||
| 344 | ✗ | break; | |
| 345 | } | ||
| 346 | } | ||
| 347 | |||
| 348 | 2 | quirc_end(qr); | |
| 349 | |||
| 350 | 2 | num_codes = quirc_count(qr); | |
| 351 | 2 | ret = GATE_RESULT_OK; | |
| 352 | 2 | success_count = 0; | |
| 353 | 2 | error_count = 0; | |
| 354 | |||
| 355 |
2/2✓ Branch 0 taken 2 times.
✓ Branch 1 taken 2 times.
|
4 | for (code_index = 0; code_index < num_codes; code_index++) |
| 356 | { | ||
| 357 | struct quirc_code code; | ||
| 358 | struct quirc_data data; | ||
| 359 | quirc_decode_error_t err; | ||
| 360 | |||
| 361 | 2 | quirc_extract(qr, code_index, &code); | |
| 362 | |||
| 363 | 2 | err = quirc_decode(&code, &data); | |
| 364 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (QUIRC_ERROR_DATA_ECC == err) |
| 365 | { | ||
| 366 | ✗ | quirc_flip(&code); | |
| 367 | ✗ | err = quirc_decode(&code, &data); | |
| 368 | } | ||
| 369 | |||
| 370 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (err != QUIRC_SUCCESS) |
| 371 | { | ||
| 372 | ✗ | GATE_DEBUG_TRACE_MSG_VALUE("Failed to decode QR Code", code_index); | |
| 373 | ✗ | GATE_DEBUG_TRACE(quirc_strerror(err)); | |
| 374 | ✗ | ++error_count; | |
| 375 | ✗ | continue; | |
| 376 | /* | ||
| 377 | ret = GATE_RESULT_NOMATCH; | ||
| 378 | break; | ||
| 379 | */ | ||
| 380 | } | ||
| 381 | |||
| 382 | /* | ||
| 383 | switch (data.data_type) | ||
| 384 | { | ||
| 385 | case QUIRC_DATA_TYPE_NUMERIC: | ||
| 386 | case QUIRC_DATA_TYPE_ALPHA: | ||
| 387 | case QUIRC_DATA_TYPE_BYTE: | ||
| 388 | case QUIRC_DATA_TYPE_KANJI: | ||
| 389 | break; | ||
| 390 | } | ||
| 391 | */ | ||
| 392 | |||
| 393 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (success_count != 0) |
| 394 | { | ||
| 395 | ✗ | gate_strbuilder_append_text(&builder, GATE_STR_NEWLINE, GATE_STR_NEWLINE_LENGTH); | |
| 396 | } | ||
| 397 | |||
| 398 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (data.payload_len != gate_strbuilder_append_text(&builder, (char const*)data.payload, data.payload_len)) |
| 399 | { | ||
| 400 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 401 | ✗ | break; | |
| 402 | } | ||
| 403 | 2 | ++success_count; | |
| 404 | } | ||
| 405 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | GATE_BREAK_IF_FAILED(ret); |
| 406 | |||
| 407 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 2 times.
|
2 | if (success_count == 0) |
| 408 | { | ||
| 409 | ✗ | if (error_count == 0) | |
| 410 | { | ||
| 411 | /* no qr code found*/ | ||
| 412 | ✗ | ret = GATE_RESULT_NOMATCH; | |
| 413 | } | ||
| 414 | else | ||
| 415 | { | ||
| 416 | /* errors during decoding detected */ | ||
| 417 | ✗ | ret = GATE_RESULT_FAILED; | |
| 418 | } | ||
| 419 | ✗ | break; | |
| 420 | } | ||
| 421 | |||
| 422 | /* we have something: */ | ||
| 423 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 2 times.
|
2 | if (NULL == gate_strbuilder_to_string(&builder, output)) |
| 424 | { | ||
| 425 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 426 | ✗ | break; | |
| 427 | } | ||
| 428 | |||
| 429 | /* we have already an OK result ;) */ | ||
| 430 | } while (0); | ||
| 431 | |||
| 432 | |||
| 433 |
1/2✓ Branch 0 taken 2 times.
✗ Branch 1 not taken.
|
2 | if (qr) |
| 434 | { | ||
| 435 | 2 | quirc_destroy(qr); | |
| 436 | } | ||
| 437 | |||
| 438 | 2 | gate_strbuilder_release(&builder); | |
| 439 | |||
| 440 | 2 | return ret; | |
| 441 | } | ||
| 442 | |||
| 443 | #endif /* GATE_EXTLIB_QUIRC */ | ||
| 444 | |||
| 445 | |||
| 446 | #if !defined(GATE_EXTLIB_QRCODE) && !defined(GATE_EXTLIB_QRCODEGENC) | ||
| 447 | |||
| 448 | gate_result_t gate_tech_qrcode_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | ||
| 449 | { | ||
| 450 | return GATE_RESULT_NOTSUPPORTED; | ||
| 451 | } | ||
| 452 | |||
| 453 | #elif defined(GATE_EXTLIB_QRCODEGENC) | ||
| 454 | |||
| 455 | #include <qrcodegenc/qrcodegen.h> | ||
| 456 | |||
| 457 | 1 | gate_result_t gate_tech_qrcode_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | |
| 458 | { | ||
| 459 | static const unsigned border = 2; | ||
| 460 | static const gate_color_t white_pixel = GATE_COLOR_WHITE; | ||
| 461 | static const gate_color_t black_pixel = GATE_COLOR_BLACK; | ||
| 462 | uint8_t buffer[qrcodegen_BUFFER_LEN_MAX]; | ||
| 463 | uint8_t qrcode[qrcodegen_BUFFER_LEN_MAX]; | ||
| 464 | 1 | gate_size_t buffer_used = gate_string_to_buffer(input, (gate_char8_t*)&buffer[0], sizeof(buffer)); | |
| 465 | 1 | size_t qrcode_len = qrcodegen_calcSegmentBufferSize(qrcodegen_Mode_BYTE, buffer_used); | |
| 466 | int qrcode_size, x, y; | ||
| 467 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (qrcode_len > sizeof(qrcode)) |
| 468 | { | ||
| 469 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 470 | } | ||
| 471 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (!qrcodegen_encodeBinary( |
| 472 | buffer, buffer_used, | ||
| 473 | qrcode, qrcodegen_Ecc_MEDIUM, | ||
| 474 | qrcodegen_VERSION_MIN, qrcodegen_VERSION_MAX, | ||
| 475 | qrcodegen_Mask_AUTO, true)) | ||
| 476 | { | ||
| 477 | ✗ | return GATE_RESULT_FAILED; | |
| 478 | } | ||
| 479 | 1 | qrcode_size = qrcodegen_getSize(qrcode); | |
| 480 | |||
| 481 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_rasterimage_create(output, GATE_IMAGE_PIXELFORMAT_GRAY8, qrcode_size + border * 2, qrcode_size + border * 2, NULL)) |
| 482 | { | ||
| 483 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 484 | } | ||
| 485 | |||
| 486 | 1 | gate_rasterimage_clear(output, &white_pixel); | |
| 487 |
2/2✓ Branch 0 taken 29 times.
✓ Branch 1 taken 1 times.
|
30 | for (y = 0; y < qrcode_size; ++y) |
| 488 | { | ||
| 489 |
2/2✓ Branch 0 taken 841 times.
✓ Branch 1 taken 29 times.
|
870 | for (x = 0; x < qrcode_size; ++x) |
| 490 | { | ||
| 491 |
2/2✓ Branch 1 taken 432 times.
✓ Branch 2 taken 409 times.
|
841 | if (qrcodegen_getModule(qrcode, x, y)) |
| 492 | { | ||
| 493 | 432 | gate_rasterimage_set_pixel(output, 2u + x , 2u + y, &black_pixel); | |
| 494 | } | ||
| 495 | } | ||
| 496 | } | ||
| 497 | 1 | return GATE_RESULT_OK; | |
| 498 | } | ||
| 499 | |||
| 500 | #else | ||
| 501 | |||
| 502 | #include <qrcode.h> | ||
| 503 | |||
| 504 | static gate_uint16_t qrcode_version_max_bin_length[] = { | ||
| 505 | 0, 7, 14, 24, 34, 44, 58, 64, 84, 98, 119, | ||
| 506 | 137, 155, 177, 194, 220, 250, 280, 310, 338, 382, | ||
| 507 | 403, 439, 461, 511, 535, 593, 625, 658, 698, 742, | ||
| 508 | 790, 842, 898, 958, 983, 1051, 1093, 1139, 1219, 1273 | ||
| 509 | }; | ||
| 510 | static gate_size_t qrcode_version_max_bin_length_count = sizeof(qrcode_version_max_bin_length) / sizeof(qrcode_version_max_bin_length[0]); | ||
| 511 | |||
| 512 | static gate_uint8_t get_version_by_length(gate_size_t len) | ||
| 513 | { | ||
| 514 | gate_uint8_t v; | ||
| 515 | for (v = 1; v < qrcode_version_max_bin_length_count; ++v) | ||
| 516 | { | ||
| 517 | const gate_uint16_t max_len_of_version = qrcode_version_max_bin_length[v]; | ||
| 518 | if (len <= max_len_of_version) | ||
| 519 | { | ||
| 520 | return v; | ||
| 521 | } | ||
| 522 | } | ||
| 523 | return (gate_uint8_t)qrcode_version_max_bin_length_count - 1; | ||
| 524 | } | ||
| 525 | |||
| 526 | gate_result_t gate_tech_qrcode_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | ||
| 527 | { | ||
| 528 | gate_result_t ret = GATE_RESULT_FAILED; | ||
| 529 | gate_blob_t buffer = GATE_INIT_EMPTY; | ||
| 530 | |||
| 531 | GATE_UNUSED_ARG(flags); | ||
| 532 | |||
| 533 | do | ||
| 534 | { | ||
| 535 | char const* input_data = gate_string_ptr(input, 0); | ||
| 536 | gate_size_t input_len = gate_string_length(input); | ||
| 537 | gate_uint8_t version = get_version_by_length(input_len); | ||
| 538 | QRCode qrcode; | ||
| 539 | gate_size_t required_size = qrcode_getBufferSize(version); | ||
| 540 | void* ptr_buffer = NULL; | ||
| 541 | int status; | ||
| 542 | static const gate_color_t black = GATE_COLOR_BLACK; | ||
| 543 | static const gate_color_t white = GATE_COLOR_WHITE; | ||
| 544 | |||
| 545 | if (NULL == gate_blob_create(&buffer, NULL, required_size)) | ||
| 546 | { | ||
| 547 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 548 | break; | ||
| 549 | } | ||
| 550 | ptr_buffer = gate_blob_databuffer(&buffer); | ||
| 551 | |||
| 552 | status = qrcode_initBytes(&qrcode, (unsigned char*)ptr_buffer, version, ECC_HIGH, | ||
| 553 | (unsigned char const*)input_data, (unsigned int)input_len); | ||
| 554 | if (status < 0) | ||
| 555 | { | ||
| 556 | ret = GATE_RESULT_FAILED; | ||
| 557 | break; | ||
| 558 | } | ||
| 559 | |||
| 560 | if (output) | ||
| 561 | { | ||
| 562 | unsigned border = 4; | ||
| 563 | unsigned image_width = border * 2 + qrcode.size; | ||
| 564 | unsigned y; | ||
| 565 | |||
| 566 | if (NULL == gate_rasterimage_create(output, GATE_IMAGE_PIXELFORMAT_GRAY8, image_width, image_width, NULL)) | ||
| 567 | { | ||
| 568 | ret = GATE_RESULT_OUTOFMEMORY; | ||
| 569 | break; | ||
| 570 | } | ||
| 571 | gate_rasterimage_clear(output, &white); | ||
| 572 | |||
| 573 | for (y = 0; y < qrcode.size; ++y) | ||
| 574 | { | ||
| 575 | unsigned x; | ||
| 576 | for (x = 0; x < qrcode.size; ++x) | ||
| 577 | { | ||
| 578 | gate_color_t const* ptr_col = qrcode_getModule(&qrcode, (gate_uint8_t)x, (gate_uint8_t)y) ? &black : &white; | ||
| 579 | gate_rasterimage_set_pixel(output, border + x, border + y, ptr_col); | ||
| 580 | } | ||
| 581 | } | ||
| 582 | } | ||
| 583 | ret = GATE_RESULT_OK; | ||
| 584 | } while (0); | ||
| 585 | |||
| 586 | gate_blob_release(&buffer); | ||
| 587 | |||
| 588 | return ret; | ||
| 589 | } | ||
| 590 | |||
| 591 | #endif /* GATE_EXTLIB_QRCODE */ | ||
| 592 | |||
| 593 | |||
| 594 | |||
| 595 | |||
| 596 | |||
| 597 | #ifndef GATE_TECH_BARCODE_MAX_STRIPES | ||
| 598 | #define GATE_TECH_BARCODE_MAX_STRIPES 1024 | ||
| 599 | #endif | ||
| 600 | |||
| 601 | |||
| 602 | /* first number encoding table for left side */ | ||
| 603 | static gate_uint8_t const EAN13_CODETABLE_A[] = { | ||
| 604 | 0x0d, /* 0b00001101, */ /* 0 */ | ||
| 605 | 0x19, /* 0b00011001, */ /* 1 */ | ||
| 606 | 0x13, /* 0b00010011, */ /* 2 */ | ||
| 607 | 0x3d, /* 0b00111101, */ /* 3 */ | ||
| 608 | 0x23, /* 0b00100011, */ /* 4 */ | ||
| 609 | 0x31, /* 0b00110001, */ /* 5 */ | ||
| 610 | 0x2f, /* 0b00101111, */ /* 6 */ | ||
| 611 | 0x3b, /* 0b00111011, */ /* 7 */ | ||
| 612 | 0x37, /* 0b00110111, */ /* 8 */ | ||
| 613 | 0x0b /* 0b00001011 */ /* 9 */ | ||
| 614 | }; | ||
| 615 | |||
| 616 | /* second number encoding table for left side */ | ||
| 617 | static gate_uint8_t const EAN13_CODETABLE_B[] = { | ||
| 618 | 0x27, /* 0b00100111, */ /* 0 */ | ||
| 619 | 0x33, /* 0b00110011, */ /* 1 */ | ||
| 620 | 0x1b, /* 0b00011011, */ /* 2 */ | ||
| 621 | 0x21, /* 0b00100001, */ /* 3 */ | ||
| 622 | 0x1d, /* 0b00011101, */ /* 4 */ | ||
| 623 | 0x39, /* 0b00111001, */ /* 5 */ | ||
| 624 | 0x05, /* 0b00000101, */ /* 6 */ | ||
| 625 | 0x11, /* 0b00010001, */ /* 7 */ | ||
| 626 | 0x09, /* 0b00001001, */ /* 8 */ | ||
| 627 | 0x17 /* 0b00010111 */ /* 9 */ | ||
| 628 | }; | ||
| 629 | |||
| 630 | /* number encoding table for right side */ | ||
| 631 | static gate_uint8_t const EAN13_CODETABLE_C[] = { | ||
| 632 | 0x72, /* 0b01110010, */ /* 0 */ | ||
| 633 | 0x66, /* 0b01100110, */ /* 1 */ | ||
| 634 | 0x6c, /* 0b01101100, */ /* 2 */ | ||
| 635 | 0x42, /* 0b01000010, */ /* 3 */ | ||
| 636 | 0x5c, /* 0b01011100, */ /* 4 */ | ||
| 637 | 0x4e, /* 0b01001110, */ /* 5 */ | ||
| 638 | 0x50, /* 0b01010000, */ /* 6 */ | ||
| 639 | 0x44, /* 0b01000100, */ /* 7 */ | ||
| 640 | 0x48, /* 0b01001000, */ /* 8 */ | ||
| 641 | 0x74 /* 0b01110100 */ /* 9 */ | ||
| 642 | }; | ||
| 643 | |||
| 644 | /* left-table (A or B) usage pattern for very first digit */ | ||
| 645 | static gate_uint8_t const EAN13_PATTERNTABLE_LEFT[] = | ||
| 646 | { | ||
| 647 | 0x00, /* 0b00000000, */ /* 0 */ | ||
| 648 | 0x0b, /* 0b00001011, */ /* 1 */ | ||
| 649 | 0x0d, /* 0b00001101, */ /* 2 */ | ||
| 650 | 0x0e, /* 0b00001110, */ /* 3 */ | ||
| 651 | 0x13, /* 0b00010011, */ /* 4 */ | ||
| 652 | 0x19, /* 0b00011001, */ /* 5 */ | ||
| 653 | 0x1c, /* 0b00011100, */ /* 6 */ | ||
| 654 | 0x15, /* 0b00010101, */ /* 7 */ | ||
| 655 | 0x16, /* 0b00010110, */ /* 8 */ | ||
| 656 | 0x1a /* 0b00011010, */ /* 9 */ | ||
| 657 | }; | ||
| 658 | |||
| 659 | #define INVALID_EAN13_NUMBER 255 | ||
| 660 | |||
| 661 | 48 | static gate_uint8_t resolve_ean13_number(gate_uint8_t needle, gate_uint8_t const haystack[10]) | |
| 662 | { | ||
| 663 | gate_uint8_t n; | ||
| 664 |
2/2✓ Branch 0 taken 279 times.
✓ Branch 1 taken 9 times.
|
288 | for (n = 0; n != 10; ++n) |
| 665 | { | ||
| 666 |
2/2✓ Branch 0 taken 39 times.
✓ Branch 1 taken 240 times.
|
279 | if (needle == haystack[n]) |
| 667 | { | ||
| 668 | 39 | return n; | |
| 669 | } | ||
| 670 | } | ||
| 671 | 9 | return INVALID_EAN13_NUMBER; | |
| 672 | } | ||
| 673 | |||
| 674 | 17 | static unsigned gate_tech_barcode_scan_line_stripes(gate_rasterimage_t const* image, | |
| 675 | unsigned x, unsigned y, unsigned pixel_count, | ||
| 676 | gate_bool_t reverse_scan, | ||
| 677 | gate_uint16_t* stripes, gate_size_t stripes_count) | ||
| 678 | { | ||
| 679 | 17 | unsigned stripes_used = 0; | |
| 680 | 17 | gate_uint16_t counter = 0; | |
| 681 | 17 | gate_uint8_t bitcolor = 0; | |
| 682 | 17 | gate_uint8_t current_bitcolor_state = 255; | |
| 683 | gate_color_t pix; | ||
| 684 | unsigned ndx; | ||
| 685 | |||
| 686 |
2/2✓ Branch 0 taken 4813 times.
✓ Branch 1 taken 17 times.
|
4830 | for (ndx = 0; ndx != pixel_count; ++ndx) |
| 687 | { | ||
| 688 |
2/2✓ Branch 0 taken 2240 times.
✓ Branch 1 taken 2573 times.
|
4813 | if (reverse_scan) |
| 689 | { | ||
| 690 | /* reverse scan (right to left) */ | ||
| 691 | 2240 | gate_rasterimage_get_pixel(image, x + pixel_count - ndx - 1, y, &pix); | |
| 692 | } | ||
| 693 | else | ||
| 694 | { | ||
| 695 | /* regular scan (left to right) */ | ||
| 696 | 2573 | gate_rasterimage_get_pixel(image, x + ndx, y, &pix); | |
| 697 | } | ||
| 698 | |||
| 699 | 4813 | bitcolor = gate_color_to_monochrome(&pix, GATE_COLOR_DEFAULT_MONOCHROME_THRESHOLD); | |
| 700 |
2/2✓ Branch 0 taken 4633 times.
✓ Branch 1 taken 180 times.
|
4813 | if (current_bitcolor_state == bitcolor) |
| 701 | { | ||
| 702 | 4633 | ++counter; | |
| 703 | } | ||
| 704 | else | ||
| 705 | { | ||
| 706 | /* save length-counter of previous color and start a new counter for current color */ | ||
| 707 |
1/2✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
|
180 | if (stripes_used < stripes_count) |
| 708 | { | ||
| 709 | 180 | stripes[stripes_used++] = counter; | |
| 710 | } | ||
| 711 | 180 | counter = 1; | |
| 712 | 180 | current_bitcolor_state = bitcolor; | |
| 713 | } | ||
| 714 | } | ||
| 715 | 17 | return stripes_used; | |
| 716 | } | ||
| 717 | |||
| 718 | 3 | static gate_size_t gate_tech_barcode_scan_stripe_bits(gate_uint16_t const* stripes, gate_size_t stripes_count, | |
| 719 | gate_uint16_t min_stripe, | ||
| 720 | gate_uint8_t* bits, gate_size_t bits_capacity) | ||
| 721 | { | ||
| 722 | 3 | gate_size_t bits_used = 0; | |
| 723 | 3 | gate_real32_t const min_length = (gate_real32_t)min_stripe; | |
| 724 | |||
| 725 |
3/4✓ Branch 0 taken 180 times.
✓ Branch 1 taken 3 times.
✓ Branch 2 taken 180 times.
✗ Branch 3 not taken.
|
183 | while ((stripes_count-- != 0) && (bits_used < bits_capacity)) |
| 726 | { | ||
| 727 | 180 | gate_real32_t length = (((gate_real32_t)*stripes) / min_length) + 0.4f; | |
| 728 | 180 | unsigned int bit_length = (unsigned int)length; | |
| 729 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 180 times.
|
180 | if (bit_length > 255) |
| 730 | { | ||
| 731 | ✗ | bit_length = 255; | |
| 732 | } | ||
| 733 | 180 | bits[bits_used] = (gate_uint8_t)bit_length; | |
| 734 | 180 | ++bits_used; | |
| 735 | 180 | ++stripes; | |
| 736 | } | ||
| 737 | 3 | return bits_used; | |
| 738 | } | ||
| 739 | |||
| 740 | |||
| 741 | 3 | static gate_bool_t gate_tech_eanbarcode_try_decode(gate_uint8_t const* bits, gate_size_t bits_count, char ean13[13]) | |
| 742 | { | ||
| 743 | gate_size_t index; | ||
| 744 | 3 | gate_size_t digit_bit_index = 0; | |
| 745 | 3 | gate_uint8_t digits[256] = GATE_INIT_EMPTY; | |
| 746 | gate_uint8_t decoded_num; | ||
| 747 | 3 | gate_bool_t black = false; | |
| 748 | 3 | gate_uint8_t x = 0; | |
| 749 | |||
| 750 |
2/2✓ Branch 0 taken 168 times.
✓ Branch 1 taken 3 times.
|
171 | for (index = 0; index < bits_count; ++index) |
| 751 | { | ||
| 752 | gate_size_t bitindex; | ||
| 753 |
2/2✓ Branch 0 taken 276 times.
✓ Branch 1 taken 168 times.
|
444 | for (bitindex = 0; bitindex < bits[index]; ++bitindex) |
| 754 | { | ||
| 755 | 276 | gate_uint8_t* ptr_digit = &digits[digit_bit_index / 7]; | |
| 756 | 276 | *ptr_digit <<= 1; | |
| 757 |
2/2✓ Branch 0 taken 147 times.
✓ Branch 1 taken 129 times.
|
276 | if (black) |
| 758 | { | ||
| 759 | 147 | *ptr_digit |= 1; | |
| 760 | } | ||
| 761 | 276 | ++digit_bit_index; | |
| 762 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 273 times.
|
276 | if (digit_bit_index == 42) |
| 763 | { | ||
| 764 | /* 5 bits of separation stripes */ | ||
| 765 | 3 | digit_bit_index += 2; | |
| 766 | } | ||
| 767 | } | ||
| 768 | 168 | black = !black; | |
| 769 | } | ||
| 770 | |||
| 771 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
|
21 | for (index = 0; index < 6; ++index) |
| 772 | { | ||
| 773 | 18 | decoded_num = resolve_ean13_number(digits[index], EAN13_CODETABLE_A); | |
| 774 |
2/2✓ Branch 0 taken 9 times.
✓ Branch 1 taken 9 times.
|
18 | if (decoded_num != INVALID_EAN13_NUMBER) |
| 775 | { | ||
| 776 | 9 | x <<= 1; | |
| 777 | } | ||
| 778 | else | ||
| 779 | { | ||
| 780 | 9 | decoded_num = resolve_ean13_number(digits[index], EAN13_CODETABLE_B); | |
| 781 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 9 times.
|
9 | if (decoded_num == INVALID_EAN13_NUMBER) |
| 782 | { | ||
| 783 | ✗ | return false; | |
| 784 | } | ||
| 785 | 9 | x <<= 1; | |
| 786 | 9 | x |= 1; | |
| 787 | } | ||
| 788 | 18 | ean13[index + 1] = '0' + decoded_num; | |
| 789 | } | ||
| 790 | |||
| 791 | 3 | decoded_num = resolve_ean13_number(x, EAN13_PATTERNTABLE_LEFT); | |
| 792 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (decoded_num == INVALID_EAN13_NUMBER) |
| 793 | { | ||
| 794 | ✗ | return false; | |
| 795 | } | ||
| 796 | 3 | ean13[0] = '0' + decoded_num; | |
| 797 | |||
| 798 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (digits[6] != 0x0a /* 0b00001010 */) |
| 799 | { | ||
| 800 | /* separation stripes */ | ||
| 801 | ✗ | return false; | |
| 802 | } | ||
| 803 | |||
| 804 |
2/2✓ Branch 0 taken 18 times.
✓ Branch 1 taken 3 times.
|
21 | for (index = 7; index < 13; ++index) |
| 805 | { | ||
| 806 | 18 | decoded_num = resolve_ean13_number(digits[index], EAN13_CODETABLE_C); | |
| 807 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 18 times.
|
18 | if (decoded_num == INVALID_EAN13_NUMBER) |
| 808 | { | ||
| 809 | ✗ | return false; | |
| 810 | } | ||
| 811 | 18 | ean13[index] = '0' + decoded_num; | |
| 812 | } | ||
| 813 | 3 | return true; | |
| 814 | } | ||
| 815 | |||
| 816 | 3 | static gate_bool_t gate_tech_eanbarcode_analyze_bits(gate_uint8_t const* bits, gate_size_t bits_count, char ean13[13]) | |
| 817 | { | ||
| 818 | gate_size_t index; | ||
| 819 | |||
| 820 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (bits_count < 20) |
| 821 | { | ||
| 822 | ✗ | return false; | |
| 823 | } | ||
| 824 | /* first entry in bits[] array is WHITE */ | ||
| 825 | /* EAN13 starts with bits 00000001010 */ | ||
| 826 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | for (index = 1; index < bits_count - 3; index += 2) |
| 827 | { | ||
| 828 |
3/6✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 3 times.
✗ Branch 5 not taken.
|
3 | if ((bits[index] == 1) && (bits[index + 1] == 1) && (bits[index + 2] == 1) |
| 829 |
1/2✓ Branch 0 taken 3 times.
✗ Branch 1 not taken.
|
3 | && (bits[index - 1] > 7)) |
| 830 | { | ||
| 831 | 3 | gate_size_t const start_index = index + 3; | |
| 832 |
1/2✓ Branch 1 taken 3 times.
✗ Branch 2 not taken.
|
3 | if (gate_tech_eanbarcode_try_decode(&bits[start_index], bits_count - start_index, ean13)) |
| 833 | { | ||
| 834 | 3 | return true; | |
| 835 | } | ||
| 836 | } | ||
| 837 | } | ||
| 838 | /* no ean13 barcode detected */ | ||
| 839 | ✗ | return false; | |
| 840 | } | ||
| 841 | |||
| 842 | 17 | static gate_bool_t gate_tech_eanbarcode_scan_line(gate_rasterimage_t const* image, unsigned x, unsigned y, unsigned pixel_count, | |
| 843 | gate_bool_t reverse_scan, char ean13[13]) | ||
| 844 | { | ||
| 845 | 17 | gate_uint16_t stripes[GATE_TECH_BARCODE_MAX_STRIPES] = GATE_INIT_EMPTY; | |
| 846 | 17 | gate_uint8_t bits[GATE_TECH_BARCODE_MAX_STRIPES] = GATE_INIT_EMPTY; | |
| 847 | 17 | gate_size_t stripes_used = 0; | |
| 848 | 17 | gate_size_t bits_used = 0; | |
| 849 | 17 | gate_uint16_t min_bit_length = 0; | |
| 850 | 17 | gate_bool_t ean_decoded = false; | |
| 851 | |||
| 852 | 17 | stripes_used = gate_tech_barcode_scan_line_stripes(image, x, y, pixel_count, reverse_scan, stripes, sizeof(stripes) / sizeof(stripes[0])); | |
| 853 | |||
| 854 |
4/4✓ Branch 0 taken 6 times.
✓ Branch 1 taken 14 times.
✓ Branch 2 taken 3 times.
✓ Branch 3 taken 3 times.
|
20 | while ((stripes_used > 20) && !ean_decoded) |
| 855 | { | ||
| 856 | gate_size_t index; | ||
| 857 | 3 | gate_uint16_t next_min_bit_length = 0xffff; | |
| 858 | |||
| 859 |
2/2✓ Branch 0 taken 180 times.
✓ Branch 1 taken 3 times.
|
183 | for (index = 0; index != stripes_used; ++index) |
| 860 | { | ||
| 861 |
3/4✓ Branch 0 taken 180 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✓ Branch 3 taken 174 times.
|
180 | if ((stripes[index] > min_bit_length) && (stripes[index] < next_min_bit_length)) |
| 862 | { | ||
| 863 | 6 | next_min_bit_length = stripes[index]; | |
| 864 | } | ||
| 865 | } | ||
| 866 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 3 times.
|
3 | if (min_bit_length == next_min_bit_length) |
| 867 | { | ||
| 868 | ✗ | break; | |
| 869 | } | ||
| 870 | 3 | min_bit_length = next_min_bit_length; | |
| 871 | |||
| 872 | 3 | bits_used = gate_tech_barcode_scan_stripe_bits(stripes, stripes_used, min_bit_length, bits, sizeof(bits)); | |
| 873 | |||
| 874 | 3 | ean_decoded = gate_tech_eanbarcode_analyze_bits(bits, bits_used, ean13); | |
| 875 | } | ||
| 876 | 17 | return ean_decoded; | |
| 877 | } | ||
| 878 | |||
| 879 | 9 | static gate_bool_t gate_tech_eanbarcode_scan_image(gate_rasterimage_t const* input, unsigned width, unsigned y, char ean13[13]) | |
| 880 | { | ||
| 881 | 9 | gate_bool_t ean_decoded = gate_tech_eanbarcode_scan_line(input, 0, y, width, false, ean13); | |
| 882 |
2/2✓ Branch 0 taken 7 times.
✓ Branch 1 taken 2 times.
|
9 | if (!ean_decoded) |
| 883 | { | ||
| 884 | 7 | ean_decoded = gate_tech_eanbarcode_scan_line(input, 0, y, width, true, ean13); | |
| 885 | } | ||
| 886 | 9 | return ean_decoded; | |
| 887 | } | ||
| 888 | |||
| 889 | |||
| 890 | 4 | gate_result_t gate_tech_eanbarcode_scan(gate_rasterimage_t const* input, gate_enumint_t flags, gate_string_t* output) | |
| 891 | { | ||
| 892 | 4 | gate_result_t ret = GATE_RESULT_FAILED; | |
| 893 | |||
| 894 | do | ||
| 895 | { | ||
| 896 | 4 | unsigned width = gate_rasterimage_width(input); | |
| 897 | 4 | unsigned height = gate_rasterimage_height(input); | |
| 898 | unsigned y; | ||
| 899 | 4 | gate_bool_t ean_decoded = false; | |
| 900 | 4 | char ean13[64] = GATE_INIT_EMPTY; | |
| 901 | |||
| 902 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (GATE_FLAG_ENABLED(flags, GATE_TECH_BARCODE_SCANNER_FLAG_INTENSIVE)) |
| 903 | { | ||
| 904 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | for (y = 0; y < height; y += 4) |
| 905 | { | ||
| 906 | 1 | ean_decoded = gate_tech_eanbarcode_scan_image(input, width, y, ean13); | |
| 907 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | if (ean_decoded) |
| 908 | { | ||
| 909 | 1 | break; | |
| 910 | } | ||
| 911 | } | ||
| 912 | } | ||
| 913 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 2 times.
|
3 | else if (GATE_FLAG_ENABLED(flags, GATE_TECH_BARCODE_SCANNER_FLAG_QUICK)) |
| 914 | { | ||
| 915 | /* quick scan */ | ||
| 916 | 1 | ean_decoded = gate_tech_eanbarcode_scan_line(input, 0, height / 2, width, false, ean13); | |
| 917 | } | ||
| 918 | else | ||
| 919 | { | ||
| 920 | /* default scan */ | ||
| 921 |
2/2✓ Branch 0 taken 8 times.
✓ Branch 1 taken 1 times.
|
9 | for (y = 1; y != 8; ++y) |
| 922 | { | ||
| 923 | 8 | ean_decoded = gate_tech_eanbarcode_scan_image(input, width, height * y / 8, ean13); | |
| 924 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 7 times.
|
8 | if (ean_decoded) |
| 925 | { | ||
| 926 | 1 | break; | |
| 927 | } | ||
| 928 | } | ||
| 929 | } | ||
| 930 | |||
| 931 |
2/2✓ Branch 0 taken 1 times.
✓ Branch 1 taken 3 times.
|
4 | if (!ean_decoded) |
| 932 | { | ||
| 933 | 1 | ret = GATE_RESULT_NOMATCH; | |
| 934 | 1 | break; | |
| 935 | } | ||
| 936 | |||
| 937 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 3 times.
|
3 | if (NULL == gate_string_create(output, ean13, 13)) |
| 938 | { | ||
| 939 | ✗ | ret = GATE_RESULT_OUTOFMEMORY; | |
| 940 | ✗ | break; | |
| 941 | } | ||
| 942 | 3 | ret = GATE_RESULT_OK; | |
| 943 | } while (0); | ||
| 944 | |||
| 945 | 4 | return ret; | |
| 946 | } | ||
| 947 | |||
| 948 | #define EAN13_BITS_COUNT (3 + 6 * 7 + 5 + 6 * 7 + 3) | ||
| 949 | |||
| 950 | #define ADD_BIT(ptr_bits, bits_len, value) \ | ||
| 951 | do { \ | ||
| 952 | if(bits_len > 0) { \ | ||
| 953 | *ptr_bits = value; \ | ||
| 954 | ++ptr_bits; \ | ||
| 955 | --bits_len; \ | ||
| 956 | } \ | ||
| 957 | } while(0) | ||
| 958 | |||
| 959 | #define ADD_BITS_OF_PATTERN(ptr_bits, bits_len, pattern_byte) \ | ||
| 960 | do { \ | ||
| 961 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x40) ? true : false); \ | ||
| 962 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x20) ? true : false); \ | ||
| 963 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x10) ? true : false); \ | ||
| 964 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x08) ? true : false); \ | ||
| 965 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x04) ? true : false); \ | ||
| 966 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x02) ? true : false); \ | ||
| 967 | ADD_BIT(ptr_bits, bits_len, (pattern_byte & 0x01) ? true : false); \ | ||
| 968 | } while(0) | ||
| 969 | |||
| 970 | |||
| 971 | |||
| 972 | 1 | static void generate_ean_bits(gate_uint8_t const digits[13], gate_bool_t* bits, gate_size_t bits_len) | |
| 973 | { | ||
| 974 | 1 | const gate_uint8_t left_pattern = EAN13_PATTERNTABLE_LEFT[digits[0]]; | |
| 975 | unsigned ndx; | ||
| 976 | |||
| 977 | /* intro: */ | ||
| 978 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 979 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, false); |
| 980 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 981 | |||
| 982 | /* left-side: */ | ||
| 983 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ndx = 0; ndx != 6; ++ndx) |
| 984 | { | ||
| 985 |
2/2✓ Branch 0 taken 3 times.
✓ Branch 1 taken 3 times.
|
6 | gate_uint8_t const* pattern_table = (left_pattern & (1 << (5 - ndx))) == 0 ? EAN13_CODETABLE_A : EAN13_CODETABLE_B; |
| 986 |
7/14✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✗ Branch 13 not taken.
|
6 | ADD_BITS_OF_PATTERN(bits, bits_len, pattern_table[digits[ndx + 1]]); |
| 987 | } | ||
| 988 | |||
| 989 | /* separators: */ | ||
| 990 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, false); |
| 991 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 992 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, false); |
| 993 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 994 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, false); |
| 995 | |||
| 996 | /* right-side: */ | ||
| 997 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ndx = 0; ndx != 6; ++ndx) |
| 998 | { | ||
| 999 |
7/14✓ Branch 0 taken 6 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 6 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 6 times.
✗ Branch 5 not taken.
✓ Branch 6 taken 6 times.
✗ Branch 7 not taken.
✓ Branch 8 taken 6 times.
✗ Branch 9 not taken.
✓ Branch 10 taken 6 times.
✗ Branch 11 not taken.
✓ Branch 12 taken 6 times.
✗ Branch 13 not taken.
|
6 | ADD_BITS_OF_PATTERN(bits, bits_len, EAN13_CODETABLE_C[digits[ndx + 7]]); |
| 1000 | } | ||
| 1001 | |||
| 1002 | /* outro: */ | ||
| 1003 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 1004 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, false); |
| 1005 |
1/2✓ Branch 0 taken 1 times.
✗ Branch 1 not taken.
|
1 | ADD_BIT(bits, bits_len, true); |
| 1006 | |||
| 1007 | 1 | } | |
| 1008 | |||
| 1009 | 1 | gate_result_t gate_tech_eanbarcode_print(gate_string_t const* input, gate_enumint_t flags, gate_rasterimage_t* output) | |
| 1010 | { | ||
| 1011 | static gate_color_t const white = GATE_COLOR_WHITE; | ||
| 1012 | static gate_color_t const black = GATE_COLOR_BLACK; | ||
| 1013 | static const unsigned separator_gaps[6] = { 0, 2, 6 * 7 + 2, 2, 6 * 7 + 2, 2 }; | ||
| 1014 | 1 | gate_uint8_t payload[13] = GATE_INIT_EMPTY; | |
| 1015 | 1 | char const* const txt = gate_string_ptr(input, 0); | |
| 1016 | 1 | gate_size_t len = gate_string_length(input); | |
| 1017 | unsigned ndx; | ||
| 1018 | gate_bool_t bits[EAN13_BITS_COUNT]; | ||
| 1019 | 1 | unsigned border = 8; | |
| 1020 | 1 | const unsigned barcode_height = 64; | |
| 1021 | 1 | const unsigned text_height = 10; | |
| 1022 | 1 | unsigned height = barcode_height; | |
| 1023 | unsigned x; | ||
| 1024 | |||
| 1025 | GATE_UNUSED_ARG(flags); | ||
| 1026 | |||
| 1027 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 1 times.
|
1 | if (len > 13) len = 13; |
| 1028 | |||
| 1029 |
2/2✓ Branch 0 taken 13 times.
✓ Branch 1 taken 1 times.
|
14 | for (ndx = 0; ndx != len; ++ndx) |
| 1030 | { | ||
| 1031 | 13 | gate_str_parse_digit(&txt[ndx], &payload[ndx]); | |
| 1032 | } | ||
| 1033 | |||
| 1034 | 1 | generate_ean_bits(payload, bits, sizeof(bits) / sizeof(bits[0])); | |
| 1035 | |||
| 1036 |
1/2✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
|
1 | if (NULL == gate_rasterimage_create(output, GATE_IMAGE_PIXELFORMAT_GRAY8, EAN13_BITS_COUNT + border * 2, height + text_height, NULL)) |
| 1037 | { | ||
| 1038 | ✗ | return GATE_RESULT_OUTOFMEMORY; | |
| 1039 | } | ||
| 1040 | |||
| 1041 | 1 | gate_rasterimage_clear(output, &white); | |
| 1042 | |||
| 1043 |
2/2✓ Branch 0 taken 64 times.
✓ Branch 1 taken 1 times.
|
65 | while (height-- > 0) |
| 1044 | { | ||
| 1045 |
2/2✓ Branch 0 taken 6080 times.
✓ Branch 1 taken 64 times.
|
6144 | for (ndx = 0; ndx != EAN13_BITS_COUNT; ++ndx) |
| 1046 | { | ||
| 1047 |
2/2✓ Branch 0 taken 3264 times.
✓ Branch 1 taken 2816 times.
|
6080 | if (bits[ndx]) |
| 1048 | { | ||
| 1049 | 3264 | gate_rasterimage_set_pixel(output, ndx + border, height, &black); | |
| 1050 | } | ||
| 1051 | } | ||
| 1052 | } | ||
| 1053 | |||
| 1054 | 1 | gate_font8_print_char(output, '0' + payload[0], 2, barcode_height + 1, black); | |
| 1055 | |||
| 1056 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ndx = 0; ndx != 6; ++ndx) |
| 1057 | { | ||
| 1058 | 6 | gate_font8_print_char(output, '0' + payload[ndx + 1], border + 3 + ndx * 7 + 1, barcode_height + 1, black); | |
| 1059 | } | ||
| 1060 | |||
| 1061 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ndx = 0; ndx != 6; ++ndx) |
| 1062 | { | ||
| 1063 | 6 | gate_font8_print_char(output, '0' + payload[ndx + 7], border + 3 + 6 * 7 + 5 + ndx * 7 + 1, barcode_height + 1, black); | |
| 1064 | } | ||
| 1065 | |||
| 1066 | 1 | x = border; | |
| 1067 |
2/2✓ Branch 0 taken 6 times.
✓ Branch 1 taken 1 times.
|
7 | for (ndx = 0; ndx != sizeof(separator_gaps) / sizeof(separator_gaps[0]); ++ndx) |
| 1068 | { | ||
| 1069 | 6 | x += separator_gaps[ndx]; | |
| 1070 | 6 | gate_draw_line(output, x, barcode_height, x, barcode_height + 3, black); | |
| 1071 | } | ||
| 1072 | |||
| 1073 | 1 | return GATE_RESULT_OK; | |
| 1074 | } | ||
| 1075 |