Change to different return values for troubleshooting
[TortoiseGit.git] / src / Utils / MiscUI / Picture.h
blob82422f19c831605c651b94e3f0540497a68b7883
1 // TortoiseGit - a Windows shell extension for easy version control
3 // Copyright (C) 2003-2007,2009, 2012, 2014 - 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;
27 /**
28 * \ingroup Utils
29 * Class for showing picture files.
30 * Use this class to show pictures of different file formats: BMP, DIB, EMF, GIF, ICO, JPG, WMF
31 * If Gdi+ is installed (default on XP and later, optional on Win2k), other image formats can
32 * be shown too: png, tiff.
33 * The class uses the IPicture interface, the same way as internet explorer does.
35 * Example of usage:
36 * \code
37 * CPicture m_picture;
38 * //load picture data into the IPicture interface
39 * m_picture.Load("Test.jpg"); //load from a file
40 * m_picture.Load(IDR_TEST, "JPG"); //load from a resource
42 * //when using in a dialog based application (CPaintDC dc(this);)
43 * m_picture.UpdateSizeOnDC(&dc); //get picture dimensions in pixels
44 * m_picture.Show(&dc, CPoint(0,0), CPoint(m_picture.m_Width, m_picture.m_Height), 0, 0);
45 * m_picture.Show(&dc, CRect(0,0,100,100)); //change original dimensions
46 * m_picture.ShowBitmapResource(&dc, IDB_TEST, CPoint(0,0)); //show bitmap resource
48 * //when using in a regular mfc application (CDC* pDC)
49 * m_picture.UpdateSizeOnDC(pDC); //get picture dimensions in pixels
50 * m_picture.Show(pDC, CPoint(0,0), CPoint(m_picture.m_Width, m_picture.m_Height), 0, 0);
51 * m_picture.Show(pDC, CRect(0,0,100,100)); //change original dimensions
52 * m_picture.ShowBitmapResource(pDC, IDB_TEST, CPoint(0,0)); //show bitmap resource
54 * //to show picture information
55 * std::string s;
56 * s.Format("Size = %4d\nWidth = %4d\nHeight = %4d\nWeight = %4d\n",
57 * m_picture.m_Weigth, m_picture.m_Width, m_picture.m_Height, m_picture.m_Weight);
58 * AfxMessageBox(s);
59 * \endcode
60 * \remark GDI+ is only used if it is installed. If you link with gdiplus.lib and mark the gdiplus.dll
61 * as a delay-loaded dll, then your application won't throw an error if it isn't installed, but of course
62 * png and tiff images can't be shown.
64 class CPicture
66 public:
67 /**
68 * open a picture file and load it (BMP, DIB, EMF, GIF, ICO, JPG, WMF).
70 * \param sFilePathName the path of the picture file
71 * \return TRUE if succeeded.
73 bool Load(tstring sFilePathName);
74 /**
75 * draws the loaded picture directly to the given device context.
76 * \note
77 * if the given size is not the actual picture size, then the picture will
78 * be drawn stretched to the given dimensions.
79 * \param pDC the device context to draw on
80 * \param DrawRect the dimensions to draw the picture on
81 * \return TRUE if succeeded
83 bool Show(HDC hDC, RECT DrawRect);
84 /**
85 * get the original picture pixel size. A pointer to a device context is needed
86 * for the pixel calculation (DPI). Also updates the classes height and width
87 * members.
88 * \param pDC the device context to perform the calculations on
89 * \return TRUE if succeeded
91 bool UpdateSizeOnDC(HDC hDC);
93 /**
94 * Return the horizontal resolutions in dpi of the loaded picture.
95 * \remark this only works if gdi+ is installed.
97 float GetHorizontalResolution() {return pBitmap ? pBitmap->GetHorizontalResolution() : 0.0f;}
98 /**
99 * Return the vertical resolution in dpi of the loaded picture.
100 * \remark this only works if gdi+ is installed.
102 float GetVerticalResolution() {return pBitmap ? pBitmap->GetVerticalResolution() : 0.0f;}
104 * Returns the picture height in pixels.
105 * \remark this only works if gdi+ is installed.
107 UINT GetHeight() const;
109 * Returns the picture width in pixels.
110 * \remark this only works if gdi+ is installed.
112 UINT GetWidth() const;
114 * Returns the pixel format of the loaded picture.
115 * \remark this only works if gdi+ is installed.
117 PixelFormat GetPixelFormat() const;
119 * Returns the color depth in bits.
120 * \remark this only works if gdi+ is installed.
122 UINT GetColorDepth() const;
125 * Sets the interpolation used for drawing the image.
126 * The interpolation mode is one of the following:
127 * InterpolationModeInvalid
128 * InterpolationModeDefault
129 * InterpolationModeLowQuality
130 * InterpolationModeHighQuality
131 * InterpolationModeBilinear
132 * InterpolationModeBicubic
133 * InterpolationModeNearestNeighbor
134 * InterpolationModeHighQualityBilinear
135 * InterpolationModeHighQualityBicubic
137 void SetInterpolationMode(InterpolationMode ip) {m_ip = ip;}
140 * Returns the number of frames in the specified dimension of the image.
142 UINT GetNumberOfFrames(int dimension);
144 * Returns the number of dimensions in the image.
145 * For example, icons can have multiple dimensions (sizes).
147 UINT GetNumberOfDimensions();
150 * Sets the active frame which is used when drawing the image.
151 * \return the delay value for this frame, i.e. how long this frame
152 * should be shown.
154 long SetActiveFrame(UINT frame);
156 DWORD GetFileSize() const {return m_nSize;}
157 tstring GetFileSizeAsText(bool bAbbrev = true);
158 CPicture();
159 virtual ~CPicture();
162 HGLOBAL hGlobal;
164 IPicture* m_IPicture; ///< Same As LPPICTURE (typedef IPicture __RPC_FAR *LPPICTURE)
166 LONG m_Height; ///< Height (in pixels)
167 LONG m_Width; ///< Width (in pixels)
168 UINT m_ColorDepth;///< the color depth
169 LONG m_Weight; ///< Size Of The Image Object In Bytes (File OR Resource)
170 tstring m_Name; ///< The FileName of the Picture as used in Load()
172 protected:
174 * reads the picture data from a source and loads it into the current IPicture object in use.
175 * \param pBuffer buffer of data source
176 * \param nSize the size of the buffer
177 * \return TRUE if succeeded
179 bool LoadPictureData(BYTE* pBuffer, int nSize);
182 * frees the allocated memory that holds the IPicture interface data and
183 * clear picture information
185 void FreePictureData();
187 private:
188 GdiplusStartupInput gdiplusStartupInput;
189 ULONG_PTR gdiplusToken;
190 Bitmap * pBitmap;
191 bool bHaveGDIPlus;
192 InterpolationMode m_ip;
193 bool bIsIcon;
194 bool bIsTiff;
195 UINT nCurrentIcon;
196 BYTE * lpIcons;
197 HICON * hIcons;
198 DWORD m_nSize;
200 #pragma pack(push, r1, 2) // n = 16, pushed to stack
202 typedef struct
204 BYTE bWidth; // Width of the image
205 BYTE bHeight; // Height of the image (times 2)
206 BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
207 BYTE bReserved; // Reserved
208 WORD wPlanes; // Color Planes
209 WORD wBitCount; // Bits per pixel
210 DWORD dwBytesInRes; // how many bytes in this resource?
211 DWORD dwImageOffset; // where in the file is this image
212 } ICONDIRENTRY, *LPICONDIRENTRY;
213 typedef struct
215 WORD idReserved; // Reserved
216 WORD idType; // resource type (1 for icons)
217 WORD idCount; // how many images?
218 ICONDIRENTRY idEntries[1]; // the entries for each image
219 } ICONDIR, *LPICONDIR;
220 #pragma pack(pop, r1)