1 |
/* 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 <windows.h> |
37 |
#include <ocidl.h> |
38 |
#include <olectl.h> |
39 |
|
40 |
#include <stdio.h> |
41 |
#include <string.h> |
42 |
#include <errno.h> |
43 |
#include <sys/stat.h> |
44 |
|
45 |
#include "wptJPG.h" |
46 |
|
47 |
#define HIMETRIC_INCH 2540 |
48 |
#define ERROR_TITLE "CJPG Error" |
49 |
|
50 |
#define out_of_core() do { \ |
51 |
MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \ |
52 |
return FALSE; \ |
53 |
} while (0) |
54 |
|
55 |
|
56 |
CJPG::CJPG (void) |
57 |
{ |
58 |
m_IPicture = NULL; |
59 |
m_Height = 0; |
60 |
m_Weight = 0; |
61 |
m_Width = 0; |
62 |
} |
63 |
|
64 |
|
65 |
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 |
m_Height = 0; |
82 |
m_Weight = 0; |
83 |
m_Width = 0; |
84 |
} |
85 |
} |
86 |
|
87 |
|
88 |
/* Open a JPG File And Load It Into IPicture (Interface) */ |
89 |
BOOL |
90 |
CJPG::load (LPCSTR sFilePathName) |
91 |
{ |
92 |
BOOL bResult = FALSE; |
93 |
FILE * f; |
94 |
int nSize = 0; |
95 |
|
96 |
if (m_IPicture != NULL) |
97 |
freePictureData (); |
98 |
|
99 |
f = fopen (sFilePathName, "rb"); |
100 |
if (f) { |
101 |
struct stat st; |
102 |
fstat (fileno (f), &st); |
103 |
nSize = st.st_size; |
104 |
BYTE *pBuffer = new BYTE[nSize]; |
105 |
if (!pBuffer) { |
106 |
fclose (f); |
107 |
out_of_core (); |
108 |
} |
109 |
memset (pBuffer, 0, nSize); |
110 |
if (fread(pBuffer, 1, nSize, f) > 0) { |
111 |
if (loadPictureData (pBuffer, nSize)) |
112 |
bResult = TRUE; |
113 |
} |
114 |
fclose (f); |
115 |
delete [] pBuffer; |
116 |
} |
117 |
else { |
118 |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
119 |
bResult = FALSE; |
120 |
} |
121 |
|
122 |
m_Weight = nSize; /* Update Picture Size Info... */ |
123 |
|
124 |
if(m_IPicture != NULL) { |
125 |
m_IPicture->get_Height (&m_Height); |
126 |
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 |
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
130 |
} |
131 |
else { |
132 |
/* Picture data is not a known picture type */ |
133 |
m_Height = 0; |
134 |
m_Width = 0; |
135 |
bResult = FALSE; |
136 |
} |
137 |
return bResult; |
138 |
} |
139 |
|
140 |
|
141 |
|
142 |
/* read the picture data from a source (file / resource) |
143 |
and load it into the current IPicture object in use */ |
144 |
BOOL |
145 |
CJPG::loadPictureData (BYTE *pBuffer, int nSize) |
146 |
|
147 |
{ |
148 |
BOOL bResult = FALSE; |
149 |
HGLOBAL hGlobal; |
150 |
void* pData; |
151 |
IStream* pStream = NULL; |
152 |
|
153 |
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
154 |
if (hGlobal == NULL) { |
155 |
out_of_core (); |
156 |
return FALSE; |
157 |
} |
158 |
|
159 |
pData = GlobalLock (hGlobal); |
160 |
memcpy (pData, pBuffer, nSize); |
161 |
GlobalUnlock (hGlobal); |
162 |
|
163 |
if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) { |
164 |
HRESULT hr; |
165 |
hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, |
166 |
(LPVOID *)&m_IPicture); |
167 |
if (hr == E_NOINTERFACE) { |
168 |
MessageBox (NULL, "IPicture interface is not supported", |
169 |
ERROR_TITLE, MB_OK|MB_ICONSTOP); |
170 |
return FALSE; |
171 |
} |
172 |
else { /* S_OK */ |
173 |
pStream->Release (); |
174 |
pStream = NULL; |
175 |
bResult = TRUE; |
176 |
} |
177 |
} |
178 |
|
179 |
GlobalFree (hGlobal); |
180 |
return (bResult); |
181 |
} |
182 |
|
183 |
|
184 |
/* Draw the loaded picture direct to the client DC */ |
185 |
BOOL |
186 |
CJPG::show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, |
187 |
int MagnifyX, int MagnifyY) |
188 |
|
189 |
{ |
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 |
RECT DrawRect; |
207 |
DrawRect.left = LeftTop->x; |
208 |
DrawRect.top = LeftTop->y; |
209 |
DrawRect.right = MagnifyX; |
210 |
DrawRect.bottom = MagnifyY; |
211 |
|
212 |
HRESULT hrP = 0; |
213 |
|
214 |
hrP = m_IPicture->Render (pDC, |
215 |
LeftTop->x, // Left |
216 |
LeftTop->y, // Top |
217 |
WidthHeight->x +MagnifyX, // Width |
218 |
WidthHeight->y +MagnifyY, // Height |
219 |
0, |
220 |
Height, |
221 |
Width, |
222 |
-Height, |
223 |
&DrawRect); |
224 |
|
225 |
if (SUCCEEDED (hrP)) |
226 |
return (TRUE); |
227 |
|
228 |
out_of_core (); |
229 |
} |
230 |
|
231 |
|
232 |
/* Get the original picture pixel size (ignore what current DC is using) |
233 |
pointer to a Device Context is needed for pixel calculation, */ |
234 |
BOOL |
235 |
CJPG::updateSizeOnDC (HDC pDC) |
236 |
|
237 |
{ |
238 |
if(pDC == NULL || m_IPicture == NULL) { |
239 |
m_Height = 0; |
240 |
m_Width = 0; |
241 |
return (FALSE); |
242 |
} |
243 |
|
244 |
m_IPicture->get_Height (&m_Height); |
245 |
m_IPicture->get_Width (&m_Width); |
246 |
|
247 |
/* Get Current DPI - Dot Per Inch */ |
248 |
int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX); |
249 |
int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY); |
250 |
|
251 |
m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH); |
252 |
m_Width = MulDiv (m_Width, CurrentDPI_X, HIMETRIC_INCH); |
253 |
|
254 |
return (TRUE); |
255 |
} |