Debian Codegraph @

Source/Core/VideoBackends/Software/CopyRegion.h

raw symbols SCIP
1 #pragma once
2
3 #include "Common/Assert.h"
4 #include "Common/MathUtil.h"
5
6 #include <cmath>
7
8 namespace SW
9 {
10 // Copies a region of source to a region of destination, performing nearest-neighbor rescaling
11 template <typename T>
12 void CopyRegion(const T* const source, const MathUtil::Rectangle<int>& srcrect, const int src_width,
13 const int src_height, T* destination, const MathUtil::Rectangle<int>& dstrect,
14 const int dst_width, const int dst_height)
15 {
16 ASSERT(srcrect.top >= 0 && srcrect.bottom <= src_height);
17 ASSERT(srcrect.left >= 0 && srcrect.right <= src_width);
18 ASSERT(dstrect.top >= 0 && dstrect.bottom <= dst_height);
19 ASSERT(dstrect.left >= 0 && dstrect.right <= dst_width);
20
21 int copy_width = dstrect.GetWidth();
22 int copy_height = dstrect.GetHeight();
23
24 double x_ratio = srcrect.GetWidth() / static_cast<double>(dstrect.GetWidth());
25 double y_ratio = srcrect.GetHeight() / static_cast<double>(dstrect.GetHeight());
26
27 for (int y_off = 0; y_off < copy_height; y_off++)
28 {
29 for (int x_off = 0; x_off < copy_width; x_off++)
30 {
31 int dst_x = dstrect.left + x_off;
32 int dst_y = dstrect.top + y_off;
33 int dst_offset = (dst_y * dst_width) + dst_x;
34
35 int src_x = srcrect.left + static_cast<int>(std::round(x_off * x_ratio));
36 int src_y = srcrect.top + static_cast<int>(std::round(y_off * y_ratio));
37 int src_offset = (src_y * src_width) + src_x;
38
39 destination[dst_offset] = source[src_offset];
40 }
41 }
42 }
43
44 // Copies the entire image from source to destination, performing nearest-neighbor rescaling
45 template <typename T>
46 void CopyRegion(const T* const source, const int src_width, const int src_height, T* destination,
47 const int dst_width, const int dst_height)
48 {
49 MathUtil::Rectangle<int> srcrect{0, 0, src_width, src_height};
50 MathUtil::Rectangle<int> dstrect{0, 0, dst_width, dst_height};
51 CopyRegion(source, srcrect, src_width, src_height, destination, dstrect, dst_width, dst_height);
52 }
53 } // namespace SW