Make jpeg loading errors non-fatal.

This commit is contained in:
SmileTheory 2015-12-20 03:08:50 -08:00
parent f78d04eba8
commit d8fd07b69a

View file

@ -20,6 +20,8 @@ Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
===========================================================================
*/
#include <setjmp.h>
#include "tr_common.h"
/*
@ -42,16 +44,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)
@ -83,7 +96,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 */
@ -115,10 +128,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);