Le source: TraitementImagesBasique.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Juin 2002 */
/* Traitement d'images */
/* Operations basiques */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "PICTBitmapGL.h"
#include "PICTMain.h"
static int f1 ;
static int f2 ;
static int f3 ;
static int op = 0 ;
static struct espace bt1 = { 0,0,0,0 } ;
static struct espace bt2 = { 0,0,0,0 } ;
static struct espace imagef = { 0,0,0,0 } ;
static int img = 0 ;
void myinit(void) {
glClearColor(0.0,0.0,0.0,1.0);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_PACK_ALIGNMENT,1);
}
void display1(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
affichageGLBitmap(&bt1) ;
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void display2(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
affichageGLBitmap(&bt2) ;
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayf(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
affichageGLBitmap(&imagef) ;
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void vide(espace *im) {
if ( im->dx != 0 ) {
liberation_espace(im);
im->dx = 0; }
}
static int vmin(int v1,int v2) {
return ((v1 < v2) ? v1 : v2);
}
void identite(espace *bt1,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
new_espace(im,bt1->dx,bt1->dy,24);
for ( int y = 0 ; y < bt1->dy ; y++ ) {
for ( int x = 0 ; x < 3*bt1->dx ; x++ ) {
im->p[y][x] = bt1->p[y][x]; } }
}
void negation(espace *bt1,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
new_espace(im,bt1->dx,bt1->dy,24);
for ( int i = 0 ; i < bt1->dy ; i++ )
for ( int j = 0 ; j < 3*bt1->dx ; j++ )
im->p[i][j] = ~bt1->p[i][j];
}
void calculEt(espace *bt1,espace *bt2,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
new_espace(im,tx,ty,24);
for ( int i = 0 ; i < ty ; i++ )
for ( int j = 0 ; j < 3*tx ; j++ )
im->p[i][j] = bt1->p[i][j] & bt2->p[i][j] ;
}
void calculOu(espace *bt1,espace *bt2,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
new_espace(im,tx,ty,24);
for ( int i = 0 ; i < ty ; i++ )
for ( int j = 0 ; j < 3*tx ; j++ )
im->p[i][j] = bt1->p[i][j] | bt2->p[i][j] ;
}
void calculXor(espace *bt1,espace *bt2,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
new_espace(im,tx,ty,24);
for ( int i = 0 ; i < ty ; i++ )
for ( int j = 0 ; j < 3*tx ; j++ )
im->p[i][j] = bt1->p[i][j] ^ bt2->p[i][j] ;
}
void soustraction(espace *bt1,espace *bt2,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
new_espace(im,tx,ty,24);
for ( int i = 0 ; i < ty ; i++ )
for ( int j = 0 ; j < 3*tx ; j++ ) {
int v =(int) bt1->p[i][j] - bt2->p[i][j];
if ( v < 0 )
v = 0;
im->p[i][j] =(GLubyte) v ; }
}
void addition(espace *bt1,espace *bt2,espace *im) {
if ( im->dx != 0 )
liberation_espace(im);
int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
new_espace(im,tx,ty,24);
for ( int i = 0 ; i < ty ; i++ )
for ( int j = 0 ; j < 3*tx ; j++ ) {
int v =(int) bt1->p[i][j] + bt2->p[i][j];
if ( v > 255 )
v = 255;
im->p[i][j] =(GLubyte) v ; }
}
void operationSurImage(espace *bt1,espace *bt2,espace *im) {
switch(op) {
case 0 : glutSetWindow(f3);
glutReshapeWindow(200,200);
vide(im);
break;
case 1 : glutSetWindow(f3);
glutReshapeWindow(bt1->dx,bt1->dy);
identite(bt1,im);
break;
case 2 : glutSetWindow(f3);
glutReshapeWindow(bt1->dx,bt1->dy);
negation(bt1,im);
break;
case 3 : if ( bt2->dx != 0 ) {
glutSetWindow(f3);
{ int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
glutReshapeWindow(tx,ty); }
calculEt(bt1,bt2,im); }
break;
case 4 : if ( bt2->dx != 0 ) {
glutSetWindow(f3);
{ int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
glutReshapeWindow(tx,ty); }
calculOu(bt1,bt2,im); }
break;
case 5 : if ( bt2->dx != 0 ) {
glutSetWindow(f3);
{ int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
glutReshapeWindow(tx,ty); }
calculXor(bt1,bt2,im); }
break;
case 6 : if ( bt2->dx != 0 ) {
glutSetWindow(f3);
{ int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
glutReshapeWindow(tx,ty); }
soustraction(bt1,bt2,im); }
break;
case 7 : if ( bt2->dx != 0 ) {
glutSetWindow(f3);
{ int tx = min(bt1->dx,bt2->dx);
int ty = min(bt1->dy,bt2->dy);
glutReshapeWindow(tx,ty); }
addition(bt1,bt2,im); }
break; }
glutPostWindowRedisplay(f3);
}
void raffraichissement(void) {
if ( bt1.dx != 0 ) {
operationSurImage(&bt1,&bt2,&imagef); }
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
glutPostWindowRedisplay(f3);
}
void incrementationOperation(void) {
op = (op+1)%8;
raffraichissement() ;
}
void chargementImage(char *fichier,espace *bt,int f) {
liberation_espace(bt) ;
lecture_PICT24_compacte(fichier,bt) ;
glutSetWindow(f);
glutReshapeWindow(bt->dx,bt->dy);
raffraichissement();
}
void chargementImageArbitraire(espace *bt,int f) {
char fichier[300];
printf("Nom du fichier PICT 24 bits a charger : ");
scanf("%s",fichier);
chargementImage(fichier,bt,f);
}
void reshape(int w,int h) {
glViewport(0,0,w,h);
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glOrtho(0,w,0,h,-1.0,1.0);
glMatrixMode(GL_MODELVIEW);
glLoadIdentity();
}
void select(int selection) {
switch (selection) {
case 1 : if ( imagef.dx != 0 ) {
char fichier[300];
printf("Nom du fichier PICT 24 bits a sauver : ");
scanf("%s",fichier);
sauvegarde_PICT24_compacte(fichier,&imagef) ; }
break;
case 0 : exit(0);
break; }
glutPostRedisplay();
}
void selectImage24(int selection,espace *bt,int f) {
switch (selection) {
case 1 : chargementImage("Pict24-1.pct",bt,f);
break;
case 2 : chargementImage("Pict24-2.pct",bt,f);
break;
case 3 : chargementImage("Pict24-3.pct",bt,f);
break;
case 4 : chargementImage("Pict24-4.pct",bt,f);
break;
case 5 : chargementImage("Thermes.pct",bt,f);
break;
case 6 : chargementImage("PlaceDeLaLiberte.pct",bt,f);
break;
case 7 : chargementImage("ABCD.pct",bt,f);
break;
case 8 : chargementImage("Formes.pct",bt,f);
break;
case 9 : chargementImage("Lignes.pct",bt,f);
break;
case 10 : chargementImage("Quadrillage.pct",bt,f);
break;
case 11 : chargementImage("Temple.pct",bt,f);
break;
case 12 : chargementImage("AB.pct",bt,f);
break;
case 13 : chargementImage("ABNegatif.pct",bt,f);
break;
case 14 : chargementImage("ThermesSmall.pct",bt,f);
break;
case -1 : chargementImageArbitraire(bt,f);
break; }
}
void selectImage24bt1(int selection) {
selectImage24(selection,&bt1,f1);
}
void selectImage24bt2(int selection) {
selectImage24(selection,&bt2,f2);
}
void selectOperation(int selection) {
switch (selection) {
case 0 :
case 1 :
case 2 :
case 3 :
case 4 :
case 5 :
case 6 :
case 7 : op = selection;
raffraichissement();
break; }
}
void creerMenus1(void) {
int menuImage24 = glutCreateMenu(selectImage24bt1);
glutAddMenuEntry("Pict24-1.pct",1);
glutAddMenuEntry("Pict24-2.pct",2);
glutAddMenuEntry("Pict24-3.pct",3);
glutAddMenuEntry("Pict24-4.pct",4);
glutAddMenuEntry("Thermes.pct",5);
glutAddMenuEntry("PlaceDeLaLiberte.pct",6);
glutAddMenuEntry("ABCD.pct",7);
glutAddMenuEntry("Formes.pct",8);
glutAddMenuEntry("Lignes.pct",9);
glutAddMenuEntry("Quadrillage.pct",10);
glutAddMenuEntry("Temple.pct",11);
glutAddMenuEntry("AB.pct",12);
glutAddMenuEntry("ABNegatif.pct",13);
glutAddMenuEntry("ThermesSmall.pct",14);
glutAddMenuEntry("Fichier PICT 24 bits",-1);
int menuOperation = glutCreateMenu(selectOperation);
glutAddMenuEntry("Vide",0);
glutAddMenuEntry("Identite",1);
glutAddMenuEntry("Negation",2);
glutAddMenuEntry("Et",3);
glutAddMenuEntry("Ou",4);
glutAddMenuEntry("Xor",5);
glutAddMenuEntry("Soustraction",6);
glutAddMenuEntry("Addition",7);
glutCreateMenu(select);
glutAddSubMenu("Charger image PICT 24 bits",menuImage24);
glutAddSubMenu("Operation",menuOperation);
glutAddMenuEntry("Sauver image PICT 24 bits",1);
glutAddMenuEntry("Quitter",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void creerMenus2(void) {
int menuImage24 = glutCreateMenu(selectImage24bt2);
glutAddMenuEntry("Pict24-1.pct",1);
glutAddMenuEntry("Pict24-2.pct",2);
glutAddMenuEntry("Pict24-3.pct",3);
glutAddMenuEntry("Pict24-4.pct",4);
glutAddMenuEntry("Thermes.pct",5);
glutAddMenuEntry("PlaceDeLaLiberte.pct",6);
glutAddMenuEntry("ABCD.pct",7);
glutAddMenuEntry("Formes.pct",8);
glutAddMenuEntry("Lignes.pct",9);
glutAddMenuEntry("Quadrillage.pct",10);
glutAddMenuEntry("Temple.pct",11);
glutAddMenuEntry("AB.pct",12);
glutAddMenuEntry("ABNegatif.pct",13);
glutAddMenuEntry("ThermesSmall.pct",14);
glutAddMenuEntry("Fichier PICT 24 bits",-1);
int menuOperation = glutCreateMenu(selectOperation);
glutAddMenuEntry("Vide",0);
glutAddMenuEntry("Identite",1);
glutAddMenuEntry("Negation",2);
glutAddMenuEntry("Et",3);
glutAddMenuEntry("Ou",4);
glutAddMenuEntry("Xor",5);
glutAddMenuEntry("Soustraction",6);
glutAddMenuEntry("Addition",7);
glutCreateMenu(select);
glutAddSubMenu("Charger image PICT 24 bits",menuImage24);
glutAddSubMenu("Operation",menuOperation);
glutAddMenuEntry("Sauver image PICT 24 bits",1);
glutAddMenuEntry("Quitter",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void creerMenus3(void) {
int menuOperation = glutCreateMenu(selectOperation);
glutAddMenuEntry("Vide",0);
glutAddMenuEntry("Identite",1);
glutAddMenuEntry("Negation",2);
glutAddMenuEntry("Et",3);
glutAddMenuEntry("Ou",4);
glutAddMenuEntry("Xor",5);
glutAddMenuEntry("Soustraction",6);
glutAddMenuEntry("Addition",7);
glutCreateMenu(select);
glutAddSubMenu("Operation",menuOperation);
glutAddMenuEntry("Sauver image PICT 24 bits",1);
glutAddMenuEntry("Quitter",0);
glutAttachMenu(GLUT_RIGHT_BUTTON);
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case '1' : chargementImageArbitraire(&bt1,f1);
break;
case '2' : chargementImageArbitraire(&bt2,f2);
break;
case 'a' : img = (img+1)%14;
selectImage24(img+1,&bt1,f1);
break;
case 'b' : img = (img+1)%14;
selectImage24(img+1,&bt2,f2);
break;
case ' ' : incrementationOperation();
break;
case 0x1B : exit(0);
break; }
}
int main(int argc,char **argv) {
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGBA|GLUT_DOUBLE);
glutInitWindowSize(200,200);
glutInitWindowPosition(30,30);
f1 = glutCreateWindow("Image 1");
creerMenus1();
myinit();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(display1);
glutInitWindowSize(200,200);
glutInitWindowPosition(30,300);
f2 = glutCreateWindow("Image 2");
myinit();
creerMenus2();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(display2);
glutInitWindowSize(200,200);
glutInitWindowPosition(540,110);
f3 = glutCreateWindow("Image traitée");
myinit();
creerMenus3();
glutDisplayFunc(displayf);
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutMainLoop();
return(0);
}
Les modules utilitaires : Modules.zip
Les modules de gestion des images au format Macintosh PICT 24bits : PICT.zip