Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

211 lines
6.6 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORPORATION, 1998
  4. *
  5. * TITLE: GPHELPER.H
  6. *
  7. * VERSION: 1.0
  8. *
  9. * AUTHOR: ShaunIv
  10. *
  11. * DATE: 10/11/1999
  12. *
  13. * DESCRIPTION: Encapsulation of common GDI plus operationss
  14. *
  15. *******************************************************************************/
  16. #ifndef __GPHELPER_H_INCLUDED
  17. #define __GPHELPER_H_INCLUDED
  18. #include <windows.h>
  19. #include <simstr.h>
  20. #include <gdiplus.h>
  21. #include <simarray.h>
  22. #include <gdiplusinit.h>
  23. class CGdiPlusInit
  24. {
  25. ULONG_PTR m_pGdiplusToken;
  26. public:
  27. CGdiPlusInit();
  28. ~CGdiPlusInit();
  29. };
  30. class CGdiPlusHelper
  31. {
  32. private:
  33. CGdiPlusInit m_Init;
  34. Gdiplus::ImageCodecInfo *m_pImageEncoderInfo;
  35. UINT m_nImageEncoderCount;
  36. Gdiplus::ImageCodecInfo *m_pImageDecoderInfo;
  37. UINT m_nImageDecoderCount;
  38. private:
  39. CGdiPlusHelper( const CGdiPlusHelper & );
  40. CGdiPlusHelper &operator=( const CGdiPlusHelper & );
  41. public:
  42. CGdiPlusHelper(void);
  43. ~CGdiPlusHelper(void);
  44. protected:
  45. HRESULT Initialize(void);
  46. static HRESULT ConstructCodecExtensionSearchStrings( CSimpleString &strExtensions, Gdiplus::ImageCodecInfo *pImageCodecInfo, UINT nImageCodecCount );
  47. void Destroy(void);
  48. public:
  49. bool IsValid(void) const;
  50. HRESULT ConstructDecoderExtensionSearchStrings( CSimpleString &strExtensions );
  51. HRESULT ConstructEncoderExtensionSearchStrings( CSimpleString &strExtensions );
  52. HRESULT GetClsidOfEncoder( const GUID &guidFormatId, CLSID &clsidFormat ) const;
  53. HRESULT GetClsidOfDecoder( const GUID &guidFormatId, CLSID &clsidFormat ) const;
  54. HRESULT Convert( LPCWSTR pszInputFilename, LPCWSTR pszOutputFilename, const CLSID &guidOutputFormat ) const;
  55. HRESULT Rotate( LPCWSTR pszInputFilename, LPCWSTR pszOutputFilename, int nRotationAngle, const CLSID &guidOutputFormat ) const;
  56. HRESULT Rotate( HBITMAP hSourceBitmap, HBITMAP &hTargetBitmap, int nRotation ) const;
  57. HRESULT LoadAndScale( HBITMAP &hTargetBitmap, LPCTSTR pszFilename, UINT nMaxWidth=0, UINT nMaxHeight=0, bool bStretchSmallImages=false );
  58. HRESULT LoadAndScale( HBITMAP &hTargetBitmap, IStream *pStream, UINT nMaxWidth=0, UINT nMaxHeight=0, bool bStretchSmallImages=false );
  59. HRESULT SaveMultipleImagesAsMultiPage( const CSimpleDynamicArray<CSimpleStringWide> &Filenames, const CSimpleStringWide &strFilename, const CLSID &guidOutputFormat );
  60. HRESULT SetBrightnessAndContrast( HBITMAP hSourceBitmap, HBITMAP &hTargetBitmap, BYTE nBrightness, BYTE nContrast );
  61. HRESULT SetThreshold( HBITMAP hSourceBitmap, HBITMAP &hTargetBitmap, BYTE nThreshold );
  62. static Gdiplus::EncoderParameters *AppendEncoderParameter( Gdiplus::EncoderParameters *pEncoderParameters, const GUID &guidProp, ULONG nType, PVOID pVoid );
  63. static inline GDISTATUS_TO_HRESULT(Gdiplus::Status status)
  64. {
  65. //
  66. // Default to turning GDI+ errors into generic failures
  67. //
  68. HRESULT hr = E_FAIL;
  69. switch( status )
  70. {
  71. case Gdiplus::Ok:
  72. hr = S_OK;
  73. break;
  74. case Gdiplus::InvalidParameter:
  75. hr = E_INVALIDARG;
  76. break;
  77. case Gdiplus::OutOfMemory:
  78. hr = E_OUTOFMEMORY;
  79. break;
  80. case Gdiplus::InsufficientBuffer:
  81. hr = HRESULT_FROM_WIN32(ERROR_INSUFFICIENT_BUFFER);
  82. break;
  83. case Gdiplus::Aborted:
  84. hr = E_ABORT;
  85. break;
  86. case Gdiplus::ObjectBusy:
  87. hr = E_PENDING;
  88. break;
  89. case Gdiplus::FileNotFound:
  90. hr = HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND);
  91. break;
  92. case Gdiplus::AccessDenied:
  93. hr = E_ACCESSDENIED;
  94. break;
  95. case Gdiplus::UnknownImageFormat:
  96. hr = HRESULT_FROM_WIN32(ERROR_INVALID_PIXEL_FORMAT);
  97. break;
  98. case Gdiplus::NotImplemented:
  99. hr = E_NOTIMPL;
  100. break;
  101. case Gdiplus::Win32Error:
  102. hr = HRESULT_FROM_WIN32(GetLastError());
  103. break;
  104. case Gdiplus::ValueOverflow:
  105. case Gdiplus::FontFamilyNotFound:
  106. case Gdiplus::FontStyleNotFound:
  107. case Gdiplus::NotTrueTypeFont:
  108. case Gdiplus::UnsupportedGdiplusVersion:
  109. case Gdiplus::GdiplusNotInitialized:
  110. case Gdiplus::WrongState:
  111. break;
  112. }
  113. return hr;
  114. }
  115. };
  116. class CImageFileFormatVerifier
  117. {
  118. private:
  119. //
  120. // Internal class used to store the file signatures
  121. //
  122. class CImageFileFormatVerifierItem
  123. {
  124. private:
  125. PBYTE m_pSignature;
  126. PBYTE m_pMask;
  127. int m_nLength;
  128. GUID m_guidFormat;
  129. CLSID m_clsidDecoder;
  130. public:
  131. //
  132. // Constructors, assignment operator and destructor
  133. //
  134. CImageFileFormatVerifierItem(void);
  135. CImageFileFormatVerifierItem( const PBYTE pSignature, const PBYTE pMask, int nLength, const GUID &guidFormat, const CLSID &guidDecoder );
  136. CImageFileFormatVerifierItem( const CImageFileFormatVerifierItem &other );
  137. CImageFileFormatVerifierItem &operator=( const CImageFileFormatVerifierItem &other );
  138. CImageFileFormatVerifierItem &Assign( const PBYTE pSignature, const PBYTE pMask, int nLength, const GUID &Format, const CLSID &guidDecoder );
  139. ~CImageFileFormatVerifierItem(void);
  140. protected:
  141. void Destroy(void);
  142. public:
  143. //
  144. // Accessor functions
  145. //
  146. PBYTE Signature(void) const;
  147. PBYTE Mask(void) const;
  148. int Length(void) const;
  149. GUID Format(void) const;
  150. CLSID Decoder(void) const;
  151. //
  152. // Does this stream of bytes match this format?
  153. //
  154. bool Match( PBYTE pBytes, int nLen ) const;
  155. };
  156. private:
  157. CSimpleDynamicArray<CImageFileFormatVerifierItem> m_FileFormatVerifierList;
  158. int m_nMaxSignatureLength;
  159. PBYTE m_pSignatureBuffer;
  160. private:
  161. CImageFileFormatVerifier( const CImageFileFormatVerifier & );
  162. CImageFileFormatVerifier &operator=( const CImageFileFormatVerifier & );
  163. public:
  164. CImageFileFormatVerifier(void);
  165. ~CImageFileFormatVerifier(void);
  166. void Destroy(void);
  167. bool IsValid(void) const;
  168. bool IsImageFile( LPCTSTR pszFilename );
  169. bool IsSupportedImageFromStream( IStream * pStream, GUID * pGuidOfFormat = NULL );
  170. GUID GetImageType( IStream * pStream );
  171. };
  172. #endif