load image from cmdline, toggle modes, help dialog

This commit is contained in:
2024-06-12 16:15:35 -04:00
parent daf0814c08
commit d5c557785d
3 changed files with 711 additions and 11 deletions

129
main.cpp
View File

@ -9,6 +9,8 @@
// folder).
// - Introduction, links and more at the top of imgui.cpp
#include <SDL2/SDL_events.h>
#include <SDL2/SDL_scancode.h>
#include <cstddef>
#include "lib/backends/imgui_impl_opengl3.h"
@ -16,6 +18,7 @@
#include "lib/imgui.h"
#include "lib/imgui_internal.h"
#include <iostream>
#include <stdexcept>
#include <stdio.h>
#define STB_IMAGE_IMPLEMENTATION
@ -39,6 +42,11 @@
#include "lib/imgui_tex_inspect.h"
#include "lib/backends/tex_inspect_opengl.h"
#include "lib/argparse.hpp"
struct Args : public argparse::Args {
std::string &fpath = arg("path to the image");
};
struct Texture
{
@ -77,9 +85,12 @@ Texture LoadTexture(const char * path)
static bool init = true;
static bool showHelp = false;
static int mode = 0;
const int maxAnnotatedTexels = 10000;
// Main code
int main(int, char **) {
int main(int argc, char* argv[]) {
// Setup SDL
if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER | SDL_INIT_GAMECONTROLLER) !=
0) {
@ -87,6 +98,10 @@ int main(int, char **) {
return -1;
}
bool TOOLTIP_ENABLED = false;
bool GRID_ENABLED = true;
bool AA_ENABLED = true;
// Decide GL+GLSL versions
#if defined(IMGUI_IMPL_OPENGL_ES2)
// GL ES 2.0 + GLSL 100
@ -160,32 +175,66 @@ int main(int, char **) {
Texture t;
if (init) {
init = false;
try {
auto args = argparse::parse<Args>(argc, argv, true);
t = LoadTexture(args.fpath.c_str());
} catch (const std::runtime_error &e) {
std::cerr << "failed to parse arguments: " << e.what() << std::endl;
return -1;
}
}
// Main loop
bool done = false;
while (!done)
{
auto flags = ImGuiTexInspect::InspectorFlags_FillVertical | ImGuiTexInspect::InspectorFlags_FillHorizontal;
// Poll and handle events (inputs, window resize, etc.)
SDL_Event event;
while (SDL_PollEvent(&event)) {
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT &&
event.window.event == SDL_WINDOWEVENT_CLOSE &&
event.window.windowID == SDL_GetWindowID(window))
done = true;
switch (event.type) {
case SDL_QUIT:
done = true;
break;
case SDL_KEYDOWN:
switch (event.key.keysym.scancode) {
case SDL_SCANCODE_G:
GRID_ENABLED = !GRID_ENABLED;
break;
case SDL_SCANCODE_T:
TOOLTIP_ENABLED = !TOOLTIP_ENABLED;
break;
case SDL_SCANCODE_A:
AA_ENABLED = !AA_ENABLED;
break;
case SDL_SCANCODE_H:
showHelp = !showHelp;
break;
case SDL_SCANCODE_D:
mode = (mode + 1) % 5;
break;
case SDL_SCANCODE_Q:
done = true;
break;
default:
break;
}
}
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();
ImGui_ImplSDL2_NewFrame();
ImGui::NewFrame();
if (init) {
init = false;
t = LoadTexture("/home/dubey/Pictures/DSC03199.JPG");
}
{
#ifdef IMGUI_HAS_VIEWPORT
ImGuiViewport *viewport = ImGui::GetMainViewport();
@ -206,18 +255,78 @@ int main(int, char **) {
ImGuiWindowFlags_NoDecoration | ImGuiWindowFlags_NoResize);
auto wSize = ImGui::GetContentRegionAvail();
if (t.texture != 0) {
if (!TOOLTIP_ENABLED) {
flags = flags | ImGuiTexInspect::InspectorFlags_NoTooltip;
}
ImGuiTexInspect::BeginInspectorPanel(
"Inspector",
t.texture,
t.size,
ImGuiTexInspect::InspectorFlags_FillVertical | ImGuiTexInspect::InspectorFlags_FillHorizontal,
flags,
ImGuiTexInspect::SizeExcludingBorder(wSize)
);
if (GRID_ENABLED) {
CurrentInspector_ClearFlags(ImGuiTexInspect::InspectorFlags_NoGrid);
}else {
CurrentInspector_SetFlags(ImGuiTexInspect::InspectorFlags_NoGrid);
}
if (TOOLTIP_ENABLED) {
CurrentInspector_ClearFlags(ImGuiTexInspect::InspectorFlags_NoTooltip);
}else {
CurrentInspector_SetFlags(ImGuiTexInspect::InspectorFlags_NoTooltip);
}
if (AA_ENABLED) {
CurrentInspector_ClearFlags(ImGuiTexInspect::InspectorFlags_NoForceFilterNearest);
}else {
CurrentInspector_SetFlags(ImGuiTexInspect::InspectorFlags_NoForceFilterNearest);
}
switch(mode) {
case 1:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::Arrow().UsePreset(ImGuiTexInspect::Arrow::NormalMap), maxAnnotatedTexels);
break;
case 2:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::HexString), maxAnnotatedTexels);
break;
case 3:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::BytesDec), maxAnnotatedTexels);
break;
case 4:
ImGuiTexInspect::DrawAnnotations(ImGuiTexInspect::ValueText(ImGuiTexInspect::ValueText::Format::Floats), maxAnnotatedTexels);
break;
default:
break;
}
ImGuiTexInspect::EndInspectorPanel();
}
ImGui::End();
ImGui::PopStyleVar();
ImGui::PopStyleVar();
if (showHelp){
ImGui::OpenPopup("HelpPopup");
showHelp = !showHelp;
}
if (ImGui::BeginPopup("HelpPopup")) {
ImGui::Text("tview Help");
ImGui::Separator();
ImGui::Text("g - toggle grid");
ImGui::Text("a - toggle filtering");
ImGui::Text("t - toggle tooltip");
ImGui::Text("d - cycle pixel detail mode");
ImGui::Text("q - quit");
ImGui::Text("h - show help popup");
ImGui::Separator();
ImGui::Text("click anywhere to continue");
ImGui::EndPopup();
}
}
// Rendering