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