jpgsrc.cpp

Go to the documentation of this file.
00001 // $Id: jpgsrc.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 "jpglib_namespace.h"
00105 #include "jpgermgr.h"
00106 //#include "errors.h" - in camtypes.h [AUTOMATICALLY REMOVED]
00107 //#include "andy.h"         // for _R(IDE_FILEREADERROR)
00108 
00109 #include "jpgsrc.h"
00110 
00111 // Place any IMPLEMENT type statements here
00112 //CC_IMPLEMENT_MEMDUMP(CCWobJob, CC_CLASS_MEMDUMP)
00113 
00114 
00115 // We want better memory tracking
00116 #define new CAM_DEBUG_NEW
00117 
00118 
00119 // Functions follow
00120 
00121 
00122 
00123 /********************************************************************************************
00124 
00125 >   JPEGDataSource::JPEGDataSource(CCFile* pInputFile)
00126 
00127     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00128     Created:    08/08/96
00129     Inputs:     pFile : a pointer to a CCFile providing the source for a JPEG filter
00130     Purpose:    Default consturctor for a JPEGDataSource.
00131                 This constructor sets up the callback functions to be used when the IJG
00132                 library requires more data to be read from the source given as the argument
00133                 to this constructor.
00134 
00135 ********************************************************************************************/
00136 JPEGDataSource::JPEGDataSource(CCFile* pInputFile) : m_pInputFile(pInputFile)
00137 {
00138     m_bStartOfFile  = FALSE;    // Provide safe defaults
00139     m_pBuffer       = NULL;
00140 
00141     // Override default "methods"
00142     init_source         = InitSource;
00143     fill_input_buffer   = FillInputBuffer;
00144     skip_input_data     = SkipInputData;
00145     /*
00146     * An additional method that can be provided by data source modules is the
00147     * resync_to_restart method for error recovery in the presence of RST markers.
00148     * For the moment, this source module just uses the default resync method
00149     * provided by the JPEG library.  That method assumes that no backtracking
00150     * is possible.
00151     */
00152     resync_to_restart   = libJPEG::jpeg_resync_to_restart;
00153     term_source         = TerminateSource;
00154 
00155     // Initialize base class!!
00156     bytes_in_buffer     = 0;        // forces fill_input_buffer on first read
00157     next_input_byte     = NULL;     // until buffer loaded
00158 }
00159 
00160 
00161 /********************************************************************************************
00162 
00163 >   boolean JPEGDataSource::InitBuffer(j_decompress_ptr cinfo)
00164 
00165     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00166     Created:    08/08/96
00167     Inputs:     cinfo : a pointer to a CCFile providing the source for a JPEG filter
00168     Purpose:    Initializes the buffer to be used by the m_pFile object.
00169     Notes:      Uses the IJG library for memory allocation
00170 
00171 ********************************************************************************************/
00172 libJPEG::boolean    JPEGDataSource::InitBuffer(libJPEG::j_decompress_ptr cinfo)
00173 {
00174     using namespace libJPEG;
00175 
00176     m_pBuffer = (JOCTET *) (*cinfo->mem->alloc_small) 
00177                     ((j_common_ptr) cinfo, JPOOL_PERMANENT, INPUT_BUF_SIZE * sizeof(JOCTET));
00178     return (m_pBuffer != NULL);
00179 }
00180 
00181 
00182 /********************************************************************************************
00183 
00184 >   void JPEGDataSource::InitSource(j_decompress_ptr cinfo)
00185 
00186     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00187     Created:    08/08/96
00188     Inputs:     cinfo : a pointer to a j_decompress_ptr provided by the IJG routines
00189     Purpose:    Initialize source --- called by jpeg_read_header before any data is actually
00190                 read.
00191 
00192     Notes:      We reset the empty-input-file flag for each image, but we don't clear the
00193                 input buffer. This is correct behavior for reading a series of images from
00194                 one source.
00195 
00196 ********************************************************************************************/
00197 void JPEGDataSource::InitSource(libJPEG::j_decompress_ptr cinfo)
00198 {
00199     JPEGDataSource* pThis = (JPEGDataSource*)cinfo->src;
00200 
00201     pThis->m_bStartOfFile = TRUE;
00202 }
00203 
00204 
00205 
00206 /********************************************************************************************
00207 
00208 >   boolean JPEGDataSource::FillInputBuffer (j_decompress_ptr cinfo)
00209 
00210     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00211     Created:    08/08/96
00212     Inputs:     cinfo : a pointer to a j_decompress_ptr provided by the IJG routines
00213     Purpose:    Overrides the standard implementation using stdio with a CCFile version.
00214 
00215     Notes:      The following extract from the IJG library explains its purpose:
00216 
00217                 Fill the input buffer --- called whenever buffer is emptied.
00218 
00219                 In typical applications, this should read fresh data into the buffer
00220                 (ignoring the current state of next_input_byte & bytes_in_buffer),
00221                 reset the pointer & count to the start of the buffer, and return TRUE
00222                 indicating that the buffer has been reloaded.  It is not necessary to
00223                 fill the buffer entirely, only to obtain at least one more byte.
00224 
00225                 There is no such thing as an EOF return.  If the end of the file has been
00226                 reached, the routine has a choice of ERREXIT() or inserting fake data into
00227                 the buffer.  In most cases, generating a warning message and inserting a
00228                 fake EOI marker is the best course of action --- this will allow the
00229                 decompressor to output however much of the image is there.  However,
00230                 the resulting error message is misleading if the real problem is an empty
00231                 input file, so we handle that case specially.
00232 
00233                 In applications that need to be able to suspend compression due to input
00234                 not being available yet, a FALSE return indicates that no more data can be
00235                 obtained right now, but more may be forthcoming later.  In this situation,
00236                 the decompressor will return to its caller (with an indication of the
00237                 number of scanlines it has read, if any).  The application should resume
00238                 decompression after it has loaded more data into the input buffer.  Note
00239                 that there are substantial restrictions on the use of suspension --- see
00240                 the documentation.
00241 
00242                 When suspending, the decompressor will back up to a convenient restart point
00243                 (typically the start of the current MCU). next_input_byte & bytes_in_buffer
00244                 indicate where the restart point will be if the current call returns FALSE.
00245                 Data beyond this point must be rescanned after resumption, so move it to
00246                 the front of the buffer rather than discarding it.
00247 
00248 ********************************************************************************************/
00249 libJPEG::boolean JPEGDataSource::FillInputBuffer (libJPEG::j_decompress_ptr cinfo)
00250 {
00251     JPEGDataSource* pThis = (JPEGDataSource*)cinfo->src;
00252     CCFile* pFile = pThis->m_pInputFile;
00253 
00254     if (pFile->bad())
00255     {
00256         JPEGErrorManager* pError = (JPEGErrorManager*)cinfo->err;
00257         pError->ThrowError(_R(IDE_FILEREADERROR));
00258     }
00259 
00260     size_t start = pFile->tellIn();
00261     pFile->read(pThis->m_pBuffer, INPUT_BUF_SIZE);
00262     size_t nbytes = pFile->tellIn() - start;
00263     if (nbytes < INPUT_BUF_SIZE)
00264     {
00265         // For some reason trying to read too many bytes from a CCFile sets the stream bad
00266         // so don't let it stick some absurd message up
00267         Error::ClearError();
00268         // & clear any eof signal
00269         pFile->SetGoodState();
00270     }
00271     if (nbytes <= 0)
00272     {
00273         if (pThis->m_bStartOfFile)  // Treat empty input file as fatal error
00274         {
00275             using namespace libJPEG;
00276             ERREXIT(cinfo, JERR_INPUT_EMPTY);
00277         }
00278         
00279         using namespace libJPEG;
00280         WARNMS(cinfo, JWRN_JPEG_EOF);
00281         // Insert a fake EOI marker
00282         pThis->m_pBuffer[0] = (JOCTET) 0xFF;
00283         pThis->m_pBuffer[1] = (JOCTET) JPEG_EOI;
00284         nbytes = 2;
00285     }
00286 
00287     pThis->next_input_byte = pThis->m_pBuffer;
00288     pThis->bytes_in_buffer = nbytes;
00289     pThis->m_bStartOfFile = FALSE;
00290 
00291     return TRUE;
00292 }
00293 
00294 
00295 /********************************************************************************************
00296 
00297 >   void JPEGDataSource::SkipInputData(j_decompress_ptr cinfo, INT32 num_bytes)
00298 
00299     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00300     Created:    08/08/96
00301     Inputs:     cinfo : a pointer to a j_decompress_ptr provided by the IJG routines
00302     Purpose:    Overrides the standard implementation using stdio with a CCFile version.
00303                 Skip data --- used to skip over a potentially large amount of
00304                 uninteresting data (such as an APPn marker).
00305 
00306                 Writers of suspendable-input applications must note that skip_input_data
00307                 is not granted the right to give a suspension return.  If the skip extends
00308                 beyond the data currently in the buffer, the buffer can be marked empty so
00309                 that the next read will cause a fill_input_buffer call that can suspend.
00310                 Arranging for additional bytes to be discarded before reloading the input
00311                 buffer is the application writer's problem.
00312 
00313 ********************************************************************************************/
00314 void JPEGDataSource::SkipInputData(libJPEG::j_decompress_ptr cinfo, long /*TYPENOTE: Correct*/ num_bytes)
00315 {
00316     JPEGDataSource* pThis = (JPEGDataSource*)cinfo->src;
00317   /* Just a dumb implementation for now.  Could use fseek() except
00318    * it doesn't work on pipes.  Not clear that being smart is worth
00319    * any trouble anyway --- large skips are infrequent.
00320    */
00321     if (num_bytes > 0)
00322     {
00323         while (num_bytes > (INT32) pThis->bytes_in_buffer)
00324         {
00325             num_bytes -= (INT32) pThis->bytes_in_buffer;
00326             (void)FillInputBuffer(cinfo);
00327             /* note we assume that fill_input_buffer will never return FALSE,
00328             * so suspension need not be handled.
00329             */
00330         }
00331         pThis->next_input_byte += (size_t) num_bytes;
00332         pThis->bytes_in_buffer -= (size_t) num_bytes;
00333     }
00334 }
00335 
00336 
00337 
00338 /********************************************************************************************
00339 
00340 >   void JPEGDataSource::SkipInputData(j_decompress_ptr cinfo, INT32 num_bytes)
00341 
00342     Author:     Colin_Barfoot (Xara Group Ltd) <camelotdev@xara.com>
00343     Created:    08/08/96
00344     Inputs:     cinfo : a pointer to a j_decompress_ptr provided by the IJG routines
00345     Purpose:    Overrides the standard implementation using stdio with a CCFile version.
00346                 The following extract from the IJG library explains its purpose:
00347                 Terminate source --- called by jpeg_finish_decompress
00348                 after all data has been read.  Often a no-op.
00349 
00350                 NB: *not* called by jpeg_abort or jpeg_destroy; surrounding
00351                 application must deal with any cleanup that should happen even
00352                 for error exit.
00353 
00354 ********************************************************************************************/
00355 void JPEGDataSource::TerminateSource(libJPEG::j_decompress_ptr cinfo)
00356 {
00357 //  JPEGDataSource* pThis = (JPEGDataSource*)cinfo->src;
00358   /* no work necessary here */
00359 }
00360 

Generated on Sat Nov 10 03:45:34 2007 for Camelot by  doxygen 1.4.4