/[winpt]/trunk/PTD/wptJPG.cpp
ViewVC logotype

Diff of /trunk/PTD/wptJPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 46 by werner, Fri Oct 28 12:57:05 2005 UTC revision 261 by twoaday, Sat Sep 30 10:24:23 2006 UTC
# Line 1  Line 1 
1  /* wptJPG.cpp : JPG picture class  /* wptJPG.cpp - Routines for showing JPG pictures
2   *      Copyright (C) 2001 Dr.Yovav Gad <[email protected]>   *      Copyright (C) 2001 Dr.Yovav Gad
3   *      Copyright (C) 2005 Timo Schulz   *      Copyright (C) 2005, 2006 Timo Schulz
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
# Line 21  Line 21 
21    
22  /*-----------------------------------------------------------------------------  /*-----------------------------------------------------------------------------
23   * Picture (Implementations) Version 1.00   * Picture (Implementations) Version 1.00
24   *   * Author: Dr. Yovav Gad, EMail: [email protected]
25   * Routinges for showing JPG pictur files   * Web: www.SuperMain.com
  *  
  * Author: Dr. Yovav Gad, EMail: [email protected] ,Web: www.SuperMain.com  
26   *   *
27   * This version uses a stripped down version of Picture.cpp and Picture.h.   * This version uses a stripped down and heavily modified version of
28     * Picture.cpp and Picture.h.
29   */   */
30  #ifdef HAVE_CONFIG_H  #ifdef HAVE_CONFIG_H
31  #include <config.h>  #include <config.h>
32  #endif  #endif
33    
34  #include <windows.h>  #include <windows.h>
 #include <windows.h>  
35  #include <ocidl.h>  #include <ocidl.h>
36  #include <olectl.h>  #include <olectl.h>
37    
# Line 45  Line 43 
43  #include "wptJPG.h"  #include "wptJPG.h"
44    
45  #define HIMETRIC_INCH   2540  #define HIMETRIC_INCH   2540
46  #define ERROR_TITLE     "CJPG Error"  #define ERROR_TITLE     "WinPT - CJPG Error"
47    
48  #define out_of_core() do { \  #define out_of_core() do { \
49          MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \          MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \
# Line 53  Line 51 
51      } while (0)      } while (0)
52    
53    
54    /* Constructor to create an empty JPG container. */
55  CJPG::CJPG (void)  CJPG::CJPG (void)
56  {  {
57      m_IPicture = NULL;      m_IPicture = NULL;
58      m_Height = 0;      m_Height = 0;
59      m_Weight = 0;      m_Weight = 0;
60      m_Width = 0;      m_Width = 0;    
61  }  }
62    
63    
64    /* Deconstructor. Free all internal data. */
65  CJPG::~CJPG (void)  CJPG::~CJPG (void)
66  {  {
67      if (m_IPicture != NULL)      if (m_IPicture != NULL)
# Line 78  CJPG::freePictureData (void) Line 78  CJPG::freePictureData (void)
78      if (m_IPicture != NULL) {      if (m_IPicture != NULL) {
79          m_IPicture->Release();          m_IPicture->Release();
80          m_IPicture = NULL;          m_IPicture = NULL;
         m_Height = 0;  
         m_Weight = 0;  
         m_Width = 0;      
81      }      }
82        m_Height = 0;
83        m_Weight = 0;
84        m_Width = 0;
85  }  }
86    
87    
# Line 89  CJPG::freePictureData (void) Line 89  CJPG::freePictureData (void)
89  BOOL  BOOL
90  CJPG::load (LPCSTR sFilePathName)  CJPG::load (LPCSTR sFilePathName)
91  {  {
92      BOOL bResult = FALSE;      struct stat st;
93      FILE * f;      FILE *fp;    
94      int nSize = 0;      BYTE *pBuffer;
95        int n;
96    
97      if (m_IPicture != NULL)      if (m_IPicture != NULL)
98          freePictureData ();          freePictureData ();
99    
100      f = fopen (sFilePathName, "rb");      fp = fopen (sFilePathName, "rb");
101      if (f) {      if (!fp) {
         struct stat st;  
         fstat (fileno (f), &st);  
         nSize = st.st_size;  
         BYTE *pBuffer = new BYTE[nSize];  
         if  (!pBuffer) {  
             fclose (f);  
             out_of_core ();  
         }  
         memset (pBuffer, 0, nSize);  
         if (fread(pBuffer, 1, nSize, f) > 0) {  
             if (loadPictureData (pBuffer, nSize))  
                 bResult = TRUE;  
         }  
         fclose (f);  
         delete [] pBuffer;  
     }  
     else {  
102          MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);          MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);
103          bResult = FALSE;          return FALSE;
104      }      }
105    
106      m_Weight = nSize; /* Update Picture Size Info... */      /* avoid to load empty JPG files and make sure we still can
107           access the file handle. */
108      if(m_IPicture != NULL) {      if (fstat (fileno (fp), &st) || st.st_size == 0) {
109          m_IPicture->get_Height (&m_Height);          fclose (fp);
110          m_IPicture->get_Width (&m_Width);          return FALSE;
111          /* Calculate Its Size On a "Standard" (96 DPI) Device Context */      }
112          m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);      pBuffer = new BYTE[st.st_size];
113          m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);              if  (!pBuffer)
114            out_of_core ();
115        memset (pBuffer, 0, st.st_size);
116        n = fread (pBuffer, 1, st.st_size, fp);
117        fclose (fp);
118    
119        /* not the entire file were read in, so abort here. */
120        if (n != st.st_size) {
121            delete []pBuffer;
122            return FALSE;
123      }      }
124      else {      if (!loadPictureData (pBuffer, st.st_size)) {
125          /* Picture data is not a known picture type */          delete []pBuffer;
126            return FALSE;
127        }
128        delete [] pBuffer;
129    
130        m_Weight = st.st_size; /* Update Picture Size Info... */
131        if (m_IPicture == NULL) {
132          m_Height = 0;          m_Height = 0;
133          m_Width = 0;          m_Width = 0;
134          bResult = FALSE;                  return FALSE;
135      }      }
136      return bResult;      m_IPicture->get_Height (&m_Height);
137        m_IPicture->get_Width (&m_Width);
138        /* Calculate Its Size On a "Standard" (96 DPI) Device Context */
139        m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);
140        m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);    
141        return TRUE;
142  }  }
143    
144    
# Line 145  BOOL Line 149  BOOL
149  CJPG::loadPictureData (BYTE *pBuffer, int nSize)  CJPG::loadPictureData (BYTE *pBuffer, int nSize)
150    
151  {  {
152      BOOL bResult = FALSE;      HGLOBAL hGlobal;    
     HGLOBAL hGlobal;  
     void* pData;  
153      IStream* pStream = NULL;      IStream* pStream = NULL;
154        void* pData;
155        BOOL bResult = FALSE;
156            
157      hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);      hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);
158      if (hGlobal == NULL) {      if (hGlobal == NULL)
159          out_of_core ();          out_of_core ();
         return FALSE;  
     }  
160    
161      pData = GlobalLock (hGlobal);      pData = GlobalLock (hGlobal);
162      memcpy (pData, pBuffer, nSize);      memcpy (pData, pBuffer, nSize);
# Line 164  CJPG::loadPictureData (BYTE *pBuffer, in Line 166  CJPG::loadPictureData (BYTE *pBuffer, in
166          HRESULT hr;          HRESULT hr;
167          hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture,          hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture,
168                               (LPVOID *)&m_IPicture);                               (LPVOID *)&m_IPicture);
169          if (hr == E_NOINTERFACE) {          if (hr == E_NOINTERFACE)
170              MessageBox (NULL, "IPicture interface is not supported",              MessageBox (NULL, "IPicture interface is not supported",
171                          ERROR_TITLE, MB_OK|MB_ICONSTOP);                          ERROR_TITLE, MB_OK|MB_ICONSTOP);
             return FALSE;  
         }  
172          else { /* S_OK */          else { /* S_OK */
173              pStream->Release ();              pStream->Release ();
174              pStream = NULL;              pStream = NULL;
# Line 177  CJPG::loadPictureData (BYTE *pBuffer, in Line 177  CJPG::loadPictureData (BYTE *pBuffer, in
177      }      }
178    
179      GlobalFree (hGlobal);      GlobalFree (hGlobal);
180      return (bResult);      return bResult;
181  }  }
182    
183    
184  /* Draw the loaded picture direct to the client DC */  /* Draw the loaded picture direct to the client DC */
185  BOOL  BOOL
186  CJPG::show (HDC pDC, POINT *LeftTop, POINT *WidthHeight,  CJPG::show (HDC pDC, POINT *leftTop, POINT *widthHeight,
187              int MagnifyX, int MagnifyY)              int magnifyX, int magnifyY)
188    
189  {  {
190      long Width  = 0;      RECT drawRect;
191      long Height = 0;      HRESULT hr = 0;
192        long width  = 0;
193        long height = 0;    
194    
195      if (pDC == NULL || m_IPicture == NULL)      if (pDC == NULL || m_IPicture == NULL)
196          return FALSE;          return FALSE;
197    
198      m_IPicture->get_Width (&Width);      m_IPicture->get_Width (&width);
199      m_IPicture->get_Height (&Height);      m_IPicture->get_Height (&height);
200                    
201      if (MagnifyX == 0)      if (magnifyX == 0)
202          MagnifyX = 0;          magnifyX = 0;
203      if (MagnifyY == 0)      if (magnifyY == 0)
204          MagnifyY = 0;          magnifyY = 0;
205      MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);      magnifyX = int(MulDiv (width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * magnifyX);
206      MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);      magnifyY = int(MulDiv (height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * magnifyY);
207        
208      RECT DrawRect;      drawRect.left = leftTop->x;
209      DrawRect.left = LeftTop->x;      drawRect.top = leftTop->y;
210      DrawRect.top = LeftTop->y;      drawRect.right = magnifyX;
211      DrawRect.right = MagnifyX;      drawRect.bottom = magnifyY;
212      DrawRect.bottom = MagnifyY;  
213        hr = m_IPicture->Render (pDC,
214      HRESULT hrP = 0;                        leftTop->x,               // Left
215                          leftTop->y,               // Top
216      hrP = m_IPicture->Render (pDC,                        widthHeight->x +magnifyX, // Width
217                        LeftTop->x,               // Left                        widthHeight->y +magnifyY, // Height
                       LeftTop->y,               // Top  
                       WidthHeight->x +MagnifyX, // Width  
                       WidthHeight->y +MagnifyY, // Height  
218                        0,                        0,
219                        Height,                        height,
220                        Width,                        width,
221                        -Height,                        -height,
222                        &DrawRect);                        &drawRect);
223    
224      if (SUCCEEDED (hrP))      if (SUCCEEDED (hr))
225          return (TRUE);          return TRUE;
226    
227      out_of_core ();      return FALSE;
228  }  }
229    
230    
# Line 238  CJPG::updateSizeOnDC (HDC pDC) Line 237  CJPG::updateSizeOnDC (HDC pDC)
237      if(pDC == NULL || m_IPicture == NULL) {      if(pDC == NULL || m_IPicture == NULL) {
238          m_Height = 0;          m_Height = 0;
239          m_Width = 0;          m_Width = 0;
240          return (FALSE);          return FALSE;
241      }      }
242    
243      m_IPicture->get_Height (&m_Height);      m_IPicture->get_Height (&m_Height);
# Line 251  CJPG::updateSizeOnDC (HDC pDC) Line 250  CJPG::updateSizeOnDC (HDC pDC)
250      m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);      m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);
251      m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);      m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);
252    
253      return (TRUE);      return TRUE;
254    }
255    
256    /* Return height of the current image. */
257    LONG
258    CJPG::getHeight (void)
259    {
260        return m_Height;
261    }
262    
263    
264    /* Return weight of the current image. */
265    LONG
266    CJPG::getWeight (void)
267    {
268        return m_Weight;
269    }
270    
271    
272    /* Return width of the current image. */
273    LONG
274    CJPG::getWidth (void)
275    {
276        return m_Width;
277  }  }

Legend:
Removed from v.46  
changed lines
  Added in v.261

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26