This repository has been archived on 2019-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
PILG/src/utilitaires.cpp
Geoffrey Frogeye 386ff0b51b Mise en utilisation des nouvelles commandes
Les commandes ajoutées dans le code sont désormais accessible depuis le programme, grâce à quelques modifications dans les différents arguments et beaucoup de copier/coller.
* Traitement de la commande
	* Ajout des arguments valeur (v1), booléen (b1), texte (t1), fichier (f1), entree (e), sortie (s), composante (p)
		* Toutes les valeurs (entier, floattant) sont désormais regroupées dans valeur
		* Entree et sortie sont des fichiers qui seront lus et écrits (respectivement) sans conditions (pour une utilisation sans REPL)
	* Ajout des commandes creer, ouvrir, sauver, trait, rectangle, cercle, disque, retourner, redimensionner, convRVB
	* Ajout et modification de quelques codes d'erreur
* Corrections et améliorations diverses
2014-05-20 22:15:37 +02:00

163 lines
4.5 KiB
C++

#include <math.h>
#include <fstream>
#define NOMBREOR 1.61803398875
ofstream journal("PILG-log.txt", ios::out | ios::trunc);
void presentation() {
cout << " ____ ___ _ ____ " << endl
<< "| _ \\ |_ _ || | / ___|" << endl
<< "| |_) | | | | | | | _ " << endl
<< "| __/ | | | |___ | |_| |" << endl
<< "|_| |___| |_____| \\____|" << endl;
}
Image imageDefaut() {
int dimY = 256, dimX = dimY * NOMBREOR, maxComposante = 255;
Image imageRoue(dimX, dimY, maxComposante, PILG_RVB);
Pixel pointRoue = imageRoue.g_pixelVide();
int x, y, step;
float substep, lum;
for (x = 0; x < dimX; x++) {
for (y = 0; y < dimY; y++) {
step = (x * 6.0) / dimX;
substep = (x - step * (dimX / 6.0)) / (dimX / 6.0) * maxComposante;
lum = 1 - ((float) y) / dimY;
switch (step) {
case 0:
pointRoue.r = maxComposante;
pointRoue.v = substep;
pointRoue.b = 0;
break;
case 1:
pointRoue.r = maxComposante - substep;
pointRoue.v = maxComposante;
pointRoue.b = 0;
break;
case 2:
pointRoue.r = 0;
pointRoue.v = maxComposante;
pointRoue.b = substep;
break;
case 3:
pointRoue.r = 0;
pointRoue.v = maxComposante - substep;
pointRoue.b = maxComposante;
break;
case 4:
pointRoue.r = substep;
pointRoue.v = 0;
pointRoue.b = maxComposante;
break;
case 5:
pointRoue.r = maxComposante;
pointRoue.v = 0;
pointRoue.b = maxComposante - substep;
break;
default:
pointRoue.r = pointRoue.v = pointRoue.b = 0;
}
// Dégradé vers le noir
pointRoue.r = pointRoue.r * lum;
pointRoue.v = pointRoue.v * lum;
pointRoue.b = pointRoue.b * lum;
imageRoue.s_pixel(x, y, pointRoue);
}
}
return imageRoue;
}
void afficherImage(Image image) {
#define ECHELLE 1
int x, y, r, v, b, eX, eY, dimensionX = image.g_dimensionX() * ECHELLE,
dimensionY = image.g_dimensionY() * ECHELLE,
typeComposantes = image.g_typeComposantes();
float ratio = (255.0 / image.g_maxComposante());
Pixel pixel;
if (fenetreOuverte && (dimensionX != fenetreDimensionX ||
dimensionY != fenetreDimensionY)) {
fermerFenetre();
}
ouvrirFenetre(dimensionX, dimensionY, "PILG");
for (x = 0; x < image.g_dimensionX(); x++) {
for (y = 0; y < image.g_dimensionY(); y++) {
image.g_pixel(x, y, pixel);
switch (typeComposantes) {
case PILG_BIN:
r = v = b = (pixel.n ? 255 : 0);
break;
case PILG_NIV:
r = v = b = pixel.g * ratio;
break;
case PILG_RVB:
r = pixel.r * ratio;
v = pixel.v * ratio;
b = pixel.b * ratio;
break;
}
for (eX = 0; eX < ECHELLE; eX++) {
for (eY = 0; eY < ECHELLE; eY++) {
pointFenetre(x * ECHELLE + eX, y * ECHELLE + eY, r, v, b);
}
}
}
}
afficherFenetre();
}
void messageErreur(string message) {
cerr << "Erreur : " << message << '.' << endl;
journal << "Erreur : " << message << '.' << endl;
}
int chaineVersEntier(string chaine, int &entier) {
entier = atoi(chaine.c_str());
if (entier == 0 && chaine != "0") {
return 1;
}
return 0;
}
int chaineVersFlottant(string chaine, float &flottant) {
flottant = atof(chaine.c_str());
if (flottant == 0 && chaine != "0") {
return 1;
}
return 0;
}
int caraVersEntier(char cara) {
// int entier = (int) (0 << 8) + cara;
// entier = entier > 0 ? entier : 256+entier;
int i, entier = 0;
for (i = 0; i < 8; i++) {
entier += ((cara >> i) & 0x01) ? pow(2, i) : 0;
}
return entier;
}