OSDN Git Service

Add TortoiseProc
[tortoisegit/TortoiseGitJp.git] / Utils / MiscUI / Picture.h
1 // TortoiseSVN - a Windows shell extension for easy version control\r
2 \r
3 // Copyright (C) 2003-2007 - TortoiseSVN\r
4 \r
5 // This program is free software; you can redistribute it and/or\r
6 // modify it under the terms of the GNU General Public License\r
7 // as published by the Free Software Foundation; either version 2\r
8 // of the License, or (at your option) any later version.\r
9 \r
10 // This program is distributed in the hope that it will be useful,\r
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of\r
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
13 // GNU General Public License for more details.\r
14 \r
15 // You should have received a copy of the GNU General Public License\r
16 // along with this program; if not, write to the Free Software Foundation,\r
17 // 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.\r
18 \r
19 #pragma once\r
20 #include <string>\r
21 #include "ocidl.h"\r
22 #include <GdiPlus.h>\r
23 \r
24 using namespace Gdiplus;\r
25 #ifdef UNICODE\r
26 #       define stdstring std::wstring\r
27 #else\r
28 #       define stdstring std::string\r
29 #endif\r
30 \r
31 /**\r
32  * \ingroup Utils\r
33  * Class for showing picture files. \r
34  * Use this class to show pictures of different file formats: BMP, DIB, EMF, GIF, ICO, JPG, WMF\r
35  * If Gdi+ is installed (default on XP and later, optional on Win2k), other image formats can\r
36  * be shown too: png, tiff.\r
37  * The class uses the IPicture interface, the same way as internet explorer does.\r
38  * \r
39  * Example of usage:\r
40  * \code\r
41  * CPicture m_picture;\r
42  * //load picture data into the IPicture interface\r
43  * m_picture.Load("Test.jpg");  //load from a file\r
44  * m_picture.Load(IDR_TEST, "JPG");     //load from a resource\r
45  * \r
46  * //when using in a dialog based application (CPaintDC dc(this);)\r
47  * m_picture.UpdateSizeOnDC(&dc);       //get picture dimensions in pixels\r
48  * m_picture.Show(&dc, CPoint(0,0), CPoint(m_picture.m_Width, m_picture.m_Height), 0, 0);\r
49  * m_picture.Show(&dc, CRect(0,0,100,100)); //change original dimensions\r
50  * m_picture.ShowBitmapResource(&dc, IDB_TEST, CPoint(0,0));    //show bitmap resource\r
51  * \r
52  * //when using in a regular mfc application (CDC* pDC)\r
53  * m_picture.UpdateSizeOnDC(pDC);       //get picture dimensions in pixels\r
54  * m_picture.Show(pDC, CPoint(0,0), CPoint(m_picture.m_Width, m_picture.m_Height), 0, 0);\r
55  * m_picture.Show(pDC, CRect(0,0,100,100)); //change original dimensions\r
56  * m_picture.ShowBitmapResource(pDC, IDB_TEST, CPoint(0,0));    //show bitmap resource\r
57  *\r
58  * //to show picture information\r
59  * std::string s;\r
60  * s.Format("Size = %4d\nWidth = %4d\nHeight = %4d\nWeight = %4d\n",\r
61  *                              m_picture.m_Weigth, m_picture.m_Width, m_picture.m_Height, m_picture.m_Weight);\r
62  * AfxMessageBox(s);\r
63  * \endcode\r
64  * \remark GDI+ is only used if it is installed. If you link with gdiplus.lib and mark the gdiplus.dll\r
65  * as a delay-loaded dll, then your application won't throw an error if it isn't installed, but of course\r
66  * png and tiff images can't be shown.\r
67  */\r
68 class CPicture\r
69 {\r
70 public:\r
71         /**\r
72          * open a picture file and load it (BMP, DIB, EMF, GIF, ICO, JPG, WMF).\r
73          *\r
74          * \param sFilePathName the path of the picture file\r
75          * \return TRUE if succeeded.\r
76          */\r
77         bool Load(stdstring sFilePathName);\r
78         /**\r
79          * draws the loaded picture directly to the given device context.\r
80          * \note\r
81          * if the given size is not the actual picture size, then the picture will\r
82          * be drawn stretched to the given dimensions.\r
83          * \param pDC the device context to draw on\r
84          * \param DrawRect the dimensions to draw the picture on\r
85          * \return TRUE if succeeded\r
86          */\r
87         bool Show(HDC hDC, RECT DrawRect);\r
88         /**\r
89          * get the original picture pixel size. A pointer to a device context is needed\r
90          * for the pixel calculation (DPI). Also updates the classes height and width\r
91          * members.\r
92          * \param pDC the device context to perform the calculations on\r
93          * \return TRUE if succeeded\r
94          */\r
95         bool UpdateSizeOnDC(HDC hDC);\r
96 \r
97         /**\r
98          * Return the horizontal resolutions in dpi of the loaded picture.\r
99          * \remark this only works if gdi+ is installed.\r
100          */\r
101         float GetHorizontalResolution() {return pBitmap ? pBitmap->GetHorizontalResolution() : 0.0f;}\r
102         /**\r
103          * Return the vertical resolution in dpi of the loaded picture.\r
104          * \remark this only works if gdi+ is installed.\r
105          */\r
106         float GetVerticalResolution() {return pBitmap ? pBitmap->GetVerticalResolution() : 0.0f;}\r
107         /**\r
108          * Returns the picture height in pixels.\r
109          * \remark this only works if gdi+ is installed.\r
110          */\r
111         UINT GetHeight() const;\r
112         /**\r
113          * Returns the picture width in pixels.\r
114          * \remark this only works if gdi+ is installed.\r
115          */\r
116         UINT GetWidth() const;\r
117         /**\r
118          * Returns the pixel format of the loaded picture.\r
119          * \remark this only works if gdi+ is installed.\r
120          */\r
121         PixelFormat GetPixelFormat() const;\r
122         /**\r
123          * Returns the color depth in bits.\r
124          * \remark this only works if gdi+ is installed.\r
125          */\r
126         UINT GetColorDepth() const;\r
127 \r
128         /**\r
129          * Sets the interpolation used for drawing the image.\r
130          * The interpolation mode is one of the following:\r
131          * InterpolationModeInvalid\r
132          * InterpolationModeDefault\r
133          * InterpolationModeLowQuality\r
134          * InterpolationModeHighQuality\r
135          * InterpolationModeBilinear\r
136          * InterpolationModeBicubic\r
137          * InterpolationModeNearestNeighbor\r
138          * InterpolationModeHighQualityBilinear\r
139          * InterpolationModeHighQualityBicubic\r
140          */\r
141         void SetInterpolationMode(InterpolationMode ip) {m_ip = ip;}\r
142 \r
143         /**\r
144          * Returns the number of frames in the specified dimension of the image.\r
145          */\r
146         UINT GetNumberOfFrames(int dimension);\r
147         /**\r
148          * Returns the number of dimensions in the image.\r
149          * For example, icons can have multiple dimensions (sizes).\r
150          */\r
151         UINT GetNumberOfDimensions();\r
152 \r
153         /**\r
154          * Sets the active frame which is used when drawing the image.\r
155          * \return the delay value for this frame, i.e. how long this frame\r
156          * should be shown.\r
157          */\r
158         long SetActiveFrame(UINT frame);\r
159 \r
160         DWORD GetFileSize() {return m_nSize;}\r
161         stdstring GetFileSizeAsText(bool bAbbrev = true);\r
162         CPicture();\r
163         virtual ~CPicture();\r
164 \r
165 \r
166         HGLOBAL         hGlobal;\r
167 \r
168         IPicture* m_IPicture;   ///< Same As LPPICTURE (typedef IPicture __RPC_FAR *LPPICTURE)\r
169 \r
170         LONG            m_Height;       ///< Height (in pixels)\r
171         LONG            m_Width;        ///< Width (in pixels)\r
172         BYTE            m_ColorDepth;///< the color depth\r
173         LONG            m_Weight;       ///< Size Of The Image Object In Bytes (File OR Resource)\r
174         stdstring       m_Name;         ///< The FileName of the Picture as used in Load()\r
175 \r
176 protected:\r
177         /**\r
178          * reads the picture data from a source and loads it into the current IPicture object in use.\r
179          * \param pBuffer buffer of data source\r
180          * \param nSize the size of the buffer\r
181          * \return TRUE if succeeded\r
182          */\r
183         bool LoadPictureData(BYTE* pBuffer, int nSize);\r
184 \r
185         /**\r
186          * frees the allocated memory that holds the IPicture interface data and\r
187          * clear picture information\r
188          */\r
189         void FreePictureData();\r
190 \r
191 private:\r
192         GdiplusStartupInput gdiplusStartupInput;\r
193         ULONG_PTR           gdiplusToken;\r
194         Bitmap *                        pBitmap;\r
195         bool                            bHaveGDIPlus;\r
196         InterpolationMode       m_ip;\r
197         bool                            bIsIcon;\r
198         UINT                            nCurrentIcon;\r
199         BYTE *                          lpIcons;\r
200         HICON *                         hIcons;\r
201         DWORD                           m_nSize;\r
202 \r
203         #pragma pack(push, r1, 2)   // n = 16, pushed to stack\r
204 \r
205         typedef struct\r
206         {\r
207                 BYTE    bWidth;               // Width of the image\r
208                 BYTE    bHeight;              // Height of the image (times 2)\r
209                 BYTE    bColorCount;          // Number of colors in image (0 if >=8bpp)\r
210                 BYTE    bReserved;            // Reserved\r
211                 WORD    wPlanes;              // Color Planes\r
212                 WORD    wBitCount;            // Bits per pixel\r
213                 DWORD   dwBytesInRes;         // how many bytes in this resource?\r
214                 DWORD   dwImageOffset;        // where in the file is this image\r
215         } ICONDIRENTRY, *LPICONDIRENTRY;\r
216         typedef struct \r
217         {\r
218                 WORD                    idReserved;   // Reserved\r
219                 WORD                    idType;       // resource type (1 for icons)\r
220                 WORD                    idCount;      // how many images?\r
221                 ICONDIRENTRY    idEntries[1]; // the entries for each image\r
222         } ICONDIR, *LPICONDIR;\r
223         #pragma pack(pop, r1)\r
224 };\r
225 \r