summaryrefslogtreecommitdiff
path: root/src/renderercommon/tr_image_jpg.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/renderercommon/tr_image_jpg.c')
-rw-r--r--src/renderercommon/tr_image_jpg.c43
1 files changed, 35 insertions, 8 deletions
diff --git a/src/renderercommon/tr_image_jpg.c b/src/renderercommon/tr_image_jpg.c
index 2bbcb365..15c280d5 100644
--- a/src/renderercommon/tr_image_jpg.c
+++ b/src/renderercommon/tr_image_jpg.c
@@ -21,6 +21,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
+#include <setjmp.h>
+
#include "tr_common.h"
/*
@@ -43,16 +45,27 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
# endif
#endif
-static void __attribute__((__noreturn__)) R_JPGErrorExit(j_common_ptr cinfo)
+/* Catching errors, as done in libjpeg's example.c */
+typedef struct q_jpeg_error_mgr_s
+{
+ struct jpeg_error_mgr pub; /* "public" fields */
+
+ jmp_buf setjmp_buffer; /* for return to caller */
+} q_jpeg_error_mgr_t;
+
+static void R_JPGErrorExit(j_common_ptr cinfo)
{
char buffer[JMSG_LENGTH_MAX];
+
+ /* cinfo->err really points to a q_jpeg_error_mgr_s struct, so coerce pointer */
+ q_jpeg_error_mgr_t *jerr = (q_jpeg_error_mgr_t *)cinfo->err;
(*cinfo->err->format_message) (cinfo, buffer);
-
- /* Let the memory manager delete any temp files before we die */
- jpeg_destroy(cinfo);
-
- ri.Error(ERR_FATAL, "%s", buffer);
+
+ ri.Printf(PRINT_ALL, "R_LoadJPG() error: %s", buffer);
+
+ /* Return control to the setjmp point */
+ longjmp(jerr->setjmp_buffer, 1);
}
static void R_JPGOutputMessage(j_common_ptr cinfo)
@@ -84,7 +97,7 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
* Note that this struct must live as long as the main JPEG parameter
* struct, to avoid dangling-pointer problems.
*/
- struct jpeg_error_mgr jerr;
+ q_jpeg_error_mgr_t jerr;
/* More stuff */
JSAMPARRAY buffer; /* Output row buffer */
unsigned int row_stride; /* physical row width in output buffer */
@@ -116,10 +129,24 @@ void R_LoadJPG(const char *filename, unsigned char **pic, int *width, int *heigh
* This routine fills in the contents of struct jerr, and returns jerr's
* address which we place into the link field in cinfo.
*/
- cinfo.err = jpeg_std_error(&jerr);
+ cinfo.err = jpeg_std_error(&jerr.pub);
cinfo.err->error_exit = R_JPGErrorExit;
cinfo.err->output_message = R_JPGOutputMessage;
+ /* Establish the setjmp return context for R_JPGErrorExit to use. */
+ if (setjmp(jerr.setjmp_buffer))
+ {
+ /* If we get here, the JPEG code has signaled an error.
+ * We need to clean up the JPEG object, close the input file, and return.
+ */
+ jpeg_destroy_decompress(&cinfo);
+ ri.FS_FreeFile(fbuffer.v);
+
+ /* Append the filename to the error for easier debugging */
+ ri.Printf(PRINT_ALL, ", file %s\n", filename);
+ return;
+ }
+
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);