ReactOS 0.4.15-dev-8131-g4988de4
pngsup.cpp
Go to the documentation of this file.
1/*
2 * PROJECT: ReactOS uxtheme.dll
3 * LICENSE: LGPL-2.1-or-later (https://spdx.org/licenses/LGPL-2.1-or-later)
4 * PURPOSE: Support for PNG images in visual styles
5 * COPYRIGHT: Copyright 2023 Ethan Rodensky <splitwirez@gmail.com>
6 */
7
8#include "uxthemep.h"
9
10#include <ole2.h>
11#include <objidl.h>
12#include <gdiplus.h>
13#include <gdipluscolor.h>
14#include <winreg.h>
15#include <shlwapi.h>
16
17BOOL
19 _In_ HINSTANCE hTheme,
20 _In_ LPCWSTR szFile,
22 _Out_ HBITMAP *phBitmap)
23{
24 BOOL ret = FALSE;
25
26 HRSRC hRes = FindResourceW(hTheme, szFile, type);
27 if (!hRes)
28 return FALSE;
29
30 HGLOBAL hAlloc = LoadResource(hTheme, hRes);
31 if (!hAlloc)
32 return FALSE;
33
34 DWORD dwSize = SizeofResource(hTheme, hRes);
35 LPVOID pData = LockResource(hAlloc);
36 if ((!pData) || (dwSize <= 0))
37 {
38 FreeResource(hAlloc);
39 return FALSE;
40 }
41
43 if (stream)
44 {
45 Gdiplus::Bitmap* gdipBitmap = Gdiplus::Bitmap::FromStream(stream, FALSE);
46 stream->Release();
47 if (gdipBitmap)
48 {
49 ret = gdipBitmap->GetHBITMAP(Gdiplus::Color(0, 0, 0, 0), phBitmap) == Gdiplus::Ok;
50 delete gdipBitmap;
51 }
52 }
53
55 FreeResource(hAlloc);
56 return ret;
57}
58
59BOOL
61 _In_ HBITMAP png,
62 _Out_ BOOL* hasAlpha)
63{
65 int n;
66 BYTE* p;
67
68 *hasAlpha = FALSE;
69
70 if (!png || GetObjectW( png, sizeof(dib), &dib ) != sizeof(dib))
71 return FALSE;
72
73 if (dib.dsBm.bmBitsPixel != 32)
74 /* nothing to do */
75 return TRUE;
76
77 p = (BYTE*)dib.dsBm.bmBits;
78 n = dib.dsBmih.biHeight * dib.dsBmih.biWidth;
79 while (n-- > 0)
80 {
81 int a = p[3] + 1;
82 if (a < 256)
83 {
84 p[0] = MulDiv(p[0], 256, a);
85 p[1] = MulDiv(p[1], 256, a);
86 p[2] = MulDiv(p[2], 256, a);
87
88 *hasAlpha = TRUE;
89 }
90 p += 4;
91 }
92
93 return TRUE;
94}
while(CdLookupNextInitialFileDirent(IrpContext, Fcb, FileContext))
#define TRUE
Definition: types.h:120
#define FALSE
Definition: types.h:117
BOOL WINAPI FreeResource(HGLOBAL handle)
Definition: res.c:559
HRSRC WINAPI FindResourceW(HINSTANCE hModule, LPCWSTR name, LPCWSTR type)
Definition: res.c:176
DWORD WINAPI SizeofResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:568
LPVOID WINAPI LockResource(HGLOBAL handle)
Definition: res.c:550
HGLOBAL WINAPI LoadResource(HINSTANCE hModule, HRSRC hRsrc)
Definition: res.c:532
unsigned int BOOL
Definition: ntddk_ex.h:94
unsigned long DWORD
Definition: ntddk_ex.h:95
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
GLdouble n
Definition: glext.h:7729
GLfloat GLfloat p
Definition: glext.h:8902
GLboolean GLboolean GLboolean GLboolean a
Definition: glext.h:6204
PSDBQUERYRESULT_VISTA PVOID DWORD * dwSize
Definition: env.c:56
static HBITMAP
Definition: button.c:44
static const BYTE dib[]
Definition: ole2.c:201
#define _Out_
Definition: ms_sal.h:345
#define _In_
Definition: ms_sal.h:308
INT WINAPI MulDiv(INT nNumber, INT nNumerator, INT nDenominator)
Definition: muldiv.c:25
BOOL MSSTYLES_TryLoadPng(_In_ HINSTANCE hTheme, _In_ LPCWSTR szFile, _In_ LPCWSTR type, _Out_ HBITMAP *phBitmap)
Definition: pngsup.cpp:18
BOOL prepare_png_alpha(_In_ HBITMAP png, _Out_ BOOL *hasAlpha)
Definition: pngsup.cpp:60
IStream *WINAPI SHCreateMemStream(const BYTE *lpbData, UINT dwDataLen)
Definition: regstream.c:652
Definition: parse.h:23
TW_UINT32 TW_UINT16 TW_UINT16 TW_MEMREF pData
Definition: twain.h:1830
int ret
#define UnlockResource(handle)
Definition: winbase.h:3372
int WINAPI GetObjectW(_In_ HANDLE h, _In_ int c, _Out_writes_bytes_opt_(c) LPVOID pv)
const WCHAR * LPCWSTR
Definition: xmlstorage.h:185
unsigned char BYTE
Definition: xxhash.c:193