Last active
November 12, 2015 11:01
-
-
Save spokendotcpp/146407ed9695feaca02f to your computer and use it in GitHub Desktop.
Imagerie
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GrayImage.hpp" | |
#include <stdexcept> | |
#include <string> | |
GrayImage::GrayImage(ushort w, ushort h) | |
:width(w), height(h), array(0) | |
{ array = new ubyte[width*height]; } | |
GrayImage::GrayImage() | |
:array(0) | |
{ throw std::runtime_error("It's ilegal to create a GrayImage's instance without parameters."); } | |
GrayImage::GrayImage(const GrayImage& source) | |
:width(source.width), height(source.height), array(new ubyte[width*height]) | |
{ for(uint i=0; i<uint(width*height); ++i) array[i]=source.array[i]; } | |
void GrayImage::clear(ubyte gray){ | |
for(uint i=0; i<uint(width*height);++i) | |
array[i]=gray; | |
} | |
void GrayImage::writeRAW(std::ostream& f){ | |
ubyte tmp[4]; | |
// poids faibles | poids forts | |
tmp[0]=width%256; tmp[1]=width>>8; | |
tmp[2]=height%256; tmp[3]=height>>8; | |
f.write((const char*)tmp, 4); | |
f.write((const char*)array, width*height); | |
} | |
void GrayImage::writePGM(std::ostream& f){ | |
f<<"P5\n"; // Magic Number | |
f<<"# Commentaire \n"; | |
f<<width<<" "<<height<<std::endl; // Utilisation du cin car il s'agit de texte | |
f<<"# Commentaire 2 \n"; | |
f<<"255\n"; // Valeur max des niveaux de gris | |
//> No more comments here | |
f.write((const char*)array, width*height); | |
} | |
static GrayImage* readPGM(std::istream& is){ | |
ushort width, height; | |
string p5 = "P5"; | |
is>>width>>height; | |
GrayImage i(width, height); | |
return | |
} | |
void GrayImage::fillRectangle(ushort x, ushort y, ushort w, ushort h, ubyte color){ | |
for(uint i=y; i< y+w ; ++i) | |
for(uint j=x; j < x+h; ++j) | |
pixel(i,j)=color; | |
} | |
void GrayImage::rectangle(ushort x, ushort y, ushort w, ushort h, ubyte color){ | |
for(uint i=x; i < uint(x+w+1) ; ++i){ | |
pixel(i,y)=color; | |
pixel(i,y+h)=color; | |
} | |
for(uint j=y+1; j < y+h ; ++j){ | |
pixel(x,j)=color; | |
pixel(x+w,j)=color; | |
} | |
} | |
// skip_line(is) permet de sauter toute une série d'octets de "is" jusqu'à trouver un '\n' | |
static void skip_line(std::istream& is){ | |
char c; | |
// Lire un caractère | |
do{ is.get(c); } while(c!='\n'); // tant qu'on n'atteint pas la fin de ligne. | |
} | |
// skip_comments(is) utilise la fonction précédente pour sauter zéro, une ou plusieurs lignes | |
// de commentaires débutées par '#' et allant jusqu'à '\n'. | |
static void skip_comments(std::istream& is){ | |
char c; | |
is.get(c); // Lire un caractère. | |
// Tant que c'est un '#'. | |
while(c=='#'){ | |
skip_line(is); // On élimine les caractères jusqu'à la fin de ligne, | |
is.get(c); // Et on lit un nouveau caractère. | |
} | |
is.putback(c); // On remet le caractère lu puisqu'il n'est pas un '#'. | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef GRAYIMAGE_HPP | |
#define GRAYIMAGE_HPP | |
#include <iostream> | |
#include <fstream> | |
typedef unsigned short ushort; | |
typedef unsigned int uint; | |
typedef unsigned char ubyte; | |
class GrayImage{ | |
private: | |
ushort width; // Largeur | |
ushort height; // Hauteur | |
ubyte* array; // Pointeur sur char(ubyte), alloue par la suite un tableau dynamique | |
public: | |
GrayImage(ushort width, ushort height); // Constructeur | |
GrayImage(); // Constructeur par défaut | |
GrayImage(const GrayImage& source); // Constructeur par copie | |
inline ~GrayImage() { delete [] array; } // Destructeur | |
// Différents accesseurs et mutateurs + surcharge opérateur () (foncteur) | |
inline const ubyte& pixel(ushort x, ushort y) const { return array[y*width + x]; } | |
inline ubyte& pixel(ushort x, ushort y) { return array[y*width + x]; } | |
inline const ubyte& operator() (ushort x, ushort y) const { return array[y*width + x]; } | |
inline ubyte& operator () (ushort x, ushort y) { return array[y*width + x]; } | |
// Dessin | |
void clear(ubyte =0); // Tous les pixels de l'image prennent la valeur ubyte donnée | |
void fillRectangle(ushort x, ushort y, ushort w, ushort h, ubyte color=0); | |
void rectangle(ushort x, ushort y, ushort w, ushort h, ubyte color=0); | |
// Ecriture & Lecture d'image | |
void writeRAW(std::ostream& ); // Ecriture RAW 'brute' | |
void writePGM(std::ostream& ); // Ecriture dans un fichier PGM | |
static GrayImage* readPGM(std::istream& ); // Lecture d'un fichier PGM | |
static void skip_line(std::istream& ); | |
static void skip_comments(std::istream& ); | |
}; | |
#endif //GRAYIMAGE_HPP |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "GrayImage.hpp" | |
int main(){ | |
//GrayImage i; //>Déclanche une erreur | |
GrayImage i(300,300); | |
i.clear(); | |
i.fillRectangle(11,11,120,150, 255); | |
i.rectangle(10,10,122,152,144); | |
std::ofstream f("test.pgm", std::ios::out | std::ios::binary); | |
std::ofstream g("test", std::ios::out | std::ios::binary); | |
i.writePGM(f); | |
i.writeRAW(g); | |
f.close(); | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment