Skip to content

Commit 9878efe

Browse files
committed
Add fallback to thumbnail if composite image is all white
1 parent 2867734 commit 9878efe

3 files changed

Lines changed: 52 additions & 42 deletions

File tree

PsdThumbnailProvider/GetThumbnail.cpp

Lines changed: 50 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -134,15 +134,15 @@ HBITMAP GetPSDThumbnail(IStream* stream) {
134134
int dataLength = width * height * 4;
135135
BYTE* data = new BYTE[dataLength];
136136

137+
for (int i = 0; i < dataLength; i++) {
138+
data[i] = 0xff;
139+
}
140+
137141
if (compression == 1) {
138142
USHORT* lengths = new USHORT[channelCount * height];
139143
int step = 4;
140144
int maxLength = 0;
141145

142-
for (int i = 0; i < dataLength; i++) {
143-
data[i] = 0xff;
144-
}
145-
146146
for (int o = 0, li = 0; o < channelCount; o++) {
147147
for (int y = 0; y < height; y++, li++) {
148148
lengths[li] = ReadUInt16(stream);
@@ -191,7 +191,6 @@ HBITMAP GetPSDThumbnail(IStream* stream) {
191191
delete lengths;
192192
delete buffer;
193193
} else {
194-
memset(data, 0xff, width * height * 4);
195194
int pixels = width * height;
196195
BYTE* buffer = new BYTE[pixels];
197196
if (channelCount > 4) channelCount = 4;
@@ -208,45 +207,56 @@ HBITMAP GetPSDThumbnail(IStream* stream) {
208207
delete buffer;
209208
}
210209

211-
if (width > 256 || height > 256) {
212-
HBITMAP fullBitmap = CreateBitmap(width, height, 1, 32, data);
213-
int thumbWidth, thumbHeight;
210+
bool allWhite = true;
211+
212+
for (int i = 0; i < dataLength; i++) {
213+
if (data[i] != 0xff) {
214+
allWhite = false;
215+
break;
216+
}
217+
}
218+
219+
if (!allWhite) {
220+
if (width > 256 || height > 256) {
221+
HBITMAP fullBitmap = CreateBitmap(width, height, 1, 32, data);
222+
int thumbWidth, thumbHeight;
214223

215-
if (width > height) {
216-
thumbWidth = 256;
217-
thumbHeight = height * thumbWidth / width;
224+
if (width > height) {
225+
thumbWidth = 256;
226+
thumbHeight = height * thumbWidth / width;
227+
}
228+
else {
229+
thumbHeight = 256;
230+
thumbWidth = width * thumbHeight / height;
231+
}
232+
233+
BLENDFUNCTION fnc;
234+
fnc.BlendOp = AC_SRC_OVER;
235+
fnc.BlendFlags = 0;
236+
fnc.SourceConstantAlpha = 0xFF;
237+
fnc.AlphaFormat = AC_SRC_ALPHA;
238+
239+
HDC dc = GetDC(NULL);
240+
HDC srcDC = CreateCompatibleDC(dc);
241+
HDC memDC = CreateCompatibleDC(dc);
242+
result = CreateCompatibleBitmap(dc, thumbWidth, thumbHeight);
243+
SelectObject(memDC, result);
244+
SelectObject(srcDC, fullBitmap);
245+
246+
RECT rect = {};
247+
rect.right = thumbWidth;
248+
rect.bottom = thumbHeight;
249+
FillRect(memDC, &rect, (HBRUSH)GetStockObject(NULL_BRUSH));
250+
AlphaBlend(memDC, 0, 0, thumbWidth, thumbHeight, srcDC, 0, 0, width, height, fnc);
251+
252+
DeleteObject(fullBitmap);
253+
DeleteDC(srcDC);
254+
DeleteDC(memDC);
255+
ReleaseDC(NULL, dc);
218256
}
219257
else {
220-
thumbHeight = 256;
221-
thumbWidth = width * thumbHeight / height;
258+
result = CreateBitmap(width, height, 1, 32, data);
222259
}
223-
224-
BLENDFUNCTION fnc;
225-
fnc.BlendOp = AC_SRC_OVER;
226-
fnc.BlendFlags = 0;
227-
fnc.SourceConstantAlpha = 0xFF;
228-
fnc.AlphaFormat = AC_SRC_ALPHA;
229-
230-
HDC dc = GetDC(NULL);
231-
HDC srcDC = CreateCompatibleDC(dc);
232-
HDC memDC = CreateCompatibleDC(dc);
233-
result = CreateCompatibleBitmap(dc, thumbWidth, thumbHeight);
234-
SelectObject(memDC, result);
235-
SelectObject(srcDC, fullBitmap);
236-
237-
RECT rect = {};
238-
rect.right = thumbWidth;
239-
rect.bottom = thumbHeight;
240-
FillRect(memDC, &rect, (HBRUSH)GetStockObject(NULL_BRUSH));
241-
AlphaBlend(memDC, 0, 0, thumbWidth, thumbHeight, srcDC, 0, 0, width, height, fnc);
242-
243-
DeleteObject(fullBitmap);
244-
DeleteDC(srcDC);
245-
DeleteDC(memDC);
246-
ReleaseDC(NULL, dc);
247-
}
248-
else {
249-
result = CreateBitmap(width, height, 1, 32, data);
250260
}
251261

252262
delete data;

Setup/Product.wxs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?xml version="1.0" encoding="UTF-8"?>
22
<Wix xmlns="http://schemas.microsoft.com/wix/2006/wi">
3-
<Product Id="*" Name="PSD Thumbnail Provider" Language="1033" Version="1.4.0.0" Manufacturer="Agamnentzar" UpgradeCode="cb88ff56-355c-48fd-8343-8d3f40ef02ce">
3+
<Product Id="*" Name="PSD Thumbnail Provider" Language="1033" Version="1.5.0.0" Manufacturer="Agamnentzar" UpgradeCode="cb88ff56-355c-48fd-8343-8d3f40ef02ce">
44
<Package InstallerVersion="200" Compressed="yes" InstallScope="perMachine" />
55
<Condition Message="You need to be an administrator to install this product.">Privileged</Condition>
66
<MajorUpgrade Schedule="afterInstallInitialize" DowngradeErrorMessage="A newer version of PSD Thumbnail Provider is already installed." />

Test/main.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LRESULT CALLBACK windowProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lPara
99
switch (message) {
1010
case WM_CREATE: {
1111
IStream* stream;
12-
SHCreateStreamOnFileEx(L"test6.psd", STGM_READ, FILE_ATTRIBUTE_NORMAL, false, NULL, &stream);
12+
SHCreateStreamOnFileEx(L"test4.psd", STGM_READ, FILE_ATTRIBUTE_NORMAL, false, NULL, &stream);
1313
bitmap = GetPSDThumbnail(stream);
1414
BITMAP bm = {};
1515
GetObject(bitmap, sizeof(bm), &bm);

0 commit comments

Comments
 (0)