Advertisement
Guest User

Untitled

a guest
Jun 24th, 2016
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.35 KB | None | 0 0
  1. #pragma region IInitializeWithStream
  2.  
  3. // Initializes the thumbnail handler with a stream.
  4. IFACEMETHODIMP ThumbnailProvider::Initialize(IStream *pStream, DWORD grfMode)
  5. {
  6. std::ofstream st;
  7. // A handler instance should be initialized only once in its lifetime.
  8. HRESULT hr = HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED);
  9. if (m_pStream == NULL)
  10. {
  11. // Take a reference to the stream if it has not been initialized yet.
  12. hr = pStream->QueryInterface(&m_pStream);
  13. }
  14. return hr;
  15. }
  16.  
  17. #pragma endregion
  18.  
  19.  
  20. #pragma region IThumbnailProvider
  21.  
  22. // Gets a thumbnail image and alpha type. The GetThumbnail is called with the
  23. // largest desired size of the image, in pixels. Although the parameter is
  24. // called cx, this is used as the maximum size of both the x and y dimensions.
  25. // If the retrieved thumbnail is not square, then the longer axis is limited
  26. // by cx and the aspect ratio of the original image respected. On exit,
  27. // GetThumbnail provides a handle to the retrieved image. It also provides a
  28. // value that indicates the color at of the image and whether it has
  29. // valid alpha ination.
  30. IFACEMETHODIMP ThumbnailProvider::GetThumbnail(UINT cx, HBITMAP *phbmp,
  31. WTS_ALPHATYPE *pdwAlpha) {
  32. stripImage(m_pStream, phbmp, cx);
  33.  
  34. *pdwAlpha = WTSAT_UNKNOWN;
  35.  
  36. return S_OK;
  37. }
  38.  
  39. #pragma endregion
  40. #pragma region Helper Functions
  41.  
  42. // The PNG signature is 137 80 78 71 13 10 26 10. This does not make sense to do backward. I will do it forward.
  43. void ThumbnailProvider::stripImage(IStream *stream, HBITMAP *phbmp, UINT cx) {
  44.  
  45. unsigned long numBytes = 0;
  46. unsigned long *numBytesPtr = &numBytes;
  47. uint8_t b = 0;
  48. uint8_t *buff = &b;
  49. byte vals[8] = { 0 };
  50. STATSTG stt = {};
  51. STATSTG *stat = &stt;
  52. DWORD temp = 1;
  53. stream->Stat(stat, temp);
  54. unsigned long long length = stat->cbSize.QuadPart;
  55. unsigned long long i;
  56. for (i = 0; i<length; i++) {
  57. stream->Read(buff, 1, numBytesPtr);
  58.  
  59. if (*buff == 137) {
  60. vals[0] = 1;
  61. }
  62. else if (*buff == 80 && vals[0]) {
  63. vals[1] = 1;
  64. }
  65. else if (*buff == 78 && vals[1]) {
  66. vals[2] = 1;
  67. }
  68. else if (*buff == 71 && vals[2]) {
  69. vals[3] = 1;
  70. }
  71. else if (*buff == 13 && vals[3]) {
  72. vals[4] = 1;
  73. }
  74. else if (*buff == 10 && vals[4] && !vals[5]) {
  75. vals[5] = 1;
  76. }
  77. else if (*buff == 26 && vals[5]) {
  78. vals[6] = 1;
  79. }
  80. else if (*buff == 10 && vals[6]) {
  81. vals[7] = 1;
  82. i -= 8;
  83. break;
  84. }
  85. else {
  86. memset(vals, 0, 8 * sizeof(vals[0]));
  87. }
  88. }
  89. if (vals[7]) {
  90. LARGE_INTEGER t;
  91. t.QuadPart = i;
  92. stream->Seek(t, STREAM_SEEK_SET, NULL);
  93. char * imgbuff = (char *)malloc(length-i);
  94. stream->Read(imgbuff, length-i, numBytesPtr);
  95. IStream *istrm = NULL;
  96. HGLOBAL glob = ::GlobalAlloc(GHND, length - i);
  97. LPBYTE bt = (LPBYTE)::GlobalLock(glob);
  98. ::GlobalUnlock(glob);
  99. if (SUCCEEDED(::CreateStreamOnHGlobal(glob, FALSE, &istrm))) {
  100. istrm->Write(imgbuff, length - i, numBytesPtr);
  101. CImage *img = NULL;
  102. img->Load(istrm);
  103. *phbmp = (HBITMAP)img->Detach();
  104. }
  105. //istrm->Release();
  106. GlobalFree(glob);
  107.  
  108. }
  109. }
  110. #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement