1/*
2
3Copyright (c) 2002-2003, Marcin 'Shard' Konicki
4All rights reserved.
5
6Redistribution and use in source and binary forms, with or without
7modification, are permitted provided that the following conditions are met:
8
9    * Redistributions of source code must retain the above copyright notice,
10      this list of conditions and the following disclaimer.
11    * Redistributions in binary form must reproduce the above copyright notice,
12      this list of conditions and the following disclaimer in the documentation and/or
13      other materials provided with the distribution.
14    * Name "Marcin Konicki", "Shard" or any combination of them,
15      must not be used to endorse or promote products derived from this
16      software without specific prior written permission from Marcin Konicki.
17
18THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30*/
31
32
33/*
34	Modified jerror.c from libjpeg
35	to make it possible to turn on/off error dialog-box
36	only two functions modified, rest is used from original error handling code
37*/
38
39
40#include "be_jerror.h"
41
42// Be headers
43#include <Alert.h>
44#include <Catalog.h>
45#include <stdio.h>
46
47// JPEG headers
48#include <jconfig.h>
49#include <jerror.h>
50
51// JPEGTtanslator settings header to get settings stuff
52#include "JPEGTranslator.h"
53#include "TranslatorSettings.h"
54
55#undef B_TRANSLATION_CONTEXT
56#define B_TRANSLATION_CONTEXT "be_jerror"
57
58// Since Translator doesn't use it's own error table, we can use error_mgr's
59// variables to store some usefull data.
60// last_addon_message (as ShowReadWarnings) is used for storing SETTINGS->ShowReadWarningBox value
61#define ShowReadWarnings last_addon_message
62
63
64/*
65 * Error exit handler: must not return to caller.
66 */
67GLOBAL(void)
68be_error_exit (j_common_ptr cinfo)
69{
70	char buffer[JMSG_LENGTH_MAX];
71
72	/* Create the message */
73	(*cinfo->err->format_message) (cinfo, buffer);
74
75	fprintf(stderr, B_TRANSLATE("JPEG Library Error: %s\n"), buffer);
76
77	be_jpeg_error_mgr* errorManager
78		= static_cast<be_jpeg_error_mgr*>(cinfo->err);
79	jmp_buf longJumpBuffer;
80	memcpy(&longJumpBuffer, errorManager->long_jump_buffer, sizeof(jmp_buf));
81
82	/* Let the memory manager delete any temp files before we die */
83	jpeg_destroy(cinfo);
84
85	// jump back directly to the high level function's "breakpoint"
86	longjmp(longJumpBuffer, 0);
87}
88
89
90/*
91 * Actual output of an error or trace message.
92 */
93GLOBAL(void)
94be_output_message (j_common_ptr cinfo)
95{
96	char buffer[JMSG_LENGTH_MAX];
97
98	/* Create the message */
99	(*cinfo->err->format_message) (cinfo, buffer);
100
101	cinfo->err->num_warnings++;
102
103	/* If it's compressing or decompressing and user turned messages on */
104	if (!cinfo->is_decompressor || cinfo->err->ShowReadWarnings) {
105		/* show warning message */
106		fprintf(stderr, B_TRANSLATE("JPEG Library Warning: %s\n"), buffer);
107	}
108}
109
110
111/*
112 * Fill in the standard error-handling methods in a jpeg_error_mgr object.
113 * Since Translator doesn't use it's own error table, we can use error_mgr's
114 * variables to store some useful data.
115 * last_addon_message (as ShowReadWarnings) is used for storing SETTINGS->ShowReadWarningBox value
116 */
117
118GLOBAL(struct jpeg_error_mgr *)
119be_jpeg_std_error(be_jpeg_error_mgr* err, TranslatorSettings* settings,
120	const jmp_buf* longJumpBuffer)
121{
122	settings->Acquire();
123	jpeg_std_error(err);
124
125	err->error_exit = be_error_exit;
126	err->output_message = be_output_message;
127
128	err->ShowReadWarnings = settings->SetGetBool(JPEG_SET_SHOWREADWARNING, NULL);
129	err->long_jump_buffer = longJumpBuffer;
130
131	settings->Release();
132	return err;
133}
134