-
Notifications
You must be signed in to change notification settings - Fork 110
feat(esp_jpeg): Added option to get image size without decoding it #498
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
cc @igrr since you reviewed some esp_jpeg PRs in the past :) |
Test Results15 files 15 suites 1m 3s ⏱️ Results for commit 3f723c5. ♻️ This comment has been updated with latest results. |
8e02f9c
to
f58ef75
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
May I suggest extending esp_jpeg_image_output_t
with a buffer_size
field, and instead of esp_jpeg_get_output_size adding something like esp_jpeg_get_image_info, returning esp_jpeg_image_output_t? Then we can get both the buffer size and image dimensions before actually calling the decode function.
Similar to what libpng allows doing:
idf-extra-components/libpng/test_apps/main/test_libpng.c
Lines 30 to 38 in c37fc0a
TEST_ASSERT(png_image_begin_read_from_memory(&image, buf, buf_len)); | |
image.format = PNG_FORMAT_GRAY; | |
int stride = PNG_IMAGE_ROW_STRIDE(image); | |
int buf_size = PNG_IMAGE_SIZE(image); | |
TEST_ASSERT_EQUAL(expected_width, image.width); | |
TEST_ASSERT_EQUAL(expected_height, image.height); | |
esp_jpeg/jpeg_decoder.c
Outdated
@@ -119,6 +119,50 @@ esp_err_t esp_jpeg_decode(esp_jpeg_image_cfg_t *cfg, esp_jpeg_image_output_t *im | |||
return ret; | |||
} | |||
|
|||
#define LDB_WORD(ptr) (unsigned short)(((unsigned short)*((unsigned char*)(ptr))<<8)|(unsigned short)*(unsigned char*)((ptr)+1)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Perhaps you can use a slightly more readable function
static inline uint16_t ldb_word(const void* ptr) {
uint16_t result;
memcpy(&result, ptr, sizeof(result);
return result;
}
the compiler will optimize out memcpy replacing it with either an unaligned load or a pair of byte loads, depending on whether the CPU supports unaligned loads or not.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@igrr I ended up with the following implementation to preserve the big-endian to little-endian transformation. All other comments have been incorporated!
static inline uint16_t ldb_word(const void *ptr)
{
const uint8_t *p = (const uint8_t *)ptr;
return ((uint16_t)p[0] << 8) | p[1];
}
@igrr Thank you for the review, will update soon! EDIT: done |
f58ef75
to
3f723c5
Compare
Change description
Added new public API to get image size without decoding it.
This is very useful for scenarios when we have a JPEG encoded image, but we do not know what resolution does it have, thus we don't know how big output buffer we need for decoding. This scenario is added to test-cases.
This is the last missing feature, before we can integrate this component to esp32-camera, in espressif/esp32-camera#740