xref: /haiku/src/add-ons/translators/jpeg/be_jerror.cpp (revision 1294543de9ac0eff000eaea1b18368c36435d08e)
1 /*
2 
3 Copyright (c) 2002-2003, Marcin 'Shard' Konicki
4 All rights reserved.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, 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 
18 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
19 ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20 THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21 ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
22 BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
23 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25 OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27 OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
28 EVEN 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 // Be headers
41 #include <Alert.h>
42 #include <stdio.h>
43 
44 // JPEG headers
45 #include <jpeglib.h>
46 #include <jconfig.h>
47 #include <jerror.h>
48 
49 // JPEGTtanslator settings header to get settings stuff
50 #include "JPEGTranslator.h"
51 #include "TranslatorSettings.h"
52 
53 
54 // Since Translator doesn't use it's own error table, we can use error_mgr's
55 // variables to store some usefull data.
56 // last_addon_message (as ShowReadWarnings) is used for storing SETTINGS->ShowReadWarningBox value
57 #define ShowReadWarnings last_addon_message
58 
59 
60 /*
61  * Error exit handler: must not return to caller.
62  */
63 GLOBAL(void)
64 be_error_exit (j_common_ptr cinfo)
65 {
66 	char buffer[JMSG_LENGTH_MAX];
67 
68 	/* Create the message */
69 	(*cinfo->err->format_message) (cinfo, buffer);
70 
71 	fprintf(stderr, "JPEG Library Error: %s\n", buffer);
72 
73 	jmp_buf longJumpBuffer;
74 	memcpy(&longJumpBuffer, &(cinfo->err->long_jump_buffer), sizeof(jmp_buf));
75 
76 	/* Let the memory manager delete any temp files before we die */
77 	jpeg_destroy(cinfo);
78 
79 	// jump back directly to the high level function's "breakpoint"
80 	longjmp(longJumpBuffer, 0);
81 }
82 
83 
84 /*
85  * Actual output of an error or trace message.
86  */
87 GLOBAL(void)
88 be_output_message (j_common_ptr cinfo)
89 {
90 	char buffer[JMSG_LENGTH_MAX];
91 
92 	/* Create the message */
93 	(*cinfo->err->format_message) (cinfo, buffer);
94 
95 	cinfo->err->num_warnings++;
96 
97 	/* If it's compressing or decompressing and user turned messages on */
98 	if (!cinfo->is_decompressor || cinfo->err->ShowReadWarnings) {
99 		/* show warning message */
100 		fprintf(stderr, "JPEG Library Warning: %s\n", buffer);
101 	}
102 }
103 
104 
105 /*
106  * Fill in the standard error-handling methods in a jpeg_error_mgr object.
107  * Since Translator doesn't use it's own error table, we can use error_mgr's
108  * variables to store some usefull data.
109  * last_addon_message (as ShowReadWarnings) is used for storing SETTINGS->ShowReadWarningBox value
110  */
111 
112 GLOBAL(struct jpeg_error_mgr *)
113 be_jpeg_std_error (struct jpeg_error_mgr * err, TranslatorSettings* settings,
114 	const jmp_buf* longJumpBuffer)
115 {
116 	settings->Acquire();
117 	jpeg_std_error(err);
118 
119 	err->error_exit = be_error_exit;
120 	err->output_message = be_output_message;
121 
122 	err->ShowReadWarnings = settings->SetGetBool(JPEG_SET_SHOWREADWARNING, NULL);
123 	memcpy(&(err->long_jump_buffer), longJumpBuffer, sizeof(jmp_buf));
124 
125 	settings->Release();
126 	return err;
127 }
128