TP2: bmp
This commit is contained in:
parent
9aa259a40c
commit
dcbf9e8d06
4 changed files with 353 additions and 0 deletions
233
tp2/bmp.cpp
Executable file
233
tp2/bmp.cpp
Executable file
|
@ -0,0 +1,233 @@
|
||||||
|
#include "bmp.h"
|
||||||
|
#include <iostream>
|
||||||
|
|
||||||
|
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<length; i+=3){
|
||||||
|
pixel[i] = 255&couleur; //B
|
||||||
|
pixel[i+1] = 255&(couleur>>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<width)&&(Y<height));
|
||||||
|
|
||||||
|
BYTE R, G, B;
|
||||||
|
LONG ind=3*(Y*width+X);
|
||||||
|
|
||||||
|
R = (BYTE)(255&(color>>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((Y<height)&&(X<width));
|
||||||
|
|
||||||
|
LONG ind=3*(Y*width+X);
|
||||||
|
|
||||||
|
R = pixel[ind+2]; //R
|
||||||
|
G = pixel[ind+1]; //G
|
||||||
|
B = pixel[ind]; //B
|
||||||
|
|
||||||
|
couleur = 0;
|
||||||
|
couleur = couleur|(R<<16);
|
||||||
|
couleur = couleur|(G<<8);
|
||||||
|
couleur = couleur|B;
|
||||||
|
|
||||||
|
return couleur;
|
||||||
|
};
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
//write(nom): écrit les données dans le fichier 'nom' (Bmp binaire)//
|
||||||
|
/////////////////////////////////////////////////////////////////////
|
||||||
|
void Bmp::write(CHAR * nom){
|
||||||
|
ofstream out(nom,ios::binary);
|
||||||
|
BYTE temp[4];
|
||||||
|
|
||||||
|
//tester si le fichier est ouvert
|
||||||
|
out.seekp(0,ios::end);
|
||||||
|
if (out.tellp()<0)
|
||||||
|
{
|
||||||
|
out.close();
|
||||||
|
throw("erreur d'ouverture de fichier");
|
||||||
|
cout << "erreur d'ouverture de fichier"<<endl;
|
||||||
|
}
|
||||||
|
|
||||||
|
//ecriture de l'en-tête
|
||||||
|
/* En tête fichier*/
|
||||||
|
|
||||||
|
BYTE Sig[2];
|
||||||
|
Sig[0] ='B';
|
||||||
|
Sig[1] ='M';
|
||||||
|
ULONG TailleFichier =length+0x36; //En 24 bits! En 8 bits(v4.0): hauteur*largeur+0x36+1024
|
||||||
|
ULONG Inconnu =0;
|
||||||
|
ULONG OffsetImage =0x36; //taille des en-têtes
|
||||||
|
ULONG taille =40;
|
||||||
|
|
||||||
|
out.write((char*)Sig, 2);
|
||||||
|
convert(temp,TailleFichier);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,Inconnu);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,OffsetImage);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,taille);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
|
||||||
|
/* En-Tête BMP*/
|
||||||
|
USHORT plan = 1;
|
||||||
|
USHORT bpp = 24;
|
||||||
|
ULONG compression = 0;
|
||||||
|
ULONG tailleimage = 3*width*height;
|
||||||
|
ULONG resol_horz = 0;
|
||||||
|
ULONG resol_vert = 0;
|
||||||
|
ULONG nb_couleurs = 0;
|
||||||
|
ULONG nb_couleurs_importantes = 0;
|
||||||
|
|
||||||
|
convert(temp,(USHORT)width);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,(USHORT)height);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,plan);
|
||||||
|
out.write((char*)temp, 2);
|
||||||
|
convert(temp,bpp);
|
||||||
|
out.write((char*)temp, 2);
|
||||||
|
convert(temp,compression);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,tailleimage);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,resol_horz);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,resol_vert);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,nb_couleurs);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
convert(temp,nb_couleurs_importantes);
|
||||||
|
out.write((char*)temp, 4);
|
||||||
|
|
||||||
|
/*ecriture des valeurs des pixels*/
|
||||||
|
out.write((char *)pixel,length);
|
||||||
|
out.close();
|
||||||
|
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
//diff(a,b): retourne la distance entre a et b//
|
||||||
|
////////////////////////////////////////////////
|
||||||
|
inline SHORT diff(ULONG a,ULONG b)
|
||||||
|
{
|
||||||
|
return a>b?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);
|
||||||
|
}
|
64
tp2/bmp.h
Executable file
64
tp2/bmp.h
Executable file
|
@ -0,0 +1,64 @@
|
||||||
|
#ifndef __BMP__
|
||||||
|
#define __BMP__
|
||||||
|
///////////////////////////////////////////////////////////
|
||||||
|
// header inclus dans tous les fichiers sources
|
||||||
|
// G. Péoux, 2009
|
||||||
|
///////////////////////////////////////////////////////////
|
||||||
|
#include <fstream>
|
||||||
|
#include <iostream>
|
||||||
|
#include <cassert>
|
||||||
|
#include <cmath>
|
||||||
|
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
|
11
tp2/main.cpp
Executable file
11
tp2/main.cpp
Executable file
|
@ -0,0 +1,11 @@
|
||||||
|
#include <iostream>
|
||||||
|
#include "bmp.h"
|
||||||
|
|
||||||
|
using namespace std;
|
||||||
|
|
||||||
|
int main()
|
||||||
|
{
|
||||||
|
Bmp image(512,512,NOIR);
|
||||||
|
image.write("test.bmp");
|
||||||
|
return 0;
|
||||||
|
}
|
45
tp2/tp2.cbp
Executable file
45
tp2/tp2.cbp
Executable file
|
@ -0,0 +1,45 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
|
||||||
|
<CodeBlocks_project_file>
|
||||||
|
<FileVersion major="1" minor="6" />
|
||||||
|
<Project>
|
||||||
|
<Option title="tp2" />
|
||||||
|
<Option pch_mode="2" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Build>
|
||||||
|
<Target title="Debug">
|
||||||
|
<Option output="bin/Debug/tp2" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Debug/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-g" />
|
||||||
|
</Compiler>
|
||||||
|
</Target>
|
||||||
|
<Target title="Release">
|
||||||
|
<Option output="bin/Release/tp2" prefix_auto="1" extension_auto="1" />
|
||||||
|
<Option object_output="obj/Release/" />
|
||||||
|
<Option type="1" />
|
||||||
|
<Option compiler="gcc" />
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-O2" />
|
||||||
|
</Compiler>
|
||||||
|
<Linker>
|
||||||
|
<Add option="-s" />
|
||||||
|
</Linker>
|
||||||
|
</Target>
|
||||||
|
</Build>
|
||||||
|
<Compiler>
|
||||||
|
<Add option="-Wall" />
|
||||||
|
<Add option="-fexceptions" />
|
||||||
|
</Compiler>
|
||||||
|
<Unit filename="bmp.cpp" />
|
||||||
|
<Unit filename="bmp.h" />
|
||||||
|
<Unit filename="main.cpp" />
|
||||||
|
<Extensions>
|
||||||
|
<code_completion />
|
||||||
|
<envvars />
|
||||||
|
<debugger />
|
||||||
|
<lib_finder disable_auto="1" />
|
||||||
|
</Extensions>
|
||||||
|
</Project>
|
||||||
|
</CodeBlocks_project_file>
|
Loading…
Add table
Reference in a new issue