|
| 1 | +#include "PsdThumbnailProvider.h" |
| 2 | +#include "gdiplus.h" |
| 3 | +#include <Shlwapi.h> |
| 4 | + |
| 5 | +#pragma comment(lib, "Shlwapi.lib") |
| 6 | + |
| 7 | +using namespace Gdiplus; |
| 8 | + |
| 9 | +extern HINSTANCE g_hInst; |
| 10 | +extern long g_cDllRef; |
| 11 | + |
| 12 | +void ReadData(IStream *pStream, BYTE *data, ULONG length) { |
| 13 | + ULONG read, total = 0; |
| 14 | + HRESULT hr; |
| 15 | + |
| 16 | + do { |
| 17 | + hr = pStream->Read(data + total, length - total, &read); |
| 18 | + total += read; |
| 19 | + } while (total < length && hr == S_OK); |
| 20 | +} |
| 21 | + |
| 22 | +UINT ReadUInt32(IStream *pStream) { |
| 23 | + BYTE b[4]; |
| 24 | + ReadData(pStream, b, 4); |
| 25 | + return b[0] << 24 | b[1] << 16 | b[2] << 8 | b[3]; |
| 26 | +} |
| 27 | + |
| 28 | +USHORT ReadUInt16(IStream *pStream) { |
| 29 | + BYTE b[2]; |
| 30 | + ReadData(pStream, b, 2); |
| 31 | + return b[0] << 8 | b[1]; |
| 32 | +} |
| 33 | + |
| 34 | +BYTE ReadByte(IStream *pStream) { |
| 35 | + BYTE b; |
| 36 | + ReadData(pStream, &b, 1); |
| 37 | + return b; |
| 38 | +} |
| 39 | + |
| 40 | +void Seek(IStream *pStream, int offset, DWORD origin) { |
| 41 | + LARGE_INTEGER pos; |
| 42 | + pos.QuadPart = offset; |
| 43 | + pStream->Seek(pos, origin, NULL); |
| 44 | +} |
| 45 | + |
| 46 | +PsdThumbnailProvider::PsdThumbnailProvider() : m_cRef(1), m_pStream(NULL) { |
| 47 | + InterlockedIncrement(&g_cDllRef); |
| 48 | +} |
| 49 | + |
| 50 | +PsdThumbnailProvider::~PsdThumbnailProvider() { |
| 51 | + InterlockedDecrement(&g_cDllRef); |
| 52 | +} |
| 53 | + |
| 54 | +// IUnknown |
| 55 | + |
| 56 | +IFACEMETHODIMP PsdThumbnailProvider::QueryInterface(REFIID riid, void **ppv) { |
| 57 | + static const QITAB qit[] = |
| 58 | + { |
| 59 | + QITABENT(PsdThumbnailProvider, IThumbnailProvider), |
| 60 | + QITABENT(PsdThumbnailProvider, IInitializeWithStream), |
| 61 | + { 0 }, |
| 62 | + }; |
| 63 | + return QISearch(this, qit, riid, ppv); |
| 64 | +} |
| 65 | + |
| 66 | +IFACEMETHODIMP_(ULONG) PsdThumbnailProvider::AddRef() { |
| 67 | + return InterlockedIncrement(&m_cRef); |
| 68 | +} |
| 69 | + |
| 70 | +IFACEMETHODIMP_(ULONG) PsdThumbnailProvider::Release() { |
| 71 | + ULONG cRef = InterlockedDecrement(&m_cRef); |
| 72 | + |
| 73 | + if (0 == cRef) |
| 74 | + delete this; |
| 75 | + |
| 76 | + return cRef; |
| 77 | +} |
| 78 | + |
| 79 | +// IInitializeWithStream |
| 80 | + |
| 81 | +IFACEMETHODIMP PsdThumbnailProvider::Initialize(IStream *pStream, DWORD grfMode) { |
| 82 | + HRESULT hr = HRESULT_FROM_WIN32(ERROR_ALREADY_INITIALIZED); |
| 83 | + |
| 84 | + if (m_pStream == NULL) |
| 85 | + hr = pStream->QueryInterface(&m_pStream); |
| 86 | + |
| 87 | + return hr; |
| 88 | +} |
| 89 | + |
| 90 | +// IThumbnailProvider |
| 91 | + |
| 92 | +IFACEMETHODIMP PsdThumbnailProvider::GetThumbnail(UINT cx, HBITMAP *phbmp, WTS_ALPHATYPE *pdwAlpha) { |
| 93 | + *phbmp = NULL; |
| 94 | + *pdwAlpha = WTSAT_ARGB; |
| 95 | + |
| 96 | + Seek(m_pStream, 4 + 2 + 6 + 2 + 4 + 4 + 2 + 2, STREAM_SEEK_SET); |
| 97 | + |
| 98 | + UINT length = ReadUInt32(m_pStream); |
| 99 | + |
| 100 | + if (length > 0) |
| 101 | + Seek(m_pStream, length, STREAM_SEEK_CUR); |
| 102 | + |
| 103 | + UINT resourcesLength = ReadUInt32(m_pStream); |
| 104 | + UINT resourceAdvanced = 0; |
| 105 | + |
| 106 | + while (resourcesLength > resourceAdvanced) { |
| 107 | + Seek(m_pStream, 4, STREAM_SEEK_CUR); |
| 108 | + |
| 109 | + USHORT id = ReadUInt16(m_pStream); |
| 110 | + BYTE strl = ReadByte(m_pStream); |
| 111 | + |
| 112 | + Seek(m_pStream, strl % 2 == 0 ? strl + 1 : strl, STREAM_SEEK_CUR); |
| 113 | + |
| 114 | + length = ReadUInt32(m_pStream); |
| 115 | + |
| 116 | + if (id == 1036) { |
| 117 | + ULONG_PTR token; |
| 118 | + GdiplusStartupInput input; |
| 119 | + |
| 120 | + if (Ok == GdiplusStartup(&token, &input, NULL)) { |
| 121 | + Seek(m_pStream, 4 + 4 + 4 + 4 + 4 + 4 + 2 + 2, STREAM_SEEK_CUR); |
| 122 | + |
| 123 | + BYTE *data = new BYTE[length]; |
| 124 | + |
| 125 | + ReadData(m_pStream, data, length); |
| 126 | + |
| 127 | + IStream *memory = SHCreateMemStream(data, length); |
| 128 | + |
| 129 | + Seek(memory, 0, STREAM_SEEK_SET); |
| 130 | + |
| 131 | + Bitmap *bitmap = Bitmap::FromStream(memory); |
| 132 | + Color color(0, 255, 255, 255); |
| 133 | + bitmap->GetHBITMAP(color, phbmp); |
| 134 | + delete bitmap; |
| 135 | + } |
| 136 | + |
| 137 | + GdiplusShutdown(token); |
| 138 | + break; |
| 139 | + } |
| 140 | + |
| 141 | + Seek(m_pStream, length % 2 ? length + 1 : length, STREAM_SEEK_CUR); |
| 142 | + |
| 143 | + resourceAdvanced += 6 + 1 + (strl % 2 == 0 ? strl + 1 : strl) + |
| 144 | + 4 + (length % 2 ? length + 1 : length); |
| 145 | + } |
| 146 | + |
| 147 | + return *phbmp != NULL ? NOERROR : E_NOTIMPL; |
| 148 | +} |
0 commit comments