aboutsummaryrefslogtreecommitdiff
path: root/jpg.c
diff options
context:
space:
mode:
authorDaniel Eklöf <daniel@ekloef.se>2020-10-01 20:03:23 +0200
committerDaniel Eklöf <daniel@ekloef.se>2020-10-01 20:03:23 +0200
commit1af2db7928029eb1ec7db3ee5b865815b22057de (patch)
treee0c3200114daa733eca16901a12a305d633d7250 /jpg.c
parentd8f96450bc8bb70012b1db9f8bd44690e36ef048 (diff)
downloadwbg-1af2db7928029eb1ec7db3ee5b865815b22057de.tar.gz
main: open file once, in main, and log an error when we fail
Diffstat (limited to 'jpg.c')
-rw-r--r--jpg.c12
1 files changed, 5 insertions, 7 deletions
diff --git a/jpg.c b/jpg.c
index a3797b1..cb013c3 100644
--- a/jpg.c
+++ b/jpg.c
@@ -24,7 +24,7 @@ error_exit(j_common_ptr cinfo)
}
pixman_image_t *
-jpg_load(const char *path)
+jpg_load(FILE *fp, const char *path)
{
struct jpeg_decompress_struct cinfo = {0};
struct my_error_mgr err_handler;
@@ -32,9 +32,10 @@ jpg_load(const char *path)
uint8_t *image_data = NULL;
pixman_image_t *pix = NULL;
- FILE *fp = fopen(path, "rb");
- if (fp == NULL)
- goto err;
+ if (fseek(fp, 0, SEEK_SET) < 0) {
+ LOG_ERRNO("%s: failed to seek to beginning of file", path);
+ return NULL;
+ }
cinfo.err = jpeg_std_error(&err_handler.mgr);
err_handler.mgr.error_exit = &error_exit;
@@ -96,7 +97,6 @@ jpg_load(const char *path)
jpeg_finish_decompress(&cinfo);
jpeg_destroy_decompress(&cinfo);
- fclose(fp);
pix = pixman_image_create_bits_no_clear(
format, width, height, (uint32_t *)image_data, stride);
@@ -113,7 +113,5 @@ err:
pixman_image_unref(pix);
free(image_data);
jpeg_destroy_decompress(&cinfo);
- if (fp != NULL)
- fclose(fp);
return NULL;
}