jpeg: provide a rough estimate of decoder memory overhead

JPEG decoding requires additional space in the bitmap buffer beyond
what is needed for the decoded pixel data. Provide a way to estimate
how much overhead is needed.

The actual overhead is sizeof(struct jpeg) + decode_buf_size, where
the latter depends on the image size and JPEG encoding used. From my
testing decode_buf_size is normally pretty small (under 5 KiB) but
looking at the code it could be large in some cases, primarily with
large images, so 32 KiB seems to be a decent compromise. Someone who
knows better about JPEG should pick a better value if that's too big.

Using a constant is obviously not the most accurate but it seems to
be the simplest option for retrofitting to existing code.

Change-Id: I573b0abb8ca2d79e43f185010487f07226edb793
This commit is contained in:
Aidan MacDonald 2022-04-01 11:26:22 +01:00
parent 7718b24401
commit 5aa0fc3b00
2 changed files with 18 additions and 0 deletions

View file

@ -2228,4 +2228,15 @@ int read_jpeg_fd(int fd,
} }
#endif #endif
const size_t JPEG_DECODE_OVERHEAD =
/* Reserve an arbitrary amount for the decode buffer
* FIXME: Somebody who knows what they're doing should look at this */
(32 * 1024)
#ifndef JPEG_FROM_MEM
/* Unless the struct jpeg is defined statically, we need to allocate
* it in the bitmap buffer as well */
+ sizeof(struct jpeg)
#endif
;
/**************** end JPEG code ********************/ /**************** end JPEG code ********************/

View file

@ -32,6 +32,13 @@
#ifndef _JPEG_LOAD_H #ifndef _JPEG_LOAD_H
#define _JPEG_LOAD_H #define _JPEG_LOAD_H
/* Approximate memory overhead required for JPEG decoding. This memory is
* taken from the bitmap buffer so you must ensure the buffer is big enough
* to contain all decoded pixel data plus decoder overhead, otherwise the
* image cannot be loaded. After the image is loaded this extra memory can
* be freed. */
extern const size_t JPEG_DECODE_OVERHEAD;
int read_jpeg_file(const char* filename, int read_jpeg_file(const char* filename,
struct bitmap *bm, struct bitmap *bm,
int maxsize, int maxsize,