From b0930505d5851e9ec6fdca518f9b6a2bb52c8475 Mon Sep 17 00:00:00 2001 From: "Kwabena W. Agyeman" Date: Sat, 13 Feb 2021 16:32:51 -0800 Subject: [PATCH] Speed up clearing trailing jpeg data --- src/omv/imlib/imlib.h | 1 + src/omv/imlib/jpeg.c | 23 ++++++++++++++--------- src/omv/ports/stm32/sensor.c | 8 ++------ 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/omv/imlib/imlib.h b/src/omv/imlib/imlib.h index 1e49edc2d..da7f4c4f9 100644 --- a/src/omv/imlib/imlib.h +++ b/src/omv/imlib/imlib.h @@ -1068,6 +1068,7 @@ void bmp_read_pixels(FIL *fp, image_t *img, int n_lines, bmp_read_settings_t *rs void bmp_read(image_t *img, const char *path); void bmp_write_subimg(image_t *img, const char *path, rectangle_t *r); bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc); +int jpeg_clean_trailing_bytes(int bpp, uint8_t *data); void jpeg_read_geometry(FIL *fp, image_t *img, const char *path, jpg_read_settings_t *rs); void jpeg_read_pixels(FIL *fp, image_t *img); void jpeg_read(image_t *img, const char *path); diff --git a/src/omv/imlib/jpeg.c b/src/omv/imlib/jpeg.c index d20e5c4b6..4bfe343c7 100644 --- a/src/omv/imlib/jpeg.c +++ b/src/omv/imlib/jpeg.c @@ -372,21 +372,17 @@ bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc) // Set output size dst->bpp = jpeg_enc.out_size; + if (!jpeg_enc.overflow) { + // Clean trailing data after 0xFFD9 at the end of the jpeg byte stream. + dst->bpp = jpeg_clean_trailing_bytes(dst->bpp, dst->data); + } + #if (TIME_JPEG==1) printf("time: %u ms\n", mp_hal_ticks_ms() - start); #endif HAL_JPEG_DeInit(&JPEG_Handle); - if (!jpeg_enc.overflow) { - // Clean trailing data. - while ((dst->bpp >= 2) - && ((dst->pixels[dst->bpp-2] != 0xFF) - || (dst->pixels[dst->bpp-1] != 0xD9))) { - dst->bpp -= 1; - } - } - return jpeg_enc.overflow; } @@ -1291,6 +1287,15 @@ jpeg_overflow: } #endif //defined OMV_HARDWARE_JPEG +int jpeg_clean_trailing_bytes(int bpp, uint8_t *data) +{ + while ((bpp > 1) && ((data[bpp-2] != 0xFF) || (data[bpp-1] != 0xD9))) { + bpp -= 1; + } + + return bpp; +} + #if defined(IMLIB_ENABLE_IMAGE_FILE_IO) // This function inits the geometry values of an image. void jpeg_read_geometry(FIL *fp, image_t *img, const char *path, jpg_read_settings_t *rs) diff --git a/src/omv/ports/stm32/sensor.c b/src/omv/ports/stm32/sensor.c index 0deec53af..1fdbcb232 100644 --- a/src/omv/ports/stm32/sensor.c +++ b/src/omv/ports/stm32/sensor.c @@ -1533,12 +1533,8 @@ int sensor_snapshot(sensor_t *sensor, image_t *image, streaming_cb_t streaming_c SCB_InvalidateDCache_by_Addr((uint32_t*)MAIN_FB()->pixels, size); #endif } - // Clean trailing data. - while ((MAIN_FB()->bpp >= 2) - && ((MAIN_FB()->pixels[MAIN_FB()->bpp-2] != 0xFF) - || (MAIN_FB()->pixels[MAIN_FB()->bpp-1] != 0xD9))) { - MAIN_FB()->bpp -= 1; - } + // Clean trailing data after 0xFFD9 at the end of the jpeg byte stream. + MAIN_FB()->bpp = jpeg_clean_trailing_bytes(MAIN_FB()->bpp, MAIN_FB()->pixels); break; default: break;