From bbe36a8246041544d6ebe420dfa1b695b6c74521 Mon Sep 17 00:00:00 2001 From: ouwou <26526779+ouwou@users.noreply.github.com> Date: Sat, 3 Oct 2020 18:49:22 -0400 Subject: maybe slightly better image loading maybe hopefully --- util.hpp | 31 +++++++++++++++++++++++++++++++ 1 file changed, 31 insertions(+) (limited to 'util.hpp') diff --git a/util.hpp b/util.hpp index 0b80053..bf6d9ee 100644 --- a/util.hpp +++ b/util.hpp @@ -29,6 +29,37 @@ inline void LaunchBrowser(std::string url) { #endif } +inline void GetImageDimensions(int inw, int inh, int &outw, int &outh, int clampw = 400, int clamph = 300) { + const auto frac = static_cast(inw) / inh; + + outw = inw; + outh = inh; + + if (outw > clampw) { + outw = clampw; + outh = clampw / frac; + } + + if (outh > clamph) { + outh = clamph; + outw = clamph * frac; + } +} + +inline std::vector ReadWholeFile(std::string path) { + std::vector ret; + FILE *fp = std::fopen(path.c_str(), "rb"); + if (fp == nullptr) + return ret; + std::fseek(fp, 0, SEEK_END); + int len = std::ftell(fp); + std::rewind(fp); + ret.resize(len); + std::fread(ret.data(), 1, ret.size(), fp); + std::fclose(fp); + return ret; +} + inline std::string HumanReadableBytes(uint64_t bytes) { constexpr static const char *x[] = { "B", "KB", "MB", "GB", "TB" }; int order = 0; -- cgit v1.2.3