Le source : TP-FiltrageSpectral.cpp
/* Auteur: Nicolas JANEY */
/* nicolas.janey@univ-fcomte.fr */
/* Avril 2002 */
/* Chargement d'une image au format RAW */
/* et filtrage spectral */
#include <stdlib.h>
#include <stdio.h>
#include <math.h>
#include <GL/glut.h>
#include <GL/gl.h>
#include <GL/glu.h>
#include "ModuleMenus.h"
#include "ModuleReshape.h"
struct spectre {
int t[256];
int max; } ;
static int f1 ;
static int f2 ;
static int f3 ;
static int f4 ;
static GLubyte *imagei = NULL ;
static GLubyte *imagef = NULL ;
static spectre si;
static spectre sf;
static int tx;
static int ty;
static char *filename;
static int im = 0;
static int filtre = 0;
static float fact = 1.0F;
void initSpectre(spectre *s) {
for ( int i = 0 ; i < 256 ; i++ ) {
s->t[i] = 0; }
s->max = 0;
}
void calculSpectre(spectre *s,GLubyte *im) {
initSpectre(s);
int i;
for ( i = 0 ; i < 3*tx*ty ; i++ )
s->t[im[i]]++ ;
s->max = s->t[0];
for ( i = 1 ; i < 256 ; i++ )
if ( s->t[i] > s->max )
s->max = s->t[i];
}
void myinit(void) {
glClearColor(0.0,0.0,0.0,1.0);
glPixelStorei(GL_UNPACK_ALIGNMENT,1);
glPixelStorei(GL_PACK_ALIGNMENT,1);
glColor3f(1.0,1.0,1.0);
}
void displayi(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRasterPos2f(0.0F,0.0F);
glDrawPixels(tx,ty,GL_RGB,GL_UNSIGNED_BYTE,imagei);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displayf(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
glRasterPos2f(0.0F,0.0F);
glDrawPixels(tx,ty,GL_RGB,GL_UNSIGNED_BYTE,imagef);
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displaySi(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
if ( si.max != 0 ) {
glBegin(GL_LINES);
for ( int i = 0 ; i < 256 ; i++ ) {
glVertex2f(i,0.0F);
glVertex2f(i,(float) si.t[i]*200.0F/si.max); }
glEnd(); }
glPopMatrix();
glFlush();
glutSwapBuffers();
}
void displaySf(void) {
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix();
if ( sf.max != 0 ) {
glBegin(GL_LINES);
for ( int i = 0 ; i < 256 ; i++ ) {
glVertex2f(i,0.0F);
glVertex2f(i,(float) sf.t[i]*200.0F/sf.max); }
glEnd(); }
glPopMatrix();
glFinish();
glutSwapBuffers();
}
void chargementImage(char *filename,int tx,int ty,GLubyte *im) {
FILE *f = fopen(filename,"rb");
if ( f )
for ( int y = ty-1 ; y >= 0 ; y-- )
fread(&im[y*3*tx],1,3*tx,f);
else
printf("Fichier non trouve...\n");
}
GLubyte negative(GLubyte b) {
return(255-b);
}
GLubyte luminosite(GLubyte b) {
return((GLubyte) (pow(b/255.0,(double) fact)*255));
}
GLubyte contraste(GLubyte b) {
if ( b > 127 ) {
return((GLubyte) (pow((b-127.5)/127.5,(double) fact)*127.5+127.5)); }
else {
return((GLubyte) (-pow((127.5-b)/127.5,(double) fact)*127.5+127.5)); }
}
void filtrageImage(int tx,int ty,GLubyte *imagei,GLubyte *imagef,GLubyte (*func)(GLubyte b)) {
for ( int i = 0 ; i < tx*ty*3 ; i++ )
imagef[i] = func(imagei[i]);
}
void filtrerImage(int tx,int ty,GLubyte *imagei,GLubyte *imagef) {
switch ( filtre ) {
case 0 : filtrageImage(tx,ty,imagei,imagef,negative);
break;
case 1 : filtrageImage(tx,ty,imagei,imagef,luminosite);
break;
case 2 : filtrageImage(tx,ty,imagei,imagef,contraste);
break; }
}
void key(unsigned char key,int x,int y) {
switch ( key ) {
case 43 : fact = fact*1.1F;
filtrerImage(tx,ty,imagei,imagef);
calculSpectre(&sf,imagef);
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
glutPostWindowRedisplay(f3);
glutPostWindowRedisplay(f4);
break;
case 45 : fact = fact/1.1F;
filtrerImage(tx,ty,imagei,imagef);
calculSpectre(&sf,imagef);
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
glutPostWindowRedisplay(f3);
glutPostWindowRedisplay(f4);
break;
case 0x0D : im = (im+1)%2;
filename =(char *) (( im ) ? "Image-500x200.raw" : "Image-350x233.raw") ;
tx = ( im ) ? 500 : 350 ;
ty = ( im ) ? 200 : 233 ;
if ( imagei != NULL ) {
free(imagei);
free(imagef); }
imagei =(GLubyte *) calloc(tx*ty*3,sizeof(GLubyte));
chargementImage(filename,tx,ty,imagei);
imagef =(GLubyte *) calloc(tx*ty*3,sizeof(GLubyte));
filtrerImage(tx,ty,imagei,imagef);
calculSpectre(&si,imagei);
calculSpectre(&sf,imagef);
glutSetWindow(f1);
glutReshapeWindow(tx,ty);
glutSetWindow(f2);
glutReshapeWindow(tx,ty);
glutPostWindowRedisplay(f3);
glutPostWindowRedisplay(f4);
break;
case ' ' : filtre = (filtre+1)%3;
filtrerImage(tx,ty,imagei,imagef);
calculSpectre(&sf,imagef);
glutPostWindowRedisplay(f1);
glutPostWindowRedisplay(f2);
glutPostWindowRedisplay(f3);
glutPostWindowRedisplay(f4);
break;
case 0x1B : exit(0);
break; }
}
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();
}
int main(int argc,char **argv) {
si.max = sf.max = 0;
glutInit(&argc,argv);
glutInitDisplayMode(GLUT_RGB);
glutInitWindowSize(200,200);
glutInitWindowPosition(10,30);
f1 = glutCreateWindow("Filtrage spectral");
myinit();
creationMenuBasique();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(displayi);
glutInitWindowSize(200,200);
glutInitWindowPosition(10,300);
f2 = glutCreateWindow("Image filtrée");
myinit();
creationMenuBasique();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(displayf);
glutInitWindowSize(256,200);
glutInitWindowPosition(520,30);
f3 = glutCreateWindow("Spectre initial");
myinit();
creationMenuBasique();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(displaySi);
glutInitWindowSize(256,200);
glutInitWindowPosition(520,300);
f4 = glutCreateWindow("Spectre modifie");
myinit();
creationMenuBasique();
glutReshapeFunc(reshape);
glutKeyboardFunc(key);
glutDisplayFunc(displaySf);
glutMainLoop();
return(0);
}