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

Annotation of /trunk/PTD/wptJPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 33 - (hide annotations)
Tue Oct 25 07:46:20 2005 UTC (19 years, 4 months ago) by twoaday
File size: 6301 byte(s)
More bug fixes and cleanups.
See ChangeLog for details.

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

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26