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