wxWidgets based Stable Diffusion C++ GUi
Ferenc Szontágh
2024-02-04 1683c8a090c3efc51c43107d5ede0dcd5d506e3b
ui/utils.hpp
@@ -3,6 +3,8 @@
#include <string>
#include <filesystem>
#include <iostream>
#include <random>
#include <opencv2/opencv.hpp>
#include <stable-diffusion.h>
@@ -10,6 +12,47 @@
namespace sd_gui_utils
{
    typedef struct VoidHolder
    {
        void *p1;
        void *p2;
    } VoidHolder;
    struct ModelFileInfo
    {
        std::string name;
        std::string path;
        std::string url;
        std::string poster;
        std::string sha256;
        uintmax_t size;
        std::string size_f;
    };
    inline void to_json(nlohmann::json &j, const ModelFileInfo &p)
    {
        j = nlohmann::json{
            {"name", p.name},
            {"path", p.path},
            {"url", p.url},
            {"poster", p.poster},
            {"sha256", p.sha256},
            {"size", p.size},
            {"size_f", p.size_f}
        };
    }
    inline void from_json(const nlohmann::json &j, ModelFileInfo &p)
    {
        j.at("name").get_to(p.name);
        j.at("path").get_to(p.path);
        j.at("url").get_to(p.url);
        j.at("poster").get_to(p.poster);
        j.at("sha256").get_to(p.sha256);
        j.at("size").get_to(p.size);
        j.at("size_f").get_to(p.size_f);
    }
    enum THREAD_STATUS_MESSAGES
    {
@@ -49,7 +92,17 @@
        bool keep_model_in_memory = true;
        bool save_all_image = true;
    };
    inline std::string formatUnixTimestampToDate(long timestamp)
    {
        std::time_t time = static_cast<std::time_t>(timestamp);
        std::tm *timeinfo = std::localtime(&time);
        std::ostringstream oss;
        oss << (timeinfo->tm_year + 1900) << "-" << std::setw(2) << std::setfill('0') << (timeinfo->tm_mon + 1) << "-" << std::setw(2) << std::setfill('0') << timeinfo->tm_mday
            << " " << std::setw(2) << std::setfill('0') << timeinfo->tm_hour << ":" << std::setw(2) << std::setfill('0') << timeinfo->tm_min;
        return oss.str();
    }
    enum DirTypes
    {
        LORA,
@@ -277,6 +330,7 @@
            {"clip_skip", p.clip_skip},
            {"width", p.width},
            {"height", p.height},
            {"batch_count", p.batch_count},
            {"sample_method", (int)p.sample_method},
            {"schedule", (int)p.schedule},
            {"sample_steps", p.sample_steps},
@@ -377,6 +431,29 @@
        cv::resize(img, square(roi), roi.size());
        return square;
    };
    inline const unsigned int generateRandomInt(unsigned int min, unsigned int max)
    {
        std::random_device rd;
        std::mt19937 gen(rd());
        std::uniform_int_distribution<unsigned int> dis(min, max);
        return dis(gen);
    };
    inline std::pair<double, std::string> humanReadableFileSize(double bytes)
    {
        static const char *sizes[] = {"B", "KB", "MB", "GB", "TB"};
        int div = 0;
        double size = bytes;
        while (size >= 1024 && div < (sizeof(sizes) / sizeof(*sizes)))
        {
            size /= 1024;
            div++;
        }
        return std::make_pair(size, sizes[div]);
    }
};