Skip to content

Commit eaefe65

Browse files
authored
jpeg-encoder: add configurable RGB565 byte order support (#820)
Adds `jpgSetRgb565BE(bool)` to allow selecting byte order when converting RGB565 input to RGB888 during JPEG encoding. The previous behavior (big-endian MSB-first) is preserved as the default. This improves compatibility with systems and pipelines producing little-endian RGB565 buffers.
1 parent de322e7 commit eaefe65

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

conversions/include/img_converters.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -129,6 +129,17 @@ bool fmt2rgb888(const uint8_t *src_buf, size_t src_len, pixformat_t format, uint
129129
#define JPG_SCALE_MAX JPEG_IMAGE_SCALE_1_8
130130
bool jpg2rgb565(const uint8_t *src, size_t src_len, uint8_t * out, esp_jpeg_image_scale_t scale);
131131

132+
/**
133+
* @brief Configure RGB565 input byte order for JPEG encoding.
134+
*
135+
* Controls how RGB565 source pixel data is interpreted before JPEG conversion.
136+
* By default, the encoder assumes big-endian byte order (MSB first), which
137+
* matches most ESP32 camera and frame buffer outputs.
138+
*
139+
* @param enable True to use big-endian RGB565 (default), false for little-endian.
140+
*/
141+
void jpgSetRgb565BE(bool enable);
142+
132143
#ifdef __cplusplus
133144
}
134145
#endif

conversions/to_jpg.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
static const char* TAG = "to_jpg";
3030
#endif
3131

32+
static bool rgb565_big_endian = true;
33+
3234
static void *_malloc(size_t size)
3335
{
3436
void * res = malloc(size);
@@ -60,9 +62,15 @@ static IRAM_ATTR void convert_line_format(uint8_t * src, pixformat_t format, uin
6062
l = width * 2;
6163
src += l * line;
6264
for(i=0; i<l; i+=2) {
63-
dst[o++] = src[i] & 0xF8;
64-
dst[o++] = (src[i] & 0x07) << 5 | (src[i+1] & 0xE0) >> 3;
65-
dst[o++] = (src[i+1] & 0x1F) << 3;
65+
if (rgb565_big_endian) {
66+
dst[o++] = src[i] & 0xF8;
67+
dst[o++] = (src[i] & 0x07) << 5 | (src[i+1] & 0xE0) >> 3;
68+
dst[o++] = (src[i+1] & 0x1F) << 3;
69+
} else {
70+
dst[o++] = src[i+1] & 0xF8;
71+
dst[o++] = (src[i+1] & 0x07) << 5 | (src[i] & 0xE0) >> 3;
72+
dst[o++] = (src[i] & 0x1F) << 3;
73+
}
6674
}
6775
} else if(format == PIXFORMAT_YUV422) {
6876
uint8_t y0, y1, u, v;
@@ -233,3 +241,8 @@ bool frame2jpg(camera_fb_t * fb, uint8_t quality, uint8_t ** out, size_t * out_l
233241
{
234242
return fmt2jpg(fb->buf, fb->len, fb->width, fb->height, fb->format, quality, out, out_len);
235243
}
244+
245+
void jpgSetRgb565BE(bool enable)
246+
{
247+
rgb565_big_endian = enable;
248+
}

0 commit comments

Comments
 (0)