Leaked source code of windows server 2003
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.

271 lines
8.9 KiB

  1. #ifndef UNIX
  2. #pragma pack( push, PNG )
  3. #pragma pack( 1 )
  4. #endif
  5. typedef struct _PNGCHUNKHEADER
  6. {
  7. ULONG nDataLength;
  8. DWORD dwChunkType;
  9. } PNGCHUNKHEADER;
  10. typedef struct _PNGIHDRDATA
  11. {
  12. ULONG nWidth;
  13. ULONG nHeight;
  14. BYTE nBitDepth;
  15. BYTE bColorType;
  16. BYTE bCompressionMethod;
  17. BYTE bFilterMethod;
  18. BYTE bInterlaceMethod;
  19. } PNGIHDRDATA;
  20. #ifndef UNIX
  21. #pragma pack( pop, PNG )
  22. #endif
  23. typedef struct _PNG_INTERLACE_INFO
  24. {
  25. ULONG nDeltaX;
  26. ULONG nDeltaY;
  27. ULONG nPixelWidth;
  28. ULONG nPixelHeight;
  29. ULONG iFirstX;
  30. ULONG iFirstY;
  31. BYTE anPixelsInPartialBlock[8];
  32. BYTE anScanLinesInPartialBlock[8];
  33. } PNG_INTERLACE_INFO;
  34. typedef struct _FLOATRGB
  35. {
  36. float fRed;
  37. float fGreen;
  38. float fBlue;
  39. } FLOATRGB;
  40. typedef void (*PNGCOPYSCANLINEPROC)( void* pDest, const void* pSrc,
  41. ULONG nPixels, ULONG nDeltaXDest, const FLOATRGB* pfrgbBackground,
  42. BYTE* pXlate );
  43. typedef void (*PNGDUPLICATESCANLINEPROC)( void* pScanLine, ULONG nDeltaX,
  44. ULONG nFullPixels, ULONG nFullPixelWidth, ULONG nPartialPixelWidth );
  45. typedef struct _PNG_FORMAT_INFO
  46. {
  47. ULONG nPossibleFormats;
  48. const GUID* pPossibleFormats;
  49. const PNGCOPYSCANLINEPROC* ppfnCopyScanLineProcs;
  50. const PNGDUPLICATESCANLINEPROC* ppfnDuplicateScanLineProcs;
  51. } PNG_FORMAT_INFO;
  52. #define PNGCHUNK( a, b, c, d ) \
  53. (MAKELONG( MAKEWORD( (a), (b) ), MAKEWORD( (c), (d) ) ))
  54. const DWORD PNG_CHUNK_IHDR = PNGCHUNK( 'I', 'H', 'D', 'R' );
  55. const DWORD PNG_CHUNK_IEND = PNGCHUNK( 'I', 'E', 'N', 'D' );
  56. const DWORD PNG_CHUNK_IDAT = PNGCHUNK( 'I', 'D', 'A', 'T' );
  57. const DWORD PNG_CHUNK_PLTE = PNGCHUNK( 'P', 'L', 'T', 'E' );
  58. const DWORD PNG_CHUNK_BKGD = PNGCHUNK( 'b', 'K', 'G', 'D' );
  59. const DWORD PNG_CHUNK_TRNS = PNGCHUNK( 't', 'R', 'N', 'S' );
  60. const DWORD PNG_CHUNK_GAMA = PNGCHUNK( 'g', 'A', 'M', 'A' );
  61. const DWORD PNG_CHUNK_ANCILLARY = 0x00000020;
  62. const BYTE PNG_COMPRESSION_DEFLATE32K = 0;
  63. const BYTE PNG_FILTER_ADAPTIVE = 0;
  64. const BYTE PNG_INTERLACE_NONE = 0;
  65. const BYTE PNG_INTERLACE_ADAM7 = 1;
  66. const ULONG PNG_BUFFER_SIZE = 4096;
  67. const DWORD CHUNK_IHDR = 0x01;
  68. const DWORD CHUNK_PLTE = 0x02;
  69. const DWORD CHUNK_POSTPLTE = 0x04;
  70. const DWORD CHUNK_IDAT = 0x08;
  71. const DWORD CHUNK_LASTIDAT = 0x10;
  72. const DWORD CHUNK_IEND = 0x20;
  73. const DWORD CHUNK_BKGD = 0x40;
  74. const DWORD CHUNK_TRNS = 0x80;
  75. const DWORD CHUNK_GAMA = 0x100;
  76. const BYTE PNG_COLORTYPE_PALETTE_MASK = 0x01;
  77. const BYTE PNG_COLORTYPE_COLOR_MASK = 0x02;
  78. const BYTE PNG_COLORTYPE_ALPHA_MASK = 0x04;
  79. const BYTE PNG_COLORTYPE_INDEXED = PNG_COLORTYPE_PALETTE_MASK|
  80. PNG_COLORTYPE_COLOR_MASK;
  81. const BYTE PNG_COLORTYPE_RGB = PNG_COLORTYPE_COLOR_MASK;
  82. const BYTE PNG_COLORTYPE_GRAY = 0x00;
  83. const BYTE PNG_COLORTYPE_RGBA = PNG_COLORTYPE_COLOR_MASK|
  84. PNG_COLORTYPE_ALPHA_MASK;
  85. const BYTE PNG_COLORTYPE_GRAYA = PNG_COLORTYPE_ALPHA_MASK;
  86. class CPNGFilter :
  87. public IImageDecodeFilter,
  88. public CComObjectRoot,
  89. public CComCoClass< CPNGFilter, &CLSID_CoPNGFilter >
  90. {
  91. public:
  92. CPNGFilter();
  93. ~CPNGFilter();
  94. BEGIN_COM_MAP( CPNGFilter )
  95. COM_INTERFACE_ENTRY( IImageDecodeFilter )
  96. END_COM_MAP()
  97. DECLARE_NOT_AGGREGATABLE( CPNGFilter )
  98. // Remove the comment from the line above if you don't want your object to
  99. // support aggregation. The default is to support it
  100. DECLARE_REGISTRY( CPNGFilter, _T( "PNGFilter.CoPNGFilter.1" ),
  101. _T( "PNGFilter.CoPNGFilter" ), IDS_COPNGFILTER_DESC, THREADFLAGS_BOTH )
  102. // DECLARE_NO_REGISTRY()
  103. // IImageDecodeFilter
  104. public:
  105. STDMETHOD( Initialize )( IImageDecodeEventSink* pEventSink );
  106. STDMETHOD( Process )( IStream* pStream );
  107. STDMETHOD( Terminate )( HRESULT hrStatus );
  108. protected:
  109. HRESULT BeginImage();
  110. HRESULT ChooseDestinationFormat( GUID* pBFID );
  111. HRESULT DetermineSourceFormat();
  112. HRESULT EatData();
  113. HRESULT FireGetSurfaceEvent();
  114. HRESULT FireOnProgressEvent();
  115. HRESULT NextState();
  116. HRESULT OutputBytes( const BYTE* pData, ULONG nBytes );
  117. HRESULT ChooseBKGD();
  118. HRESULT ProcessBKGD();
  119. HRESULT ProcessIDAT();
  120. HRESULT ProcessIEND();
  121. HRESULT ProcessIHDR();
  122. HRESULT ProcessPLTE();
  123. HRESULT ProcessTRNS();
  124. HRESULT ProcessGAMA();
  125. HRESULT ReadChunkHeader();
  126. HRESULT ReadChunkData();
  127. HRESULT ReadChunkCRC();
  128. HRESULT ReadFileHeader();
  129. HRESULT ReadIDATData();
  130. HRESULT NextPass();
  131. HRESULT NextScanLine();
  132. BOOL BeginPass( ULONG iPass );
  133. HRESULT WriteScanLine();
  134. HRESULT LockBits(RECT *prcBounds, DWORD dwLockFlags, void **ppBits, long *pPitch);
  135. HRESULT UnlockBits(RECT *prcBounds, void *pBits);
  136. void NoneFilterScanLine();
  137. void SubFilterScanLine();
  138. void UpFilterScanLine();
  139. void AverageFilterScanLine();
  140. void PaethFilterScanLine();
  141. protected:
  142. static const PNG_INTERLACE_INFO s_aInterlaceInfoNone[1];
  143. static const PNG_INTERLACE_INFO s_aInterlaceInfoAdam7[7];
  144. static const PNG_FORMAT_INFO s_aFormatInfo[15];
  145. protected:
  146. typedef enum _EInternalState
  147. {
  148. ISTATE_READFILEHEADER,
  149. ISTATE_READCHUNKHEADER,
  150. ISTATE_READCHUNKDATA,
  151. ISTATE_READIDATDATA,
  152. ISTATE_READCHUNKCRC,
  153. ISTATE_PROCESSIHDR,
  154. ISTATE_PROCESSIEND,
  155. ISTATE_PROCESSPLTE,
  156. ISTATE_PROCESSBKGD,
  157. ISTATE_PROCESSTRNS,
  158. ISTATE_PROCESSGAMA,
  159. ISTATE_CHOOSEBKGD,
  160. ISTATE_EATDATA,
  161. ISTATE_DONE
  162. } EInternalState;
  163. typedef enum _ESrcFormat
  164. {
  165. SRC_GRAY_1,
  166. SRC_GRAY_2,
  167. SRC_GRAY_4,
  168. SRC_GRAY_8,
  169. SRC_GRAY_16,
  170. SRC_RGB_24,
  171. SRC_RGB_48,
  172. SRC_INDEXED_RGB_1,
  173. SRC_INDEXED_RGB_2,
  174. SRC_INDEXED_RGB_4,
  175. SRC_INDEXED_RGB_8,
  176. SRC_GRAYA_16,
  177. SRC_GRAYA_32,
  178. SRC_RGBA_32,
  179. SRC_RGBA_64
  180. } ESrcFormat;
  181. EInternalState m_eInternalState; // State of decode state machine
  182. DWORD m_dwEvents; // Events the event sink wants to receive
  183. PNGCOPYSCANLINEPROC m_pfnCopyScanLine;
  184. PNGDUPLICATESCANLINEPROC m_pfnDuplicateScanLine;
  185. const PNG_INTERLACE_INFO* m_pInterlaceInfo;
  186. ULONG m_nFormats; // Number of formats the event sink supports
  187. GUID* m_pFormats; // Formats supported by the event sink
  188. BOOL m_bPalette; // Does image use a palette?
  189. BOOL m_bColor; // Does image use color?
  190. BOOL m_bAlpha; // Does image have an alpha channel
  191. BOOL m_bSurfaceUsesAlpha;
  192. BOOL m_bConvertAlpha;
  193. BOOL m_bSkipData;
  194. ESrcFormat m_eSrcFormat; // Source pixel format
  195. DWORD m_dwCRC; // CRC accumulator
  196. DWORD m_dwChunkCRC; // Stored CRC of current chunk
  197. ULONG m_nColors; // Number of colors in palette
  198. ULONG m_iBackgroundIndex; // Index of background color
  199. RGBQUAD m_rgbBackground; // Background color
  200. FLOATRGB m_frgbBackground; // Floating-point background color
  201. DWORD m_dwTransKey; // Transparent color key (RGB or indexed
  202. ULONG m_nTransparentColors; // # transparent indices
  203. IStream* m_pStream; // Source stream
  204. CComPtr< IImageDecodeEventSink > m_pEventSink; // Event sink
  205. PNGCHUNKHEADER m_pngChunkHeader; // Header of current chunk
  206. PNGIHDRDATA m_pngIHDR; // IHDR chunk
  207. DWORD m_dwChunksEncountered; // CHUNK_* flags for what chunks have been
  208. // encountered in the image stream so far
  209. CComPtr< IDirectDrawSurface > m_pDDrawSurface;
  210. BOOL m_bFinishedIDAT; // Have we finished the IDAT section?
  211. ULONG m_nBytesLeftInCurrentTask; // Bytes remaining before we switch to a
  212. // new state
  213. ULONG m_nDataBytesRead; // Bytes of chunk data read
  214. ULONG m_iAppend; // Where to append data in buffer
  215. BYTE* m_pbScanLine; // Current decoded scan line (including filter byte)
  216. BYTE* m_pbPrevScanLine; // Previous decoded scan line
  217. ULONG m_iPass; // Current pass
  218. ULONG m_nPasses; // Number of passes
  219. ULONG m_nBytesInScanLine; // Number of bytes in one scan line
  220. ULONG m_nPixelsInScanLine; // Number of pixels in one scan line
  221. ULONG m_nBitsPerPixel; // Bits per pixel in source image
  222. BOOL m_bExpandPixels; // Expand interlaced pixels?
  223. ULONG m_iScanLine; // Current scan line
  224. ULONG m_nScanLinesInPass; // Number of scan lines in current pass
  225. ULONG m_iScanLineInPass; // Current scan line in pass
  226. ULONG m_iFirstStaleScanLine; // First scan line whose progress has not been
  227. // reported
  228. ULONG m_nBPP; // Bytes per pixel
  229. ULONG m_nDeltaX; // Horizontal distance between pixels
  230. ULONG m_nDeltaY; // Vertical distance between pixels
  231. ULONG m_nPixelWidth; // Width of a pixel
  232. ULONG m_nPixelHeight; // Height of a pixel
  233. ULONG m_iFirstX; // Horizontal position of first pixel in scan line
  234. ULONG m_iFirstY; // Vertical position of first scan line in pass
  235. ULONG m_nFullPixelsInScanLine;
  236. ULONG m_nPartialPixelWidth;
  237. z_stream m_zlibStream; // ZLib data
  238. BYTE m_abData[PNG_BUFFER_SIZE]; // Data buffer
  239. BYTE m_abTrans[256]; // table to collapse multiple transparent indices
  240. BYTE m_abGamma[256]; // gamma correction table
  241. RGBQUAD m_argbColors[256];
  242. };