m2b-gd-tp8/astico2d.hpp

137 lines
4.0 KiB
C++

/*
Module d'interface utilisateur pour Astico2D :
Atelier Simple de Transformations d'Images au Clavier avec OpenCV en 2D
CC BY-SA Edouard.Thiel@univ-amu.fr - 03/09/2021 - v1.0
*/
#include <iostream>
#include <iomanip>
#include <cstring>
#include <opencv2/opencv.hpp>
// Mesure du temps d'exécution ; mettre à 0 en cas de problème de compilation
#define MESURER_TEMPS 1
#if MESURER_TEMPS
# ifdef _WIN32
# include <windows.h>
# else
# include <sys/time.h>
# endif
class MesureTemps {
public:
MesureTemps ();
double mesurer ();
private:
# ifdef _WIN32
struct timeval _start_time; // Initial date
LARGE_INTEGER _start_count; // Counter to compute time
double _perf_freq; // Frequency to compute time
void _init_timeofday (void);
void _get_systemtime (struct timeval *t);
void _get_countertime (struct timeval *t);
# endif
void _gettimeofday (struct timeval *t);
};
#else // MESURER_TEMPS
class MesureTemps {
public:
MesureTemps () {}
double mesurer () { return 0.; };
};
#endif // MESURER_TEMPS
// Cette macro permet de vérifier si une image a le type attendu
#define CHECK_MAT_TYPE(mat, format_type) \
if (mat.type() != int(format_type)) \
throw std::runtime_error(std::string(__func__) +\
": format non géré '" + std::to_string(mat.type()) +\
"' pour la matrice '" # mat "'")
// Gestion des couleurs
void representer_en_couleurs_vga (cv::Mat &img_niv, cv::Mat &img_coul);
void inverser_couleurs (cv::Mat &img);
// Classe Loupe pour afficher une portion de l'image avec un zoom
class Loupe {
public:
int zoom = 5, zoom_max = 20;
int zoom_x0 = 0, zoom_y0 = 0, zoom_x1 = 100, zoom_y1 = 100;
int vue_min = 200, vue_max = 2000, vue_inc = 100;
int opt_w = 1600, opt_h = 1000;
void reborner (cv::Mat &res1, cv::Mat &res2);
void deplacer (cv::Mat &res1, cv::Mat &res2, int dx, int dy);
int calculer_zoom_optimal (cv::Mat &src);
void calculer_portion_optimale (cv::Mat &src, int &ph, int &pw);
void dessiner_rect (cv::Mat &src, cv::Mat &dest);
void dessiner_portion (cv::Mat &src, cv::Mat &dest);
void afficher_tableau_niveaux (cv::Mat &src, int ex, int ey, int rx, int ry);
void afficher_tableau_couleurs (cv::Mat &src, int ex, int ey, int rx, int ry);
};
// Classe Astico2D de gestion de l'interface utilisateur
class Astico2D {
public:
int init_ok = true;
char *nom_in1, *nom_out2;
cv::Mat img_src, img_res1, img_res2, img_niv, img_coul;
Loupe loupe;
int seuil_niv = 127, seuil_max = 255;
int clic_x = 0;
int clic_y = 0;
int clic_n = 0;
int zoom_w = 600, zoom_h = 500;
enum RecalculsImages { R_RIEN, R_LOUPE, R_TOUTES };
RecalculsImages recalc = R_TOUTES;
char touche_transfo = 0;
enum ModeTransfo { M_AUCUN, M_NIVEAUX, M_COULEURS };
ModeTransfo mode_transfo = M_AUCUN;
MesureTemps temps;
Astico2D (int &argc, char **&argv);
int run ();
void afficher_usage (const char *nom_prog);
virtual void afficher_touches_clavier ();
int analyser_arguments (int &argc, char **&argv);
int lire_image_entree ();
int enregistrer_image_resultat ();
void creer_images_resultat ();
void creer_slider (const char* nom_slider, int *valeur_courante,
int valeur_max);
void modifier_slider (const char* nom_slider, int nouvelle_valeur);
void diminuer_slider (const char* nom_slider, int valeur_min);
void augmenter_slider (const char* nom_slider, int valeur_max);
void creer_fenetres ();
void traiter_evenements ();
virtual bool traiter_touche_clavier (char key) { return false; };
virtual void effectuer_transformations () {};
private:
static void onZoomSlide (int pos, void *data);
static void onDefaultTrackbarSlide (int pos, void *data);
static void onMouseEventSrc (int event, int x, int y, int flags, void *data);
static void onMouseEventLoupe (int event, int x, int y, int flags, void *data);
static int onKeyPressEvent (int key, void *data);
};