Game Development Community

Malformed JPGs

by Tim McClarren · in Torque Game Engine · 04/11/2007 (11:33 am) · 0 replies

I posted a patch a while ago for fixing TGE so it doesn't exit on malformed PNGs.

Here's the patch for JPGs:

Index: bitmapJpeg.cc
===================================================================
--- bitmapJpeg.cc	(revision 16288)
+++ bitmapJpeg.cc	(revision 18147)
@@ -11,6 +11,8 @@
 
 #include "jpeglib.h"
 
+#include <setjmp.h>
+
 U32 gJpegQuality = 90;
 
 
@@ -60,7 +62,20 @@
    return (stream->getStatus() != Stream::Ok);      
 }   
 
+typedef struct extError
+{
+   jpeg_error_mgr jerr;
+   jmp_buf jmpBuf;
+} extError;
+
+void errorHandler(j_common_ptr cinfo)
+{
+   extError* jerr = (extError*)cinfo->err;
+   longjmp(jerr->jmpBuf, 1);
+}
+
 extern bool gDedicated;
+
 //--------------------------------------
 bool GBitmap::readJPEG(Stream &stream)
 {
@@ -68,22 +83,23 @@
    JFERROR = jpegErrorFn;
 
    jpeg_decompress_struct cinfo;
-   jpeg_error_mgr jerr;
+   extError jerr;
 
    // We set up the normal JPEG error routines, then override error_exit.
    //cinfo.err = jpeg_std_error(&jerr.pub);
    //jerr.pub.error_exit = my_error_exit;
 
-   // 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);
-   //    return false;
-   // }
-
+   if (setjmp(jerr.jmpBuf)) 
+   {
+       // 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);
+      return false;
+   }
    
-   cinfo.err = jpeg_std_error(&jerr);    // set up the normal JPEG error routines.
+   cinfo.err = jpeg_std_error(&jerr.jerr);    // set up the normal JPEG error routines.
+   cinfo.err->error_exit = errorHandler;
+   
    cinfo.client_data = (void*)&stream;       // set the stream into the client_data
 
    // Now we can initialize the JPEG decompression object.