Speed up clearing trailing jpeg data

This commit is contained in:
Kwabena W. Agyeman 2021-02-13 16:32:51 -08:00
parent c04b519e38
commit b0930505d5
3 changed files with 17 additions and 15 deletions

View File

@ -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);

View File

@ -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)

View File

@ -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;