├── _images ├── thumbnails.png ├── reg_command.png └── reg_succeeded.png ├── QOIThumbnailProvider.def ├── .gitignore ├── CMakeLists.txt ├── README.md ├── LICENSE ├── Dll.rc ├── Dll.cpp └── QOIThumbnailProvider.cpp /_images/thumbnails.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iOrange/QOIThumbnailProvider/HEAD/_images/thumbnails.png -------------------------------------------------------------------------------- /_images/reg_command.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iOrange/QOIThumbnailProvider/HEAD/_images/reg_command.png -------------------------------------------------------------------------------- /_images/reg_succeeded.png: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/iOrange/QOIThumbnailProvider/HEAD/_images/reg_succeeded.png -------------------------------------------------------------------------------- /QOIThumbnailProvider.def: -------------------------------------------------------------------------------- 1 | EXPORTS 2 | DllGetClassObject PRIVATE 3 | DllCanUnloadNow PRIVATE 4 | DllRegisterServer PRIVATE 5 | DllUnregisterServer PRIVATE 6 | DllMain PRIVATE 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | CMakeLists.txt.user 2 | CMakeCache.txt 3 | CMakeFiles 4 | CMakeScripts 5 | Testing 6 | Makefile 7 | cmake_install.cmake 8 | install_manifest.txt 9 | compile_commands.json 10 | CTestTestfile.cmake 11 | CMakeSettings.json 12 | _deps 13 | .vs 14 | out 15 | -------------------------------------------------------------------------------- /CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.16) 2 | 3 | project(QOIThumbnailProvider LANGUAGES CXX) 4 | set(CMAKE_SYSTEM_NAME Windows) 5 | set(CMAKE_CXX_STANDARD 17) 6 | 7 | if (MSVC) 8 | set(CMAKE_STATIC_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /NODEFAULTLIB:LIBCMT") 9 | set(CMAKE_MODULE_LINKER_FLAGS "${CMAKE_MODULE_LINKER_FLAGS} /DEF:${QOIThumbnailProvider_SOURCE_DIR}/QOIThumbnailProvider.def /NODEFAULTLIB:LIBCMT") 10 | endif() 11 | 12 | set(SOURCE_FILES 13 | Dll.cpp 14 | Dll.rc 15 | QOIThumbnailProvider.cpp 16 | ) 17 | 18 | add_definitions(-DUNICODE) 19 | add_definitions(-D_UNICODE) 20 | 21 | add_library(QOIThumbnailProvider MODULE ${SOURCE_FILES}) 22 | 23 | target_link_libraries(QOIThumbnailProvider PRIVATE shlwapi.lib) 24 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # QOI Thumbnail Provider 2 | Adds thumbnails for [QOI images](https://github.com/phoboslab/qoi) in Windows Explorer 3 | 4 | ![QOI images preview in Windows Explorer](./_images/thumbnails.png) 5 | 6 | ## Supported windows versions 7 | 8 | I've tested it on Windows 10 and Windows 11, but I guess it should work on Vista, 7 and 8 as well. 9 | 10 | **x64 only at the moment!** 11 | 12 | ## Installation 13 | 14 | 1. Download the dll from the [Releases][releases] section 15 | 2. Put the dll into the desired place in your system 16 | 3. Register the dll using `regsvr32`. 17 | ![registration command](./_images/reg_command.png) 18 | ``` 19 | regsvr32 C:\Path\To\The\Dll\Downloaded\QOIThumbnailProvider.dll 20 | ``` 21 | 22 | You should see confirmation like this: 23 | ![registration succeeded](./_images/reg_succeeded.png) 24 | 25 | To unregister the dll later again use `regsvr32`: 26 | ``` 27 | regsvr32 /u C:\Path\To\The\Dll\Downloaded\QOIThumbnailProvider.dll 28 | ``` 29 | 30 | [releases]: https://github.com/iOrange/QOIThumbnailProvider/releases 31 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 iOrange 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /Dll.rc: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | #ifndef DEBUG 4 | #define VER_DEBUG 0 5 | #else 6 | #define VER_DEBUG VS_FF_DEBUG 7 | #endif 8 | 9 | VS_VERSION_INFO VERSIONINFO 10 | FILEVERSION 0,0,2,0 11 | PRODUCTVERSION 0,0,2,0 12 | FILEFLAGSMASK VS_FFI_FILEFLAGSMASK 13 | FILEFLAGS VER_DEBUG 14 | FILEOS VOS__WINDOWS32 15 | FILETYPE VFT_DLL 16 | FILESUBTYPE VFT2_UNKNOWN 17 | BEGIN 18 | BLOCK "StringFileInfo" 19 | BEGIN 20 | BLOCK "040904E4" 21 | BEGIN 22 | VALUE "CompanyName", "iOrange" 23 | VALUE "FileDescription", "QOI Thumbnail Provider" 24 | VALUE "FileVersion", "0.0.2.0" 25 | VALUE "InternalName", "QOIThumbnailProvider.dll" 26 | VALUE "LegalCopyright", "2022, iOrange" 27 | VALUE "LegalTrademarks1", "" 28 | VALUE "LegalTrademarks2", "" 29 | VALUE "OriginalFilename", "QOIThumbnailProvider.dll" 30 | VALUE "ProductName", "QOIThumbnailProvider" 31 | VALUE "ProductVersion", "0, 0, 2, 0" 32 | END 33 | END 34 | BLOCK "VarFileInfo" 35 | BEGIN 36 | VALUE "Translation", 0x409, 1200 37 | END 38 | END 39 | -------------------------------------------------------------------------------- /Dll.cpp: -------------------------------------------------------------------------------- 1 | // QOI Thumbnail Provider for Windows Explorer 2 | // Written by iOrange in 2021 3 | // 4 | // Based on Microsoft's example 5 | // https://github.com/microsoft/windows-classic-samples/tree/main/Samples/Win7Samples/winui/shell/appshellintegration/RecipeThumbnailProvider 6 | // 7 | // Also more info here: 8 | // https://docs.microsoft.com/en-us/previous-versions/windows/desktop/legacy/cc144118(v=vs.85) 9 | 10 | #include 11 | #include 12 | #include // For IThumbnailProvider. 13 | #include // For SHChangeNotify 14 | #include 15 | #include 16 | #include // For std::size 17 | 18 | #ifdef QOI_THUMB_DEBUG_OUTPUT_ENABLED 19 | #define QOI_THUMB_DEBUG_OUTPUT(s) OutputDebugStringA() 20 | #else 21 | #define QOI_THUMB_DEBUG_OUTPUT(s) 22 | #endif 23 | 24 | 25 | extern HRESULT CQOIThumbProvider_CreateInstance(REFIID riid, void** ppv); 26 | 27 | #define SZ_CLSID_QOITHUMBHANDLER L"{98238d8e-7201-4588-bd77-61e41ad3e977}" 28 | #define SZ_QOITHUMBHANDLER L"QOI Thumbnail Handler" 29 | 30 | constexpr CLSID kCLSID_QOIThumbHandler = { 0x98238d8e, 0x7201, 0x4588, { 0xbd, 0x77, 0x61, 0xe4, 0x1a, 0xd3, 0xe9, 0x77 } }; 31 | 32 | typedef HRESULT(*PFNCREATEINSTANCE)(REFIID riid, void** ppvObject); 33 | struct CLASS_OBJECT_INIT { 34 | const CLSID* pClsid; 35 | PFNCREATEINSTANCE pfnCreate; 36 | }; 37 | 38 | // add classes supported by this module here 39 | constexpr CLASS_OBJECT_INIT kClassObjectInit[] = { 40 | { &kCLSID_QOIThumbHandler, CQOIThumbProvider_CreateInstance } 41 | }; 42 | 43 | 44 | std::atomic_long gModuleReferences(0); 45 | HINSTANCE gModuleInstance = nullptr; 46 | 47 | // Standard DLL functions 48 | STDAPI_(BOOL) DllMain(HINSTANCE hInstance, DWORD dwReason, void*) { 49 | if (DLL_PROCESS_ATTACH == dwReason) { 50 | gModuleInstance = hInstance; 51 | ::DisableThreadLibraryCalls(hInstance); 52 | } else if (DLL_PROCESS_DETACH == dwReason) { 53 | gModuleInstance = nullptr; 54 | } 55 | return TRUE; 56 | } 57 | 58 | STDAPI DllCanUnloadNow() { 59 | // Only allow the DLL to be unloaded after all outstanding references have been released 60 | return (gModuleReferences > 0) ? S_FALSE : S_OK; 61 | } 62 | 63 | void DllAddRef() { 64 | ++gModuleReferences; 65 | } 66 | 67 | void DllRelease() { 68 | --gModuleReferences; 69 | } 70 | 71 | class CClassFactory : public IClassFactory { 72 | public: 73 | static HRESULT CreateInstance(REFCLSID clsid, const CLASS_OBJECT_INIT* pClassObjectInits, size_t cClassObjectInits, REFIID riid, void** ppv) { 74 | *ppv = NULL; 75 | HRESULT hr = CLASS_E_CLASSNOTAVAILABLE; 76 | for (size_t i = 0; i < cClassObjectInits; ++i) { 77 | if (clsid == *pClassObjectInits[i].pClsid) { 78 | IClassFactory* pClassFactory = new (std::nothrow) CClassFactory(pClassObjectInits[i].pfnCreate); 79 | hr = pClassFactory ? S_OK : E_OUTOFMEMORY; 80 | if (SUCCEEDED(hr)) { 81 | hr = pClassFactory->QueryInterface(riid, ppv); 82 | pClassFactory->Release(); 83 | } 84 | break; // match found 85 | } 86 | } 87 | return hr; 88 | } 89 | 90 | CClassFactory(PFNCREATEINSTANCE pfnCreate) 91 | : mReferences(1) 92 | , mCreateFunc(pfnCreate) { 93 | DllAddRef(); 94 | } 95 | 96 | // IUnknown 97 | IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv) { 98 | static const QITAB qit[] = { 99 | QITABENT(CClassFactory, IClassFactory), 100 | { 0 } 101 | }; 102 | return QISearch(this, qit, riid, ppv); 103 | } 104 | 105 | IFACEMETHODIMP_(ULONG) AddRef() { 106 | return ++mReferences; 107 | } 108 | 109 | IFACEMETHODIMP_(ULONG) Release() { 110 | const long refs = --mReferences; 111 | if (!refs) { 112 | delete this; 113 | } 114 | return refs; 115 | } 116 | 117 | // IClassFactory 118 | IFACEMETHODIMP CreateInstance(IUnknown* punkOuter, REFIID riid, void** ppv) { 119 | return punkOuter ? CLASS_E_NOAGGREGATION : mCreateFunc(riid, ppv); 120 | } 121 | 122 | IFACEMETHODIMP LockServer(BOOL fLock) { 123 | if (fLock) { 124 | DllAddRef(); 125 | } else { 126 | DllRelease(); 127 | } 128 | return S_OK; 129 | } 130 | 131 | private: 132 | ~CClassFactory() { 133 | DllRelease(); 134 | } 135 | 136 | std::atomic_long mReferences; 137 | PFNCREATEINSTANCE mCreateFunc; 138 | }; 139 | 140 | STDAPI DllGetClassObject(REFCLSID clsid, REFIID riid, void** ppv) { 141 | return CClassFactory::CreateInstance(clsid, kClassObjectInit, std::size(kClassObjectInit), riid, ppv); 142 | } 143 | 144 | // A struct to hold the information required for a registry entry 145 | struct REGISTRY_ENTRY { 146 | HKEY hkeyRoot; 147 | PCWSTR pszKeyName; 148 | PCWSTR pszValueName; 149 | PCWSTR pszData; 150 | }; 151 | 152 | // Creates a registry key (if needed) and sets the default value of the key 153 | HRESULT CreateRegKeyAndSetValue(const REGISTRY_ENTRY* pRegistryEntry) { 154 | HKEY hKey; 155 | HRESULT hr = HRESULT_FROM_WIN32(RegCreateKeyExW(pRegistryEntry->hkeyRoot, 156 | pRegistryEntry->pszKeyName, 157 | 0, nullptr, REG_OPTION_NON_VOLATILE, 158 | KEY_SET_VALUE | KEY_WOW64_64KEY, 159 | nullptr, &hKey, nullptr)); 160 | if (SUCCEEDED(hr)) { 161 | hr = HRESULT_FROM_WIN32(RegSetValueExW(hKey, pRegistryEntry->pszValueName, 0, REG_SZ, 162 | reinterpret_cast(pRegistryEntry->pszData), 163 | static_cast(wcslen(pRegistryEntry->pszData) + 1) * sizeof(WCHAR))); 164 | RegCloseKey(hKey); 165 | } 166 | return hr; 167 | } 168 | 169 | // Registers this COM server 170 | STDAPI DllRegisterServer() { 171 | HRESULT hr; 172 | WCHAR szModuleName[MAX_PATH] = { 0 }; 173 | 174 | if (!GetModuleFileNameW(gModuleInstance, szModuleName, ARRAYSIZE(szModuleName))) { 175 | hr = HRESULT_FROM_WIN32(GetLastError()); 176 | } else { 177 | // List of registry entries we want to create 178 | const REGISTRY_ENTRY registryEntries[] = { 179 | // RootKey KeyName ValueName Data 180 | {HKEY_CURRENT_USER, L"Software\\Classes\\CLSID\\" SZ_CLSID_QOITHUMBHANDLER, nullptr, SZ_QOITHUMBHANDLER}, 181 | {HKEY_CURRENT_USER, L"Software\\Classes\\CLSID\\" SZ_CLSID_QOITHUMBHANDLER L"\\InProcServer32", nullptr, szModuleName}, 182 | {HKEY_CURRENT_USER, L"Software\\Classes\\CLSID\\" SZ_CLSID_QOITHUMBHANDLER L"\\InProcServer32", L"ThreadingModel", L"Apartment"}, 183 | {HKEY_CURRENT_USER, L"Software\\Classes\\.qoi", L"PerceivedType", L"image"}, 184 | {HKEY_CURRENT_USER, L"Software\\Classes\\.qoi\\ShellEx\\{e357fccd-a995-4576-b01f-234630154e96}", nullptr, SZ_CLSID_QOITHUMBHANDLER}, 185 | }; 186 | 187 | hr = S_OK; 188 | for (size_t i = 0; i < std::size(registryEntries) && SUCCEEDED(hr); ++i) { 189 | hr = CreateRegKeyAndSetValue(®istryEntries[i]); 190 | } 191 | } 192 | 193 | if (SUCCEEDED(hr)) { 194 | // This tells the shell to invalidate the thumbnail cache. This is important because any .qoi files 195 | // viewed before registering this handler would otherwise show cached blank thumbnails. 196 | SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, nullptr, nullptr); 197 | } 198 | 199 | return hr; 200 | } 201 | 202 | // Unregisters this COM server 203 | STDAPI DllUnregisterServer() { 204 | HRESULT hr = S_OK; 205 | 206 | const PCWSTR regKeys[] = { 207 | L"Software\\Classes\\CLSID\\" SZ_CLSID_QOITHUMBHANDLER, 208 | L"Software\\Classes\\.qoi" 209 | }; 210 | 211 | // Delete the registry entries 212 | for (size_t i = 0; i < std::size(regKeys) && SUCCEEDED(hr); ++i) { 213 | hr = HRESULT_FROM_WIN32(RegDeleteTreeW(HKEY_CURRENT_USER, regKeys[i])); 214 | if (hr == HRESULT_FROM_WIN32(ERROR_FILE_NOT_FOUND)) { 215 | // If the registry entry has already been deleted, say S_OK. 216 | hr = S_OK; 217 | } 218 | } 219 | 220 | return hr; 221 | } 222 | -------------------------------------------------------------------------------- /QOIThumbnailProvider.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include // For IThumbnailProvider. 3 | #include // For ComPtr 4 | #include 5 | #include 6 | #include 7 | 8 | template 9 | using ComPtr = Microsoft::WRL::ComPtr; 10 | 11 | // this thumbnail provider implements IInitializeWithStream to enable being hosted 12 | // in an isolated process for robustness 13 | class CQOIThumbProvider final : public IInitializeWithStream, public IThumbnailProvider { 14 | __pragma(pack(push, 1)) struct QOIRgba { 15 | uint8_t r, g, b, a; 16 | 17 | inline size_t Hash() const { 18 | return static_cast(r) * 3 + 19 | static_cast(g) * 5 + 20 | static_cast(b) * 7 + 21 | static_cast(a) * 11; 22 | } 23 | } __pragma(pack(pop)); 24 | 25 | struct QOIImage { 26 | static const int ColorspaceSRGB = 0; 27 | static const int ColorspaceLinear = 1; 28 | 29 | uint32_t width; 30 | uint32_t height; 31 | uint32_t channels; 32 | uint32_t colorSpace; 33 | std::vector pixels; 34 | }; 35 | 36 | public: 37 | CQOIThumbProvider() 38 | : mReferences(1) 39 | , mStream{} { 40 | } 41 | 42 | virtual ~CQOIThumbProvider() { 43 | } 44 | 45 | // IUnknown 46 | IFACEMETHODIMP QueryInterface(REFIID riid, void** ppv) { 47 | static const QITAB qit[] = { 48 | QITABENT(CQOIThumbProvider, IInitializeWithStream), 49 | QITABENT(CQOIThumbProvider, IThumbnailProvider), 50 | { 0 }, 51 | }; 52 | return QISearch(this, qit, riid, ppv); 53 | } 54 | 55 | IFACEMETHODIMP_(ULONG) AddRef() { 56 | return ++mReferences; 57 | } 58 | 59 | IFACEMETHODIMP_(ULONG) Release() { 60 | const long refs = --mReferences; 61 | if (!refs) { 62 | delete this; 63 | } 64 | return refs; 65 | } 66 | 67 | // IInitializeWithStream 68 | IFACEMETHODIMP Initialize(IStream* pStream, DWORD /*grfMode*/) { 69 | HRESULT hr = E_UNEXPECTED; // can only be inited once 70 | if (!mStream) { 71 | // take a reference to the stream if we have not been inited yet 72 | hr = pStream->QueryInterface(mStream.ReleaseAndGetAddressOf()); 73 | } 74 | return hr; 75 | } 76 | 77 | // IThumbnailProvider 78 | IFACEMETHODIMP GetThumbnail(UINT /*cx*/, HBITMAP* phbmp, WTS_ALPHATYPE* pdwAlpha) { 79 | QOIImage image; 80 | if (!this->LoadQOIImageFromStream(image)) { 81 | return E_FAIL; 82 | } 83 | 84 | if (!this->QOIImageToHBITMAP(image, phbmp)) { 85 | return E_OUTOFMEMORY; 86 | } 87 | 88 | *pdwAlpha = image.channels == 3 ? WTSAT_RGB : WTSAT_ARGB; 89 | 90 | return S_OK; 91 | } 92 | 93 | private: 94 | uint8_t ReadU8() { 95 | uint8_t result = 0; 96 | ULONG bytesRead = 0; 97 | HRESULT hr = mStream->Read(&result, 1, &bytesRead); 98 | return (FAILED(hr) || bytesRead < 1) ? 0 : result; 99 | } 100 | 101 | uint32_t ReadU32_BE() { 102 | uint32_t a = this->ReadU8(); 103 | uint32_t b = this->ReadU8(); 104 | uint32_t c = this->ReadU8(); 105 | uint32_t d = this->ReadU8(); 106 | return a << 24 | b << 16 | c << 8 | d; 107 | } 108 | 109 | bool LoadQOIImageFromStream(QOIImage& image) { 110 | constexpr uint8_t QOI_OP_INDEX = 0x00; /* 00xxxxxx */ 111 | constexpr uint8_t QOI_OP_DIFF = 0x40; /* 01xxxxxx */ 112 | constexpr uint8_t QOI_OP_LUMA = 0x80; /* 10xxxxxx */ 113 | constexpr uint8_t QOI_OP_RUN = 0xC0; /* 11xxxxxx */ 114 | constexpr uint8_t QOI_OP_RGB = 0xFE; /* 11111110 */ 115 | constexpr uint8_t QOI_OP_RGBA = 0xFF; /* 11111111 */ 116 | 117 | constexpr uint8_t QOI_MASK_2 = 0xC0; /* 11000000 */ 118 | 119 | constexpr uint32_t QOI_MAGIC = ((uint32_t('q') << 24) | (uint32_t('o') << 16) | (uint32_t('i') << 8) | uint32_t('f')); 120 | constexpr uint32_t QOI_HEADER_SIZE = 14u; 121 | constexpr uint32_t QOI_PADDING = 8u; 122 | constexpr uint32_t QOI_PIXELS_MAX = 400000000u; 123 | 124 | if (!mStream) { 125 | return false; 126 | } 127 | 128 | const uint32_t magic = this->ReadU32_BE(); 129 | if (QOI_MAGIC != magic) { 130 | return false; 131 | } 132 | 133 | image.width = this->ReadU32_BE(); 134 | image.height = this->ReadU32_BE(); 135 | image.channels = this->ReadU8(); 136 | image.colorSpace = this->ReadU8(); 137 | 138 | if (!image.width || !image.height || 139 | image.channels < 3 || image.channels > 4 || 140 | image.colorSpace > QOIImage::ColorspaceLinear || 141 | image.height >= QOI_PIXELS_MAX / image.width) { 142 | return false; 143 | } 144 | 145 | const size_t numPixels = static_cast(image.width) * image.height; 146 | image.pixels.resize(numPixels); 147 | 148 | QOIRgba index[64] = {}; 149 | QOIRgba px = { 0, 0, 0, 255 }; 150 | 151 | int run = 0; 152 | for (size_t i = 0; i < numPixels; ++i) { 153 | if (run > 0) { 154 | run--; 155 | } else { 156 | const uint8_t b1 = this->ReadU8(); 157 | 158 | if (QOI_OP_RGB == b1) { 159 | px.r = this->ReadU8(); 160 | px.g = this->ReadU8(); 161 | px.b = this->ReadU8(); 162 | } else if (QOI_OP_RGBA == b1) { 163 | px.r = this->ReadU8(); 164 | px.g = this->ReadU8(); 165 | px.b = this->ReadU8(); 166 | px.a = this->ReadU8(); 167 | } else if (QOI_OP_INDEX == (b1 & QOI_MASK_2)) { 168 | px = index[b1]; 169 | } else if (QOI_OP_DIFF == (b1 & QOI_MASK_2)) { 170 | px.r += ((b1 >> 4) & 0x03) - 2; 171 | px.g += ((b1 >> 2) & 0x03) - 2; 172 | px.b += (b1 & 0x03) - 2; 173 | } else if (QOI_OP_LUMA == (b1 & QOI_MASK_2)) { 174 | const uint8_t b2 = this->ReadU8(); 175 | const int vg = (b1 & 0x3F) - 32; 176 | px.r += vg - 8 + ((b2 >> 4) & 0x0F); 177 | px.g += vg; 178 | px.b += vg - 8 + (b2 & 0x0F); 179 | } else if (QOI_OP_RUN == (b1 & QOI_MASK_2)) { 180 | run = (b1 & 0x3F); 181 | } 182 | 183 | index[px.Hash() % 64] = px; 184 | } 185 | 186 | image.pixels[i] = px; 187 | } 188 | 189 | return true; 190 | } 191 | 192 | bool QOIImageToHBITMAP(const QOIImage& image, HBITMAP* phbmp) { 193 | BITMAPINFO bmi = {}; 194 | bmi.bmiHeader.biSize = sizeof(bmi.bmiHeader); 195 | bmi.bmiHeader.biWidth = static_cast(image.width); 196 | bmi.bmiHeader.biHeight = -static_cast(image.height); 197 | bmi.bmiHeader.biPlanes = 1; 198 | bmi.bmiHeader.biBitCount = 32; 199 | bmi.bmiHeader.biCompression = BI_RGB; 200 | 201 | QOIRgba* dstPixels; 202 | HBITMAP hbmp = CreateDIBSection(nullptr, &bmi, DIB_RGB_COLORS, reinterpret_cast(&dstPixels), nullptr, 0); 203 | if (!hbmp) { 204 | return false; 205 | } 206 | 207 | const QOIRgba* srcPixels = image.pixels.data(); 208 | // copy pixels over and swap RGBA -> BGRA 209 | const size_t numPixels = static_cast(image.width) * image.height; 210 | for (size_t i = 0; i < numPixels; ++i) { 211 | dstPixels[i].b = srcPixels[i].r; 212 | dstPixels[i].g = srcPixels[i].g; 213 | dstPixels[i].r = srcPixels[i].b; 214 | dstPixels[i].a = srcPixels[i].a; 215 | } 216 | 217 | *phbmp = hbmp; 218 | 219 | return true; 220 | } 221 | 222 | private: 223 | std::atomic_long mReferences; 224 | ComPtr mStream; // provided during initialization. 225 | }; 226 | 227 | 228 | // 229 | HRESULT CQOIThumbProvider_CreateInstance(REFIID riid, void** ppv) { 230 | CQOIThumbProvider* provider = new (std::nothrow) CQOIThumbProvider(); 231 | HRESULT hr = provider ? S_OK : E_OUTOFMEMORY; 232 | if (SUCCEEDED(hr)) { 233 | hr = provider->QueryInterface(riid, ppv); 234 | provider->Release(); 235 | } 236 | return hr; 237 | } --------------------------------------------------------------------------------