Debian Codegraph @

Source/Core/InputCommon/ImageOperations.cpp

raw symbols SCIP
1 // Copyright 2019 Dolphin Emulator Project
2 // SPDX-License-Identifier: GPL-2.0-or-later
3
4 #include "InputCommon/ImageOperations.h"
5
6 #include <algorithm>
7 #include <cmath>
8 #include <string>
9 #include <vector>
10
11 #include "Common/IOFile.h"
12 #include "Common/Image.h"
13
14 namespace InputCommon
15 {
16 namespace
17 {
18 Pixel SampleNearest(const ImagePixelData& src, double u, double v)
19 {
20 const u32 x = std::clamp(static_cast<u32>(u * src.width), 0u, src.width - 1);
21 const u32 y = std::clamp(static_cast<u32>(v * src.height), 0u, src.height - 1);
22 return src.pixels[x + y * src.width];
23 }
24 } // namespace
25
26 void CopyImageRegion(const ImagePixelData& src, ImagePixelData& dst, const Rect& src_region,
27 const Rect& dst_region)
28 {
29 if (src_region.GetWidth() != dst_region.GetWidth() ||
30 src_region.GetHeight() != dst_region.GetHeight())
31 {
32 return;
33 }
34
35 for (u32 x = 0; x < dst_region.GetWidth(); x++)
36 {
37 for (u32 y = 0; y < dst_region.GetHeight(); y++)
38 {
39 dst.pixels[(y + dst_region.top) * dst.width + x + dst_region.left] =
40 src.pixels[(y + src_region.top) * src.width + x + src_region.left];
41 }
42 }
43 }
44
45 std::optional<ImagePixelData> LoadImage(const std::string& path)
46 {
47 File::IOFile file;
48 if (!file.Open(path, "rb"))
49 return std::nullopt;
50
51 Common::UniqueBuffer<u8> buffer(file.GetSize());
52 if (!file.ReadBytes(buffer.data(), file.GetSize()))
53 return std::nullopt;
54
55 ImagePixelData image;
56 Common::UniqueBuffer<u8> data;
57 if (!Common::LoadPNG(buffer, &data, &image.width, &image.height))
58 return std::nullopt;
59
60 image.pixels.resize(image.width * image.height);
61 for (u32 x = 0; x < image.width; x++)
62 {
63 for (u32 y = 0; y < image.height; y++)
64 {
65 const u32 index = y * image.width + x;
66 const auto pixel =
67 Pixel{data[index * 4], data[index * 4 + 1], data[index * 4 + 2], data[index * 4 + 3]};
68 image.pixels[index] = pixel;
69 }
70 }
71
72 return image;
73 }
74
75 bool WriteImage(const std::string& path, const ImagePixelData& image)
76 {
77 Common::UniqueBuffer<u8> buffer;
78 buffer.reset(image.width * image.height * 4);
79
80 std::size_t buffer_index = 0;
81 for (u32 y = 0; y < image.height; ++y)
82 {
83 for (u32 x = 0; x < image.width; ++x)
84 {
85 const auto index = x + y * image.width;
86 const auto pixel = image.pixels[index];
87 buffer[buffer_index++] = pixel.r;
88 buffer[buffer_index++] = pixel.g;
89 buffer[buffer_index++] = pixel.b;
90 buffer[buffer_index++] = pixel.a;
91 }
92 }
93
94 return Common::SavePNG(path, buffer.data(), Common::ImageByteFormat::RGBA, image.width,
95 image.height, image.width * 4);
96 }
97
98 ImagePixelData Resize(ResizeMode mode, const ImagePixelData& src, u32 new_width, u32 new_height)
99 {
100 ImagePixelData result(new_width, new_height);
101
102 for (u32 x = 0; x < new_width; x++)
103 {
104 const double u = x / static_cast<double>(new_width - 1);
105 for (u32 y = 0; y < new_height; y++)
106 {
107 const double v = y / static_cast<double>(new_height - 1);
108
109 switch (mode)
110 {
111 case ResizeMode::Nearest:
112 result.pixels[y * new_width + x] = SampleNearest(src, u, v);
113 break;
114 }
115 }
116 }
117
118 return result;
119 }
120
121 ImagePixelData ResizeKeepAspectRatio(ResizeMode mode, const ImagePixelData& src, u32 new_width,
122 u32 new_height, const Pixel& background_color)
123 {
124 ImagePixelData result(new_width, new_height, background_color);
125
126 const double corrected_height = new_width * (src.height / static_cast<double>(src.width));
127 const double corrected_width = new_height * (src.width / static_cast<double>(src.height));
128 // initially no borders
129 u32 top = 0;
130 u32 left = 0;
131
132 ImagePixelData resized;
133 if (corrected_height <= new_height)
134 {
135 // Handle vertical padding
136
137 const int diff = new_height - std::trunc(corrected_height);
138 top = diff / 2;
139 if (diff % 2 != 0)
140 {
141 // If the difference is odd, we need to have one side be slightly larger
142 top += 1;
143 }
144 resized = Resize(mode, src, new_width, corrected_height);
145 }
146 else
147 {
148 // Handle horizontal padding
149
150 const int diff = new_width - std::trunc(corrected_width);
151 left = diff / 2;
152 if (diff % 2 != 0)
153 {
154 // If the difference is odd, we need to have one side be slightly larger
155 left += 1;
156 }
157 resized = Resize(mode, src, corrected_width, new_height);
158 }
159 CopyImageRegion(resized, result, Rect{0, 0, resized.width, resized.height},
160 Rect{left, top, left + resized.width, top + resized.height});
161
162 return result;
163 }
164
165 } // namespace InputCommon