Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #pragma region IInitializeWithStream
- // Initializes the thumbnail handler with a stream.
- IFACEMETHODIMP ThumbnailProvider::Initialize(IStream *pStream, DWORD grfMode)
- {
- std::ofstream st;
- // A handler instance should be initialized only once in its lifetime.
- HRESULT hr = HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED);
- if (m_pStream == NULL)
- {
- // Take a reference to the stream if it has not been initialized yet.
- hr = pStream->QueryInterface(&m_pStream);
- }
- return hr;
- }
- #pragma endregion
- #pragma region IThumbnailProvider
- // Gets a thumbnail image and alpha type. The GetThumbnail is called with the
- // largest desired size of the image, in pixels. Although the parameter is
- // called cx, this is used as the maximum size of both the x and y dimensions.
- // If the retrieved thumbnail is not square, then the longer axis is limited
- // by cx and the aspect ratio of the original image respected. On exit,
- // GetThumbnail provides a handle to the retrieved image. It also provides a
- // value that indicates the color at of the image and whether it has
- // valid alpha ination.
- IFACEMETHODIMP ThumbnailProvider::GetThumbnail(UINT cx, HBITMAP *phbmp,
- WTS_ALPHATYPE *pdwAlpha) {
- stripImage(m_pStream, phbmp, cx);
- *pdwAlpha = WTSAT_UNKNOWN;
- return S_OK;
- }
- #pragma endregion
- #pragma region Helper Functions
- // 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.
- void ThumbnailProvider::stripImage(IStream *stream, HBITMAP *phbmp, UINT cx) {
- unsigned long numBytes = 0;
- unsigned long *numBytesPtr = &numBytes;
- uint8_t b = 0;
- uint8_t *buff = &b;
- byte vals[8] = { 0 };
- STATSTG stt = {};
- STATSTG *stat = &stt;
- DWORD temp = 1;
- stream->Stat(stat, temp);
- unsigned long long length = stat->cbSize.QuadPart;
- unsigned long long i;
- for (i = 0; i<length; i++) {
- stream->Read(buff, 1, numBytesPtr);
- if (*buff == 137) {
- vals[0] = 1;
- }
- else if (*buff == 80 && vals[0]) {
- vals[1] = 1;
- }
- else if (*buff == 78 && vals[1]) {
- vals[2] = 1;
- }
- else if (*buff == 71 && vals[2]) {
- vals[3] = 1;
- }
- else if (*buff == 13 && vals[3]) {
- vals[4] = 1;
- }
- else if (*buff == 10 && vals[4] && !vals[5]) {
- vals[5] = 1;
- }
- else if (*buff == 26 && vals[5]) {
- vals[6] = 1;
- }
- else if (*buff == 10 && vals[6]) {
- vals[7] = 1;
- i -= 8;
- break;
- }
- else {
- memset(vals, 0, 8 * sizeof(vals[0]));
- }
- }
- if (vals[7]) {
- LARGE_INTEGER t;
- t.QuadPart = i;
- stream->Seek(t, STREAM_SEEK_SET, NULL);
- char * imgbuff = (char *)malloc(length-i);
- stream->Read(imgbuff, length-i, numBytesPtr);
- IStream *istrm = NULL;
- HGLOBAL glob = ::GlobalAlloc(GHND, length - i);
- LPBYTE bt = (LPBYTE)::GlobalLock(glob);
- ::GlobalUnlock(glob);
- if (SUCCEEDED(::CreateStreamOnHGlobal(glob, FALSE, &istrm))) {
- istrm->Write(imgbuff, length - i, numBytesPtr);
- CImage *img = NULL;
- img->Load(istrm);
- *phbmp = (HBITMAP)img->Detach();
- }
- //istrm->Release();
- GlobalFree(glob);
- }
- }
- #pragma endregion
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement