From 60478d536e035a48c7ed9b6d212727b5f722f870 Mon Sep 17 00:00:00 2001
From: Tristan Walter <twalter@orn.mpg.de>
Date: Fri, 6 Nov 2020 14:56:04 +0100
Subject: [PATCH] * moved casts to misc/checked_casts.h * average_samples is
 uint32_t

---
 Application/src/ProcessedVideo/pv.cpp         |   1 +
 Application/src/commons/common/file/Path.cpp  |   2 +-
 .../src/commons/common/gui/DrawBase.cpp       |   2 +-
 .../src/commons/common/gui/DrawCVBase.cpp     |   2 +-
 .../src/commons/common/gui/DrawStructure.cpp  |   1 +
 Application/src/commons/common/gui/GLImpl.cpp |   5 +-
 .../src/commons/common/gui/IMGUIBase.cpp      |   2 +
 .../commons/common/gui/types/ScrollableList.h |   2 +-
 Application/src/commons/common/misc/Grid.cpp  |   2 +-
 .../src/commons/common/misc/checked_casts.h   | 184 ++++++++++++++++++
 .../src/commons/common/misc/metastring.h      | 175 -----------------
 .../src/commons/common/video/GenericVideo.cpp |   1 +
 .../src/commons/common/video/VideoSource.cpp  |   3 +-
 Application/src/grabber/default_config.cpp    |   2 +-
 Application/src/grabber/gui.cpp               |   6 +-
 Application/src/tracker/VideoOpener.cpp       |   4 +-
 .../src/tracker/gui/IdentityHeatmap.cpp       |   1 +
 .../src/tracker/misc/EventAnalysis.cpp        |   1 +
 Application/src/tracker/misc/Output.cpp       |   1 +
 .../src/tracker/misc/OutputLibrary.cpp        |   1 +
 .../src/tracker/tracking/Accumulation.cpp     |   7 +-
 Application/src/tracker/tracking/Export.cpp   |   1 +
 Application/src/tracker/tracking/Outline.cpp  |   1 +
 23 files changed, 215 insertions(+), 192 deletions(-)
 create mode 100644 Application/src/commons/common/misc/checked_casts.h

diff --git a/Application/src/ProcessedVideo/pv.cpp b/Application/src/ProcessedVideo/pv.cpp
index ef854de..331a658 100644
--- a/Application/src/ProcessedVideo/pv.cpp
+++ b/Application/src/ProcessedVideo/pv.cpp
@@ -4,6 +4,7 @@
 #include <misc/GlobalSettings.h>
 #include <misc/Timer.h>
 #include <misc/PVBlob.h>
+#include <misc/checked_casts.h>
 
 /**
  * =============================
diff --git a/Application/src/commons/common/file/Path.cpp b/Application/src/commons/common/file/Path.cpp
index 98b4bae..9ec9e43 100644
--- a/Application/src/commons/common/file/Path.cpp
+++ b/Application/src/commons/common/file/Path.cpp
@@ -25,7 +25,7 @@
 #include <filesystem>
 #endif
 
-#include <misc/metastring.h>
+#include <misc/checked_casts.h>
 
 namespace file {
     char Path::os_sep() { return OS_SEP; }
diff --git a/Application/src/commons/common/gui/DrawBase.cpp b/Application/src/commons/common/gui/DrawBase.cpp
index e6b8153..5a79788 100644
--- a/Application/src/commons/common/gui/DrawBase.cpp
+++ b/Application/src/commons/common/gui/DrawBase.cpp
@@ -1,5 +1,5 @@
 #include "DrawBase.h"
-#include <misc/metastring.h>
+#include <misc/checked_casts.h>
 
 namespace gui {
     Base *_latest_base = nullptr;
diff --git a/Application/src/commons/common/gui/DrawCVBase.cpp b/Application/src/commons/common/gui/DrawCVBase.cpp
index b25039b..3c4dc97 100644
--- a/Application/src/commons/common/gui/DrawCVBase.cpp
+++ b/Application/src/commons/common/gui/DrawCVBase.cpp
@@ -1,5 +1,5 @@
 #include "DrawCVBase.h"
-#include <misc/metastring.h>
+#include <misc/checked_casts.h>
 
 namespace gui {
     IMPLEMENT(CVBase::_static_pixels);
diff --git a/Application/src/commons/common/gui/DrawStructure.cpp b/Application/src/commons/common/gui/DrawStructure.cpp
index f8b2c18..e1882de 100644
--- a/Application/src/commons/common/gui/DrawStructure.cpp
+++ b/Application/src/commons/common/gui/DrawStructure.cpp
@@ -3,6 +3,7 @@
 #include <gui/types/StaticText.h>
 #include <gui/types/Button.h>
 #include <misc/GlobalSettings.h>
+#include <misc/checked_casts.h>
 
 namespace gui {
     struct ErrorMessage {
diff --git a/Application/src/commons/common/gui/GLImpl.cpp b/Application/src/commons/common/gui/GLImpl.cpp
index ba10ec8..b9e1a3f 100644
--- a/Application/src/commons/common/gui/GLImpl.cpp
+++ b/Application/src/commons/common/gui/GLImpl.cpp
@@ -50,6 +50,7 @@ using ImTextureID_t = ImGui_OpenGL2_TextureID;
 
 #include "GLImpl.h"
 #include <misc/Timer.h>
+#include <misc/checked_casts.h>
 
 #define GLIMPL_CHECK_THREAD_ID() check_thread_id( __LINE__ , __FILE__ )
 
@@ -105,8 +106,8 @@ void GLImpl::set_icons(const std::vector<file::Path>& icons) {
         data.push_back(ptr);
         images.push_back(GLFWimage());
         images.back().pixels = ptr->data();
-        images.back().width = ptr->cols;
-        images.back().height = ptr->rows;
+        images.back().width = sign_cast<int>(ptr->cols);
+        images.back().height = sign_cast<int>(ptr->rows);
     }
 
     glfwSetWindowIcon(window, images.size(), images.data());
diff --git a/Application/src/commons/common/gui/IMGUIBase.cpp b/Application/src/commons/common/gui/IMGUIBase.cpp
index d698da4..c2221fa 100644
--- a/Application/src/commons/common/gui/IMGUIBase.cpp
+++ b/Application/src/commons/common/gui/IMGUIBase.cpp
@@ -30,6 +30,8 @@
 #define GLFW_HAVE_MONITOR_SCALE true
 #endif
 
+#include <misc/checked_casts.h>
+
 namespace gui {
 
 
diff --git a/Application/src/commons/common/gui/types/ScrollableList.h b/Application/src/commons/common/gui/types/ScrollableList.h
index 7fbf461..9e569e5 100644
--- a/Application/src/commons/common/gui/types/ScrollableList.h
+++ b/Application/src/commons/common/gui/types/ScrollableList.h
@@ -2,7 +2,7 @@
 
 #include <gui/types/Entangled.h>
 #include <gui/DrawSFBase.h>
-#include <misc/metastring.h>
+#include <misc/checked_casts.h>
 
 namespace gui {
     class CustomItem {
diff --git a/Application/src/commons/common/misc/Grid.cpp b/Application/src/commons/common/misc/Grid.cpp
index 274c537..55844d9 100644
--- a/Application/src/commons/common/misc/Grid.cpp
+++ b/Application/src/commons/common/misc/Grid.cpp
@@ -1,5 +1,5 @@
 #include "Grid.h"
-#include <misc/metastring.h>
+#include <misc/checked_casts.h>
 
 namespace cmn {
 namespace grid {
diff --git a/Application/src/commons/common/misc/checked_casts.h b/Application/src/commons/common/misc/checked_casts.h
new file mode 100644
index 0000000..b81197a
--- /dev/null
+++ b/Application/src/commons/common/misc/checked_casts.h
@@ -0,0 +1,184 @@
+#pragma once
+
+#include <types.h>
+#ifndef NDEBUG
+#include <misc/metastring.h>
+#endif
+
+namespace cmn {
+namespace tag {
+struct warn_on_error {};
+struct fail_on_error {};
+}
+
+template<typename T>
+using try_make_unsigned =
+typename std::conditional<
+    std::is_integral<T>::value,
+    std::make_unsigned<T>,
+    double
+>::type;
+
+template<typename T>
+using try_make_signed =
+typename std::conditional<
+    std::is_integral<T>::value,
+    std::make_signed<T>,
+    double
+>::type;
+
+template<typename To, typename From>
+void fail_type(From&& value) {
+#ifndef NDEBUG
+    using FromType = typename remove_cvref<From>::type;
+    using ToType = typename remove_cvref<To>::type;
+    
+    auto type1 = Meta::name<FromType>();
+    auto type2 = Meta::name<ToType>();
+    
+    auto value1 = Meta::toStr(value);
+    
+    auto start1 = Meta::toStr(std::numeric_limits<FromType>::min());
+    auto end1 = Meta::toStr(std::numeric_limits<FromType>::max());
+    
+    auto start2 = Meta::toStr(std::numeric_limits<ToType>::min());
+    auto end2 = Meta::toStr(std::numeric_limits<ToType>::max());
+    
+    Warning("Failed converting %S(%S) [%S,%S] -> type %S [%S,%S]", &type1, &value1, &start1, &end1, &type2, &start2, &end2);
+#endif
+}
+
+template<typename To, typename From>
+constexpr To sign_cast(From&& value) {
+#ifndef NDEBUG
+    using FromType = typename remove_cvref<From>::type;
+    using ToType = typename remove_cvref<To>::type;
+    
+    if constexpr(!std::is_floating_point<ToType>::value
+                 && std::is_integral<ToType>::value)
+    {
+        if constexpr(std::is_signed<ToType>::value) {
+            if (value > std::numeric_limits<ToType>::max())
+                fail_type<To, From>(std::forward<From>(value));
+            
+        } else if constexpr(std::is_signed<FromType>::value) {
+            if (value < 0)
+                fail_type<To, From>(std::forward<From>(value));
+            
+            using bigger_type = typename std::conditional<(sizeof(FromType) > sizeof(ToType)), FromType, ToType>::type;
+            if (bigger_type(value) > bigger_type(std::numeric_limits<ToType>::max()))
+                fail_type<To, From>(std::forward<From>(value));
+        }
+    }
+#endif
+    return static_cast<To>(std::forward<From>(value));
+}
+
+template<typename To, typename From>
+constexpr bool check_narrow_cast(const From& value) {
+#ifndef NDEBUG
+    using FromType = typename remove_cvref<From>::type;
+    using ToType = typename remove_cvref<To>::type;
+
+    auto str = Meta::toStr(value);
+    if constexpr (
+        std::is_floating_point<ToType>::value
+        || (std::is_signed<FromType>::value == std::is_signed<ToType>::value && !std::is_floating_point<FromType>::value)
+        )
+    {
+        // unsigned to unsigned
+#ifdef _NARROW_PRINT_VERBOSE
+        auto tstr0 = Meta::name<FromType>();
+        auto tstr1 = Meta::name<ToType>();
+        Debug("Narrowing %S -> %S (same) = %S.", &tstr0, &tstr1, &str);
+#endif
+        return true;
+    }
+    else if constexpr (std::is_floating_point<FromType>::value && std::is_signed<ToType>::value) {
+        using signed_t = int64_t;
+#ifdef _NARROW_PRINT_VERBOSE
+        auto tstr0 = Meta::name<FromType>();
+        auto tstr1 = Meta::name<ToType>();
+        auto tstr2 = Meta::name<signed_t>();
+        Debug("Narrowing %S -> %S | converting to %S and comparing (fs) = %S.", &tstr0, &tstr1, &tstr2, &str);
+#endif
+        return static_cast<signed_t>(value) >= static_cast<signed_t>(std::numeric_limits<To>::min())
+            && static_cast<signed_t>(value) <= static_cast<signed_t>(std::numeric_limits<To>::max());
+    }
+    else if constexpr (std::is_floating_point<FromType>::value && std::is_unsigned<ToType>::value) {
+        using unsigned_t = uint64_t;
+#ifdef _NARROW_PRINT_VERBOSE
+        auto tstr0 = Meta::name<FromType>();
+        auto tstr1 = Meta::name<ToType>();
+        auto tstr2 = Meta::name<unsigned_t>();
+        Debug("Narrowing %S -> %S | converting to %S and comparing (fs) = %S.", &tstr0, &tstr1, &tstr2, &str);
+#endif
+        return value >= FromType(0)
+            && static_cast<unsigned_t>(value) <= static_cast<unsigned_t>(std::numeric_limits<To>::max());
+    }
+    else if constexpr (std::is_unsigned<FromType>::value && std::is_signed<ToType>::value) {
+        // unsigned to signed
+        using signed_t = int64_t;
+#ifdef _NARROW_PRINT_VERBOSE
+        auto tstr0 = Meta::name<FromType>();
+        auto tstr1 = Meta::name<ToType>();
+        auto tstr2 = Meta::name<signed_t>();
+        Debug("Narrowing %S -> %S | converting to %S and comparing (us) = %S.", &tstr0, &tstr1, &tstr2, &str);
+#endif
+        return static_cast<signed_t>(value) < static_cast<signed_t>(std::numeric_limits<To>::max());
+
+    }
+    else {
+        static_assert(std::is_signed<FromType>::value && std::is_unsigned<ToType>::value, "Expecting signed to unsigned conversion");
+        // signed to unsigned
+        using unsigned_t = typename try_make_unsigned<FromType>::type;
+#ifdef _NARROW_PRINT_VERBOSE
+        auto tstr0 = Meta::name<FromType>();
+        auto tstr1 = Meta::name<ToType>();
+        auto tstr2 = Meta::name<unsigned_t>();
+        Debug("Narrowing %S -> %S | converting to %S and comparing (su) = %S.", &tstr0, &tstr1, &tstr2, &str);
+#endif
+        return value >= 0 && static_cast<unsigned_t>(value) <= static_cast<unsigned_t>(std::numeric_limits<To>::max());
+    }
+#else
+    return true;
+#endif
+}
+
+template<typename To, typename From>
+constexpr To narrow_cast(From&& value, struct tag::warn_on_error) {
+#ifndef NDEBUG
+    if (!check_narrow_cast<To, From>(value)) {
+        auto vstr = Meta::toStr(value);
+        auto lstr = Meta::toStr(std::numeric_limits<To>::min());
+        auto rstr = Meta::toStr(std::numeric_limits<To>::max());
+
+        auto tstr = Meta::name<To>();
+        auto fstr = Meta::name<From>();
+        Warning("Value '%S' in narrowing conversion of %S -> %S is not within limits [%S,%S].", &vstr, &fstr, &tstr, &lstr, &rstr);
+    }
+#endif
+    return static_cast<To>(std::forward<From>(value));
+}
+
+template<typename To, typename From>
+constexpr To narrow_cast(From&& value, struct tag::fail_on_error) {
+#ifndef NDEBUG
+    if (!check_narrow_cast<To, From>(value)) {
+        auto vstr = Meta::toStr(value);
+        auto lstr = Meta::toStr(std::numeric_limits<To>::min());
+        auto rstr = Meta::toStr(std::numeric_limits<To>::max());
+
+        auto tstr = Meta::name<To>();
+        auto fstr = Meta::name<From>();
+        U_EXCEPTION("Value '%S' in narrowing conversion of %S -> %S is not within limits [%S,%S].", &vstr, &fstr, &tstr, &lstr, &rstr);
+    }
+#endif
+    return static_cast<To>(std::forward<From>(value));
+}
+
+template<typename To, typename From>
+constexpr To narrow_cast(From&& value) {
+    return narrow_cast<To, From>(std::forward<From>(value), tag::warn_on_error{});
+}
+}
diff --git a/Application/src/commons/common/misc/metastring.h b/Application/src/commons/common/misc/metastring.h
index 7eaaa5c..35a3384 100644
--- a/Application/src/commons/common/misc/metastring.h
+++ b/Application/src/commons/common/misc/metastring.h
@@ -1034,178 +1034,3 @@ namespace cmn {
     }
 }
 
-namespace cmn {
-namespace tag {
-struct warn_on_error {};
-struct fail_on_error {};
-}
-
-template<typename T>
-using try_make_unsigned =
-typename std::conditional<
-	std::is_integral<T>::value,
-	std::make_unsigned<T>,
-	double
->::type;
-
-template<typename T>
-using try_make_signed =
-typename std::conditional<
-	std::is_integral<T>::value,
-	std::make_signed<T>,
-	double
->::type;
-
-template<typename To, typename From>
-void fail_type(From&& value) {
-    using FromType = typename remove_cvref<From>::type;
-    using ToType = typename remove_cvref<To>::type;
-    
-    auto type1 = Meta::name<FromType>();
-    auto type2 = Meta::name<ToType>();
-    
-    auto value1 = Meta::toStr(value);
-    
-    auto start1 = Meta::toStr(std::numeric_limits<FromType>::min());
-    auto end1 = Meta::toStr(std::numeric_limits<FromType>::max());
-    
-    auto start2 = Meta::toStr(std::numeric_limits<ToType>::min());
-    auto end2 = Meta::toStr(std::numeric_limits<ToType>::max());
-    
-    Warning("Failed converting %S(%S) [%S,%S] -> type %S [%S,%S]", &type1, &value1, &start1, &end1, &type2, &start2, &end2);
-}
-
-template<typename To, typename From>
-constexpr To sign_cast(From&& value) {
-#ifndef NDEBUG
-    using FromType = typename remove_cvref<From>::type;
-    using ToType = typename remove_cvref<To>::type;
-    
-    if constexpr(!std::is_floating_point<ToType>::value
-                 && std::is_integral<ToType>::value)
-    {
-        if constexpr(std::is_signed<ToType>::value) {
-            if constexpr(value > std::numeric_limits<ToType>::max())
-                fail_type<To, From>(std::forward<FromType>(value));
-            
-        } else if constexpr(std::is_signed<FromType>::value) {
-            if (value < 0)
-                fail_type<To, From>(std::forward<From>(value));
-            
-            using bigger_type = typename std::conditional<(sizeof(FromType) > sizeof(ToType)), FromType, ToType>::type;
-            if (bigger_type(value) > bigger_type(std::numeric_limits<ToType>::max()))
-                fail_type<To, From>(std::forward<From>(value));
-        }
-    }
-#endif
-    return static_cast<To>(std::forward<From>(value));
-}
-
-template<typename To, typename From>
-constexpr bool check_narrow_cast(const From& value) {
-#ifndef NDEBUG
-    using FromType = typename remove_cvref<From>::type;
-    using ToType = typename remove_cvref<To>::type;
-
-    auto str = Meta::toStr(value);
-    if constexpr (
-        std::is_floating_point<ToType>::value
-        || (std::is_signed<FromType>::value == std::is_signed<ToType>::value && !std::is_floating_point<FromType>::value)
-        )
-    {
-        // unsigned to unsigned
-#ifdef _NARROW_PRINT_VERBOSE
-        auto tstr0 = Meta::name<FromType>();
-        auto tstr1 = Meta::name<ToType>();
-        Debug("Narrowing %S -> %S (same) = %S.", &tstr0, &tstr1, &str);
-#endif
-        return true;
-    }
-    else if constexpr (std::is_floating_point<FromType>::value && std::is_signed<ToType>::value) {
-        using signed_t = int64_t;
-#ifdef _NARROW_PRINT_VERBOSE
-        auto tstr0 = Meta::name<FromType>();
-        auto tstr1 = Meta::name<ToType>();
-        auto tstr2 = Meta::name<signed_t>();
-        Debug("Narrowing %S -> %S | converting to %S and comparing (fs) = %S.", &tstr0, &tstr1, &tstr2, &str);
-#endif
-        return static_cast<signed_t>(value) >= static_cast<signed_t>(std::numeric_limits<To>::min())
-            && static_cast<signed_t>(value) <= static_cast<signed_t>(std::numeric_limits<To>::max());
-    }
-    else if constexpr (std::is_floating_point<FromType>::value && std::is_unsigned<ToType>::value) {
-        using unsigned_t = uint64_t;
-#ifdef _NARROW_PRINT_VERBOSE
-        auto tstr0 = Meta::name<FromType>();
-        auto tstr1 = Meta::name<ToType>();
-        auto tstr2 = Meta::name<unsigned_t>();
-        Debug("Narrowing %S -> %S | converting to %S and comparing (fs) = %S.", &tstr0, &tstr1, &tstr2, &str);
-#endif
-        return value >= FromType(0)
-            && static_cast<unsigned_t>(value) <= static_cast<unsigned_t>(std::numeric_limits<To>::max());
-    }
-    else if constexpr (std::is_unsigned<FromType>::value && std::is_signed<ToType>::value) {
-        // unsigned to signed
-        using signed_t = int64_t;
-#ifdef _NARROW_PRINT_VERBOSE
-        auto tstr0 = Meta::name<FromType>();
-        auto tstr1 = Meta::name<ToType>();
-        auto tstr2 = Meta::name<signed_t>();
-        Debug("Narrowing %S -> %S | converting to %S and comparing (us) = %S.", &tstr0, &tstr1, &tstr2, &str);
-#endif
-        return static_cast<signed_t>(value) < static_cast<signed_t>(std::numeric_limits<To>::max());
-
-    }
-    else {
-        static_assert(std::is_signed<FromType>::value && std::is_unsigned<ToType>::value, "Expecting signed to unsigned conversion");
-        // signed to unsigned
-        using unsigned_t = typename try_make_unsigned<FromType>::type;
-#ifdef _NARROW_PRINT_VERBOSE
-        auto tstr0 = Meta::name<FromType>();
-        auto tstr1 = Meta::name<ToType>();
-        auto tstr2 = Meta::name<unsigned_t>();
-        Debug("Narrowing %S -> %S | converting to %S and comparing (su) = %S.", &tstr0, &tstr1, &tstr2, &str);
-#endif
-        return value >= 0 && static_cast<unsigned_t>(value) <= static_cast<unsigned_t>(std::numeric_limits<To>::max());
-    }
-#else
-    return true;
-#endif
-}
-
-template<typename To, typename From>
-constexpr To narrow_cast(From&& value, struct tag::warn_on_error) {
-#ifndef NDEBUG
-    if (!check_narrow_cast<To, From>(value)) {
-        auto vstr = Meta::toStr(value);
-        auto lstr = Meta::toStr(std::numeric_limits<To>::min());
-        auto rstr = Meta::toStr(std::numeric_limits<To>::max());
-
-        auto tstr = Meta::name<To>();
-        auto fstr = Meta::name<From>();
-        Warning("Value '%S' in narrowing conversion of %S -> %S is not within limits [%S,%S].", &vstr, &fstr, &tstr, &lstr, &rstr);
-    }
-#endif
-    return static_cast<To>(std::forward<From>(value));
-}
-
-template<typename To, typename From>
-constexpr To narrow_cast(From&& value, struct tag::fail_on_error) {
-#ifndef NDEBUG
-    if (!check_narrow_cast<To, From>(value)) {
-        auto vstr = Meta::toStr(value);
-        auto lstr = Meta::toStr(std::numeric_limits<To>::min());
-        auto rstr = Meta::toStr(std::numeric_limits<To>::max());
-
-        auto tstr = Meta::name<To>();
-        auto fstr = Meta::name<From>();
-        U_EXCEPTION("Value '%S' in narrowing conversion of %S -> %S is not within limits [%S,%S].", &vstr, &fstr, &tstr, &lstr, &rstr);
-    }
-#endif
-    return static_cast<To>(std::forward<From>(value));
-}
-
-template<typename To, typename From>
-constexpr To narrow_cast(From&& value) {
-    return narrow_cast<To, From>(std::forward<From>(value), tag::warn_on_error{});
-}
-}
diff --git a/Application/src/commons/common/video/GenericVideo.cpp b/Application/src/commons/common/video/GenericVideo.cpp
index 8c3bb02..82e861b 100644
--- a/Application/src/commons/common/video/GenericVideo.cpp
+++ b/Application/src/commons/common/video/GenericVideo.cpp
@@ -3,6 +3,7 @@
 #include <misc/GlobalSettings.h>
 #include <grabber/default_config.h>
 #include <misc/Image.h>
+#include <misc/checked_casts.h>
 
 using namespace cmn;
 
diff --git a/Application/src/commons/common/video/VideoSource.cpp b/Application/src/commons/common/video/VideoSource.cpp
index d9ca985..80bd174 100644
--- a/Application/src/commons/common/video/VideoSource.cpp
+++ b/Application/src/commons/common/video/VideoSource.cpp
@@ -4,6 +4,7 @@
 #include <file/Path.h>
 #include <misc/GlobalSettings.h>
 #include <misc/ThreadPool.h>
+#include <misc/checked_casts.h>
 
 using namespace cmn;
 
@@ -534,7 +535,7 @@ void VideoSource::generate_average(cv::Mat &av, uint64_t) {
         }
     }
     
-    Debug("generating average in threads step %d for %d files (%d per file)", step, _files_in_seq.size(), frames_per_file);
+    Debug("generating average in threads step %lu for %lu files (%lu per file)", step, _files_in_seq.size(), frames_per_file);
     
     std::mutex mutex;
     GenericThreadPool pool(cmn::hardware_concurrency(), [](auto e) { std::rethrow_exception(e); }, "AverageImage");
diff --git a/Application/src/grabber/default_config.cpp b/Application/src/grabber/default_config.cpp
index ae4ca55..3afaf62 100644
--- a/Application/src/grabber/default_config.cpp
+++ b/Application/src/grabber/default_config.cpp
@@ -119,7 +119,7 @@ namespace default_config {
         CONFIG("equalize_histogram", false, "Equalizes the histogram of the image before thresholding and background subtraction.");
         CONFIG("quit_after_average", false, "If set to true, this will terminate the program directly after generating (or loading) a background average image.", STARTUP);
         CONFIG("averaging_method", averaging_method_t::mean, "Determines the way in which the background samples are combined. The background generated in the process will be used to subtract background from foreground objects during conversion.");
-        CONFIG("average_samples", int(100), "Number of samples taken to generate an average image. Usually fewer are necessary for `average_method`s max, and min.");
+        CONFIG("average_samples", uint32_t(100), "Number of samples taken to generate an average image. Usually fewer are necessary for `average_method`s max, and min.");
         CONFIG("reset_average", false, "If set to true, the average will be regenerated using the live stream of images (video or camera).");
         
         CONFIG("video_size", Size2(-1,-1), "Is set to the dimensions of the resulting image.", SYSTEM);
diff --git a/Application/src/grabber/gui.cpp b/Application/src/grabber/gui.cpp
index 1dd9b1b..37f8de6 100644
--- a/Application/src/grabber/gui.cpp
+++ b/Application/src/grabber/gui.cpp
@@ -336,9 +336,9 @@ void GUI::draw(gui::DrawStructure &base) {
                 base.wrap_object(*background);
             }
             
-            base.text("generating average ("+std::to_string(_grabber.average_samples())+"/"+std::to_string(SETTING(average_samples).value<uint32_t>())+")", Vec2(_size.width/2, _size.height/2), Red, Font(0.8, Align::Center), base.scale().reciprocal());
+            base.text("generating average ("+std::to_string(_grabber.average_samples())+"/"+std::to_string(SETTING(average_samples).value<uint32_t>())+")", Vec2(_size.width/2, _size.height/2), Red, Font(0.8f, Align::Center), base.scale().reciprocal());
         } else {
-            base.text("waiting for frame...", Vec2(_size.width/2, _size.height/2), Red, Font(0.8, Align::Center), base.scale().reciprocal());
+            base.text("waiting for frame...", Vec2(_size.width/2, _size.height/2), Red, Font(0.8f, Align::Center), base.scale().reciprocal());
         }
     }
     
@@ -361,7 +361,7 @@ void GUI::draw(gui::DrawStructure &base) {
             text_color = Black;
         }
     }
-    base.text(info_text(), Vec2(20, 10), text_color, Font(0.7), base.scale().reciprocal());
+    base.text(info_text(), Vec2(20, 10), text_color, Font(0.7f), base.scale().reciprocal());
     base.draw_log_messages();
     
     if(_grabber.tracker_instance()) {
diff --git a/Application/src/tracker/VideoOpener.cpp b/Application/src/tracker/VideoOpener.cpp
index 53f1dcd..86d4b52 100644
--- a/Application/src/tracker/VideoOpener.cpp
+++ b/Application/src/tracker/VideoOpener.cpp
@@ -392,7 +392,7 @@ VideoOpener::VideoOpener() {
                 if(!contains(_raw_info->children(), (Drawable*)_loading_text.get()))
                     _raw_info->add_child(2, _loading_text);
                 //_loading_text->set_pos(_screenshot->pos());
-                _loading_text->set_txt("generating average ("+Meta::toStr(min(TEMP_SETTING(average_samples).value<int>(), (int)_buffer->_number_samples.load()))+"/"+TEMP_SETTING(average_samples).get().valueString()+")");
+                _loading_text->set_txt("generating average ("+Meta::toStr(min(TEMP_SETTING(average_samples).value<uint32_t>(), _buffer->_number_samples.load()))+"/"+TEMP_SETTING(average_samples).get().valueString()+")");
                 
             } else if(contains(_raw_info->children(), (Drawable*)_loading_text.get())) {
                 _raw_info->remove_child(_loading_text);
@@ -498,7 +498,7 @@ void VideoOpener::BufferedVideo::restart_background() {
         
         _terminated_background_task = false;
         
-        uint step = max(1u, uint(background_video->length() / max(2u, uint(TEMP_SETTING(average_samples).value<int>()))));
+        uint step = max(1u, uint(background_video->length() / max(2u, uint(TEMP_SETTING(average_samples).value<uint32_t>()))));
         cv::Mat flt, img;
         _number_samples = 0;
         
diff --git a/Application/src/tracker/gui/IdentityHeatmap.cpp b/Application/src/tracker/gui/IdentityHeatmap.cpp
index 6b28235..78182bb 100644
--- a/Application/src/tracker/gui/IdentityHeatmap.cpp
+++ b/Application/src/tracker/gui/IdentityHeatmap.cpp
@@ -3,6 +3,7 @@
 #include <tracker/gui/gui.h>
 #include <tracking/Individual.h>
 #include <misc/cnpy_wrapper.h>
+#include <misc/checked_casts.h>
 
 namespace gui {
 namespace heatmap {
diff --git a/Application/src/tracker/misc/EventAnalysis.cpp b/Application/src/tracker/misc/EventAnalysis.cpp
index 575dfb1..3afb5a9 100644
--- a/Application/src/tracker/misc/EventAnalysis.cpp
+++ b/Application/src/tracker/misc/EventAnalysis.cpp
@@ -2,6 +2,7 @@
 #include <tracking/Tracker.h>
 #include <numeric>
 #include <misc/Timer.h>
+#include <misc/checked_casts.h>
 
 namespace track {
 namespace EventAnalysis {
diff --git a/Application/src/tracker/misc/Output.cpp b/Application/src/tracker/misc/Output.cpp
index 1a7b366..500b9a6 100644
--- a/Application/src/tracker/misc/Output.cpp
+++ b/Application/src/tracker/misc/Output.cpp
@@ -6,6 +6,7 @@
 #include <lzo/minilzo.h>
 #include <gui/gui.h>
 #include <gui/WorkProgress.h>
+#include <misc/checked_casts.h>
 
 using namespace track;
 typedef int64_t data_long_t;
diff --git a/Application/src/tracker/misc/OutputLibrary.cpp b/Application/src/tracker/misc/OutputLibrary.cpp
index c2f4f70..73a770e 100644
--- a/Application/src/tracker/misc/OutputLibrary.cpp
+++ b/Application/src/tracker/misc/OutputLibrary.cpp
@@ -4,6 +4,7 @@
 #include <misc/EventAnalysis.h>
 #include <file/CSVExport.h>
 #include <misc/cnpy_wrapper.h>
+#include <misc/checked_casts.h>
 
 namespace Output {
     using namespace gui;
diff --git a/Application/src/tracker/tracking/Accumulation.cpp b/Application/src/tracker/tracking/Accumulation.cpp
index 97be1f5..04c94da 100644
--- a/Application/src/tracker/tracking/Accumulation.cpp
+++ b/Application/src/tracker/tracking/Accumulation.cpp
@@ -12,6 +12,7 @@
 #include <misc/default_settings.h>
 #include <gui/Graph.h>
 #include <gui/types/StaticText.h>
+#include <misc/checked_casts.h>
 
 namespace track {
 using namespace file;
@@ -258,14 +259,14 @@ std::tuple<bool, std::map<long_t, long_t>> Accumulation::check_additional_range(
     
     for(auto && [id, tup] : averages) {
         auto & [samples, values] = tup;
-        long_t max_index = -1;
+        int64_t max_index = -1;
         float max_p = 0;
         if(samples > 0) {
             for(auto & v : values)
                 v /= samples;
         }
         
-        for(long_t i=0; i<(long_t)values.size(); ++i) {
+        for(uint32_t i=0; i<values.size(); ++i) {
             auto v = values[i];
             if(v > max_p) {
                 max_index = i;
@@ -424,7 +425,7 @@ std::tuple<std::shared_ptr<TrainingData>, std::vector<Image::Ptr>, std::map<long
         
         std::map<long_t, std::set<long_t>> disc_individuals_per_frame;
         
-        for(long_t frame = analysis_range.start; frame <= analysis_range.end; frame += max(1, analysis_range.length() * 0.003))
+        for(long_t frame = analysis_range.start; frame <= analysis_range.end; frame += max(1, analysis_range.length() / 333))
         {
             if(frame < Tracker::start_frame())
                 continue;
diff --git a/Application/src/tracker/tracking/Export.cpp b/Application/src/tracker/tracking/Export.cpp
index 1adab06..fd37d75 100644
--- a/Application/src/tracker/tracking/Export.cpp
+++ b/Application/src/tracker/tracking/Export.cpp
@@ -10,6 +10,7 @@
 #include <misc/cnpy_wrapper.h>
 #include <tracker/misc/MemoryStats.h>
 #include <pv.h>
+#include <misc/checked_casts.h>
 
 #if WIN32
 #include <io.h>
diff --git a/Application/src/tracker/tracking/Outline.cpp b/Application/src/tracker/tracking/Outline.cpp
index 63a4420..19bd5ab 100644
--- a/Application/src/tracker/tracking/Outline.cpp
+++ b/Application/src/tracker/tracking/Outline.cpp
@@ -12,6 +12,7 @@
 #include <gui/DrawCVBase.h>
 #include <misc/default_config.h>
 #include <misc/create_struct.h>
+#include <misc/checked_casts.h>
 
 using namespace track;
 //#define _DEBUG_MEMORY
-- 
GitLab