/[winpt]/trunk/PTD/wptJPG.cpp
ViewVC logotype

Annotation of /trunk/PTD/wptJPG.cpp

Parent Directory Parent Directory | Revision Log Revision Log


Revision 23 - (hide annotations)
Fri Sep 30 10:10:16 2005 UTC (19 years, 5 months ago) by twoaday
File size: 6330 byte(s)
Almost finished phase 1 of the WinPT GPGME port.
Still need more cleanup, comments and tests.


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 twoaday 23 #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 twoaday 2
50 twoaday 23
51     CJPG::CJPG (void)
52 twoaday 2 {
53     m_IPicture = NULL;
54     m_Height = 0;
55     m_Weight = 0;
56     m_Width = 0;
57     }
58    
59    
60 twoaday 23 CJPG::~CJPG (void)
61 twoaday 2 {
62     if (m_IPicture != NULL)
63     FreePictureData ();
64     }
65    
66    
67    
68 twoaday 23 /* Free the allocated memory that holdes the IPicture Interface data
69     and clear picture information. */
70     void
71     CJPG::FreePictureData (void)
72 twoaday 2 {
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 twoaday 23 BOOL
85     CJPG::Load (LPCSTR sFilePathName)
86 twoaday 2 {
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 twoaday 23 BYTE *pBuffer = new BYTE[nSize];
100     if (!pBuffer) {
101     fclose (f);
102     out_of_core ();
103     }
104     memset (pBuffer, 0, nSize);
105 twoaday 2 if (fread(pBuffer, 1, nSize, f) > 0) {
106     if (LoadPictureData (pBuffer, nSize))
107     bResult = TRUE;
108     }
109     fclose (f);
110     delete [] pBuffer;
111     }
112 twoaday 23 else {
113     MessageBox (NULL, strerror (errno), ERROR_TITLE, MB_OK|MB_ICONSTOP);
114     bResult = FALSE;
115 twoaday 2 }
116    
117     m_Weight = nSize; /* Update Picture Size Info... */
118    
119 twoaday 23 if(m_IPicture != NULL) {
120 twoaday 2 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 twoaday 23 else {
127     /* Picture data is not a known picture type */
128 twoaday 2 m_Height = 0;
129     m_Width = 0;
130     bResult = FALSE;
131     }
132 twoaday 23 return bResult;
133 twoaday 2 }
134    
135    
136    
137 twoaday 23 /* 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 twoaday 2
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 twoaday 23 out_of_core ();
151     return FALSE;
152 twoaday 2 }
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 twoaday 23 hr = OleLoadPicture (pStream, nSize, FALSE, IID_IPicture, (LPVOID *)&m_IPicture);
161     if (hr == E_NOINTERFACE) {
162     MessageBox (NULL, "IPicture interface is not supported", ERROR_TITLE, MB_OK|MB_ICONSTOP);
163     return FALSE;
164 twoaday 2 }
165 twoaday 23 else { /* S_OK */
166 twoaday 2 pStream->Release ();
167     pStream = NULL;
168     bResult = TRUE;
169     }
170     }
171    
172     GlobalFree (hGlobal);
173     return (bResult);
174     }
175    
176    
177 twoaday 23 /* Draw the loaded picture direct to the client DC */
178     BOOL
179     CJPG::Show (HDC pDC, POINT *LeftTop, POINT *WidthHeight,
180     int MagnifyX, int MagnifyY)
181 twoaday 2
182     {
183 twoaday 23 long Width = 0;
184     long Height = 0;
185    
186 twoaday 2 if (pDC == NULL || m_IPicture == NULL)
187     return FALSE;
188 twoaday 23
189 twoaday 2 m_IPicture->get_Width (&Width);
190     m_IPicture->get_Height (&Height);
191    
192     if (MagnifyX == NULL)
193     MagnifyX = 0;
194     if (MagnifyY == NULL)
195     MagnifyY = 0;
196     MagnifyX = int(MulDiv (Width, GetDeviceCaps(pDC, LOGPIXELSX), HIMETRIC_INCH) * MagnifyX);
197     MagnifyY = int(MulDiv (Height, GetDeviceCaps(pDC, LOGPIXELSY), HIMETRIC_INCH) * MagnifyY);
198    
199     RECT DrawRect;
200     DrawRect.left = LeftTop->x;
201     DrawRect.top = LeftTop->y;
202     DrawRect.right = MagnifyX;
203     DrawRect.bottom = MagnifyY;
204    
205     HRESULT hrP = NULL;
206    
207     hrP = m_IPicture->Render (pDC,
208     LeftTop->x, // Left
209     LeftTop->y, // Top
210     WidthHeight->x +MagnifyX, // Width
211     WidthHeight->y +MagnifyY, // Height
212     0,
213     Height,
214     Width,
215     -Height,
216     &DrawRect);
217    
218     if (SUCCEEDED (hrP))
219     return (TRUE);
220    
221 twoaday 23 out_of_core ();
222 twoaday 2 }
223    
224    
225 twoaday 23 /* Get the original picture pixel size (ignore what current DC is using)
226     pointer to a Device Context is needed for pixel calculation, */
227     BOOL
228     CJPG::UpdateSizeOnDC (HDC pDC)
229 twoaday 2
230     {
231     if(pDC == NULL || m_IPicture == NULL) {
232     m_Height = 0;
233     m_Width = 0;
234     return (FALSE);
235     }
236    
237     m_IPicture->get_Height (&m_Height);
238     m_IPicture->get_Width (&m_Width);
239    
240 twoaday 23 /* Get Current DPI - Dot Per Inch */
241 twoaday 2 int CurrentDPI_X = GetDeviceCaps (pDC, LOGPIXELSX);
242     int CurrentDPI_Y = GetDeviceCaps (pDC, LOGPIXELSY);
243    
244     m_Height = MulDiv (m_Height, CurrentDPI_Y, HIMETRIC_INCH);
245     m_Width = MulDiv (m_Width, CurrentDPI_X, HIMETRIC_INCH);
246    
247     return (TRUE);
248     }

[email protected]
ViewVC Help
Powered by ViewVC 1.1.26