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

Annotation of /trunk/PTD/wptJPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 261 - (hide annotations)
Sat Sep 30 10:24:23 2006 UTC (18 years, 5 months ago) by twoaday
File size: 6637 byte(s)


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

Properties

Name Value
svn:eol-style native

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26