Modifications
- Save in PNG instead of BMP (fixing memory lost) - Use QuadTree class (not yet fully implemented) - Add comments
This commit is contained in:
parent
b5e5a448d2
commit
1014f64f2a
1 changed files with 24 additions and 14 deletions
38
src/main.cpp
38
src/main.cpp
|
@ -1,31 +1,34 @@
|
|||
#include <SDL_image.h>
|
||||
#include <iostream>
|
||||
#include <fstream>
|
||||
|
||||
#include "../includes/quadtree.hpp"
|
||||
|
||||
// Enregistre une image au format PNG
|
||||
void enregistrementImage(QuadTree, std::string, short);
|
||||
|
||||
int main(int argc, char const *argv[]) {
|
||||
// Récupération informations sur l'image
|
||||
if(argc != 2) {
|
||||
std::cerr << "Aucune image renseignée." << std::endl;
|
||||
std::cerr << "Usage : " << argv[0] << " image" << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// Création d'une surface SDL à partir de l'image renseignée
|
||||
SDL_Surface * s;
|
||||
if((s = IMG_Load(argv[1])) == NULL) {
|
||||
std::cerr << "Impossible de charger l'image." << std::endl;
|
||||
return 1;
|
||||
}
|
||||
|
||||
// TODO: Faire une vraie compression
|
||||
/* if(SDL_LockSurface(s) == 0) {
|
||||
SDL_memset(s->pixels, static_cast<int>(SDL_MapRGB(s->format, 0, 0, 0)), static_cast<size_t>(s->h * s->pitch));
|
||||
SDL_UnlockSurface(s);
|
||||
} else {
|
||||
std::cerr << "Impossible de modifier l'image (" << SDL_GetError() << ")." << std::endl;
|
||||
return 1;
|
||||
} */
|
||||
// Création du quadtree
|
||||
QuadTree quadtree(s);
|
||||
|
||||
std::string chemin_image("resultat.bmp");
|
||||
if(std::ifstream(chemin_image).is_open()) {
|
||||
SDL_FreeSurface(s);
|
||||
|
||||
// Enregistrement du résultat
|
||||
std::string chemin_image("resultat.png");
|
||||
if(std::ifstream(chemin_image).is_open()) { // demande à l'utilisateur d'écraser ou non le fichier existant
|
||||
char choix;
|
||||
do {
|
||||
std::cout << "Le fichier \""<< chemin_image << "\" existe déjà, voulez vous l'écraser ? y/n" << std::endl;
|
||||
|
@ -33,14 +36,21 @@ int main(int argc, char const *argv[]) {
|
|||
choix = tolower(choix);
|
||||
} while(choix != 'n' && choix != 'y' && choix != 'o');
|
||||
if(choix == 'y' || choix == 'o') {
|
||||
SDL_SaveBMP(s, chemin_image.c_str());
|
||||
enregistrementImage(quadtree, chemin_image, 0);
|
||||
} else {
|
||||
std::cout << "Passe..." << std::endl;
|
||||
}
|
||||
} else {
|
||||
SDL_SaveBMP(s, chemin_image.c_str());
|
||||
enregistrementImage(quadtree, chemin_image, 0);
|
||||
}
|
||||
|
||||
SDL_FreeSurface(s);
|
||||
return 0;
|
||||
}
|
||||
|
||||
void enregistrementImage(QuadTree quadtree, std::string chemin, short compression) {
|
||||
SDL_Surface * img = quadtree.image(compression);
|
||||
if(IMG_SavePNG(img, chemin.c_str()) != 0) {
|
||||
std::cerr << "Impossible de sauvegarder l'image (" << SDL_GetError() << ")." << std::endl;
|
||||
}
|
||||
SDL_FreeSurface(img);
|
||||
}
|
||||
|
|
Reference in a new issue