From dcbf9e8d0631f2ced7fff96e9134b7a11259817d Mon Sep 17 00:00:00 2001 From: Yohan Boujon Date: Sat, 18 Dec 2021 14:57:48 +0100 Subject: [PATCH] TP2: bmp --- tp2/bmp.cpp | 233 +++++++++++++++++++++++++++++++++++++++++++++++++++ tp2/bmp.h | 64 ++++++++++++++ tp2/main.cpp | 11 +++ tp2/tp2.cbp | 45 ++++++++++ 4 files changed, 353 insertions(+) create mode 100755 tp2/bmp.cpp create mode 100755 tp2/bmp.h create mode 100755 tp2/main.cpp create mode 100755 tp2/tp2.cbp diff --git a/tp2/bmp.cpp b/tp2/bmp.cpp new file mode 100755 index 0000000..df427a7 --- /dev/null +++ b/tp2/bmp.cpp @@ -0,0 +1,233 @@ +#include "bmp.h" +#include + +using namespace std; + +////////////////////////////////////////////////////////////////////////////////////////////////// +//Bmp(w,h, color): constructeur affectant la mémoire pour une image w*h de couleur de fond color// +////////////////////////////////////////////////////////////////////////////////////////////////// + +Bmp::Bmp(SHORT w,SHORT h, ULONG couleur_fond){ + width=w; + height=h; + length=3*width*height; + couleur=couleur_fond; + pixel=new BYTE[length]; + for (LONG i=0; i>8); //V + pixel[i+2] = 255&(couleur>>16); //R + } +} + +//////////////////////////////////////////////////////////////// +//destructeur de Bmp: libère la memoire occupée par les pixels// +//////////////////////////////////////////////////////////////// +Bmp::~Bmp(void){ + if(pixel) + delete [] pixel; +} + +//////////////////////////////////////////////////////////////// +//affecte la couleur 'color' au pixel de coordonnees (X,Y) // +//////////////////////////////////////////////////////////////// +void Bmp:: setpixel(SHORT X,SHORT Y,ULONG color) { + assert((X>16)); + G = (BYTE)(255&(color>>8)); + B = (BYTE)(255&color); + pixel[ind] = B; //B + pixel[ind+1]= G; //G + pixel[ind+2]= R; //R + return; +} +////////////////////////////////////////////////////////////// +//retourne la couleur du pixel de coordonnees (X,Y) // +////////////////////////////////////////////////////////////// +ULONG Bmp::getpixel(SHORT X,SHORT Y){ + ULONG couleur; + BYTE R,G,B; + + assert((Yb?a-b:b-a; +} + + +void Bmp::line(SHORT I1,SHORT J1,SHORT I2,SHORT J2,ULONG color){ + + SHORT dI,dJ,incrI,incrJ,currentI,currentJ,dPi,dPj,dPij,P; + + + //Algorithme de Bresenham (1962) + dI=diff(I1,I2); + dJ=diff(J1,J2); + currentI=I1; + currentJ=J1; + incrI=(I2>I1?1:-1); + incrJ=(J2>J1?1:-1); + + + if (dJ>=dI) //|tan(theta)|<=1 + { + dPj=dI<<1; + dPij=dPj-(dJ<<1); + P=dPj-dJ; + + for(;dJ>=0;dJ--){ + setpixel(currentI,currentJ,color); + if(P>0){ + currentI+=incrI; + currentJ+=incrJ; + P+=dPij; + } + else{ + currentJ+=incrJ; + P+=dPj; + } + } + } + else{ //|tan(theta)|>1 + dPi=dJ<<1; + dPij=dPi-(dI<<1); + P=dPi-dI; + + for(;dI>=0;dI--){ + setpixel(currentI,currentJ,color); + if(P>0) { + currentJ+=incrJ; + currentI+=incrI; + P+=dPij; + } + else { + currentI+=incrI; + P+=dPi; + } + } + } + + return; +} + +////////////////////////////////////////////////////////// +// Les fonctions privées // +////////////////////////////////////////////////////////// + +void Bmp:: convert(BYTE *temp, ULONG Champ) { + BYTE *ptr; + ULONG *ptr_u; + + ptr_u=&Champ; + ptr = (BYTE*)ptr_u; + + temp[0]= *ptr; + temp[1]= *(ptr+1); + temp[2]= *(ptr+2); + temp[3]= *(ptr+3); +} +void Bmp:: convert(BYTE *temp, USHORT Champ) { + BYTE *ptr; + USHORT *ptr_u; + + ptr_u=&Champ; + ptr = (BYTE*)ptr_u; + + temp[0]= *ptr; + temp[1]= *(ptr+1); +} diff --git a/tp2/bmp.h b/tp2/bmp.h new file mode 100755 index 0000000..226bbf1 --- /dev/null +++ b/tp2/bmp.h @@ -0,0 +1,64 @@ +#ifndef __BMP__ +#define __BMP__ +/////////////////////////////////////////////////////////// +// header inclus dans tous les fichiers sources +// G. Péoux, 2009 +/////////////////////////////////////////////////////////// +#include +#include +#include +#include +using namespace std; + +//////////////////////////////////// +//Redefinition des types d'entiers// +//////////////////////////////////// +typedef unsigned char BYTE; +typedef unsigned long int ULONG; +typedef unsigned short int USHORT; +typedef long LONG; +typedef short SHORT; +typedef char CHAR; + +//////////////////////////////////// +//Definition des couleurs de base +//////////////////////////////////// +const ULONG ROUGE =0x00FF0000; +const ULONG BLEU =0x000000FF; +const ULONG VERT =0x0000FF00; +const ULONG JAUNE =0x00FFFF00; +const ULONG ROSE =0x00FF00FF; +const ULONG CYAN =0x0000FFFF; +const ULONG NOIR =0x00000000; +const ULONG BLANC =0x00FFFFFF; + +////////////////////////////////////////////////////////////////// +// Definition de la classe Bmp // +// Représente un fichier image au format Bmp // +////////////////////////////////////////////////////////////////// +class Bmp { + public: + //Bmp(w,h, color): constructeur affectant la mémoire pour une image w*h de couleur de fond coul + Bmp(SHORT,SHORT, ULONG coul=NOIR); + ~Bmp(void); + //Récupérer les attributs privés + SHORT getW() {return width;}; + SHORT getH() {return height;}; + //affecte la couleur 'color' au pixel de coordonnees (X,Y) + void setpixel(SHORT X,SHORT Y,ULONG color); + //retourne la couleur d'un pixel + ULONG getpixel(SHORT X,SHORT Y); + //trace une ligne à partir de deux points + void line(SHORT,SHORT,SHORT,SHORT,ULONG color); + //Ecriture de l'image dans un fichier + void write(CHAR *); + private: + SHORT width; //largeur + SHORT height; //hauteur + LONG length; //nombre d'octets de 'pixel' (3*largeur*hauteur) + ULONG couleur; //Couleur de fond de l'image + BYTE * pixel; //tableau des pixels + void convert(BYTE *, ULONG ); + void convert(BYTE *, USHORT); +}; +#endif \ No newline at end of file diff --git a/tp2/main.cpp b/tp2/main.cpp new file mode 100755 index 0000000..10c0c1f --- /dev/null +++ b/tp2/main.cpp @@ -0,0 +1,11 @@ +#include +#include "bmp.h" + +using namespace std; + +int main() +{ + Bmp image(512,512,NOIR); + image.write("test.bmp"); + return 0; +} diff --git a/tp2/tp2.cbp b/tp2/tp2.cbp new file mode 100755 index 0000000..4e83d0b --- /dev/null +++ b/tp2/tp2.cbp @@ -0,0 +1,45 @@ + + + + + +