summaryrefslogtreecommitdiffstats
path: root/src/util.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cpp')
-rw-r--r--src/util.cpp27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/util.cpp b/src/util.cpp
index 2ae856f..1fb6ac8 100644
--- a/src/util.cpp
+++ b/src/util.cpp
@@ -37,3 +37,30 @@ unique_fd::~unique_fd() {
close(this->fd);
}
}
+
+void rectangle::scale(int to_w, int to_h, bool keep_aspect)
+{
+ if (!keep_aspect) {
+ w = to_w;
+ h = to_h;
+ return;
+ }
+
+ int64_t resized_w = int64_t(to_h) * int64_t(w) / int64_t(h);
+
+ if (resized_w <= to_w) {
+ /* use aspect by height */
+ w = resized_w;
+ h = to_h;
+ } else {
+ /* use aspect by width */
+ w = to_h;
+ h = int64_t(to_w) * int64_t(h) / int64_t(w);
+ }
+}
+
+void rectangle::center(int outer_w, int outer_h)
+{
+ x = (outer_w - w) / 2;
+ y = (outer_h - h) / 2;
+}