1
0
mirror of synced 2024-12-04 20:17:57 +01:00
ImHex/plugins/builtin/source/content/minimap_visualizers.cpp
2024-01-30 00:00:51 +01:00

68 lines
2.1 KiB
C++

#include <hex/api/content_registry.hpp>
#include <imgui.h>
#include <hex/ui/imgui_imhex_extensions.h>
#include <hex/helpers/utils.hpp>
#include <wolv/utils/string.hpp>
namespace hex::plugin::builtin {
namespace {
ImColor entropyMiniMapVisualizer(const std::vector<u8> &data) {
std::array<u8, 256> frequencies = { 0 };
for (u8 byte : data)
frequencies[byte] += 1;
double entropy = 0.0;
for (u32 frequency : frequencies) {
if (frequency == 0)
continue;
double probability = static_cast<double>(frequency) / data.size();
entropy -= probability * std::log2(probability);
}
// Calculate color
ImColor color = ImColor::HSV(0.0F, 0.0F, 1.0F);
if (entropy > 0.0) {
double hue = std::clamp(entropy / 8.0, 0.0, 1.0);
color = ImColor::HSV(static_cast<float>(hue) / 0.75F, 0.8F, 1.0F);
}
return color;
}
ImColor zerosMiniMapVisualizer(const std::vector<u8> &data) {
u32 zerosCount = 0;
for (u8 byte : data) {
if (byte == 0x00)
zerosCount += 1;
}
return ImColor::HSV(0.0F, 0.0F, 1.0F - (double(zerosCount) / data.size()));
}
ImColor byteTypeMiniMapVisualizer(const std::vector<u8> &data) {
u8 asciiCount = 0;
for (u8 byte : data) {
if (std::isprint(byte))
asciiCount += 1;
}
return ImColor::HSV(0.5F, 0.5F, (double(asciiCount) / data.size()));
}
}
void registerMiniMapVisualizers() {
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.entropy", entropyMiniMapVisualizer);
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.zeros", zerosMiniMapVisualizer);
ContentRegistry::HexEditor::addMiniMapVisualizer("hex.builtin.minimap_visualizer.ascii", byteTypeMiniMapVisualizer);
}
}