39 |
|
|
40 |
#include "wptJPG.h" |
#include "wptJPG.h" |
41 |
|
|
42 |
#define HIMETRIC_INCH 2540 |
#define HIMETRIC_INCH 2540 |
43 |
#define ERROR_TITLE "CJPG Error" /* Error Title (Related To This Class)...*/ |
#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 |
CJPG::CJPG () |
|
51 |
|
CJPG::CJPG (void) |
52 |
{ |
{ |
53 |
m_IPicture = NULL; |
m_IPicture = NULL; |
54 |
m_Height = 0; |
m_Height = 0; |
57 |
} |
} |
58 |
|
|
59 |
|
|
60 |
CJPG::~CJPG () |
CJPG::~CJPG (void) |
61 |
{ |
{ |
62 |
if (m_IPicture != NULL) |
if (m_IPicture != NULL) |
63 |
FreePictureData (); |
FreePictureData (); |
65 |
|
|
66 |
|
|
67 |
|
|
68 |
/* Free The Allocated Memory That Holdes The IPicture Interface Data |
/* Free the allocated memory that holdes the IPicture Interface data |
69 |
And Clear Picture Information. */ |
and clear picture information. */ |
70 |
void CJPG::FreePictureData() |
void |
71 |
|
CJPG::FreePictureData (void) |
72 |
{ |
{ |
73 |
if (m_IPicture != NULL) { |
if (m_IPicture != NULL) { |
74 |
m_IPicture->Release(); |
m_IPicture->Release(); |
81 |
|
|
82 |
|
|
83 |
/* Open a JPG File And Load It Into IPicture (Interface) */ |
/* Open a JPG File And Load It Into IPicture (Interface) */ |
84 |
BOOL CJPG::Load(LPCSTR sFilePathName) |
BOOL |
85 |
|
CJPG::Load (LPCSTR sFilePathName) |
86 |
{ |
{ |
87 |
BOOL bResult = FALSE; |
BOOL bResult = FALSE; |
88 |
FILE * f; |
FILE * f; |
96 |
struct stat st; |
struct stat st; |
97 |
fstat (fileno (f), &st); |
fstat (fileno (f), &st); |
98 |
nSize = st.st_size; |
nSize = st.st_size; |
99 |
BYTE* pBuffer = new BYTE[nSize]; |
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) { |
if (fread(pBuffer, 1, nSize, f) > 0) { |
106 |
if (LoadPictureData (pBuffer, nSize)) |
if (LoadPictureData (pBuffer, nSize)) |
107 |
bResult = TRUE; |
bResult = TRUE; |
109 |
fclose (f); |
fclose (f); |
110 |
delete [] pBuffer; |
delete [] pBuffer; |
111 |
} |
} |
112 |
else { /* Open Failed... */ |
else { |
113 |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK | MB_ICONSTOP); |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
114 |
bResult = FALSE; |
bResult = FALSE; |
115 |
} |
} |
116 |
|
|
117 |
m_Weight = nSize; /* Update Picture Size Info... */ |
m_Weight = nSize; /* Update Picture Size Info... */ |
118 |
|
|
119 |
if(m_IPicture != NULL) { /* Do Not Try To Read From Memory That Is Not Exist... */ |
if(m_IPicture != NULL) { |
120 |
m_IPicture->get_Height (&m_Height); |
m_IPicture->get_Height (&m_Height); |
121 |
m_IPicture->get_Width (&m_Width); |
m_IPicture->get_Width (&m_Width); |
122 |
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
123 |
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
124 |
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
125 |
} |
} |
126 |
else { /* Picture Data Is Not a Known Picture Type */ |
else { |
127 |
|
/* Picture data is not a known picture type */ |
128 |
m_Height = 0; |
m_Height = 0; |
129 |
m_Width = 0; |
m_Width = 0; |
130 |
bResult = FALSE; |
bResult = FALSE; |
131 |
} |
} |
132 |
return (bResult); |
return bResult; |
133 |
} |
} |
134 |
|
|
135 |
|
|
136 |
|
|
137 |
/* Read The Picture Data From a Source (File / Resource) |
/* read the picture data from a source (file / resource) |
138 |
And Load It Into The Current IPicture Object In Use */ |
and load it into the current IPicture object in use */ |
139 |
BOOL CJPG::LoadPictureData (BYTE *pBuffer, int nSize) |
BOOL |
140 |
|
CJPG::LoadPictureData (BYTE *pBuffer, int nSize) |
141 |
|
|
142 |
{ |
{ |
143 |
BOOL bResult = FALSE; |
BOOL bResult = FALSE; |
147 |
|
|
148 |
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
149 |
if (hGlobal == NULL) { |
if (hGlobal == NULL) { |
150 |
MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
out_of_core (); |
151 |
return (FALSE); |
return FALSE; |
152 |
} |
} |
153 |
|
|
154 |
pData = GlobalLock (hGlobal); |
pData = GlobalLock (hGlobal); |
157 |
|
|
158 |
if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) { |
if (CreateStreamOnHGlobal (hGlobal, TRUE, &pStream) == S_OK) { |
159 |
HRESULT hr; |
HRESULT hr; |
160 |
if ((hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture)) == E_NOINTERFACE) { |
hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, |
161 |
MessageBox (NULL, "IPicture interface is not supported\t", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
(LPVOID *)&m_IPicture); |
162 |
return(FALSE); |
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 |
else { /* S_OK */ |
168 |
pStream->Release (); |
pStream->Release (); |
169 |
pStream = NULL; |
pStream = NULL; |
170 |
bResult = TRUE; |
bResult = TRUE; |
176 |
} |
} |
177 |
|
|
178 |
|
|
179 |
/* Draw The Loaded Picture Direct To The Client DC */ |
/* Draw the loaded picture direct to the client DC */ |
180 |
BOOL CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, int MagnifyX, int MagnifyY) |
BOOL |
181 |
|
CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight, |
182 |
|
int MagnifyX, int MagnifyY) |
183 |
|
|
184 |
{ |
{ |
|
if (pDC == NULL || m_IPicture == NULL) |
|
|
return FALSE; |
|
|
|
|
185 |
long Width = 0; |
long Width = 0; |
186 |
long Height = 0; |
long Height = 0; |
187 |
|
|
188 |
|
if (pDC == NULL || m_IPicture == NULL) |
189 |
|
return FALSE; |
190 |
|
|
191 |
m_IPicture->get_Width (&Width); |
m_IPicture->get_Width (&Width); |
192 |
m_IPicture->get_Height (&Height); |
m_IPicture->get_Height (&Height); |
193 |
|
|
220 |
if (SUCCEEDED (hrP)) |
if (SUCCEEDED (hrP)) |
221 |
return (TRUE); |
return (TRUE); |
222 |
|
|
223 |
MessageBox (NULL, "Can not allocate enough memory", ERROR_TITLE, MB_OK | MB_ICONSTOP); |
out_of_core (); |
|
return (FALSE); |
|
224 |
} |
} |
225 |
|
|
226 |
|
|
227 |
/* Get The Original Picture Pixel Size (Ignor What Current DC Is Using) |
/* Get the original picture pixel size (ignore what current DC is using) |
228 |
Pointer To a Device Context Is Needed For Pixel Calculation, */ |
pointer to a Device Context is needed for pixel calculation, */ |
229 |
BOOL CJPG::UpdateSizeOnDC(HDC pDC) |
BOOL |
230 |
|
CJPG::UpdateSizeOnDC (HDC pDC) |
231 |
|
|
232 |
{ |
{ |
233 |
if(pDC == NULL || m_IPicture == NULL) { |
if(pDC == NULL || m_IPicture == NULL) { |
239 |
m_IPicture->get_Height (&m_Height); |
m_IPicture->get_Height (&m_Height); |
240 |
m_IPicture->get_Width (&m_Width); |
m_IPicture->get_Width (&m_Width); |
241 |
|
|
242 |
// Get Current DPI - Dot Per Inch |
/* Get Current DPI - Dot Per Inch */ |
243 |
int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX); |
int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX); |
244 |
int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY); |
int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY); |
245 |
|
|
248 |
|
|
249 |
return (TRUE); |
return (TRUE); |
250 |
} |
} |
|
|
|
|
|
|
|
int |
|
|
PTD_jpg_show (HWND hwnd, POINT *p, LPCSTR name) |
|
|
{ |
|
|
CJPG jpg; |
|
|
HDC hdc; |
|
|
POINT p2; |
|
|
BOOL rc; |
|
|
|
|
|
rc = jpg.Load (name); |
|
|
if (!rc) |
|
|
return -1; |
|
|
hdc = GetWindowDC (hwnd); |
|
|
rc = jpg.UpdateSizeOnDC (hdc); |
|
|
if (!rc) { |
|
|
ReleaseDC (hwnd, hdc); |
|
|
return -2; |
|
|
} |
|
|
|
|
|
p2.x = jpg.m_Width; |
|
|
p2.y = jpg.m_Height; |
|
|
rc = jpg.Show (hdc, p, &p2, 0, 0); |
|
|
|
|
|
ReleaseDC (hwnd, hdc); |
|
|
jpg.FreePictureData (); |
|
|
return rc; |
|
|
} |
|