/[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 45 by twoaday, Tue Oct 25 07:46:20 2005 UTC revision 46 by werner, Fri Oct 28 12:57:05 2005 UTC
# Line 1  Line 1 
1  /* wptJPG.cpp : JPG picture class  /* wptJPG.cpp : JPG picture class
2   *      Copyright (C) 2001 Dr.Yovav Gad <[email protected]>   *      Copyright (C) 2001 Dr.Yovav Gad <[email protected]>
3   *      Copyright (C) 2005 Timo Schulz   *      Copyright (C) 2005 Timo Schulz
4   *   *
5   * This file is part of WinPT.   * This file is part of WinPT.
6   *   *
7   * WinPT is free software; you can redistribute it and/or modify   * WinPT is free software; you can redistribute it and/or modify
8   * it under the terms of the GNU General Public License as published by   * it under the terms of the GNU General Public License as published by
9   * the Free Software Foundation; either version 2 of the License, or   * the Free Software Foundation; either version 2 of the License, or
10   * (at your option) any later version.   * (at your option) any later version.
11   *   *
12   * WinPT is distributed in the hope that it will be useful,   * WinPT is distributed in the hope that it will be useful,
13   * but WITHOUT ANY WARRANTY; without even the implied warranty of   * but WITHOUT ANY WARRANTY; without even the implied warranty of
14   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15   * GNU General Public License for more details.   * GNU General Public License for more details.
16   *   *
17   * You should have received a copy of the GNU General Public License   * You should have received a copy of the GNU General Public License
18   * along with PTD; if not, write to the Free Software Foundation,   * along with PTD; if not, write to the Free Software Foundation,
19   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA   * Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA
20   */   */
21    
22  /*-----------------------------------------------------------------------------  /*-----------------------------------------------------------------------------
23   * Picture (Implementations) Version 1.00   * Picture (Implementations) Version 1.00
24   *   *
25   * Routinges for showing JPG pictur files   * Routinges for showing JPG pictur files
26   *   *
27   * Author: Dr. Yovav Gad, EMail: [email protected] ,Web: www.SuperMain.com   * Author: Dr. Yovav Gad, EMail: [email protected] ,Web: www.SuperMain.com
28   *   *
29   * This version uses a stripped down version of Picture.cpp and Picture.h.   * This version uses a stripped down version of Picture.cpp and Picture.h.
30   */   */
31  #include <windows.h>  #ifdef HAVE_CONFIG_H
32  #include <ocidl.h>  #include <config.h>
33  #include <olectl.h>  #endif
34    
35  #include <stdio.h>  #include <windows.h>
36  #include <string.h>  #include <windows.h>
37  #include <errno.h>  #include <ocidl.h>
38  #include <sys/stat.h>  #include <olectl.h>
39    
40  #include "wptJPG.h"  #include <stdio.h>
41    #include <string.h>
42  #define HIMETRIC_INCH   2540  #include <errno.h>
43  #define ERROR_TITLE     "CJPG Error"  #include <sys/stat.h>
44    
45  #define out_of_core() do { \  #include "wptJPG.h"
46          MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \  
47          return FALSE; \  #define HIMETRIC_INCH   2540
48      } while (1)  #define ERROR_TITLE     "CJPG Error"
49    
50    #define out_of_core() do { \
51  CJPG::CJPG (void)          MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \
52  {          return FALSE; \
53      m_IPicture = NULL;      } while (0)
54      m_Height = 0;  
55      m_Weight = 0;  
56      m_Width = 0;  CJPG::CJPG (void)
57  }  {
58        m_IPicture = NULL;
59        m_Height = 0;
60  CJPG::~CJPG (void)      m_Weight = 0;
61  {      m_Width = 0;
62      if (m_IPicture != NULL)  }
63          freePictureData ();  
64  }  
65    CJPG::~CJPG (void)
66    {
67        if (m_IPicture != NULL)
68  /* Free the allocated memory that holdes the IPicture Interface data          freePictureData ();
69     and clear picture information. */  }
70  void  
71  CJPG::freePictureData (void)  
72  {  
73      if (m_IPicture != NULL) {  /* Free the allocated memory that holdes the IPicture Interface data
74          m_IPicture->Release();     and clear picture information. */
75          m_IPicture = NULL;  void
76          m_Height = 0;  CJPG::freePictureData (void)
77          m_Weight = 0;  {
78          m_Width = 0;          if (m_IPicture != NULL) {
79      }          m_IPicture->Release();
80  }          m_IPicture = NULL;
81            m_Height = 0;
82            m_Weight = 0;
83  /* Open a JPG File And Load It Into IPicture (Interface) */          m_Width = 0;    
84  BOOL      }
85  CJPG::load (LPCSTR sFilePathName)  }
86  {  
87      BOOL bResult = FALSE;  
88      FILE * f;  /* Open a JPG File And Load It Into IPicture (Interface) */
89      int nSize = 0;  BOOL
90    CJPG::load (LPCSTR sFilePathName)
91      if (m_IPicture != NULL)  {
92          freePictureData ();      BOOL bResult = FALSE;
93        FILE * f;
94      f = fopen (sFilePathName, "rb");      int nSize = 0;
95      if (f) {  
96          struct stat st;      if (m_IPicture != NULL)
97          fstat (fileno (f), &st);          freePictureData ();
98          nSize = st.st_size;  
99          BYTE *pBuffer = new BYTE[nSize];      f = fopen (sFilePathName, "rb");
100          if  (!pBuffer) {      if (f) {
101              fclose (f);          struct stat st;
102              out_of_core ();          fstat (fileno (f), &st);
103          }          nSize = st.st_size;
104          memset (pBuffer, 0, nSize);          BYTE *pBuffer = new BYTE[nSize];
105          if (fread(pBuffer, 1, nSize, f) > 0) {          if  (!pBuffer) {
106              if (loadPictureData (pBuffer, nSize))              fclose (f);
107                  bResult = TRUE;              out_of_core ();
108          }          }
109          fclose (f);          memset (pBuffer, 0, nSize);
110          delete [] pBuffer;          if (fread(pBuffer, 1, nSize, f) > 0) {
111      }              if (loadPictureData (pBuffer, nSize))
112      else {                  bResult = TRUE;
113          MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);          }
114          bResult = FALSE;          fclose (f);
115      }          delete [] pBuffer;
116        }
117      m_Weight = nSize; /* Update Picture Size Info... */      else {
118            MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);
119      if(m_IPicture != NULL) {          bResult = FALSE;
120          m_IPicture->get_Height (&m_Height);      }
121          m_IPicture->get_Width (&m_Width);  
122          /* Calculate Its Size On a "Standard" (96 DPI) Device Context */      m_Weight = nSize; /* Update Picture Size Info... */
123          m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);  
124          m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);              if(m_IPicture != NULL) {
125      }          m_IPicture->get_Height (&m_Height);
126      else {          m_IPicture->get_Width (&m_Width);
127          /* Picture data is not a known picture type */          /* Calculate Its Size On a "Standard" (96 DPI) Device Context */
128          m_Height = 0;          m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);
129          m_Width = 0;          m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);        
130          bResult = FALSE;              }
131      }      else {
132      return bResult;          /* Picture data is not a known picture type */
133  }          m_Height = 0;
134            m_Width = 0;
135            bResult = FALSE;        
136        }
137  /* read the picture data from a source (file / resource)      return bResult;
138     and load it into the current IPicture object in use */  }
139  BOOL  
140  CJPG::loadPictureData (BYTE *pBuffer, int nSize)  
141    
142  {  /* read the picture data from a source (file / resource)
143      BOOL bResult = FALSE;     and load it into the current IPicture object in use */
144      HGLOBAL hGlobal;  BOOL
145      void* pData;  CJPG::loadPictureData (BYTE *pBuffer, int nSize)
146      IStream* pStream = NULL;  
147        {
148      hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);      BOOL bResult = FALSE;
149      if (hGlobal == NULL) {      HGLOBAL hGlobal;
150          out_of_core ();      void* pData;
151          return FALSE;      IStream* pStream = NULL;
152      }      
153        hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);
154      pData = GlobalLock (hGlobal);      if (hGlobal == NULL) {
155      memcpy (pData, pBuffer, nSize);          out_of_core ();
156      GlobalUnlock (hGlobal);          return FALSE;
157        }
158      if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) {  
159          HRESULT hr;      pData = GlobalLock (hGlobal);
160          hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture,      memcpy (pData, pBuffer, nSize);
161                               (LPVOID *)&m_IPicture);      GlobalUnlock (hGlobal);
162          if (hr == E_NOINTERFACE) {  
163              MessageBox (NULL, "IPicture interface is not supported",      if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) {
164                          ERROR_TITLE, MB_OK|MB_ICONSTOP);          HRESULT hr;
165              return FALSE;          hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture,
166          }                               (LPVOID *)&m_IPicture);
167          else { /* S_OK */          if (hr == E_NOINTERFACE) {
168              pStream->Release ();              MessageBox (NULL, "IPicture interface is not supported",
169              pStream = NULL;                          ERROR_TITLE, MB_OK|MB_ICONSTOP);
170              bResult = TRUE;              return FALSE;
171          }                }
172      }          else { /* S_OK */
173                pStream->Release ();
174      GlobalFree (hGlobal);              pStream = NULL;
175      return (bResult);              bResult = TRUE;
176  }          }      
177        }
178    
179  /* Draw the loaded picture direct to the client DC */      GlobalFree (hGlobal);
180  BOOL      return (bResult);
181  CJPG::show (HDC pDC, POINT *LeftTop, POINT *WidthHeight,  }
182              int MagnifyX, int MagnifyY)  
183    
184  {  /* Draw the loaded picture direct to the client DC */
185      long Width  = 0;  BOOL
186      long Height = 0;  CJPG::show (HDC pDC, POINT *LeftTop, POINT *WidthHeight,
187                int MagnifyX, int MagnifyY)
188      if (pDC == NULL || m_IPicture == NULL)  
189          return FALSE;  {
190        long Width  = 0;
191      m_IPicture->get_Width (&Width);      long Height = 0;
192      m_IPicture->get_Height (&Height);  
193                if (pDC == NULL || m_IPicture == NULL)
194      if (MagnifyX == 0)          return FALSE;
195          MagnifyX = 0;  
196      if (MagnifyY == 0)      m_IPicture->get_Width (&Width);
197          MagnifyY = 0;      m_IPicture->get_Height (&Height);
198      MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);          
199      MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);      if (MagnifyX == 0)
200                MagnifyX = 0;
201      RECT DrawRect;      if (MagnifyY == 0)
202      DrawRect.left = LeftTop->x;          MagnifyY = 0;
203      DrawRect.top = LeftTop->y;      MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
204      DrawRect.right = MagnifyX;      MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
205      DrawRect.bottom = MagnifyY;      
206        RECT DrawRect;
207      HRESULT hrP = NULL;      DrawRect.left = LeftTop->x;
208        DrawRect.top = LeftTop->y;
209      hrP = m_IPicture->Render (pDC,      DrawRect.right = MagnifyX;
210                        LeftTop->x,               // Left      DrawRect.bottom = MagnifyY;
211                        LeftTop->y,               // Top  
212                        WidthHeight->x +MagnifyX, // Width      HRESULT hrP = 0;
213                        WidthHeight->y +MagnifyY, // Height  
214                        0,      hrP = m_IPicture->Render (pDC,
215                        Height,                        LeftTop->x,               // Left
216                        Width,                        LeftTop->y,               // Top
217                        -Height,                        WidthHeight->x +MagnifyX, // Width
218                        &DrawRect);                        WidthHeight->y +MagnifyY, // Height
219                          0,
220      if (SUCCEEDED (hrP))                        Height,
221          return (TRUE);                        Width,
222                          -Height,
223      out_of_core ();                        &DrawRect);
224  }  
225        if (SUCCEEDED (hrP))
226            return (TRUE);
227  /* Get the original picture pixel size (ignore what current DC is using)  
228     pointer to a Device Context is needed for pixel calculation, */      out_of_core ();
229  BOOL  }
230  CJPG::updateSizeOnDC (HDC pDC)  
231    
232  {        /* Get the original picture pixel size (ignore what current DC is using)
233      if(pDC == NULL || m_IPicture == NULL) {     pointer to a Device Context is needed for pixel calculation, */
234          m_Height = 0;  BOOL
235          m_Width = 0;  CJPG::updateSizeOnDC (HDC pDC)
236          return (FALSE);  
237      }  {      
238        if(pDC == NULL || m_IPicture == NULL) {
239      m_IPicture->get_Height (&m_Height);          m_Height = 0;
240      m_IPicture->get_Width (&m_Width);          m_Width = 0;
241            return (FALSE);
242      /* Get Current DPI - Dot Per Inch */      }
243      int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX);  
244      int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY);      m_IPicture->get_Height (&m_Height);
245        m_IPicture->get_Width (&m_Width);
246      m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);  
247      m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);      /* Get Current DPI - Dot Per Inch */
248        int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX);
249      return (TRUE);      int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY);
250  }  
251        m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);
252        m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);
253    
254        return (TRUE);
255    }

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26