Skip to content

Commit 8f4b3c5

Browse files
quirc_decode.c: attempt zero-copy but crashes and black artifacts
1 parent 97a4a92 commit 8f4b3c5

File tree

1 file changed

+27
-8
lines changed

1 file changed

+27
-8
lines changed

c_mpos/src/quirc_decode.c

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ size_t uxTaskGetStackHighWaterMark(void * unused) {
1717
#endif
1818

1919
#include "../quirc/lib/quirc.h"
20+
#include "../quirc/lib/quirc_internal.h" // Exposes full struct quirc
2021

2122
#define QRDECODE_DEBUG_PRINT(...) mp_printf(&mp_plat_print, __VA_ARGS__)
2223

@@ -46,23 +47,39 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
4647
if (!qr) {
4748
mp_raise_OSError(MP_ENOMEM);
4849
}
49-
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc object\n");
50+
//QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc object\n");
5051

5152
if (quirc_resize(qr, width, height) < 0) {
5253
quirc_destroy(qr);
5354
mp_raise_OSError(MP_ENOMEM);
5455
}
55-
QRDECODE_DEBUG_PRINT("qrdecode: Resized quirc object\n");
56+
//QRDECODE_DEBUG_PRINT("qrdecode: Resized quirc object\n");
5657

57-
uint8_t *image;
58-
image = quirc_begin(qr, NULL, NULL);
59-
memcpy(image, bufinfo.buf, width * height);
58+
uint8_t *image = quirc_begin(qr, NULL, NULL);
59+
//memcpy(image, bufinfo.buf, width * height);
60+
uint8_t *temp_image = image;
61+
//image = bufinfo.buf; // use existing buffer, rather than memcpy - but this doesnt find any images anymore :-/
62+
qr->image = bufinfo.buf; // if this works then we can also eliminate quirc's ps_alloc()
6063
quirc_end(qr);
64+
qr->image = temp_image; // restore, because quirc will try to free it
65+
66+
/*
67+
// Pointer swap - NO memcpy, NO internal.h needed
68+
uint8_t *quirc_buffer = quirc_begin(qr, NULL, NULL);
69+
uint8_t *saved_bufinfo = bufinfo.buf;
70+
bufinfo.buf = quirc_buffer; // quirc now uses your buffer
71+
quirc_end(qr); // QR detection works!
72+
// Restore your buffer pointer
73+
//bufinfo.buf = saved_bufinfo;
74+
*/
75+
76+
// now num_grids is set, as well as others, probably
6177

6278
int count = quirc_count(qr);
6379
if (count == 0) {
80+
// Restore your buffer pointer
6481
quirc_destroy(qr);
65-
QRDECODE_DEBUG_PRINT("qrdecode: No QR code found, freed quirc object\n");
82+
//QRDECODE_DEBUG_PRINT("qrdecode: No QR code found, freed quirc object\n");
6683
mp_raise_ValueError(MP_ERROR_TEXT("no QR code found"));
6784
}
6885

@@ -71,16 +88,18 @@ static mp_obj_t qrdecode(mp_uint_t n_args, const mp_obj_t *args) {
7188
quirc_destroy(qr);
7289
mp_raise_OSError(MP_ENOMEM);
7390
}
74-
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code\n");
91+
//QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_code\n");
7592
quirc_extract(qr, 0, code);
93+
// the code struct now contains the corners of the QR code, as well as the bitmap of the values
94+
// this could be used to display debug info to the user - they might even be able to see which modules are being misidentified!
7695

7796
struct quirc_data *data = (struct quirc_data *)malloc(sizeof(struct quirc_data));
7897
if (!data) {
7998
free(code);
8099
quirc_destroy(qr);
81100
mp_raise_OSError(MP_ENOMEM);
82101
}
83-
QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_data\n");
102+
//QRDECODE_DEBUG_PRINT("qrdecode: Allocated quirc_data\n");
84103

85104
int err = quirc_decode(code, data);
86105
if (err != QUIRC_SUCCESS) {

0 commit comments

Comments
 (0)