/[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 2 by twoaday, Mon Jan 31 11:02:21 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 PTD.   * This file is part of WinPT.
6   *   *
7   * PTD 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   * PTD 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" /* Error Title (Related To This Class)...*/  #include <sys/stat.h>
44    
45    #include "wptJPG.h"
46  CJPG::CJPG ()  
47  {  #define HIMETRIC_INCH   2540
48      m_IPicture = NULL;  #define ERROR_TITLE     "CJPG Error"
49      m_Height = 0;  
50      m_Weight = 0;  #define out_of_core() do { \
51      m_Width = 0;          MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \
52  }          return FALSE; \
53        } while (0)
54    
55  CJPG::~CJPG ()  
56  {  CJPG::CJPG (void)
57      if (m_IPicture != NULL)  {
58          FreePictureData ();      m_IPicture = NULL;
59  }      m_Height = 0;
60        m_Weight = 0;
61        m_Width = 0;
62    }
63  /* Free The Allocated Memory That Holdes The IPicture Interface Data  
64     And Clear Picture Information. */  
65  void CJPG::FreePictureData()  CJPG::~CJPG (void)
66  {  {
67      if (m_IPicture != NULL) {      if (m_IPicture != NULL)
68          m_IPicture->Release();          freePictureData ();
69          m_IPicture = NULL;  }
70          m_Height = 0;  
71          m_Weight = 0;  
72          m_Width = 0;      
73      }  /* Free the allocated memory that holdes the IPicture Interface data
74  }     and clear picture information. */
75    void
76    CJPG::freePictureData (void)
77  /* Open a JPG File And Load It Into IPicture (Interface) */  {
78  BOOL CJPG::Load(LPCSTR sFilePathName)      if (m_IPicture != NULL) {
79  {          m_IPicture->Release();
80      BOOL bResult = FALSE;          m_IPicture = NULL;
81      FILE * f;          m_Height = 0;
82      int nSize = 0;          m_Weight = 0;
83            m_Width = 0;    
84      if (m_IPicture != NULL)      }
85          FreePictureData ();  }
86    
87      f = fopen (sFilePathName, "rb");  
88      if (f) {  /* Open a JPG File And Load It Into IPicture (Interface) */
89          struct stat st;  BOOL
90          fstat (fileno (f), &st);  CJPG::load (LPCSTR sFilePathName)
91          nSize = st.st_size;  {
92          BYTE* pBuffer = new BYTE[nSize];      BOOL bResult = FALSE;
93          if (fread(pBuffer, 1, nSize, f) > 0) {      FILE * f;
94              if (LoadPictureData (pBuffer, nSize))      int nSize = 0;
95                  bResult = TRUE;  
96          }      if (m_IPicture != NULL)
97          fclose (f);          freePictureData ();
98          delete [] pBuffer;  
99      }      f = fopen (sFilePathName, "rb");
100      else { /* Open Failed... */      if (f) {
101          MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK | MB_ICONSTOP);          struct stat st;
102          bResult = FALSE;                  fstat (fileno (f), &st);
103      }          nSize = st.st_size;
104            BYTE *pBuffer = new BYTE[nSize];
105      m_Weight = nSize; /* Update Picture Size Info... */          if  (!pBuffer) {
106                fclose (f);
107      if(m_IPicture != NULL) { /* Do Not Try To Read From Memory That Is Not Exist... */              out_of_core ();
108          m_IPicture->get_Height (&m_Height);          }
109          m_IPicture->get_Width (&m_Width);          memset (pBuffer, 0, nSize);
110          /* Calculate Its Size On a "Standard" (96 DPI) Device Context */          if (fread(pBuffer, 1, nSize, f) > 0) {
111          m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);              if (loadPictureData (pBuffer, nSize))
112          m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);                          bResult = TRUE;
113      }          }
114      else { /* Picture Data Is Not a Known Picture Type */          fclose (f);
115          m_Height = 0;          delete [] pBuffer;
116          m_Width = 0;      }
117          bResult = FALSE;              else {
118      }          MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);
119      return (bResult);          bResult = FALSE;
120  }      }
121    
122        m_Weight = nSize; /* Update Picture Size Info... */
123    
124  /* Read The Picture Data From a Source (File / Resource)      if(m_IPicture != NULL) {
125     And Load It Into The Current IPicture Object In Use */          m_IPicture->get_Height (&m_Height);
126  BOOL CJPG::LoadPictureData (BYTE *pBuffer, int nSize)          m_IPicture->get_Width (&m_Width);
127            /* Calculate Its Size On a "Standard" (96 DPI) Device Context */
128  {          m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH);
129      BOOL bResult = FALSE;          m_Width  = MulDiv (m_Width,  96, HIMETRIC_INCH);        
130      HGLOBAL hGlobal;      }
131      void* pData;      else {
132      IStream* pStream = NULL;          /* Picture data is not a known picture type */
133                m_Height = 0;
134      hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);          m_Width = 0;
135      if (hGlobal == NULL) {          bResult = FALSE;        
136          MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP);      }
137          return (FALSE);      return bResult;
138      }  }
139    
140      pData = GlobalLock (hGlobal);  
141      memcpy (pData, pBuffer, nSize);  
142      GlobalUnlock (hGlobal);  /* read the picture data from a source (file / resource)
143       and load it into the current IPicture object in use */
144      if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) {  BOOL
145          HRESULT hr;  CJPG::loadPictureData (BYTE *pBuffer, int nSize)
146          if ((hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE) {  
147              MessageBox (NULL, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP);  {
148              return(FALSE);      BOOL bResult = FALSE;
149          }      HGLOBAL hGlobal;
150          else { // S_OK      void* pData;
151              pStream->Release ();      IStream* pStream = NULL;
152              pStream = NULL;      
153              bResult = TRUE;      hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize);
154          }            if (hGlobal == NULL) {
155      }          out_of_core ();
156            return FALSE;
157      GlobalFree (hGlobal);      }
158      return (bResult);  
159  }      pData = GlobalLock (hGlobal);
160        memcpy (pData, pBuffer, nSize);
161        GlobalUnlock (hGlobal);
162  /* Draw The Loaded Picture Direct To The Client DC */  
163  BOOL CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, int MagnifyX, int MagnifyY)      if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) {
164            HRESULT hr;
165  {          hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture,
166      if (pDC == NULL || m_IPicture == NULL)                               (LPVOID *)&m_IPicture);
167          return FALSE;          if (hr == E_NOINTERFACE) {
168                    MessageBox (NULL, "IPicture interface is not supported",
169      long Width  = 0;                          ERROR_TITLE, MB_OK|MB_ICONSTOP);
170      long Height = 0;              return FALSE;
171      m_IPicture->get_Width (&Width);          }
172      m_IPicture->get_Height (&Height);          else { /* S_OK */
173                        pStream->Release ();
174      if (MagnifyX == NULL)              pStream = NULL;
175          MagnifyX = 0;              bResult = TRUE;
176      if (MagnifyY == NULL)          }      
177          MagnifyY = 0;      }
178      MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);  
179      MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);      GlobalFree (hGlobal);
180            return (bResult);
181      RECT DrawRect;  }
182      DrawRect.left = LeftTop->x;  
183      DrawRect.top = LeftTop->y;  
184      DrawRect.right = MagnifyX;  /* Draw the loaded picture direct to the client DC */
185      DrawRect.bottom = MagnifyY;  BOOL
186    CJPG::show (HDC pDC, POINT *LeftTop, POINT *WidthHeight,
187      HRESULT hrP = NULL;              int MagnifyX, int MagnifyY)
188    
189      hrP = m_IPicture->Render (pDC,  {
190                        LeftTop->x,               // Left      long Width  = 0;
191                        LeftTop->y,               // Top      long Height = 0;
192                        WidthHeight->x +MagnifyX, // Width  
193                        WidthHeight->y +MagnifyY, // Height      if (pDC == NULL || m_IPicture == NULL)
194                        0,          return FALSE;
195                        Height,  
196                        Width,      m_IPicture->get_Width (&Width);
197                        -Height,      m_IPicture->get_Height (&Height);
198                        &DrawRect);          
199        if (MagnifyX == 0)
200      if (SUCCEEDED (hrP))          MagnifyX = 0;
201          return (TRUE);      if (MagnifyY == 0)
202            MagnifyY = 0;
203      MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP);      MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
204      return (FALSE);      MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
205  }      
206        RECT DrawRect;
207        DrawRect.left = LeftTop->x;
208  /* Get The Original Picture Pixel Size (Ignor What Current DC Is Using)      DrawRect.top = LeftTop->y;
209     Pointer To a Device Context Is Needed For Pixel Calculation, */      DrawRect.right = MagnifyX;
210  BOOL CJPG::UpdateSizeOnDC(HDC pDC)      DrawRect.bottom = MagnifyY;
211    
212  {            HRESULT hrP = 0;
213      if(pDC == NULL || m_IPicture == NULL) {  
214          m_Height = 0;      hrP = m_IPicture->Render (pDC,
215          m_Width = 0;                        LeftTop->x,               // Left
216          return (FALSE);                        LeftTop->y,               // Top
217      }                        WidthHeight->x +MagnifyX, // Width
218                          WidthHeight->y +MagnifyY, // Height
219      m_IPicture->get_Height (&m_Height);                        0,
220      m_IPicture->get_Width (&m_Width);                        Height,
221                          Width,
222      // Get Current DPI - Dot Per Inch                        -Height,
223      int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX);                        &DrawRect);
224      int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY);  
225        if (SUCCEEDED (hrP))
226      m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);          return (TRUE);
227      m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);  
228        out_of_core ();
229      return (TRUE);  }
230  }  
231    
232    /* Get the original picture pixel size (ignore what current DC is using)
233  int     pointer to a Device Context is needed for pixel calculation, */
234  PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name)  BOOL
235  {  CJPG::updateSizeOnDC (HDC pDC)
236      CJPG jpg;  
237      HDC hdc;  {      
238      POINT p2;      if(pDC == NULL || m_IPicture == NULL) {
239      BOOL rc;          m_Height = 0;
240            m_Width = 0;
241      rc = jpg.Load (name);          return (FALSE);
242      if (!rc)      }
243          return -1;  
244      hdc = GetWindowDC (hwnd);      m_IPicture->get_Height (&m_Height);
245      rc = jpg.UpdateSizeOnDC (hdc);      m_IPicture->get_Width (&m_Width);
246      if (!rc) {  
247          ReleaseDC (hwnd, hdc);      /* Get Current DPI - Dot Per Inch */
248          return -2;      int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX);
249      }      int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY);
250    
251      p2.x = jpg.m_Width;      m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);
252      p2.y = jpg.m_Height;      m_Width  = MulDiv (m_Width,  CurrentDPI_X, HIMETRIC_INCH);
253      rc = jpg.Show (hdc, p, &p2, 0, 0);  
254        return (TRUE);
255      ReleaseDC (hwnd, hdc);  }
     jpg.FreePictureData ();  
     return rc;  
 }  

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26