00001 // $Id: jpgermgr.cpp 1282 2006-06-09 09:46:49Z alex $ 00002 /* @@tag:xara-cn@@ DO NOT MODIFY THIS LINE 00003 ================================XARAHEADERSTART=========================== 00004 00005 Xara LX, a vector drawing and manipulation program. 00006 Copyright (C) 1993-2006 Xara Group Ltd. 00007 Copyright on certain contributions may be held in joint with their 00008 respective authors. See AUTHORS file for details. 00009 00010 LICENSE TO USE AND MODIFY SOFTWARE 00011 ---------------------------------- 00012 00013 This file is part of Xara LX. 00014 00015 Xara LX is free software; you can redistribute it and/or modify it 00016 under the terms of the GNU General Public License version 2 as published 00017 by the Free Software Foundation. 00018 00019 Xara LX and its component source files are distributed in the hope 00020 that it will be useful, but WITHOUT ANY WARRANTY; without even the 00021 implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 00022 See the GNU General Public License for more details. 00023 00024 You should have received a copy of the GNU General Public License along 00025 with Xara LX (see the file GPL in the root directory of the 00026 distribution); if not, write to the Free Software Foundation, Inc., 51 00027 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 00028 00029 00030 ADDITIONAL RIGHTS 00031 ----------------- 00032 00033 Conditional upon your continuing compliance with the GNU General Public 00034 License described above, Xara Group Ltd grants to you certain additional 00035 rights. 00036 00037 The additional rights are to use, modify, and distribute the software 00038 together with the wxWidgets library, the wxXtra library, and the "CDraw" 00039 library and any other such library that any version of Xara LX relased 00040 by Xara Group Ltd requires in order to compile and execute, including 00041 the static linking of that library to XaraLX. In the case of the 00042 "CDraw" library, you may satisfy obligation under the GNU General Public 00043 License to provide source code by providing a binary copy of the library 00044 concerned and a copy of the license accompanying it. 00045 00046 Nothing in this section restricts any of the rights you have under 00047 the GNU General Public License. 00048 00049 00050 SCOPE OF LICENSE 00051 ---------------- 00052 00053 This license applies to this program (XaraLX) and its constituent source 00054 files only, and does not necessarily apply to other Xara products which may 00055 in part share the same code base, and are subject to their own licensing 00056 terms. 00057 00058 This license does not apply to files in the wxXtra directory, which 00059 are built into a separate library, and are subject to the wxWindows 00060 license contained within that directory in the file "WXXTRA-LICENSE". 00061 00062 This license does not apply to the binary libraries (if any) within 00063 the "libs" directory, which are subject to a separate license contained 00064 within that directory in the file "LIBS-LICENSE". 00065 00066 00067 ARRANGEMENTS FOR CONTRIBUTION OF MODIFICATIONS 00068 ---------------------------------------------- 00069 00070 Subject to the terms of the GNU Public License (see above), you are 00071 free to do whatever you like with your modifications. However, you may 00072 (at your option) wish contribute them to Xara's source tree. You can 00073 find details of how to do this at: 00074 http://www.xaraxtreme.org/developers/ 00075 00076 Prior to contributing your modifications, you will need to complete our 00077 contributor agreement. This can be found at: 00078 http://www.xaraxtreme.org/developers/contribute/ 00079 00080 Please note that Xara will not accept modifications which modify any of 00081 the text between the start and end of this header (marked 00082 XARAHEADERSTART and XARAHEADEREND). 00083 00084 00085 MARKS 00086 ----- 00087 00088 Xara, Xara LX, Xara X, Xara X/Xtreme, Xara Xtreme, the Xtreme and Xara 00089 designs are registered or unregistered trademarks, design-marks, and/or 00090 service marks of Xara Group Ltd. All rights in these marks are reserved. 00091 00092 00093 Xara Group Ltd, Gaddesden Place, Hemel Hempstead, HP2 6EX, UK. 00094 http://www.xara.com/ 00095 00096 =================================XARAHEADEREND============================ 00097 */ 00098 // 00099 00100 00101 #include "camtypes.h" 00102 00103 #include "jpgermgr.h" 00104 00105 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00106 //#include "filtrres.h" // for Error strings 00107 00108 // Place any IMPLEMENT type statements here 00109 //CC_IMPLEMENT_MEMDUMP(CCWobJob, CC_CLASS_MEMDUMP) 00110 00111 00112 // We want better memory tracking 00113 #define new CAM_DEBUG_NEW 00114 00115 00116 // Build a list of error messages for trace purposes 00117 #ifdef _DEBUG 00118 #define JMESSAGE(code,string,errorclass) string , 00119 00120 const char* const MessageTable[] = 00121 { 00122 #include "jerror.h" 00123 NULL 00124 }; 00125 #else 00126 const char* const MessageTable[] = 00127 { 00128 NULL 00129 }; 00130 #endif 00131 00132 // Build a list of error classes 00133 #define JMESSAGE(code,string,errorclass) errorclass , 00134 00135 const JPEGErrorManager::ERROR_CLASS ErrorClasses[] = 00136 { 00137 #include "jerror.h" 00138 JPEGErrorManager::ERR_NONE // to terminate the enum 00139 }; 00140 00141 00142 // Functions follow 00143 00144 00145 /******************************************************************************************** 00146 00147 > METHODDEF(void) format_message (j_common_ptr cinfo, char * buffer) 00148 00149 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> (copied from IJG library) 00150 Created: 02/08/96 00151 Purpose: For formatting TRACE strings 00152 Notes: From IJG library: 00153 Format a message string for the most recent JPEG error or message. 00154 The message is stored into buffer, which should be at least JMSG_LENGTH_MAX 00155 characters. Note that no '\n' character is added to the string. 00156 Few applications should need to override this method. 00157 00158 ********************************************************************************************/ 00159 METHODDEF(void) format_message (libJPEG::j_common_ptr cinfo, char * buffer) 00160 { 00161 struct libJPEG::jpeg_error_mgr * err = cinfo->err; 00162 INT32 msg_code = err->msg_code; 00163 const char * msgtext = NULL; 00164 const char * msgptr; 00165 char ch; 00166 libJPEG::boolean isstring; 00167 00168 // Look up message string in proper table 00169 if (msg_code > 0 && msg_code <= err->last_jpeg_message) { 00170 msgtext = err->jpeg_message_table[msg_code]; 00171 } else if (err->addon_message_table != NULL && 00172 msg_code >= err->first_addon_message && 00173 msg_code <= err->last_addon_message) { 00174 msgtext = err->addon_message_table[msg_code - err->first_addon_message]; 00175 } 00176 00177 // Defend against bogus message number 00178 if (msgtext == NULL) { 00179 err->msg_parm.i[0] = msg_code; 00180 msgtext = err->jpeg_message_table[0]; 00181 } 00182 00183 // Check for string parameter, as indicated by %s in the message text 00184 isstring = FALSE; 00185 msgptr = msgtext; 00186 while ((ch = *msgptr++) != '\0') { 00187 if (ch == '%') { 00188 if (*msgptr == 's') isstring = TRUE; 00189 break; 00190 } 00191 } 00192 00193 // Format the message into the passed buffer 00194 if (isstring) 00195 sprintf(buffer, msgtext, err->msg_parm.s); 00196 else 00197 sprintf(buffer, msgtext, 00198 err->msg_parm.i[0], err->msg_parm.i[1], 00199 err->msg_parm.i[2], err->msg_parm.i[3], 00200 err->msg_parm.i[4], err->msg_parm.i[5], 00201 err->msg_parm.i[6], err->msg_parm.i[7]); 00202 } 00203 00204 00205 /******************************************************************************************** 00206 00207 > JPEGErrorManager::JPEGErrorManager() 00208 00209 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00210 Created: 02/08/96 00211 Purpose: Default constructor for the JPEGErrorManager providing callbacks for 00212 error handling in the IJG JPEG Library. 00213 By default no exceptions will be thrown. 00214 00215 ********************************************************************************************/ 00216 JPEGErrorManager::JPEGErrorManager() 00217 { 00218 m_errmgr.error_exit = ErrorExit; 00219 m_errmgr.emit_message = EmitMessage; 00220 m_errmgr.output_message = OutputMessage; 00221 m_errmgr.format_message = ::format_message; 00222 m_errmgr.reset_error_mgr = ResetErrorManager; 00223 00224 m_errmgr.trace_level = 0; /* default = no tracing */ 00225 m_errmgr.num_warnings = 0; /* no warnings emitted yet */ 00226 m_errmgr.msg_code = 0; // may be useful as a flag for "no error" */ 00227 00228 /* Initialize message table pointers */ 00229 m_errmgr.jpeg_message_table = ::MessageTable; 00230 m_errmgr.last_jpeg_message = (INT32) JMSG_LASTMSGCODE - 1; 00231 00232 m_errmgr.addon_message_table = NULL; 00233 m_errmgr.first_addon_message = 0; /* for safety */ 00234 m_errmgr.last_addon_message = 0; 00235 00236 // ERROR_CLASS m_ErrorClass = ERR_NONE; 00237 } 00238 00239 00240 00241 /******************************************************************************************** 00242 00243 > J_MESSAGE_CODE JPEGErrorManager::GetLastError() const 00244 00245 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00246 Created: 02/08/96 00247 Returns: Retrieves the last error code 00248 Errors: ERROR3 if invalid last error 00249 Purpose: Used internally for easy access 00250 00251 ********************************************************************************************/ 00252 J_MESSAGE_CODE JPEGErrorManager::GetLastError() const 00253 { 00254 if (m_errmgr.msg_code <= 0 || m_errmgr.msg_code > m_errmgr.last_jpeg_message) 00255 { 00256 ERROR3(m_errmgr.jpeg_message_table[JMSG_NOMESSAGE]); 00257 return JMSG_NOMESSAGE; 00258 } 00259 00260 return (J_MESSAGE_CODE) m_errmgr.msg_code; 00261 } 00262 00263 00264 /******************************************************************************************** 00265 00266 > StringID JPEGErrorManager::GetStringIDForError() const 00267 00268 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00269 Created: 02/08/96 00270 Returns: A resource StringID for the last error thrown. 00271 Purpose: Allows class users to obtain a resource string id suitable for reporting to 00272 the user (usually via Error::SetError(...)) 00273 00274 ********************************************************************************************/ 00275 StringID JPEGErrorManager::GetStringIDForError() const 00276 { 00277 ERROR_CLASS lastClass = GetLastErrorClass(); 00278 00279 if (lastClass == ERR_STANDARD) 00280 { 00281 return m_MessageID; 00282 } 00283 else 00284 { 00285 switch (lastClass) 00286 { 00287 case ERR_NONE: return (StringID)-1; break; 00288 case ERR_STANDARD: return 0; break; // should never happen 00289 case ERR_TRACE: return 1; break; // what's this for? 00290 case ERR_STRUCTURE: return(_R(IDS_JPEG_ERROR_STRUCTURE)); break; 00291 case ERR_FORMAT: return(_R(IDS_JPEG_ERROR_FORMAT)); break; 00292 case ERR_LIMITS: return(_R(IDS_JPEG_ERROR_LIMITS)); break; 00293 case ERR_UNSUPPORTED: return(_R(IDS_JPEG_ERROR_UNSUPPORTED)); break; 00294 default: return(_R(IDS_JPEG_ERROR_INTERNAL)); 00295 } 00296 } 00297 } 00298 00299 00300 /******************************************************************************************** 00301 00302 > void JPEGErrorManager::ThrowError(StringID UserErrorMessage) 00303 00304 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00305 Created: 02/08/96 00306 Inputs: UserErrorMessage : A resource ID for a string to be used when reporting 00307 this error to the user. 00308 Purpose: Classes external to the IJG Library should use this entry point to throw 00309 exceptions. 00310 00311 ********************************************************************************************/ 00312 void JPEGErrorManager::ThrowError(StringID UserErrorMessage) 00313 { 00314 m_ErrorClass = ERR_STANDARD; 00315 m_MessageID = UserErrorMessage; 00316 00317 throw 0; 00318 } 00319 00320 00321 /******************************************************************************************** 00322 00323 > JPEGErrorManager::ERROR_CLASS JPEGErrorManager::GetLastErrorClass() const 00324 00325 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00326 Created: 02/08/96 00327 Returns: The error class of the last error thrown via a call to ErrorExit 00328 or ThrowError 00329 Errors: ERROR3 if class is ERR_NONE or ERR_TRACE 00330 Purpose: Used internally to provide easy access to m_ErrorClass 00331 00332 ********************************************************************************************/ 00333 JPEGErrorManager::ERROR_CLASS JPEGErrorManager::GetLastErrorClass() const 00334 { 00335 if (m_ErrorClass == ERR_NONE || m_ErrorClass == ERR_TRACE) 00336 { 00337 ERROR3("JPEGErrorManager::GetLastErrorClass() - Trying to get dummy error class"); 00338 return ERR_INTERNAL; 00339 } 00340 return m_ErrorClass; 00341 } 00342 00343 00344 /******************************************************************************************** 00345 00346 > void JPEGErrorManager::ErrorExit(j_common_ptr cinfo) 00347 00348 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00349 Created: 02/08/96 00350 Purpose: The IJG library requires a callback to exit once an error has occurred. 00351 This is that callback. It throws an exception. 00352 00353 ********************************************************************************************/ 00354 void JPEGErrorManager::ErrorExit(libJPEG::j_common_ptr cinfo) 00355 { 00356 JPEGErrorManager* pThis = (JPEGErrorManager*)cinfo->err; 00357 00358 J_MESSAGE_CODE msg = pThis->GetLastError(); 00359 00360 pThis->m_ErrorClass = ErrorClasses[msg]; 00361 00362 #ifdef _DEBUG 00363 EmitMessage(cinfo, -1); 00364 #endif 00365 00366 throw 0; 00367 } 00368 00369 00370 /******************************************************************************************** 00371 00372 > void JPEGImportFilter::OutputMessage(j_common_ptr cinfo) 00373 00374 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00375 Created: 02/08/96 00376 Purpose: Actual output of an error or trace message. 00377 Overrides standard implementation of sending errors to stderr. 00378 00379 ********************************************************************************************/ 00380 void JPEGErrorManager::OutputMessage(libJPEG::j_common_ptr cinfo) 00381 { 00382 char buffer[JMSG_LENGTH_MAX]; 00383 00384 /* Create the message */ 00385 (*cinfo->err->format_message) (cinfo, buffer); 00386 00387 /* Send it to stderr, adding a newline */ 00388 TRACE( _T("%s\n"), buffer ); 00389 } 00390 00391 00392 /******************************************************************************************** 00393 00394 > void JPEGImportFilter::EmitMessage(j_common_ptr cinfo, INT32 msg_level) 00395 00396 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> (based on IJG emit_message) 00397 Created: 02/08/96 00398 Purpose: Decide whether to emit a trace or warning message. 00399 00400 Notes: msg_level is one of: 00401 00402 -1: recoverable corrupt-data warning, may want to abort. 00403 0: important advisory messages (always display to user). 00404 1: first level of tracing detail. 00405 2,3,...: successively more detailed tracing messages. 00406 00407 ********************************************************************************************/ 00408 void JPEGErrorManager::EmitMessage(libJPEG::j_common_ptr cinfo, INT32 msg_level) 00409 { 00410 JPEGErrorManager* pThis = (JPEGErrorManager*)cinfo->err; 00411 00412 if (msg_level < 0) 00413 { 00414 /* 00415 * It's a warning message. Since corrupt files may generate many warnings, 00416 * the policy implemented here is to show only the first warning, 00417 * unless trace_level >= 3. 00418 */ 00419 if (pThis->m_errmgr.num_warnings == 0 || pThis->m_errmgr.trace_level >= 3) 00420 { 00421 (*pThis->m_errmgr.output_message) (cinfo); 00422 } 00423 // Always count warnings in num_warnings. 00424 pThis->m_errmgr.num_warnings++; 00425 } 00426 else 00427 { 00428 // It's a trace message. Show it if trace_level >= msg_level. 00429 if (pThis->m_errmgr.trace_level >= msg_level) 00430 { 00431 (*pThis->m_errmgr.output_message) (cinfo); 00432 } 00433 } 00434 } 00435 00436 00437 00438 /******************************************************************************************** 00439 00440 > void JPEGErrorManager::ResetErrorManager(j_common_ptr cinfo) 00441 00442 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> (based on IJG emit_message) 00443 Created: 02/08/96 00444 Purpose: Resets the the error manager prior to starting the filter proper 00445 00446 ********************************************************************************************/ 00447 void JPEGErrorManager::ResetErrorManager(libJPEG::j_common_ptr cinfo) 00448 { 00449 JPEGErrorManager* pThis = (JPEGErrorManager*)cinfo->err; 00450 00451 pThis->m_errmgr.num_warnings = 0; 00452 /* trace_level is not reset since it is an application-supplied parameter */ 00453 pThis->m_errmgr.msg_code = 0; /* may be useful as a flag for "no error" */ 00454 } 00455 00456