summaryrefslogtreecommitdiff
path: root/src/renderercommon/tr_image_jpg.c
diff options
context:
space:
mode:
authorTim Angus <tim@ngus.net>2016-04-09 17:57:28 +0100
committerTim Angus <tim@ngus.net>2016-04-09 17:57:28 +0100
commitf45fbef604e05144057dec8d1dbfc5d4f5a2a822 (patch)
tree152d2a428b078f7a89756ea9e156695fc69f1686 /src/renderercommon/tr_image_jpg.c
parent7f9e97d611b4b267d9dd913144cb9632f96c90c2 (diff)
parent87abdd914988724e164ffb16380ad26be8420b84 (diff)
Merge branch 'master' into gpp
Diffstat (limited to 'src/renderercommon/tr_image_jpg.c')
-rw-r--r--src/renderercommon/tr_image_jpg.c59
1 files changed, 49 insertions, 10 deletions
diff --git a/src/renderercommon/tr_image_jpg.c b/src/renderercommon/tr_image_jpg.c
index 2bbcb365..3e369c20 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, "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, ", loading file %s\n", filename);
+ return;
+ }
+
/* Now we can initialize the JPEG decompression object. */
jpeg_create_decompress(&cinfo);
@@ -362,17 +389,29 @@ size_t RE_SaveJPGToBuffer(byte *buffer, size_t bufSize, int quality,
int image_width, int image_height, byte *image_buffer, int padding)
{
struct jpeg_compress_struct cinfo;
- struct jpeg_error_mgr jerr;
+ q_jpeg_error_mgr_t jerr;
JSAMPROW row_pointer[1]; /* pointer to JSAMPLE row[s] */
my_dest_ptr dest;
int row_stride; /* physical row width in image buffer */
size_t outcount;
/* Step 1: allocate and initialize JPEG compression object */
- 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 and return.
+ */
+ jpeg_destroy_compress(&cinfo);
+
+ ri.Printf(PRINT_ALL, "\n");
+ return 0;
+ }
+
/* Now we can initialize the JPEG compression object. */
jpeg_create_compress(&cinfo);