From 03434efeae4b4383217effbd780331d3b4d25104 Mon Sep 17 00:00:00 2001 From: Erik Auerswald Date: Fri, 11 Jan 2013 05:30:21 +0000 Subject: [PATCH] Check for errors when loading or saving an image with Imlib2. This relies on the definition of enum _imlib_load_error in Imlib2.h and might break on different versions of Imlib2, but should be better than the total lack of error reporting before. Developed with Imlib2 version 1.4.5 from Debian/Sid: $ dpkg -l libimlib2-dev Desired=Unknown/Install/Remove/Purge/Hold | Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend |/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad) ||/ Name Version Architecture Description +++-==============-============-============-================================= ii libimlib2-dev 1.4.5-1 i386 Imlib2 development files --- imgproc.c | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++++++- imgproc.h | 3 +++ ssocr.c | 4 +++- 3 files changed, 68 insertions(+), 2 deletions(-) diff --git a/imgproc.c b/imgproc.c index 3b00564..ed4bcc3 100644 --- a/imgproc.c +++ b/imgproc.c @@ -1017,6 +1017,7 @@ void save_image(const char *image_type, Imlib_Image *image, const char *fmt, { const char *tmp; Imlib_Image *current_image; + Imlib_Load_Error save_error=0; const char *const stdout_file = "/proc/self/fd/1"; current_image = imlib_context_get_image(); @@ -1045,7 +1046,11 @@ void save_image(const char *image_type, Imlib_Image *image, const char *fmt, /* write image to disk */ if(flags & VERBOSE) fprintf(stderr, "writing %s image to file %s\n", image_type, filename); - imlib_save_image(filename); + imlib_save_image_with_error_return(filename, &save_error); + if(save_error && save_error != IMLIB_LOAD_ERROR_NONE) { + fprintf(stderr, "error saving image file %s\n", filename); + report_imlib_error(save_error); + } imlib_context_set_image(current_image); } @@ -1075,3 +1080,59 @@ luminance_t parse_lum(char *keyword) return DEFAULT_LUM_FORMULA; } } + +/* report Imlib2 load/save error to stderr */ +void report_imlib_error(Imlib_Load_Error error) +{ + fputs(" Imlib2 error code: ",stderr); + switch (error) { + case IMLIB_LOAD_ERROR_NONE: + fputs("IMLIB_LOAD_ERROR_NONE\n", stderr); + break; + case IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST: + fputs("IMLIB_LOAD_ERROR_FILE_DOES_NOT_EXIST\n", stderr); + break; + case IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY: + fputs("IMLIB_LOAD_ERROR_FILE_IS_DIRECTORY\n", stderr); + break; + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ: + fputs("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_READ\n", stderr); + break; + case IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT: + fputs("IMLIB_LOAD_ERROR_NO_LOADER_FOR_FILE_FORMAT\n", stderr); + break; + case IMLIB_LOAD_ERROR_PATH_TOO_LONG: + fputs("IMLIB_LOAD_ERROR_PATH_TOO_LONG\n", stderr); + break; + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT: + fputs("IMLIB_LOAD_ERROR_PATH_COMPONENT_NON_EXISTANT\n", stderr); + break; + case IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY: + fputs("IMLIB_LOAD_ERROR_PATH_COMPONENT_NOT_DIRECTORY\n", stderr); + break; + case IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE: + fputs("IMLIB_LOAD_ERROR_PATH_POINTS_OUTSIDE_ADDRESS_SPACE\n", stderr); + break; + case IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS: + fputs("IMLIB_LOAD_ERROR_TOO_MANY_SYMBOLIC_LINKS\n", stderr); + break; + case IMLIB_LOAD_ERROR_OUT_OF_MEMORY: + fputs("IMLIB_LOAD_ERROR_OUT_OF_MEMORY\n", stderr); + break; + case IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS: + fputs("IMLIB_LOAD_ERROR_OUT_OF_FILE_DESCRIPTORS\n", stderr); + break; + case IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE: + fputs("IMLIB_LOAD_ERROR_PERMISSION_DENIED_TO_WRITE\n", stderr); + break; + case IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE: + fputs("IMLIB_LOAD_ERROR_OUT_OF_DISK_SPACE\n", stderr); + break; + case IMLIB_LOAD_ERROR_UNKNOWN: + fputs("IMLIB_LOAD_ERROR_UNKNOWN\n", stderr); + break; + default: + fprintf(stderr, "unknown error code %d, please report\n", error); + break; + } +} diff --git a/imgproc.h b/imgproc.h index 5b8cc21..6940669 100644 --- a/imgproc.h +++ b/imgproc.h @@ -171,4 +171,7 @@ int clip(int value, int min, int max); void save_image(const char *image_type, Imlib_Image *image, const char *fmt, const char *filename, int flags); +/* report Imlib2 load/save error to stderr */ +void report_imlib_error(Imlib_Load_Error error); + #endif /* SSOCR2_IMGPROC_H */ diff --git a/ssocr.c b/ssocr.c index 48bf68f..210049b 100644 --- a/ssocr.c +++ b/ssocr.c @@ -113,6 +113,7 @@ int main(int argc, char **argv) Imlib_Image image=NULL; /* an image handle */ Imlib_Image new_image=NULL; /* a temporary image handle */ Imlib_Image debug_image=NULL; /* DEBUG */ + Imlib_Load_Error load_error=0; /* save Imlib2 error code on image I/O*/ char *imgfile=NULL; /* filename of image file */ int use_tmpfile=0; /* flag to know if temporary image file is used */ @@ -379,7 +380,7 @@ int main(int argc, char **argv) if(flags & VERBOSE) { fprintf(stderr, "loading image %s\n", imgfile); } - image = imlib_load_image_immediately_without_cache(imgfile); + image = imlib_load_image_with_error_return(imgfile, &load_error); if(use_tmpfile) { if(flags & VERBOSE) fprintf(stderr, "removing temporary image file %s\n", imgfile); @@ -389,6 +390,7 @@ int main(int argc, char **argv) } if(!image) { fprintf(stderr, "could not load image %s\n", imgfile); + report_imlib_error(load_error); exit(99); }