1 |
/* wptJPG.cpp : JPG picture class |
/* wptJPG.cpp - Routines for showing JPG pictures |
2 |
* Copyright (C) 2001 Dr.Yovav Gad <[email protected]> |
* Copyright (C) 2001 Dr.Yovav Gad |
3 |
* Copyright (C) 2005 Timo Schulz |
* Copyright (C) 2005, 2006 Timo Schulz |
4 |
* |
* |
5 |
* This file is part of WinPT. |
* This file is part of WinPT. |
6 |
* |
* |
21 |
|
|
22 |
/*----------------------------------------------------------------------------- |
/*----------------------------------------------------------------------------- |
23 |
* Picture (Implementations) Version 1.00 |
* Picture (Implementations) Version 1.00 |
24 |
* |
* Author: Dr. Yovav Gad, EMail: [email protected] |
25 |
* Routinges for showing JPG pictur files |
* Web: www.SuperMain.com |
|
* |
|
|
* Author: Dr. Yovav Gad, EMail: [email protected] ,Web: www.SuperMain.com |
|
26 |
* |
* |
27 |
* This version uses a stripped down version of Picture.cpp and Picture.h. |
* This version uses a stripped down and heavily modified version of |
28 |
|
* Picture.cpp and Picture.h. |
29 |
*/ |
*/ |
30 |
#ifdef HAVE_CONFIG_H |
#ifdef HAVE_CONFIG_H |
31 |
#include <config.h> |
#include <config.h> |
43 |
#include "wptJPG.h" |
#include "wptJPG.h" |
44 |
|
|
45 |
#define HIMETRIC_INCH 2540 |
#define HIMETRIC_INCH 2540 |
46 |
#define ERROR_TITLE "CJPG Error" |
#define ERROR_TITLE "WinPT - CJPG Error" |
47 |
|
|
48 |
#define out_of_core() do { \ |
#define out_of_core() do { \ |
49 |
MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \ |
MessageBox (NULL, "Can not allocate memory", ERROR_TITLE, MB_OK|MB_ICONSTOP); \ |
51 |
} while (0) |
} while (0) |
52 |
|
|
53 |
|
|
54 |
|
/* Constructor to create an empty JPG container. */ |
55 |
CJPG::CJPG (void) |
CJPG::CJPG (void) |
56 |
{ |
{ |
57 |
m_IPicture = NULL; |
m_IPicture = NULL; |
58 |
m_Height = 0; |
m_Height = 0; |
59 |
m_Weight = 0; |
m_Weight = 0; |
60 |
m_Width = 0; |
m_Width = 0; |
61 |
} |
} |
62 |
|
|
63 |
|
|
64 |
|
/* Deconstructor. Free all internal data. */ |
65 |
CJPG::~CJPG (void) |
CJPG::~CJPG (void) |
66 |
{ |
{ |
67 |
if (m_IPicture != NULL) |
if (m_IPicture != NULL) |
78 |
if (m_IPicture != NULL) { |
if (m_IPicture != NULL) { |
79 |
m_IPicture->Release(); |
m_IPicture->Release(); |
80 |
m_IPicture = NULL; |
m_IPicture = NULL; |
|
m_Height = 0; |
|
|
m_Weight = 0; |
|
|
m_Width = 0; |
|
81 |
} |
} |
82 |
|
m_Height = 0; |
83 |
|
m_Weight = 0; |
84 |
|
m_Width = 0; |
85 |
} |
} |
86 |
|
|
87 |
|
|
89 |
BOOL |
BOOL |
90 |
CJPG::load (LPCSTR sFilePathName) |
CJPG::load (LPCSTR sFilePathName) |
91 |
{ |
{ |
92 |
BOOL bResult = FALSE; |
struct stat st; |
93 |
FILE * f; |
FILE *fp; |
94 |
int nSize = 0; |
BYTE *pBuffer; |
95 |
|
int n; |
96 |
|
|
97 |
if (m_IPicture != NULL) |
if (m_IPicture != NULL) |
98 |
freePictureData (); |
freePictureData (); |
99 |
|
|
100 |
f = fopen (sFilePathName, "rb"); |
fp = fopen (sFilePathName, "rb"); |
101 |
if (f) { |
if (!fp) { |
|
struct stat st; |
|
|
fstat (fileno (f), &st); |
|
|
nSize = st.st_size; |
|
|
BYTE *pBuffer = new BYTE[nSize]; |
|
|
if (!pBuffer) { |
|
|
fclose (f); |
|
|
out_of_core (); |
|
|
} |
|
|
memset (pBuffer, 0, nSize); |
|
|
if (fread(pBuffer, 1, nSize, f) > 0) { |
|
|
if (loadPictureData (pBuffer, nSize)) |
|
|
bResult = TRUE; |
|
|
} |
|
|
fclose (f); |
|
|
delete [] pBuffer; |
|
|
} |
|
|
else { |
|
102 |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP); |
103 |
bResult = FALSE; |
return FALSE; |
104 |
} |
} |
105 |
|
|
106 |
m_Weight = nSize; /* Update Picture Size Info... */ |
/* avoid to load empty JPG files and make sure we still can |
107 |
|
access the file handle. */ |
108 |
if(m_IPicture != NULL) { |
if (fstat (fileno (fp), &st) || st.st_size == 0) { |
109 |
m_IPicture->get_Height (&m_Height); |
fclose (fp); |
110 |
m_IPicture->get_Width (&m_Width); |
return FALSE; |
|
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
|
|
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
|
|
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
|
111 |
} |
} |
112 |
else { |
pBuffer = new BYTE[st.st_size]; |
113 |
/* Picture data is not a known picture type */ |
if (!pBuffer) |
114 |
|
out_of_core (); |
115 |
|
memset (pBuffer, 0, st.st_size); |
116 |
|
n = fread (pBuffer, 1, st.st_size, fp); |
117 |
|
fclose (fp); |
118 |
|
|
119 |
|
/* not the entire file were read in, so abort here. */ |
120 |
|
if (n != st.st_size) { |
121 |
|
delete []pBuffer; |
122 |
|
return FALSE; |
123 |
|
} |
124 |
|
if (!loadPictureData (pBuffer, st.st_size)) { |
125 |
|
delete []pBuffer; |
126 |
|
return FALSE; |
127 |
|
} |
128 |
|
delete [] pBuffer; |
129 |
|
|
130 |
|
m_Weight = st.st_size; /* Update Picture Size Info... */ |
131 |
|
if (m_IPicture == NULL) { |
132 |
m_Height = 0; |
m_Height = 0; |
133 |
m_Width = 0; |
m_Width = 0; |
134 |
bResult = FALSE; |
return FALSE; |
135 |
} |
} |
136 |
return bResult; |
m_IPicture->get_Height (&m_Height); |
137 |
|
m_IPicture->get_Width (&m_Width); |
138 |
|
/* Calculate Its Size On a "Standard" (96 DPI) Device Context */ |
139 |
|
m_Height = MulDiv (m_Height, 96, HIMETRIC_INCH); |
140 |
|
m_Width = MulDiv (m_Width, 96, HIMETRIC_INCH); |
141 |
|
return TRUE; |
142 |
} |
} |
143 |
|
|
144 |
|
|
149 |
CJPG::loadPictureData (BYTE *pBuffer, int nSize) |
CJPG::loadPictureData (BYTE *pBuffer, int nSize) |
150 |
|
|
151 |
{ |
{ |
152 |
BOOL bResult = FALSE; |
HGLOBAL hGlobal; |
|
HGLOBAL hGlobal; |
|
|
void* pData; |
|
153 |
IStream* pStream = NULL; |
IStream* pStream = NULL; |
154 |
|
void* pData; |
155 |
|
BOOL bResult = FALSE; |
156 |
|
|
157 |
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
hGlobal = GlobalAlloc (GMEM_MOVEABLE, nSize); |
158 |
if (hGlobal == NULL) { |
if (hGlobal == NULL) |
159 |
out_of_core (); |
out_of_core (); |
|
return FALSE; |
|
|
} |
|
160 |
|
|
161 |
pData = GlobalLock (hGlobal); |
pData = GlobalLock (hGlobal); |
162 |
memcpy (pData, pBuffer, nSize); |
memcpy (pData, pBuffer, nSize); |
166 |
HRESULT hr; |
HRESULT hr; |
167 |
hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, |
hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, |
168 |
(LPVOID *)&m_IPicture); |
(LPVOID *)&m_IPicture); |
169 |
if (hr == E_NOINTERFACE) { |
if (hr == E_NOINTERFACE) |
170 |
MessageBox (NULL, "IPicture interface is not supported", |
MessageBox (NULL, "IPicture interface is not supported", |
171 |
ERROR_TITLE, MB_OK|MB_ICONSTOP); |
ERROR_TITLE, MB_OK|MB_ICONSTOP); |
|
return FALSE; |
|
|
} |
|
172 |
else { /* S_OK */ |
else { /* S_OK */ |
173 |
pStream->Release (); |
pStream->Release (); |
174 |
pStream = NULL; |
pStream = NULL; |
177 |
} |
} |
178 |
|
|
179 |
GlobalFree (hGlobal); |
GlobalFree (hGlobal); |
180 |
return (bResult); |
return bResult; |
181 |
} |
} |
182 |
|
|
183 |
|
|
188 |
|
|
189 |
{ |
{ |
190 |
RECT drawRect; |
RECT drawRect; |
191 |
|
HRESULT hr = 0; |
192 |
long width = 0; |
long width = 0; |
193 |
long height = 0; |
long height = 0; |
|
HRESULT hrP = 0; |
|
194 |
|
|
195 |
if (pDC == NULL || m_IPicture == NULL) |
if (pDC == NULL || m_IPicture == NULL) |
196 |
return FALSE; |
return FALSE; |
210 |
drawRect.right = magnifyX; |
drawRect.right = magnifyX; |
211 |
drawRect.bottom = magnifyY; |
drawRect.bottom = magnifyY; |
212 |
|
|
213 |
hrP = m_IPicture->Render (pDC, |
hr = m_IPicture->Render (pDC, |
214 |
leftTop->x, // Left |
leftTop->x, // Left |
215 |
leftTop->y, // Top |
leftTop->y, // Top |
216 |
widthHeight->x +magnifyX, // Width |
widthHeight->x +magnifyX, // Width |
221 |
-height, |
-height, |
222 |
&drawRect); |
&drawRect); |
223 |
|
|
224 |
if (SUCCEEDED (hrP)) |
if (SUCCEEDED (hr)) |
225 |
return (TRUE); |
return TRUE; |
226 |
|
|
227 |
out_of_core ();} |
return FALSE; |
228 |
|
} |
229 |
|
|
230 |
|
|
231 |
/* Get the original picture pixel size (ignore what current DC is using) |
/* Get the original picture pixel size (ignore what current DC is using) |
237 |
if(pDC == NULL || m_IPicture == NULL) { |
if(pDC == NULL || m_IPicture == NULL) { |
238 |
m_Height = 0; |
m_Height = 0; |
239 |
m_Width = 0; |
m_Width = 0; |
240 |
return (FALSE); |
return FALSE; |
241 |
} |
} |
242 |
|
|
243 |
m_IPicture->get_Height (&m_Height); |
m_IPicture->get_Height (&m_Height); |
250 |
m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH); |
m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH); |
251 |
m_Width = MulDiv (m_Width, CurrentDPI_X, HIMETRIC_INCH); |
m_Width = MulDiv (m_Width, CurrentDPI_X, HIMETRIC_INCH); |
252 |
|
|
253 |
return (TRUE); |
return TRUE; |
254 |
} |
} |
255 |
|
|
256 |
/* Return height of the current image. */ |
/* Return height of the current image. */ |