Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members | Related Pages

pljpegdec.cpp

Go to the documentation of this file.
00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      $Id: pljpegdec.cpp,v 1.1 2004/05/21 21:02:52 maxx Exp $
00005 |      JPEG Decoder Class
00006 |
00007 |      JPEG file decoder. Uses the independent JPEG group's library
00008 |      to do the actual conversion.
00009 |
00010 |      Copyright (c) 1996-1998 Ulrich von Zadow
00011 |
00012 \--------------------------------------------------------------------
00013 */
00014 
00015 #include "plstdpch.h"
00016 #include "pljpegdec.h"
00017 
00018 #include "plexcept.h"
00019 
00020 #include "jmemsrc.h"
00021 
00022 // This is for RGB_RED, RGB_GREEN, RGB_BLUE, RGB_PIXELSIZE
00023 #define JPEG_INTERNALS
00024 #include <jmorecfg.h>
00025 
00026 
00028 // Error handling.
00029 
00030 METHODDEF(void)
00031 error_exit (j_common_ptr pcinfo)
00032 // This procedure is called by the IJPEG library when an error
00033 // occurs.
00034 {
00035   /* Create the message string */
00036   char sz[256];
00037   (pcinfo->err->format_message) (pcinfo, sz);
00038   strcat (sz, "\n");
00039 
00040   PLPicDecoder::raiseError (PL_ERRFORMAT_NOT_SUPPORTED, sz);
00041 }
00042 
00044 // Class functions
00045 
00046 PLJPEGDecoder::PLJPEGDecoder
00047     ()
00048     : PLPicDecoder(),
00049       m_bFast(true)
00050     // Creates a decoder
00051 {
00052   cinfo.err = jpeg_std_error (&jerr);
00053   jerr.error_exit = error_exit;  // Register custom error manager.
00054 
00055   jpeg_create_decompress (&cinfo);
00056 }
00057 
00058 
00059 PLJPEGDecoder::~PLJPEGDecoder
00060     ()
00061 {
00062   jpeg_destroy_decompress (&cinfo);
00063 }
00064 
00065 void PLJPEGDecoder::SetFast
00066     ( bool bFast
00067     )
00068     // true (the default) selects fast but sloppy decoding.
00069 {
00070   m_bFast = bFast;
00071 }
00072 
00073 
00074 void PLJPEGDecoder::DoDecode
00075     ( PLBmp * pBmp,
00076       PLDataSource * pDataSrc
00077     )
00078 {
00079   try
00080   {
00081   // Jo Hagelberg 15.4.99: added progress notification callback
00082     JMETHOD( void, notify, (j_common_ptr));
00083     notify = JNotification;
00084 
00085     // Initialize custom data source.
00086     // Jo Hagelberg 15.4.99: added pDataSrc and notify
00087     jpeg_mem_src (&cinfo, pDataSrc->ReadEverything(),
00088                   pDataSrc->GetFileSize(), (void*)pDataSrc, notify);
00089 
00090     jpeg_read_header (&cinfo, true);
00091 
00092     if (m_bFast)
00093     {
00094       cinfo.do_fancy_upsampling = false;
00095     }
00096 
00097     // Choose floating point DCT method.
00098     cinfo.dct_method = JDCT_FLOAT;
00099 
00100     int w = cinfo.image_width;
00101     int h = cinfo.image_height;
00102 
00103     jpeg_start_decompress (&cinfo);
00104 
00105     if (cinfo.out_color_space == JCS_GRAYSCALE)
00106       decodeGray (pBmp, w, h);
00107     else
00108       decodeRGB (pBmp, w, h);
00109     jpeg_finish_decompress (&cinfo);
00110 
00111           PLPoint DPI;
00112 
00113           DPI.x = cinfo.X_density;
00114           if(DPI.x <= 1)
00115                   DPI.x = 72;
00116 
00117           DPI.y = cinfo.Y_density;
00118           if(DPI.y <= 1)
00119                   DPI.y = 72;
00120           pBmp->SetResolution (DPI);
00121   }
00122   catch (PLTextException)
00123   {
00124     jpeg_abort_decompress(&cinfo);
00125     throw;
00126   }
00127 
00128 
00129 }
00130 
00131 void PLJPEGDecoder::decodeRGB
00132     ( PLBmp * pBmp,
00133       int w,
00134       int h
00135     )
00136     // Assumes IJPEG decoder is already set up.
00137 {
00138   int CurLine = 0;
00139   PLBYTE * pBuf;
00140   JSAMPARRAY ppBuf = &pBuf;
00141 
00142   pBmp->Create (w, h, 32, false);
00143 #if ((PL_RGBA_RED!=RGB_RED)||(PL_RGBA_GREEN!=RGB_GREEN)||(PL_RGBA_BLUE!=RGB_BLUE)||(4!=RGB_PIXELSIZE))
00144   pBuf = new PLBYTE[w*sizeof (PLPixel32)];
00145 #endif
00146 
00147   pBmp->Lock(false, true);
00148   PLPixel32 ** pLineArray = pBmp->GetLineArray32();
00149 
00150   int readed = 0;
00151   while (CurLine < h)
00152   {
00153     // Correct pixel ordering if libjpeg is configured differently
00154     // than paintlib.
00155 #if ((PL_RGBA_RED!=RGB_RED)||(PL_RGBA_GREEN!=RGB_GREEN)||(PL_RGBA_BLUE!=RGB_BLUE)||(4!=RGB_PIXELSIZE))
00156     jpeg_read_scanlines (&cinfo, ppBuf, 1);
00157     int i;
00158     for (i=0;i<w;i++)
00159     {
00160       PLBYTE * pSrcPixel = pBuf+i*RGB_PIXELSIZE;
00161       PLPixel32 * pDestPixel = pLineArray[CurLine]+i;
00162       pDestPixel->SetR (pSrcPixel[RGB_RED]);
00163       pDestPixel->SetG (pSrcPixel[RGB_GREEN]);
00164       pDestPixel->SetB (pSrcPixel[RGB_BLUE]);
00165     }
00166     CurLine++;
00167 #else
00168     readed = jpeg_read_scanlines (&cinfo,&pLineArray[CurLine],32);
00169     CurLine+=min(readed,(h-CurLine));
00170     if(readed==0)
00171       break;
00172 #endif
00173   }
00174   pBmp->Unlock();
00175   if(pBuf)
00176   {
00177     delete[] pBuf;
00178   }
00179 }
00180 
00181 void PLJPEGDecoder::decodeGray
00182     ( PLBmp * pBmp,
00183       int w,
00184       int h
00185     )
00186     // Assumes IJPEG decoder is already set up.
00187 {
00188   PLBYTE * pDst;
00189   int CurLine = 0;
00190   PLBYTE * pBuf = new PLBYTE [w];
00191   try
00192   {
00193     JSAMPARRAY ppBuf = &pBuf;
00194 
00195     pBmp->Create (w, h, 8, false);
00196 
00197     pBmp->Lock(false, true);
00198     PLBYTE ** pLineArray = pBmp->GetLineArray();
00199 
00200     while (CurLine < h)
00201     {
00202       ppBuf = &pDst;
00203       *ppBuf = pLineArray[CurLine];
00204       jpeg_read_scanlines (&cinfo, ppBuf, 1);
00205 
00206       CurLine++;
00207     }
00208     pBmp->Unlock();
00209   }
00210   catch(...)
00211   {
00212     delete [] pBuf;
00213     throw;
00214   }
00215   delete [] pBuf;
00216 }
00217 
00218 
00219 /*
00220  * Jo Hagelberg 15.4.99
00221  * progress notification callback
00222  * since this is a static function we need pDataSrc from cinfo->client_data
00223  * progress is calculated (0...1) and PaintLib's Notification called
00224  */
00225 
00226 void PLJPEGDecoder::JNotification (j_common_ptr cinfo)
00227 {
00228   double       part;
00229   PLDataSource *pDataSrc;
00230 
00231   /* calculated according to jpeg lib manual
00232    * note: this may not be precice when using buffered image mode
00233    * todo: think hard of alternatives 4 this case ... :-)
00234    */
00235   part = ( (double)cinfo->progress->completed_passes +
00236            ((double)cinfo->progress->pass_counter/cinfo->progress->pass_limit) ) /
00237          (double)cinfo->progress->total_passes;
00238 
00239   // call Notification in PLDataSource
00240   pDataSrc = (PLDataSource*) cinfo->client_data;
00241   if (pDataSrc)
00242   {
00243     pDataSrc->OProgressNotification( part);
00244   }
00245 
00246 }
00247 
00248 /*
00249 /--------------------------------------------------------------------
00250 |
00251 |      $Log: pljpegdec.cpp,v $
00252 |      Revision 1.1  2004/05/21 21:02:52  maxx
00253 |      Initial Version of vuVolume, moderatly changed to make it compile on my windows and linux machine.
00254 |
00255 |      Revision 1.1  2002/11/13 01:58:21  mspindle
00256 |      *** empty log message ***
00257 |
00258 |      Revision 1.4  2001/10/21 17:12:40  uzadow
00259 |      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
00260 |
00261 |      Revision 1.3  2001/10/16 17:12:26  uzadow
00262 |      Added support for resolution information (Luca Piergentili)
00263 |
00264 |      Revision 1.2  2001/10/06 22:37:08  uzadow
00265 |      Linux compatibility.
00266 |
00267 |      Revision 1.1  2001/09/16 19:03:22  uzadow
00268 |      Added global name prefix PL, changed most filenames.
00269 |
00270 |      Revision 1.19  2001/09/15 17:12:40  uzadow
00271 |      Added jpeg codec optimizations by Petr Kures.
00272 |
00273 |      Revision 1.18  2001/02/04 14:31:52  uzadow
00274 |      Member initialization list cleanup (Erik Hoffmann).
00275 |
00276 |      Revision 1.17  2001/01/21 14:28:21  uzadow
00277 |      Changed array cleanup from delete to delete[].
00278 |
00279 |      Revision 1.16  2000/12/18 22:42:52  uzadow
00280 |      Replaced RGBAPIXEL with PLPixel32.
00281 |
00282 |      Revision 1.15  2000/11/10 10:40:53  jmbuena
00283 |      Fixed jpeg decoder bug on GNU/Linux
00284 |
00285 |      Revision 1.5  2000/11/08 13:36:32  jmbuena
00286 |      Changes due to paintlib changes
00287 |
00288 |      Revision 1.14  2000/10/30 14:32:51  uzadow
00289 |      Removed dependency on jinclude.h
00290 |
00291 |      Revision 1.13  2000/10/27 11:58:25  jmbuena
00292 |      paintlib-config added, libjpeg and so on taked from the system ones, copied libs internal headers onto paintlib
00293 |
00294 |      Revision 1.12  2000/10/26 21:06:17  uzadow
00295 |      Removed dependency on jpegint.h
00296 |
00297 |      Revision 1.11  2000/10/24 23:00:09  uzadow
00298 |      Added byte order conversion.
00299 |
00300 |      Revision 1.10  2000/08/13 12:11:43  Administrator
00301 |      Added experimental DirectDraw-Support
00302 |
00303 |      Revision 1.9  2000/01/16 20:43:13  anonymous
00304 |      Removed MFC dependencies
00305 |
00306 |      Revision 1.8  1999/12/14 12:30:13  Ulrich von Zadow
00307 |      Corrected copy constructor and assignment operator.
00308 |
00309 |      Revision 1.7  1999/12/08 16:31:40  Ulrich von Zadow
00310 |      Unix compatibility
00311 |
00312 |      Revision 1.6  1999/12/08 15:39:45  Ulrich von Zadow
00313 |      Unix compatibility changes
00314 |
00315 |      Revision 1.5  1999/10/19 21:25:16  Ulrich von Zadow
00316 |      no message
00317 |
00318 |      Revision 1.4  1999/10/03 18:50:51  Ulrich von Zadow
00319 |      Added automatic logging of changes.
00320 |
00321 |
00322 \--------------------------------------------------------------------
00323 */

Generated on Wed Dec 15 21:20:30 2004 for vuVolume by  doxygen 1.3.9.1