Skip to content

Commit 5168f6e

Browse files
committed
Add support for RGB565 images in qrio
Most cameras produce RGB565_SWAPPED data
1 parent 86f9d98 commit 5168f6e

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

shared-bindings/qrio/PixelPolicy.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,23 @@
3939
//| ODD_BYTES: PixelPolicy
4040
//| """The input buffer to `QRDecoder.decode` consists of greyscale values in positions 1, 3, …, and ignored bytes in positions 0, 2, …. This can decode directly from YUV images where the odd bytes hold the Y (luminance) data"""
4141
//|
42+
//| RGB565_SWAPPED: PixelPolicy
43+
//| """The input buffer to `QRDecoder.decode` consists of RGB565 values in byte-swapped order. The green component is used."""
44+
//|
45+
//| RGB565: PixelPolicy
46+
//| """The input buffer to `QRDecoder.decode` consists of RGB565 values. The green component is used."""
47+
//|
4248

4349
MAKE_ENUM_VALUE(qrio_pixel_policy_type, qrio_pixel_policy, EVERY_BYTE, QRIO_EVERY_BYTE);
50+
MAKE_ENUM_VALUE(qrio_pixel_policy_type, qrio_pixel_policy, RGB565, QRIO_RGB565);
51+
MAKE_ENUM_VALUE(qrio_pixel_policy_type, qrio_pixel_policy, RGB565_SWAPPED, QRIO_RGB565_SWAPPED);
4452
MAKE_ENUM_VALUE(qrio_pixel_policy_type, qrio_pixel_policy, EVEN_BYTES, QRIO_EVEN_BYTES);
4553
MAKE_ENUM_VALUE(qrio_pixel_policy_type, qrio_pixel_policy, ODD_BYTES, QRIO_EVEN_BYTES);
4654

4755
MAKE_ENUM_MAP(qrio_pixel_policy) {
4856
MAKE_ENUM_MAP_ENTRY(qrio_pixel_policy, EVERY_BYTE),
57+
MAKE_ENUM_MAP_ENTRY(qrio_pixel_policy, RGB565),
58+
MAKE_ENUM_MAP_ENTRY(qrio_pixel_policy, RGB565_SWAPPED),
4959
MAKE_ENUM_MAP_ENTRY(qrio_pixel_policy, EVEN_BYTES),
5060
MAKE_ENUM_MAP_ENTRY(qrio_pixel_policy, ODD_BYTES),
5161
};

shared-bindings/qrio/PixelPolicy.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
extern const mp_obj_type_t qrio_pixel_policy_type;
3434

3535
typedef enum {
36-
QRIO_EVERY_BYTE, QRIO_EVEN_BYTES, QRIO_ODD_BYTES
36+
QRIO_EVERY_BYTE, QRIO_EVEN_BYTES, QRIO_ODD_BYTES, QRIO_RGB565, QRIO_RGB565_SWAPPED
3737
} qrio_pixel_policy_t;
3838

3939
extern const cp_enum_obj_t qrio_pixel_policy_EVERY_BYTE_obj;

shared-module/qrio/QRDecoder.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,20 @@ mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, co
104104
uint8_t *src = bufinfo->buf;
105105

106106
switch (policy) {
107+
case QRIO_RGB565: {
108+
uint16_t *src16 = bufinfo->buf;
109+
for (int i = 0; i < width * height; i++) {
110+
framebuffer[i] = (src16[i] >> 3) & 0xfc;
111+
}
112+
break;
113+
}
114+
case QRIO_RGB565_SWAPPED: {
115+
uint16_t *src16 = bufinfo->buf;
116+
for (int i = 0; i < width * height; i++) {
117+
framebuffer[i] = (__builtin_bswap16(src16[i]) >> 3) & 0xfc;
118+
}
119+
break;
120+
}
107121
case QRIO_EVERY_BYTE:
108122
memcpy(framebuffer, src, width * height);
109123
break;
@@ -116,6 +130,7 @@ mp_obj_t shared_module_qrio_qrdecoder_decode(qrdecoder_qrdecoder_obj_t *self, co
116130
for (int i = 0; i < width * height; i++) {
117131
framebuffer[i] = src[2 * i];
118132
}
133+
break;
119134
}
120135
quirc_end(self->quirc);
121136

0 commit comments

Comments
 (0)