lib/jpeg: return string (not int) error messages

Change-Id: I465a6eebc2a41ca9a618b1e86dee015cea40800b
Signed-off-by: Nigel Tao <nigeltao@golang.org>
Reviewed-on: https://review.coreboot.org/c/coreboot/+/84341
Tested-by: build bot (Jenkins) <no-reply@coreboot.org>
Reviewed-by: Nico Huber <nico.h@gmx.de>
This commit is contained in:
Nigel Tao 2024-09-14 12:43:34 +10:00 committed by Nico Huber
commit b5b97c4122
3 changed files with 30 additions and 33 deletions

View file

@ -28,8 +28,9 @@ void set_bootsplash(unsigned char *framebuffer, unsigned int x_resolution,
}
unsigned int image_width, image_height;
if (jpeg_fetch_size(jpeg, filesize, &image_width, &image_height) != 0) {
printk(BIOS_ERR, "Could not parse bootsplash.jpg\n");
const char *err = jpeg_fetch_size(jpeg, filesize, &image_width, &image_height);
if (err != NULL) {
printk(BIOS_ERR, "Could not parse bootsplash.jpg: %s\n", err);
return;
}
@ -45,12 +46,11 @@ void set_bootsplash(unsigned char *framebuffer, unsigned int x_resolution,
framebuffer += (yres - image_height) / 2 * bytes_per_line
+ (xres - image_width) / 2 * (fb_resolution / 8);
int ret = jpeg_decode(jpeg, filesize, framebuffer, image_width, image_height,
bytes_per_line, fb_resolution);
err = jpeg_decode(jpeg, filesize, framebuffer, image_width, image_height,
bytes_per_line, fb_resolution);
cbfs_unmap(jpeg);
if (ret != 0) {
printk(BIOS_ERR, "Bootsplash could not be decoded. jpeg_decode returned %d.\n",
ret);
if (err != NULL) {
printk(BIOS_ERR, "Could not decode bootsplash.jpg: %s\n", err);
return;
}
printk(BIOS_INFO, "Bootsplash loaded\n");

View file

@ -19,38 +19,38 @@
/* ~16K is big enough to move this off the stack */
static wuffs_jpeg__decoder dec;
int jpeg_fetch_size(unsigned char *filedata, size_t filesize, unsigned int *width,
unsigned int *height)
const char *jpeg_fetch_size(unsigned char *filedata, size_t filesize, unsigned int *width,
unsigned int *height)
{
if (!width || !height) {
return JPEG_DECODE_FAILED;
return "invalid arg";
}
wuffs_base__status status = wuffs_jpeg__decoder__initialize(
&dec, sizeof(dec), WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
if (status.repr) {
return JPEG_DECODE_FAILED;
return status.repr;
}
wuffs_base__image_config imgcfg;
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(filedata, filesize, true);
status = wuffs_jpeg__decoder__decode_image_config(&dec, &imgcfg, &src);
if (status.repr) {
return JPEG_DECODE_FAILED;
return status.repr;
}
*width = wuffs_base__pixel_config__width(&imgcfg.pixcfg);
*height = wuffs_base__pixel_config__height(&imgcfg.pixcfg);
return 0;
return NULL;
}
int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
unsigned int width, unsigned int height, unsigned int bytes_per_line,
unsigned int depth)
const char *jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
unsigned int width, unsigned int height, unsigned int bytes_per_line,
unsigned int depth)
{
if (!filedata || !pic) {
return JPEG_DECODE_FAILED;
return "invalid arg";
}
/* Relatively arbitrary limit that shouldn't hurt anybody.
* 300M (10k*10k*3bytes/pixel) is already larger than our heap, so
@ -59,7 +59,7 @@ int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
* calculations in this function.
*/
if ((width > 10000) || (height > 10000)) {
return JPEG_DECODE_FAILED;
return "invalid arg";
}
uint32_t pixfmt;
@ -74,13 +74,13 @@ int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
pixfmt = WUFFS_BASE__PIXEL_FORMAT__BGRA_NONPREMUL;
break;
default:
return JPEG_DECODE_FAILED;
return "invalid arg";
}
wuffs_base__status status = wuffs_jpeg__decoder__initialize(
&dec, sizeof(dec), WUFFS_VERSION, WUFFS_INITIALIZE__DEFAULT_OPTIONS);
if (status.repr) {
return JPEG_DECODE_FAILED;
return status.repr;
}
/* Opting in to lower quality means that we can pass an empty slice as the
@ -105,7 +105,7 @@ int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
wuffs_base__io_buffer src = wuffs_base__ptr_u8__reader(filedata, filesize, true);
status = wuffs_jpeg__decoder__decode_image_config(&dec, &imgcfg, &src);
if (status.repr) {
return JPEG_DECODE_FAILED;
return status.repr;
}
wuffs_base__pixel_config pixcfg;
@ -117,15 +117,11 @@ int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *pic,
wuffs_base__make_table_u8(pic, width * (depth / 8), height, bytes_per_line),
wuffs_base__empty_slice_u8());
if (status.repr) {
return JPEG_DECODE_FAILED;
return status.repr;
}
status = wuffs_jpeg__decoder__decode_frame(&dec, &pixbuf, &src,
WUFFS_BASE__PIXEL_BLEND__SRC,
wuffs_base__empty_slice_u8(), NULL);
if (status.repr) {
return JPEG_DECODE_FAILED;
}
return 0;
return status.repr;
}

View file

@ -5,12 +5,13 @@
#include <stdlib.h>
#define JPEG_DECODE_FAILED 1
/* These functions return NULL on success and a short error message on
* failure. Callers should not free the returned pointer. */
int jpeg_fetch_size(unsigned char *filedata, size_t filesize, unsigned int *width,
unsigned int *height);
int jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *framebuffer,
unsigned int width, unsigned int height, unsigned int bytes_per_line,
unsigned int depth);
const char *jpeg_fetch_size(unsigned char *filedata, size_t filesize, unsigned int *width,
unsigned int *height);
const char *jpeg_decode(unsigned char *filedata, size_t filesize, unsigned char *framebuffer,
unsigned int width, unsigned int height, unsigned int bytes_per_line,
unsigned int depth);
#endif