1 |
twoaday |
340 |
/* wptJPG.cpp - Routines for showing JPG pictures |
2 |
|
|
* Copyright (C) 2005-2006, 2008-2009, 2011 Timo Schulz |
3 |
|
|
* Copyright (C) 2001 Dr.Yovav Gad |
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 |
|
|
|
18 |
|
|
/*----------------------------------------------------------------------------- |
19 |
|
|
* Picture (Implementations) Version 1.00 |
20 |
|
|
* Author: Dr. Yovav Gad, EMail: [email protected] |
21 |
|
|
* Web: www.SuperMain.com |
22 |
|
|
* |
23 |
|
|
* This version uses a stripped down and heavily modified version of |
24 |
|
|
* Picture.cpp and Picture.h. |
25 |
|
|
*/ |
26 |
|
|
#ifdef HAVE_CONFIG_H |
27 |
|
|
#include <config.h> |
28 |
|
|
#endif |
29 |
|
|
|
30 |
|
|
#include <windows.h> |
31 |
|
|
#include <ocidl.h> |
32 |
|
|
#include <olectl.h> |
33 |
|
|
|
34 |
|
|
#include <stdio.h> |
35 |
|
|
#include <string.h> |
36 |
|
|
#include <errno.h> |
37 |
|
|
#include <sys/stat.h> |
38 |
|
|
|
39 |
|
|
#include "wptJPG.h" |
40 |
|
|
#include "wptErrors.h" |
41 |
|
|
#include "wptTypes.h" |
42 |
|
|
|
43 |
|
|
#define HIMETRIC_INCH 2540 |
44 |
|
|
#define ERROR_TITLE "WinPT - CJPG Error" |
45 |
|
|
|
46 |
|
|
|
47 |
|
|
/* Constructor to create an empty JPG container. */ |
48 |
|
|
CJPG::CJPG (void) |
49 |
|
|
{ |
50 |
|
|
m_IPicture = NULL; |
51 |
|
|
m_Height = 0; |
52 |
|
|
m_Weight = 0; |
53 |
|
|
m_Width = 0; |
54 |
|
|
} |
55 |
|
|
|
56 |
|
|
|
57 |
|
|
/* Deconstructor. Free all internal data. */ |
58 |
|
|
CJPG::~CJPG (void) |
59 |
|
|
{ |
60 |
|
|
if (m_IPicture != NULL) |
61 |
|
|
freePictureData (); |
62 |
|
|
} |
63 |
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
/* Free the allocated memory that holdes the IPicture Interface data |
67 |
|
|
and clear picture information. */ |
68 |
|
|
void |
69 |
|
|
CJPG::freePictureData (void) |
70 |
|
|
{ |
71 |
|
|
if (m_IPicture != NULL) { |
72 |
|
|
m_IPicture->Release(); |
73 |
|
|
m_IPicture = NULL; |
74 |
|
|
} |
75 |
|
|
m_Height = 0; |
76 |
|
|
m_Weight = 0; |
77 |
|
|
m_Width = 0; |
78 |
|
|
} |
79 |
|
|
|
80 |
|
|
|
81 |
|
|
/* Open a JPG File And Load It Into IPicture (Interface) */ |
82 |
|
|
BOOL |
83 |
|
|
CJPG::load (LPCSTR filepath) |
84 |
|
|
{ |
85 |
|
|
FILE *fp; |
86 |
|
|
|
87 |
|
|
if (m_IPicture != NULL) |
88 |
|
|
freePictureData (); |
89 |
|
|
|
90 |
|
|
fp = fopen (filepath, "rb"); |
91 |
|
|
if (!fp) { |
92 |
|
|
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
93 |
|
|
return FALSE; |
94 |
|
|
} |
95 |
|
|
|
96 |
|
|
/* avoid to load empty JPG files and make sure we still can |
97 |
|
|
access the file handle. */ |
98 |
|
|
struct stat st; |
99 |
|
|
if (fstat (fileno (fp), &st) || st.st_size == 0) { |
100 |
|
|
fclose (fp); |
101 |
|
|
return FALSE; |
102 |
|
|
} |
103 |
|
|
BYTE *buffer = new BYTE[st.st_size]; |
104 |
|
|
if (!buffer) |
105 |
|
|
BUG (0); |
106 |
|
|
memset (buffer, 0, st.st_size); |
107 |
|
|
int n = fread (buffer, 1, st.st_size, fp); |
108 |
|
|
fclose (fp); |
109 |
|
|
|
110 |
|
|
/* not the entire file were read in, so abort here. */ |
111 |
|
|
if (n != st.st_size) { |
112 |
|
|
delete []buffer; |
113 |
|
|
return FALSE; |
114 |
|
|
} |
115 |
|
|
if (!loadPictureData (buffer, st.st_size)) { |
116 |
|
|
delete []buffer; |
117 |
|
|
return FALSE; |
118 |
|
|
} |
119 |
|
|
delete [] buffer; |
120 |
|
|
|
121 |
|
|
m_Weight = st.st_size; /* Update Picture Size Info... */ |
122 |
|
|
if (m_IPicture == NULL) { |
123 |
|
|
m_Height = 0; |
124 |
|
|
m_Width = 0; |
125 |
|
|
return FALSE; |
126 |
|
|
} |
127 |
|
|
m_IPicture->get_Height (&m_Height); |
128 |
|
|
m_IPicture->get_Width (&m_Width); |
129 |
|
|
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
130 |
|
|
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
131 |
|
|
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
132 |
|
|
return TRUE; |
133 |
|
|
} |
134 |
|
|
|
135 |
|
|
|
136 |
|
|
|
137 |
|
|
/* read the picture data from a source (file / resource) |
138 |
|
|
and load it into the current IPicture object in use */ |
139 |
|
|
BOOL |
140 |
|
|
CJPG::loadPictureData (BYTE *buffer, int nsize) |
141 |
|
|
{ |
142 |
|
|
BOOL result = FALSE; |
143 |
|
|
|
144 |
|
|
HGLOBAL hglobal; |
145 |
|
|
hglobal = GlobalAlloc (GMEM_MOVEABLE, nsize); |
146 |
|
|
if (hglobal == NULL) |
147 |
|
|
BUG (0); |
148 |
|
|
|
149 |
|
|
void *data; |
150 |
|
|
data = GlobalLock (hglobal); |
151 |
|
|
memcpy (data, buffer, nsize); |
152 |
|
|
GlobalUnlock (hglobal); |
153 |
|
|
|
154 |
|
|
IStream *stream = NULL; |
155 |
|
|
if (CreateStreamOnHGlobal (hglobal, TRUE, &stream) != S_OK) { |
156 |
|
|
GlobalFree (hglobal); |
157 |
|
|
return result; |
158 |
|
|
} |
159 |
|
|
|
160 |
|
|
HRESULT hr; |
161 |
|
|
hr = OleLoadPicture (stream, nsize, FALSE, IID_IPicture, |
162 |
|
|
(LPVOID *)&m_IPicture); |
163 |
|
|
if (hr == E_NOINTERFACE) { |
164 |
|
|
MessageBox (NULL, "IPicture interface is not supported", |
165 |
|
|
ERROR_TITLE, MB_OK|MB_ICONSTOP); |
166 |
|
|
} |
167 |
|
|
else if (hr == E_POINTER) { |
168 |
|
|
MessageBox (NULL, "The provided stream is not valid", |
169 |
|
|
ERROR_TITLE, MB_ERR); |
170 |
|
|
} |
171 |
|
|
else { /* S_OK */ |
172 |
|
|
stream->Release (); |
173 |
|
|
stream = NULL; |
174 |
|
|
result = TRUE; |
175 |
|
|
} |
176 |
|
|
|
177 |
|
|
GlobalFree (hglobal); |
178 |
|
|
return result; |
179 |
|
|
} |
180 |
|
|
|
181 |
|
|
|
182 |
|
|
/* Draw the loaded picture direct to the client DC */ |
183 |
|
|
BOOL |
184 |
|
|
CJPG::show (HDC pDC, POINT *leftTop, POINT *widthHeight, |
185 |
|
|
int magnifyX, int magnifyY) |
186 |
|
|
|
187 |
|
|
{ |
188 |
|
|
RECT drawRect; |
189 |
|
|
HRESULT hr; |
190 |
|
|
long width = 0; |
191 |
|
|
long height = 0; |
192 |
|
|
|
193 |
|
|
if (pDC == NULL || m_IPicture == NULL) |
194 |
|
|
return FALSE; |
195 |
|
|
|
196 |
|
|
m_IPicture->get_Width (&width); |
197 |
|
|
m_IPicture->get_Height (&height); |
198 |
|
|
|
199 |
|
|
if (magnifyX == 0) |
200 |
|
|
magnifyX = 0; |
201 |
|
|
if (magnifyY == 0) |
202 |
|
|
magnifyY = 0; |
203 |
|
|
magnifyX = int(MulDiv (width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * magnifyX); |
204 |
|
|
magnifyY = int(MulDiv (height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * magnifyY); |
205 |
|
|
|
206 |
|
|
drawRect.left = leftTop->x; |
207 |
|
|
drawRect.top = leftTop->y; |
208 |
|
|
drawRect.right = magnifyX; |
209 |
|
|
drawRect.bottom = magnifyY; |
210 |
|
|
|
211 |
|
|
hr = m_IPicture->Render (pDC, |
212 |
|
|
leftTop->x, // Left |
213 |
|
|
leftTop->y, // Top |
214 |
|
|
widthHeight->x +magnifyX, // Width |
215 |
|
|
widthHeight->y +magnifyY, // Height |
216 |
|
|
0, |
217 |
|
|
height, |
218 |
|
|
width, |
219 |
|
|
-height, |
220 |
|
|
&drawRect); |
221 |
|
|
if (SUCCEEDED (hr)) |
222 |
|
|
return TRUE; |
223 |
|
|
return FALSE; |
224 |
|
|
} |
225 |
|
|
|
226 |
|
|
|
227 |
|
|
/* 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 |
|
|
CJPG::updateSizeOnDC (HDC pDC) |
231 |
|
|
|
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 |
|
|
/* Get Current DPI - Dot Per Inch */ |
243 |
|
|
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 |
|
|
} |
251 |
|
|
|
252 |
|
|
|
253 |
|
|
/* Return height of the current image. */ |
254 |
|
|
LONG |
255 |
|
|
CJPG::getHeight (void) |
256 |
|
|
{ |
257 |
|
|
return m_Height; |
258 |
|
|
} |
259 |
|
|
|
260 |
|
|
|
261 |
|
|
/* Return weight of the current image. */ |
262 |
|
|
LONG |
263 |
|
|
CJPG::getWeight (void) |
264 |
|
|
{ |
265 |
|
|
return m_Weight; |
266 |
|
|
} |
267 |
|
|
|
268 |
|
|
|
269 |
|
|
/* Return width of the current image. */ |
270 |
|
|
LONG |
271 |
|
|
CJPG::getWidth (void) |
272 |
|
|
{ |
273 |
|
|
return m_Width; |
274 |
|
|
} |
275 |
|
|
|
276 |
|
|
/* Display a JPG picture in the given window at the given point. */ |
277 |
|
|
int |
278 |
|
|
jpg_show (HWND hwnd, POINT *p, LPCSTR name) |
279 |
|
|
{ |
280 |
|
|
CJPG jpg; |
281 |
|
|
BOOL rc; |
282 |
|
|
|
283 |
|
|
rc = jpg.load (name); |
284 |
|
|
if (!rc) |
285 |
|
|
return WPTERR_GENERAL; |
286 |
|
|
HDC hdc = GetWindowDC (hwnd); |
287 |
|
|
rc = jpg.updateSizeOnDC (hdc); |
288 |
|
|
if (!rc) { |
289 |
|
|
ReleaseDC (hwnd, hdc); |
290 |
|
|
return WPTERR_GENERAL; |
291 |
|
|
} |
292 |
|
|
|
293 |
|
|
POINT sizewnd; |
294 |
|
|
RECT rwnd; |
295 |
|
|
GetWindowRect (hwnd, &rwnd); |
296 |
|
|
sizewnd.x = rwnd.right - rwnd.left; |
297 |
|
|
sizewnd.y = rwnd.bottom - rwnd.top; |
298 |
|
|
rc = jpg.show (hdc, p, &sizewnd, 0, 0); |
299 |
|
|
|
300 |
|
|
ReleaseDC (hwnd, hdc); |
301 |
|
|
jpg.freePictureData (); |
302 |
|
|
return rc == TRUE? WPTERR_SUCESS : WPTERR_GENERAL; |
303 |
|
|
} |