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 PTD. |
6 |
* |
7 |
* PTD 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 |
* PTD 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 |
#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 |
#define HIMETRIC_INCH 2540 |
43 |
#define ERROR_TITLE "CJPG Error" |
44 |
|
45 |
#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 |
|
50 |
|
51 |
CJPG::CJPG (void) |
52 |
{ |
53 |
m_IPicture = NULL; |
54 |
m_Height = 0; |
55 |
m_Weight = 0; |
56 |
m_Width = 0; |
57 |
} |
58 |
|
59 |
|
60 |
CJPG::~CJPG (void) |
61 |
{ |
62 |
if (m_IPicture != NULL) |
63 |
FreePictureData (); |
64 |
} |
65 |
|
66 |
|
67 |
|
68 |
/* Free the allocated memory that holdes the IPicture Interface data |
69 |
and clear picture information. */ |
70 |
void |
71 |
CJPG::FreePictureData (void) |
72 |
{ |
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 |
BOOL |
85 |
CJPG::Load (LPCSTR sFilePathName) |
86 |
{ |
87 |
BOOL bResult = FALSE; |
88 |
FILE * f; |
89 |
int nSize = 0; |
90 |
|
91 |
if (m_IPicture != NULL) |
92 |
FreePictureData (); |
93 |
|
94 |
f = fopen (sFilePathName, "rb"); |
95 |
if (f) { |
96 |
struct stat st; |
97 |
fstat (fileno (f), &st); |
98 |
nSize = st.st_size; |
99 |
BYTE *pBuffer = new BYTE[nSize]; |
100 |
if (!pBuffer) { |
101 |
fclose (f); |
102 |
out_of_core (); |
103 |
} |
104 |
memset (pBuffer, 0, nSize); |
105 |
if (fread(pBuffer, 1, nSize, f) > 0) { |
106 |
if (LoadPictureData (pBuffer, nSize)) |
107 |
bResult = TRUE; |
108 |
} |
109 |
fclose (f); |
110 |
delete [] pBuffer; |
111 |
} |
112 |
else { |
113 |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
114 |
bResult = FALSE; |
115 |
} |
116 |
|
117 |
m_Weight = nSize; /* Update Picture Size Info... */ |
118 |
|
119 |
if(m_IPicture != NULL) { |
120 |
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 |
else { |
127 |
/* Picture data is not a known picture type */ |
128 |
m_Height = 0; |
129 |
m_Width = 0; |
130 |
bResult = FALSE; |
131 |
} |
132 |
return bResult; |
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 *pBuffer, int nSize) |
141 |
|
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 |
out_of_core (); |
151 |
return FALSE; |
152 |
} |
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 |
hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, |
161 |
(LPVOID *)&m_IPicture); |
162 |
if (hr == E_NOINTERFACE) { |
163 |
MessageBox (NULL, "IPicture interface is not supported", |
164 |
ERROR_TITLE, MB_OK|MB_ICONSTOP); |
165 |
return FALSE; |
166 |
} |
167 |
else { /* S_OK */ |
168 |
pStream->Release (); |
169 |
pStream = NULL; |
170 |
bResult = TRUE; |
171 |
} |
172 |
} |
173 |
|
174 |
GlobalFree (hGlobal); |
175 |
return (bResult); |
176 |
} |
177 |
|
178 |
|
179 |
/* Draw the loaded picture direct to the client DC */ |
180 |
BOOL |
181 |
CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, |
182 |
int MagnifyX, int MagnifyY) |
183 |
|
184 |
{ |
185 |
long Width = 0; |
186 |
long Height = 0; |
187 |
|
188 |
if (pDC == NULL || m_IPicture == NULL) |
189 |
return FALSE; |
190 |
|
191 |
m_IPicture->get_Width (&Width); |
192 |
m_IPicture->get_Height (&Height); |
193 |
|
194 |
if (MagnifyX == NULL) |
195 |
MagnifyX = 0; |
196 |
if (MagnifyY == NULL) |
197 |
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 |
out_of_core (); |
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 |
} |