icc_detect_win32.cc (1852B)
1 // Copyright (c) the JPEG XL Project Authors. All rights reserved. 2 // 3 // Use of this source code is governed by a BSD-style 4 // license that can be found in the LICENSE file. 5 6 #include "tools/icc_detect/icc_detect.h" 7 8 #include <windows.h> 9 10 #include <memory> 11 #include <type_traits> 12 13 namespace jpegxl { 14 namespace tools { 15 16 namespace { 17 18 struct HandleDeleter { 19 void operator()(const HANDLE handle) const { 20 if (handle != INVALID_HANDLE_VALUE) { 21 CloseHandle(handle); 22 } 23 } 24 }; 25 using HandleUniquePtr = 26 std::unique_ptr<std::remove_pointer<HANDLE>::type, HandleDeleter>; 27 28 } // namespace 29 30 QByteArray GetMonitorIccProfile(const QWidget* const widget) { 31 const HWND window = reinterpret_cast<HWND>(widget->effectiveWinId()); 32 const HDC dc = GetDC(window); 33 wchar_t profile_path[MAX_PATH]; 34 DWORD profile_path_size = MAX_PATH; 35 if (!GetICMProfileW(dc, &profile_path_size, profile_path)) { 36 ReleaseDC(window, dc); 37 return QByteArray(); 38 } 39 ReleaseDC(window, dc); 40 HandleUniquePtr file(CreateFileW(profile_path, GENERIC_READ, FILE_SHARE_READ, 41 nullptr, OPEN_EXISTING, 42 FILE_FLAG_SEQUENTIAL_SCAN, nullptr)); 43 if (file.get() == INVALID_HANDLE_VALUE) { 44 return QByteArray(); 45 } 46 LARGE_INTEGER profile_size; 47 if (!GetFileSizeEx(file.get(), &profile_size)) { 48 return QByteArray(); 49 } 50 HandleUniquePtr mapping( 51 CreateFileMappingW(file.get(), nullptr, PAGE_READONLY, 0, 0, nullptr)); 52 if (mapping == nullptr) { 53 return QByteArray(); 54 } 55 const char* const view = reinterpret_cast<const char*>( 56 MapViewOfFile(mapping.get(), FILE_MAP_READ, 0, 0, 0)); 57 if (view == nullptr) { 58 return QByteArray(); 59 } 60 QByteArray profile(view, profile_size.QuadPart); 61 UnmapViewOfFile(view); 62 return profile; 63 } 64 65 } // namespace tools 66 } // namespace jpegxl