diff options
| -rw-r--r-- | src/yuzu/util/util.cpp | 81 |
1 files changed, 53 insertions, 28 deletions
diff --git a/src/yuzu/util/util.cpp b/src/yuzu/util/util.cpp index 61cf00176..f2854c8ec 100644 --- a/src/yuzu/util/util.cpp +++ b/src/yuzu/util/util.cpp | |||
| @@ -63,25 +63,15 @@ bool SaveIconToFile(const std::string_view path, const QImage& image) { | |||
| 63 | }; | 63 | }; |
| 64 | #pragma pack(pop) | 64 | #pragma pack(pop) |
| 65 | 65 | ||
| 66 | QImage source_image = image.convertToFormat(QImage::Format_RGB32); | 66 | const QImage source_image = image.convertToFormat(QImage::Format_RGB32); |
| 67 | constexpr std::array<int, 7> scale_sizes{256, 128, 64, 48, 32, 24, 16}; | ||
| 67 | constexpr int bytes_per_pixel = 4; | 68 | constexpr int bytes_per_pixel = 4; |
| 68 | const int image_size = source_image.width() * source_image.height() * bytes_per_pixel; | 69 | |
| 69 | 70 | const IconDir icon_dir{ | |
| 70 | BITMAPINFOHEADER info_header{}; | 71 | .id_reserved = 0, |
| 71 | info_header.biSize = sizeof(BITMAPINFOHEADER), info_header.biWidth = source_image.width(), | 72 | .id_type = 1, |
| 72 | info_header.biHeight = source_image.height() * 2, info_header.biPlanes = 1, | 73 | .id_count = static_cast<WORD>(scale_sizes.size()), |
| 73 | info_header.biBitCount = bytes_per_pixel * 8, info_header.biCompression = BI_RGB; | 74 | }; |
| 74 | |||
| 75 | const IconDir icon_dir{.id_reserved = 0, .id_type = 1, .id_count = 1}; | ||
| 76 | const IconDirEntry icon_entry{.width = static_cast<BYTE>(source_image.width()), | ||
| 77 | .height = static_cast<BYTE>(source_image.height() * 2), | ||
| 78 | .color_count = 0, | ||
| 79 | .reserved = 0, | ||
| 80 | .planes = 1, | ||
| 81 | .bit_count = bytes_per_pixel * 8, | ||
| 82 | .bytes_in_res = | ||
| 83 | static_cast<DWORD>(sizeof(BITMAPINFOHEADER) + image_size), | ||
| 84 | .image_offset = sizeof(IconDir) + sizeof(IconDirEntry)}; | ||
| 85 | 75 | ||
| 86 | Common::FS::IOFile icon_file(path, Common::FS::FileAccessMode::Write, | 76 | Common::FS::IOFile icon_file(path, Common::FS::FileAccessMode::Write, |
| 87 | Common::FS::FileType::BinaryFile); | 77 | Common::FS::FileType::BinaryFile); |
| @@ -92,20 +82,55 @@ bool SaveIconToFile(const std::string_view path, const QImage& image) { | |||
| 92 | if (!icon_file.Write(icon_dir)) { | 82 | if (!icon_file.Write(icon_dir)) { |
| 93 | return false; | 83 | return false; |
| 94 | } | 84 | } |
| 95 | if (!icon_file.Write(icon_entry)) { | 85 | |
| 96 | return false; | 86 | std::size_t image_offset = sizeof(IconDir) + (sizeof(IconDirEntry) * scale_sizes.size()); |
| 97 | } | 87 | for (std::size_t i = 0; i < scale_sizes.size(); i++) { |
| 98 | if (!icon_file.Write(info_header)) { | 88 | const int image_size = scale_sizes[i] * scale_sizes[i] * bytes_per_pixel; |
| 99 | return false; | 89 | const IconDirEntry icon_entry{ |
| 90 | .width = static_cast<BYTE>(scale_sizes[i]), | ||
| 91 | .height = static_cast<BYTE>(scale_sizes[i]), | ||
| 92 | .color_count = 0, | ||
| 93 | .reserved = 0, | ||
| 94 | .planes = 1, | ||
| 95 | .bit_count = bytes_per_pixel * 8, | ||
| 96 | .bytes_in_res = static_cast<DWORD>(sizeof(BITMAPINFOHEADER) + image_size), | ||
| 97 | .image_offset = static_cast<DWORD>(image_offset), | ||
| 98 | }; | ||
| 99 | image_offset += icon_entry.bytes_in_res; | ||
| 100 | if (!icon_file.Write(icon_entry)) { | ||
| 101 | return false; | ||
| 102 | } | ||
| 100 | } | 103 | } |
| 101 | 104 | ||
| 102 | for (int y = 0; y < image.height(); y++) { | 105 | for (std::size_t i = 0; i < scale_sizes.size(); i++) { |
| 103 | const auto* line = source_image.scanLine(source_image.height() - 1 - y); | 106 | const QImage scaled_image = source_image.scaled( |
| 104 | std::vector<u8> line_data(source_image.width() * bytes_per_pixel); | 107 | scale_sizes[i], scale_sizes[i], Qt::IgnoreAspectRatio, Qt::SmoothTransformation); |
| 105 | std::memcpy(line_data.data(), line, line_data.size()); | 108 | const BITMAPINFOHEADER info_header{ |
| 106 | if (!icon_file.Write(line_data)) { | 109 | .biSize = sizeof(BITMAPINFOHEADER), |
| 110 | .biWidth = scaled_image.width(), | ||
| 111 | .biHeight = scaled_image.height() * 2, | ||
| 112 | .biPlanes = 1, | ||
| 113 | .biBitCount = bytes_per_pixel * 8, | ||
| 114 | .biCompression = BI_RGB, | ||
| 115 | .biSizeImage{}, | ||
| 116 | .biXPelsPerMeter{}, | ||
| 117 | .biYPelsPerMeter{}, | ||
| 118 | .biClrUsed{}, | ||
| 119 | .biClrImportant{}, | ||
| 120 | }; | ||
| 121 | |||
| 122 | if (!icon_file.Write(info_header)) { | ||
| 107 | return false; | 123 | return false; |
| 108 | } | 124 | } |
| 125 | |||
| 126 | for (int y = 0; y < scaled_image.height(); y++) { | ||
| 127 | const auto* line = scaled_image.scanLine(scaled_image.height() - 1 - y); | ||
| 128 | std::vector<u8> line_data(scaled_image.width() * bytes_per_pixel); | ||
| 129 | std::memcpy(line_data.data(), line, line_data.size()); | ||
| 130 | if (!icon_file.Write(line_data)) { | ||
| 131 | return false; | ||
| 132 | } | ||
| 133 | } | ||
| 109 | } | 134 | } |
| 110 | icon_file.Close(); | 135 | icon_file.Close(); |
| 111 | 136 | ||