diff --git a/src/add-ons/translators/jpeg/JPEGTranslator.cpp b/src/add-ons/translators/jpeg/JPEGTranslator.cpp index fbf873c2aa..6a27db07a6 100644 --- a/src/add-ons/translators/jpeg/JPEGTranslator.cpp +++ b/src/add-ons/translators/jpeg/JPEGTranslator.cpp @@ -1101,21 +1101,44 @@ Identify(BPositionIO *inSource, const translation_format *inFormat, return B_OK; } +static jmp_buf sLongJumpBuffer; +jmp_buf* gLongJumpBuffer = &sLongJumpBuffer; + /*! Arguably the most important method in the add-on */ status_t Translate(BPositionIO *inSource, const translator_info *inInfo, BMessage *ioExtension, uint32 outType, BPositionIO *outDestination) { // If no specific type was requested, convert to the interchange format - if (outType == 0) outType = B_TRANSLATOR_BITMAP; - - // What action to take, based on the findings of Identify() - if (outType == inInfo->type) { - return Copy(inSource, outDestination); - } else if (inInfo->type == B_TRANSLATOR_BITMAP && outType == JPEG_FORMAT) { - return Compress(inSource, outDestination); - } else if (inInfo->type == JPEG_FORMAT && outType == B_TRANSLATOR_BITMAP) { - return Decompress(inSource, outDestination, ioExtension); + if (outType == 0) + outType = B_TRANSLATOR_BITMAP; + + // Setup a "breakpoint" since throwing exceptions does not seem to work + // at all in an add-on. (?) + // In the be_jerror.cpp we implement a handler for critical library errors + // (be_error_exit()) and there we use the longjmp() function to return to + // this place. If this happens, it is as if the setjmp() call is called + // a second time, but this time the return value will be 1. The first + // invokation will return 0. + int jmpRet = setjmp(sLongJumpBuffer); + if (jmpRet == 1) + return B_ERROR; + + try { + // What action to take, based on the findings of Identify() + if (outType == inInfo->type) { + return Copy(inSource, outDestination); + } else if (inInfo->type == B_TRANSLATOR_BITMAP + && outType == JPEG_FORMAT) { + return Compress(inSource, outDestination); + } else if (inInfo->type == JPEG_FORMAT + && outType == B_TRANSLATOR_BITMAP) { + return Decompress(inSource, outDestination, ioExtension); + } + } catch (...) { + fprintf(stderr, "libjpeg encoutered a critical error " + "(caught C++ exception).\n"); + return B_ERROR; } return B_NO_TRANSLATOR; diff --git a/src/add-ons/translators/jpeg/JPEGTranslator.h b/src/add-ons/translators/jpeg/JPEGTranslator.h index 778b582ff4..0beac983c1 100644 --- a/src/add-ons/translators/jpeg/JPEGTranslator.h +++ b/src/add-ons/translators/jpeg/JPEGTranslator.h @@ -42,6 +42,7 @@ EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include #include +#include #include #include #include @@ -214,5 +215,6 @@ EXTERN(void) be_jpeg_stdio_dest(j_compress_ptr cinfo, BPositionIO *outfile); // //--------------------------------------------------- EXTERN(struct jpeg_error_mgr *) be_jpeg_std_error (struct jpeg_error_mgr * err, jpeg_settings * settings); // from "be_jerror.cpp" +extern jmp_buf* gLongJumpBuffer; #endif // _JPEGTRANSLATOR_H_ diff --git a/src/add-ons/translators/jpeg/be_jerror.cpp b/src/add-ons/translators/jpeg/be_jerror.cpp index f2713ee8cc..007afd95bf 100644 --- a/src/add-ons/translators/jpeg/be_jerror.cpp +++ b/src/add-ons/translators/jpeg/be_jerror.cpp @@ -67,15 +67,12 @@ be_error_exit (j_common_ptr cinfo) /* Create the message */ (*cinfo->err->format_message) (cinfo, buffer); -#if 0 - /* show error message */ - (new BAlert("JPEG Library Error", buffer, "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_STOP_ALERT))->Go(); -#endif + fprintf(stderr, "JPEG Library Error: %s\n", buffer); /* Let the memory manager delete any temp files before we die */ jpeg_destroy(cinfo); - exit(B_ERROR); + longjmp(*gLongJumpBuffer, 0); } @@ -90,12 +87,11 @@ be_output_message (j_common_ptr cinfo) /* Create the message */ (*cinfo->err->format_message) (cinfo, buffer); -#if 0 /* If it's compressing or decompressing and user turned messages on */ - if (!cinfo->is_decompressor || cinfo->err->ShowReadWarnings) + if (!cinfo->is_decompressor || cinfo->err->ShowReadWarnings) { /* show warning message */ - (new BAlert("JPEG Library Warning", buffer, "OK", NULL, NULL, B_WIDTH_AS_USUAL, B_WARNING_ALERT))->Go(); -#endif + fprintf(stderr, "JPEG Library Warning: %s\n", buffer); + } }