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 |
|
|
* 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" /* Error Title (Related To This Class)...*/ |
44 |
|
|
|
45 |
|
|
|
46 |
|
|
CJPG::CJPG () |
47 |
|
|
{ |
48 |
|
|
m_IPicture = NULL; |
49 |
|
|
m_Height = 0; |
50 |
|
|
m_Weight = 0; |
51 |
|
|
m_Width = 0; |
52 |
|
|
} |
53 |
|
|
|
54 |
|
|
|
55 |
|
|
CJPG::~CJPG () |
56 |
|
|
{ |
57 |
|
|
if (m_IPicture != NULL) |
58 |
|
|
FreePictureData (); |
59 |
|
|
} |
60 |
|
|
|
61 |
|
|
|
62 |
|
|
|
63 |
|
|
/* Free The Allocated Memory That Holdes The IPicture Interface Data |
64 |
|
|
And Clear Picture Information. */ |
65 |
|
|
void CJPG::FreePictureData() |
66 |
|
|
{ |
67 |
|
|
if (m_IPicture != NULL) { |
68 |
|
|
m_IPicture->Release(); |
69 |
|
|
m_IPicture = NULL; |
70 |
|
|
m_Height = 0; |
71 |
|
|
m_Weight = 0; |
72 |
|
|
m_Width = 0; |
73 |
|
|
} |
74 |
|
|
} |
75 |
|
|
|
76 |
|
|
|
77 |
|
|
/* Open a JPG File And Load It Into IPicture (Interface) */ |
78 |
|
|
BOOL CJPG::Load(LPCSTR sFilePathName) |
79 |
|
|
{ |
80 |
|
|
BOOL bResult = FALSE; |
81 |
|
|
FILE * f; |
82 |
|
|
int nSize = 0; |
83 |
|
|
|
84 |
|
|
if (m_IPicture != NULL) |
85 |
|
|
FreePictureData (); |
86 |
|
|
|
87 |
|
|
f = fopen (sFilePathName, "rb"); |
88 |
|
|
if (f) { |
89 |
|
|
struct stat st; |
90 |
|
|
fstat (fileno (f), &st); |
91 |
|
|
nSize = st.st_size; |
92 |
|
|
BYTE* pBuffer = new BYTE[nSize]; |
93 |
|
|
if (fread(pBuffer, 1, nSize, f) > 0) { |
94 |
|
|
if (LoadPictureData (pBuffer, nSize)) |
95 |
|
|
bResult = TRUE; |
96 |
|
|
} |
97 |
|
|
fclose (f); |
98 |
|
|
delete [] pBuffer; |
99 |
|
|
} |
100 |
|
|
else { /* Open Failed... */ |
101 |
|
|
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK | MB_ICONSTOP); |
102 |
|
|
bResult = FALSE; |
103 |
|
|
} |
104 |
|
|
|
105 |
|
|
m_Weight = nSize; /* Update Picture Size Info... */ |
106 |
|
|
|
107 |
|
|
if(m_IPicture != NULL) { /* Do Not Try To Read From Memory That Is Not Exist... */ |
108 |
|
|
m_IPicture->get_Height (&m_Height); |
109 |
|
|
m_IPicture->get_Width (&m_Width); |
110 |
|
|
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
111 |
|
|
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
112 |
|
|
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
113 |
|
|
} |
114 |
|
|
else { /* Picture Data Is Not a Known Picture Type */ |
115 |
|
|
m_Height = 0; |
116 |
|
|
m_Width = 0; |
117 |
|
|
bResult = FALSE; |
118 |
|
|
} |
119 |
|
|
return (bResult); |
120 |
|
|
} |
121 |
|
|
|
122 |
|
|
|
123 |
|
|
|
124 |
|
|
/* Read The Picture Data From a Source (File / Resource) |
125 |
|
|
And Load It Into The Current IPicture Object In Use */ |
126 |
|
|
BOOL CJPG::LoadPictureData (BYTE *pBuffer, int nSize) |
127 |
|
|
|
128 |
|
|
{ |
129 |
|
|
BOOL bResult = FALSE; |
130 |
|
|
HGLOBAL hGlobal; |
131 |
|
|
void* pData; |
132 |
|
|
IStream* pStream = NULL; |
133 |
|
|
|
134 |
|
|
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
135 |
|
|
if (hGlobal == NULL) { |
136 |
|
|
MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
137 |
|
|
return (FALSE); |
138 |
|
|
} |
139 |
|
|
|
140 |
|
|
pData = GlobalLock (hGlobal); |
141 |
|
|
memcpy (pData, pBuffer, nSize); |
142 |
|
|
GlobalUnlock (hGlobal); |
143 |
|
|
|
144 |
|
|
if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) { |
145 |
|
|
HRESULT hr; |
146 |
|
|
if ((hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE) { |
147 |
|
|
MessageBox (NULL, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
148 |
|
|
return(FALSE); |
149 |
|
|
} |
150 |
|
|
else { // S_OK |
151 |
|
|
pStream->Release (); |
152 |
|
|
pStream = NULL; |
153 |
|
|
bResult = TRUE; |
154 |
|
|
} |
155 |
|
|
} |
156 |
|
|
|
157 |
|
|
GlobalFree (hGlobal); |
158 |
|
|
return (bResult); |
159 |
|
|
} |
160 |
|
|
|
161 |
|
|
|
162 |
|
|
/* Draw The Loaded Picture Direct To The Client DC */ |
163 |
|
|
BOOL CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, int MagnifyX, int MagnifyY) |
164 |
|
|
|
165 |
|
|
{ |
166 |
|
|
if (pDC == NULL || m_IPicture == NULL) |
167 |
|
|
return FALSE; |
168 |
|
|
|
169 |
|
|
long Width = 0; |
170 |
|
|
long Height = 0; |
171 |
|
|
m_IPicture->get_Width (&Width); |
172 |
|
|
m_IPicture->get_Height (&Height); |
173 |
|
|
|
174 |
|
|
if (MagnifyX == NULL) |
175 |
|
|
MagnifyX = 0; |
176 |
|
|
if (MagnifyY == NULL) |
177 |
|
|
MagnifyY = 0; |
178 |
|
|
MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX); |
179 |
|
|
MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY); |
180 |
|
|
|
181 |
|
|
RECT DrawRect; |
182 |
|
|
DrawRect.left = LeftTop->x; |
183 |
|
|
DrawRect.top = LeftTop->y; |
184 |
|
|
DrawRect.right = MagnifyX; |
185 |
|
|
DrawRect.bottom = MagnifyY; |
186 |
|
|
|
187 |
|
|
HRESULT hrP = NULL; |
188 |
|
|
|
189 |
|
|
hrP = m_IPicture->Render (pDC, |
190 |
|
|
LeftTop->x, // Left |
191 |
|
|
LeftTop->y, // Top |
192 |
|
|
WidthHeight->x +MagnifyX, // Width |
193 |
|
|
WidthHeight->y +MagnifyY, // Height |
194 |
|
|
0, |
195 |
|
|
Height, |
196 |
|
|
Width, |
197 |
|
|
-Height, |
198 |
|
|
&DrawRect); |
199 |
|
|
|
200 |
|
|
if (SUCCEEDED (hrP)) |
201 |
|
|
return (TRUE); |
202 |
|
|
|
203 |
|
|
MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
204 |
|
|
return (FALSE); |
205 |
|
|
} |
206 |
|
|
|
207 |
|
|
|
208 |
|
|
/* Get The Original Picture Pixel Size (Ignor What Current DC Is Using) |
209 |
|
|
Pointer To a Device Context Is Needed For Pixel Calculation, */ |
210 |
|
|
BOOL CJPG::UpdateSizeOnDC(HDC pDC) |
211 |
|
|
|
212 |
|
|
{ |
213 |
|
|
if(pDC == NULL || m_IPicture == NULL) { |
214 |
|
|
m_Height = 0; |
215 |
|
|
m_Width = 0; |
216 |
|
|
return (FALSE); |
217 |
|
|
} |
218 |
|
|
|
219 |
|
|
m_IPicture->get_Height (&m_Height); |
220 |
|
|
m_IPicture->get_Width (&m_Width); |
221 |
|
|
|
222 |
|
|
// Get Current DPI - Dot Per Inch |
223 |
|
|
int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX); |
224 |
|
|
int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY); |
225 |
|
|
|
226 |
|
|
m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH); |
227 |
|
|
m_Width = MulDiv (m_Width, CurrentDPI_X, HIMETRIC_INCH); |
228 |
|
|
|
229 |
|
|
return (TRUE); |
230 |
|
|
} |
231 |
|
|
|
232 |
|
|
|
233 |
|
|
int |
234 |
|
|
PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name) |
235 |
|
|
{ |
236 |
|
|
CJPG jpg; |
237 |
|
|
HDC hdc; |
238 |
|
|
POINT p2; |
239 |
|
|
BOOL rc; |
240 |
|
|
|
241 |
|
|
rc = jpg.Load (name); |
242 |
|
|
if (!rc) |
243 |
|
|
return -1; |
244 |
|
|
hdc = GetWindowDC (hwnd); |
245 |
|
|
rc = jpg.UpdateSizeOnDC (hdc); |
246 |
|
|
if (!rc) { |
247 |
|
|
ReleaseDC (hwnd, hdc); |
248 |
|
|
return -2; |
249 |
|
|
} |
250 |
|
|
|
251 |
|
|
p2.x = jpg.m_Width; |
252 |
|
|
p2.y = jpg.m_Height; |
253 |
|
|
rc = jpg.Show (hdc, p, &p2, 0, 0); |
254 |
|
|
|
255 |
|
|
ReleaseDC (hwnd, hdc); |
256 |
|
|
jpg.FreePictureData (); |
257 |
|
|
return rc; |
258 |
|
|
} |