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

pljpegenc.cpp

Go to the documentation of this file.
00001 /*
00002 /--------------------------------------------------------------------
00003 |
00004 |      $Id: pljpegenc.cpp,v 1.1 2004/05/21 21:02:52 maxx Exp $
00005 |
00006 |      JPEG file encoder. Uses the independent JPEG group's library
00007 |      to do the actual conversion.
00008 |
00009 |      Copyright (c) 1996-1998 Ulrich von Zadow
00010 |
00011 \--------------------------------------------------------------------
00012 */
00013 
00014 #include "plstdpch.h"
00015 
00016 #include <stdio.h>
00017 
00018 extern "C"
00019 {
00020 #include "jpeglib.h"
00021 }
00022 
00023 #include "pljpegenc.h"
00024 #include "plexcept.h"
00025 #include "jmemdest.h"
00026 
00027 // This is for RGB_RED, RGB_GREEN, RGB_BLUE, RGB_PIXELSIZE
00028 #define JPEG_INTERNALS
00029 #include <jmorecfg.h>
00030 
00031 
00033 // Error handling.
00034 
00035 METHODDEF(void)
00036 error_exit (j_common_ptr pcinfo)
00037 // This procedure is called by the IJPEG library when an error
00038 // occurs.
00039 {
00040   /* Create the message string */
00041   char sz[256];
00042   (pcinfo->err->format_message) (pcinfo, sz);
00043   strcat (sz, "\n");
00044 
00045   PLPicEncoder::raiseError (PL_ERRFORMAT_NOT_SUPPORTED, sz);
00046 }
00047 
00049 // Class functions
00050 
00051 PLJPEGEncoder::PLJPEGEncoder
00052     ()
00053     : PLPicEncoder(),
00054       m_pcinfo ( new jpeg_compress_struct ),
00055       m_pjerr ( new jpeg_error_mgr ),   // Custom error manager.
00056       iQuality_ (0),
00057       bOptimizeCoding_ (false),
00058       iSmoothingFactor_ (0),
00059       uiDensityX_ (0),
00060       uiDensityY_ (0)
00061     // Creates an encoder
00062 {
00063   m_pcinfo->err = jpeg_std_error (m_pjerr);
00064   m_pjerr->error_exit = error_exit;  // Register custom error manager.
00065 
00066   jpeg_create_compress (m_pcinfo);
00067 }
00068 
00069 
00070 PLJPEGEncoder::~PLJPEGEncoder
00071     ()
00072 {
00073   jpeg_destroy_compress (m_pcinfo);
00074   delete m_pjerr;
00075   delete m_pcinfo;
00076 }
00077 
00078 void PLJPEGEncoder::DoEncode
00079     ( PLBmp * pBmp,
00080       PLDataSink * pDataSink
00081     )
00082 {
00083   PLASSERT (pBmp->GetBitsPerPixel() == 32); // Only true-color supported for now.
00084   try
00085   {
00086   // todo: notification not yet implemented for encoders (3.6.99 MS)
00087   /*
00088     JMETHOD( void, notify, (j_common_ptr));
00089     notify = JNotification;
00090   */
00091 
00092     // Initialize custom data destination
00093     jpeg_mem_dest(m_pcinfo, pDataSink->GetBufferPtr(), pDataSink->GetMaxDataSize(), pDataSink);
00094 
00095     // Set Header Fields
00096     m_pcinfo->image_width = pBmp->GetWidth();
00097     m_pcinfo->image_height = pBmp->GetHeight();
00098 
00099     m_pcinfo->input_components = RGB_PIXELSIZE; // Constant from  libjpeg
00100     m_pcinfo->in_color_space = JCS_RGB;
00101 
00102     jpeg_set_defaults (m_pcinfo);
00103 
00104     m_pcinfo->X_density = pBmp->GetResolution().x;
00105     m_pcinfo->Y_density = pBmp->GetResolution().y;
00106 
00107     // on good FPUs (e.g. Pentium) this is the fastest and "best" DCT method
00108     m_pcinfo->dct_method = JDCT_FLOAT;
00109 
00110     // set up user settings
00111     if (iQuality_)
00112         jpeg_set_quality(m_pcinfo, iQuality_, true);
00113     m_pcinfo->optimize_coding = bOptimizeCoding_;
00114     m_pcinfo->smoothing_factor = iSmoothingFactor_;
00115     // density will be in DPI
00116     if (uiDensityX_ || uiDensityY_)
00117     {
00118         m_pcinfo->density_unit = 1;
00119         m_pcinfo->X_density = uiDensityX_;
00120         m_pcinfo->Y_density = uiDensityY_;
00121     }
00122 
00123     jpeg_start_compress (m_pcinfo,true);
00124 
00125     encodeRGB (pBmp, pBmp->GetHeight());
00126 
00127     jpeg_finish_compress (m_pcinfo);
00128   }
00129   catch (PLTextException)
00130   {
00131     jpeg_abort_compress(m_pcinfo);
00132     throw;
00133   }
00134 
00135 }
00136 
00137 // กกก RGB_PIXELSIZE constant from libjpeg !!!
00138 // กกก RGB_RED, RGB_GREEN, RGB_BLUE constants from libjpeg !!!
00139 void PLJPEGEncoder::encodeRGB
00140     ( PLBmp * pBmp,
00141       int iScanLines
00142     )
00143     // Assumes IJPEG decoder is already set up.
00144 {
00145 
00146   PLBYTE * pBuf = NULL;
00147   int CurLine = 0;
00148   JSAMPARRAY ppBuf = &pBuf;
00149 
00150   pBmp->Lock(true, false);
00151   PLPixel32 ** pLineArray = pBmp->GetLineArray32();
00152 
00153 #if ((PL_RGBA_RED!=RGB_RED)||(PL_RGBA_GREEN!=RGB_GREEN)||(PL_RGBA_BLUE!=RGB_BLUE)||(4!=RGB_PIXELSIZE))
00154   pBuf = new PLBYTE [pBmp->GetWidth()*RGB_PIXELSIZE];
00155 #endif
00156 
00157   int written = 0;
00158   while (CurLine < iScanLines)
00159   {
00160 #if ((PL_RGBA_RED!=RGB_RED)||(PL_RGBA_GREEN!=RGB_GREEN)||(PL_RGBA_BLUE!=RGB_BLUE)||(4!=RGB_PIXELSIZE))
00161     int i;
00162     for (i=0;i<pBmp->GetWidth();i++)
00163     {
00164 
00165       PLPixel32 * pSrcPixel = pLineArray[CurLine]+i;
00166       PLBYTE * pDestPixel = pBuf+i*RGB_PIXELSIZE;
00167       pDestPixel[RGB_RED] = pSrcPixel->GetR();
00168       pDestPixel[RGB_GREEN] = pSrcPixel->GetG();
00169       pDestPixel[RGB_BLUE] = pSrcPixel->GetB();
00170     }
00171     jpeg_write_scanlines (m_pcinfo, ppBuf, 1);
00172     CurLine++;
00173 #else
00174     written = jpeg_write_scanlines (m_pcinfo,&pLineArray[CurLine], 32);
00175     CurLine+=written;
00176 
00177     if(!written)
00178       break;
00179 #endif
00180   }
00181   pBmp->Unlock();
00182   if(pBuf)
00183   {
00184     delete[] pBuf;
00185   }
00186 }
00187 
00189 
00190 void PLJPEGEncoder::SetQuality(int iQuality)
00191 {
00192   iQuality_ = iQuality;
00193 }
00194 
00195 void PLJPEGEncoder::SetOptimizeCoding(bool bOptimizeCoding)
00196 {
00197   bOptimizeCoding_ = bOptimizeCoding;
00198 }
00199 
00200 void PLJPEGEncoder::SetSmoothingFactor(int iSmoothingFactor)
00201 {
00202   iSmoothingFactor_ = iSmoothingFactor;
00203 }
00204 
00205 void PLJPEGEncoder::SetDensity(unsigned int uiX, unsigned int uiY)
00206 {
00207   uiDensityX_ = uiX;
00208   uiDensityY_ = uiY;
00209 }
00210 
00211 /*
00212 /--------------------------------------------------------------------
00213 |
00214 |      $Log: pljpegenc.cpp,v $
00215 |      Revision 1.1  2004/05/21 21:02:52  maxx
00216 |      Initial Version of vuVolume, moderatly changed to make it compile on my windows and linux machine.
00217 |
00218 |      Revision 1.1  2002/11/13 01:58:21  mspindle
00219 |      *** empty log message ***
00220 |
00221 |      Revision 1.5  2001/10/21 17:12:40  uzadow
00222 |      Added PSD decoder beta, removed BPPWanted from all decoders, added PLFilterPixel.
00223 |
00224 |      Revision 1.4  2001/10/16 17:12:26  uzadow
00225 |      Added support for resolution information (Luca Piergentili)
00226 |
00227 |      Revision 1.3  2001/10/06 22:37:08  uzadow
00228 |      Linux compatibility.
00229 |
00230 |      Revision 1.2  2001/10/06 20:44:45  uzadow
00231 |      Linux compatibility
00232 |
00233 |      Revision 1.1  2001/09/16 19:03:22  uzadow
00234 |      Added global name prefix PL, changed most filenames.
00235 |
00236 |      Revision 1.14  2001/09/15 21:02:44  uzadow
00237 |      Cleaned up stdpch.h and config.h to make them internal headers.
00238 |
00239 |      Revision 1.13  2001/09/15 17:12:40  uzadow
00240 |      Added jpeg codec optimizations by Petr Kures.
00241 |
00242 |      Revision 1.12  2001/02/04 14:31:52  uzadow
00243 |      Member initialization list cleanup (Erik Hoffmann).
00244 |
00245 |      Revision 1.11  2000/12/18 22:42:52  uzadow
00246 |      Replaced RGBAPIXEL with PLPixel32.
00247 |
00248 |      Revision 1.10  2000/11/10 10:41:15  jmbuena
00249 |      Fixed jpeg encoder bug on GNU/Linux
00250 |
00251 |      Revision 1.5  2000/11/08 13:36:32  jmbuena
00252 |      Changes due to paintlib changes
00253 |
00254 |      Revision 1.9  2000/10/26 21:06:17  uzadow
00255 |      Removed dependency on jpegint.h
00256 |
00257 |      Revision 1.8  2000/10/25 13:58:22  jmbuena
00258 |      Fixed a non defined symbol problem for GNU/Linux
00259 |
00260 |      Revision 1.7  2000/10/24 23:00:09  uzadow
00261 |      Added byte order conversion.
00262 |
00263 |      Revision 1.6  2000/08/13 12:11:43  Administrator
00264 |      Added experimental DirectDraw-Support
00265 |
00266 |      Revision 1.5  2000/07/11 17:11:01  Ulrich von Zadow
00267 |      Added support for RGBA pixel ordering (Jose Miguel Buenaposada Biencinto).
00268 |
00269 |      Revision 1.4  2000/05/22 17:43:25  Ulrich von Zadow
00270 |      Added SetQuality(), SetDensity(), SetOptimizeCoding() and
00271 |      SetSmoothingFactor().
00272 |
00273 |      Revision 1.3  2000/01/16 20:43:13  anonymous
00274 |      Removed MFC dependencies
00275 |
00276 |      Revision 1.2  1999/12/08 15:39:45  Ulrich von Zadow
00277 |      Unix compatibility changes
00278 |
00279 |      Revision 1.1  1999/10/19 21:28:05  Ulrich von Zadow
00280 |      Added jpeg encoder
00281 |
00282 |
00283 \--------------------------------------------------------------------
00284 */

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