IMG_jpg.c
changeset 61 a8a23146b406
parent 53 96b084473b47
child 71 e2eb6980be07
     1.1 --- a/IMG_jpg.c	Fri Dec 28 14:19:28 2001 +0000
     1.2 +++ b/IMG_jpg.c	Sat Dec 29 01:55:32 2001 +0000
     1.3 @@ -26,6 +26,7 @@
     1.4  
     1.5  #include <stdio.h>
     1.6  #include <string.h>
     1.7 +#include <setjmp.h>
     1.8  
     1.9  #include "SDL_image.h"
    1.10  
    1.11 @@ -171,16 +172,42 @@
    1.12    src->pub.next_input_byte = NULL; /* until buffer loaded */
    1.13  }
    1.14  
    1.15 +struct my_error_mgr {
    1.16 +	struct jpeg_error_mgr errmgr;
    1.17 +	jmp_buf escape;
    1.18 +};
    1.19 +
    1.20 +static void my_error_exit(j_common_ptr cinfo)
    1.21 +{
    1.22 +	struct my_error_mgr *err = (struct my_error_mgr *)cinfo->err;
    1.23 +	longjmp(err->escape, 1);
    1.24 +}
    1.25 +
    1.26 +static void output_no_message(j_common_ptr cinfo)
    1.27 +{
    1.28 +	/* do nothing */
    1.29 +}
    1.30 +
    1.31  /* Load a JPEG type image from an SDL datasource */
    1.32  SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src)
    1.33  {
    1.34 -	struct jpeg_error_mgr errmgr;
    1.35  	struct jpeg_decompress_struct cinfo;
    1.36  	JSAMPROW rowptr[1];
    1.37 -	SDL_Surface *surface;
    1.38 +	SDL_Surface *volatile surface = NULL;
    1.39 +	struct my_error_mgr jerr;
    1.40  
    1.41  	/* Create a decompression structure and load the JPEG header */
    1.42 -	cinfo.err = jpeg_std_error(&errmgr);
    1.43 +	cinfo.err = jpeg_std_error(&jerr.errmgr);
    1.44 +	jerr.errmgr.error_exit = my_error_exit;
    1.45 +	jerr.errmgr.output_message = output_no_message;
    1.46 +	if(setjmp(jerr.escape)) {
    1.47 +		/* If we get here, libjpeg found an error */
    1.48 +		jpeg_destroy_decompress(&cinfo);
    1.49 +		IMG_SetError("JPEG loading error");
    1.50 +		SDL_FreeSurface(surface);
    1.51 +		return NULL;
    1.52 +	}
    1.53 +
    1.54  	jpeg_create_decompress(&cinfo);
    1.55  	jpeg_SDL_RW_src(&cinfo, src);
    1.56  	jpeg_read_header(&cinfo, TRUE);