00001 // $Id: jpgdest.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 //#include "ccfile.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00103 00104 //#include "jinclude.h" 00105 #include "jpglib_namespace.h" 00106 #include "jpgermgr.h" 00107 00108 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED] 00109 //#include "tim.h" // for _R(IDE_FILE_WRITE_ERROR) 00110 00111 #include "jpgdest.h" 00112 00113 00114 // Place any IMPLEMENT type statements here 00115 CC_IMPLEMENT_MEMDUMP(JPEGDataDestination, CC_CLASS_MEMDUMP) 00116 00117 00118 // We want better memory tracking 00119 #define new CAM_DEBUG_NEW 00120 00121 00122 // Functions follow 00123 00124 00125 00126 /******************************************************************************************** 00127 00128 > JPEGDataDestination::JPEGDataDestination(j_compress_ptr cinfo, CCFile* pOutputFile) 00129 00130 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00131 Created: 29/10/96 00132 Inputs: pOutputFile : a pointer to a CCFile providing the destination for the JPEG 00133 output 00134 Purpose: Default constructor 00135 00136 ********************************************************************************************/ 00137 JPEGDataDestination::JPEGDataDestination(CCFile* pOutputFile) 00138 { 00139 ERROR3IF(pOutputFile == NULL || !pOutputFile->IS_KIND_OF(CCFile), "pOutputFile isn't"); 00140 00141 00142 m_DestMgr.pThis = this; 00143 00144 m_DestMgr.init_destination = InitDestination; 00145 m_DestMgr.empty_output_buffer = EmptyOutputBuffer; 00146 m_DestMgr.term_destination = TerminateDestination; 00147 00148 m_pOutputFile = pOutputFile; 00149 m_pBuffer = NULL; 00150 } 00151 00152 00153 /******************************************************************************************** 00154 00155 > BOOL JPEGDataDestination::Init(j_compress_ptr cinfo) 00156 00157 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00158 Created: 29/10/96 00159 Inputs: cinfo : a pointer to the IJG compression structure 00160 Purpose: Initializes the given compression structure with this JPEGDataDestination 00161 00162 ********************************************************************************************/ 00163 BOOL JPEGDataDestination::Init(libJPEG::j_compress_ptr cinfo) 00164 { 00165 ERROR3IF(cinfo == NULL, "cinfo NULL"); 00166 00167 cinfo->dest = &m_DestMgr; 00168 00169 return TRUE; 00170 } 00171 00172 00173 /******************************************************************************************** 00174 00175 > void JPEGDataDestination::InitDestination(j_compress_ptr cinfo) 00176 00177 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00178 Created: 29/10/96 00179 Inputs: cinfo : a pointer to the IJG compression control structure 00180 Purpose: Callback for IJG destination file handler 00181 Initialize destination --- called by jpeg_start_compress 00182 before any data is actually written. 00183 00184 ********************************************************************************************/ 00185 void JPEGDataDestination::InitDestination(libJPEG::j_compress_ptr cinfo) 00186 { 00187 using namespace libJPEG; 00188 00189 JPEGDataDestination* pThis = GetThis(cinfo); 00190 00191 /* Allocate the output buffer --- it will be released when done with image */ 00192 pThis->m_pBuffer = (JOCTET *)(*cinfo->mem->alloc_small) ((j_common_ptr) cinfo, JPOOL_IMAGE, 00193 OUTPUT_BUF_SIZE * sizeof(JOCTET)); 00194 00195 pThis->m_DestMgr.next_output_byte = pThis->m_pBuffer; 00196 pThis->m_DestMgr.free_in_buffer = OUTPUT_BUF_SIZE; 00197 } 00198 00199 00200 /******************************************************************************************** 00201 00202 > boolean JPEGDataDestination::EmptyOutputBuffer(j_compress_ptr cinfo) 00203 00204 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00205 Created: 29/10/96 00206 Inputs: cinfo : a pointer to the IJG compression control structure 00207 Purpose: Callback for IJG destination file handler 00208 Empty the output buffer --- called whenever buffer fills up. 00209 Notes: Says IJG docs, 00210 "In typical applications, this should write the entire output buffer 00211 (ignoring the current state of next_output_byte & free_in_buffer), 00212 reset the pointer & count to the start of the buffer, and return TRUE 00213 indicating that the buffer has been dumped." 00214 Scope: static 00215 00216 ********************************************************************************************/ 00217 libJPEG::boolean JPEGDataDestination::EmptyOutputBuffer(libJPEG::j_compress_ptr cinfo) 00218 { 00219 JPEGDataDestination* pThis = GetThis(cinfo); 00220 CCFile* pFile = pThis->GetFile(); 00221 00222 // Write out the full buffer 00223 pFile->write(pThis->m_pBuffer, OUTPUT_BUF_SIZE); 00224 00225 // Check written OK 00226 if (pFile->bad() || pFile->fail()) 00227 { 00228 JPEGErrorManager* pError = (JPEGErrorManager*)cinfo->err; 00229 pError->ThrowError(_R(IDE_FILE_WRITE_ERROR)); 00230 } 00231 00232 pThis->m_DestMgr.next_output_byte = pThis->m_pBuffer; 00233 pThis->m_DestMgr.free_in_buffer = OUTPUT_BUF_SIZE; 00234 00235 return TRUE; 00236 } 00237 00238 00239 /******************************************************************************************** 00240 00241 > void JPEGDataDestination::TerminateDestination(j_compress_ptr cinfo) 00242 00243 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00244 Created: 29/10/96 00245 Inputs: cinfo : a pointer to the IJG compression control structure 00246 Purpose: Terminate destination --- called by jpeg_finish_compress 00247 after all data has been written. Usually needs to flush buffer. 00248 Notes: *not* called by jpeg_abort or jpeg_destroy; surrounding 00249 application must deal with any cleanup that should happen even 00250 for error exit. 00251 00252 ********************************************************************************************/ 00253 void JPEGDataDestination::TerminateDestination(libJPEG::j_compress_ptr cinfo) 00254 { 00255 JPEGDataDestination* pThis = GetThis(cinfo); 00256 CCFile* pFile = pThis->GetFile(); 00257 00258 size_t DataCount = OUTPUT_BUF_SIZE - pThis->m_DestMgr.free_in_buffer; 00259 00260 // Write any data remaining in the buffer 00261 if (DataCount > 0) 00262 { 00263 // Write out the full buffer 00264 pFile->write(pThis->m_pBuffer, DataCount); 00265 } 00266 pFile->flush(); 00267 00268 // Make sure we wrote the output file OK 00269 if (pFile->bad() || pFile->fail()) 00270 { 00271 JPEGErrorManager* pError = (JPEGErrorManager*)cinfo->err; 00272 pError->ThrowError(_R(IDE_FILE_WRITE_ERROR)); 00273 } 00274 } 00275 00276 00277 00278 /******************************************************************************************** 00279 00280 > JPEGDataDestination* JPEGDataDestination::GetThis(j_compress_ptr cinfo) 00281 00282 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00283 Created: 29/10/96 00284 Inputs: cinfo : a pointer to the IJG compression control structure 00285 Purpose: Helper function to return the this pointer buried in the cinfo structure 00286 00287 ********************************************************************************************/ 00288 JPEGDataDestination* JPEGDataDestination::GetThis(libJPEG::j_compress_ptr cinfo) 00289 { 00290 JPEGDataDestination* pThis = ((IJGDestMgr*)(cinfo->dest))->pThis; 00291 ERROR3IF(!pThis->IS_KIND_OF(JPEGDataDestination), "GetThis - pThis isn't"); 00292 00293 return pThis; 00294 } 00295 00296 00297 /******************************************************************************************** 00298 00299 > CCFile* JPEGDataDestination::GetFile() const 00300 00301 Author: Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com> 00302 Created: 29/10/96 00303 Purpose: Helper function to return the CCFile associated with this JPEGDataDestination 00304 00305 ********************************************************************************************/ 00306 CCFile* JPEGDataDestination::GetFile() const 00307 { 00308 return m_pOutputFile; 00309 }