diff --git a/src/omv/imlib/jpeg.c b/src/omv/imlib/jpeg.c index 2835d7b4d..4feaf4bda 100644 --- a/src/omv/imlib/jpeg.c +++ b/src/omv/imlib/jpeg.c @@ -318,6 +318,10 @@ bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc) mp_uint_t start = mp_hal_ticks_ms(); #endif + if (!dst->data) { + dst->data = fb_alloc_all((uint32_t *) &dst->bpp, FB_ALLOC_PREFER_SIZE | FB_ALLOC_CACHE_ALIGN); + } + uint32_t pad_w = src->w; if (pad_w % 8 != 0) { pad_w += (8 - (pad_w % 8)); @@ -969,6 +973,10 @@ bool jpeg_compress(image_t *src, image_t *dst, int quality, bool realloc) uint32_t start = mp_hal_ticks_ms(); #endif + if (!dst->data) { + dst->data = fb_alloc_all((uint32_t *) &dst->bpp, FB_ALLOC_PREFER_SIZE | FB_ALLOC_CACHE_ALIGN); + } + // JPEG buffer jpeg_buf_t jpeg_buf = { .idx =0, @@ -1378,15 +1386,13 @@ void jpeg_write(image_t *img, const char *path, int quality) if (IM_IS_JPEG(img)) { write_data(&fp, img->pixels, img->bpp); } else { - uint32_t size; - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=img->w, .h=img->h, .bpp=size, .pixels=buffer }; + image_t out = { .w=img->w, .h=img->h, .bpp=0, .pixels=NULL }; // alloc in jpeg compress // When jpeg_compress needs more memory than in currently allocated it // will try to realloc. MP will detect that the pointer is outside of // the heap and return NULL which will cause an out of memory error. jpeg_compress(img, &out, quality, false); write_data(&fp, out.pixels, out.bpp); - fb_free(); + fb_free(); // frees alloc in jpeg_compress() } file_close(&fp); } diff --git a/src/omv/imlib/mjpeg.c b/src/omv/imlib/mjpeg.c index e476b31ed..f07b59816 100644 --- a/src/omv/imlib/mjpeg.c +++ b/src/omv/imlib/mjpeg.c @@ -103,9 +103,7 @@ void mjpeg_add_frame(FIL *fp, uint32_t *frames, uint32_t *bytes, image_t *img, i write_data(fp, img->pixels, img->bpp + pad); // reading past okay *bytes += img->bpp + pad; } else { - uint32_t size; - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=img->w, .h=img->h, .bpp=size, .pixels=buffer }; + image_t out = { .w=img->w, .h=img->h, .bpp=0, .pixels=NULL }; // alloc in jpeg compress // When jpeg_compress needs more memory than in currently allocated it // will try to realloc. MP will detect that the pointer is outside of // the heap and return NULL which will cause an out of memory error. @@ -114,7 +112,7 @@ void mjpeg_add_frame(FIL *fp, uint32_t *frames, uint32_t *bytes, image_t *img, i write_long(fp, out.bpp + pad); // DWORD cb; write_data(fp, out.pixels, out.bpp + pad); // reading past okay *bytes += out.bpp + pad; - fb_free(); + fb_free(); // frees alloc in jpeg_compress() } } diff --git a/src/omv/modules/py_image.c b/src/omv/modules/py_image.c index 8358c7dc0..4c8fd607f 100644 --- a/src/omv/modules/py_image.c +++ b/src/omv/modules/py_image.c @@ -1063,10 +1063,8 @@ static mp_obj_t py_image_compress(uint n_args, const mp_obj_t *args, mp_map_t *k int arg_q = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_quality), 50); PY_ASSERT_TRUE_MSG((1 <= arg_q) && (arg_q <= 100), "Error: 1 <= quality <= 100!"); - uint32_t size; fb_alloc_mark(); - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=size, .data=buffer }; + image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=0, .data=NULL }; // alloc in jpeg compress PY_ASSERT_FALSE_MSG(jpeg_compress(arg_img, &out, arg_q, false), "Out of Memory!"); PY_ASSERT_TRUE_MSG(out.bpp <= image_size(arg_img), "Can't compress in place!"); memcpy(arg_img->data, out.data, out.bpp); @@ -1085,10 +1083,8 @@ static mp_obj_t py_image_compress_for_ide(uint n_args, const mp_obj_t *args, mp_ int arg_q = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_quality), 50); PY_ASSERT_TRUE_MSG((1 <= arg_q) && (arg_q <= 100), "Error: 1 <= quality <= 100!"); - uint32_t size; fb_alloc_mark(); - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=size, .data=buffer }; + image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=0, .data=NULL }; // alloc in jpeg compress PY_ASSERT_FALSE_MSG(jpeg_compress(arg_img, &out, arg_q, false), "Out of Memory!"); int new_size = fb_encode_for_ide_new_size(&out); PY_ASSERT_TRUE_MSG(new_size <= image_size(arg_img), "Can't compress in place!"); @@ -1116,10 +1112,8 @@ static mp_obj_t py_image_compressed(uint n_args, const mp_obj_t *args, mp_map_t int arg_q = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_quality), 50); PY_ASSERT_TRUE_MSG((1 <= arg_q) && (arg_q <= 100), "Error: 1 <= quality <= 100!"); - uint32_t size; fb_alloc_mark(); - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=size, .data=buffer }; + image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=0, .data=NULL }; // alloc in jpeg compress PY_ASSERT_FALSE_MSG(jpeg_compress(arg_img, &out, arg_q, false), "Out of Memory!"); uint8_t *temp = xalloc(out.bpp); memcpy(temp, out.data, out.bpp); @@ -1138,10 +1132,8 @@ static mp_obj_t py_image_compressed_for_ide(uint n_args, const mp_obj_t *args, m int arg_q = py_helper_keyword_int(n_args, args, 1, kw_args, MP_OBJ_NEW_QSTR(MP_QSTR_quality), 50); PY_ASSERT_TRUE_MSG((1 <= arg_q) && (arg_q <= 100), "Error: 1 <= quality <= 100!"); - uint32_t size; fb_alloc_mark(); - uint8_t *buffer = fb_alloc_all(&size, FB_ALLOC_PREFER_SIZE); - image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=size, .data=buffer }; + image_t out = { .w=arg_img->w, .h=arg_img->h, .bpp=0, .data=NULL }; // alloc in jpeg compress PY_ASSERT_FALSE_MSG(jpeg_compress(arg_img, &out, arg_q, false), "Out of Memory!"); int new_size = fb_encode_for_ide_new_size(&out); uint8_t *temp = xalloc(new_size);