1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <MLV/MLV_all.h>
typedef struct arbre
{
int point[2]; //le point haut a gauche d'image
int n; // taille de l'image est n x n
int *couleur;//couleur=NULL si l'image comptien plusieurs couleur
struct arbre *sarbre1, *sarbre2,*sarbre3,*sarbre4;
}quadTree;
quadTree *racine=NULL;
int image[512][512];
int oneColor(quadTree *img)
{
/***
verifie si l'image contien une seule couleur
***/
int i,j, dl=img->point[0], dc=img->point[1];
for(i=0;i<img->n;i++)
for(j=0;j<img->n;j++)
if (image[dl+1][dc+j]!= image[dl][dc]) return 0;
return 1;
}
quadTree* creat(int dl,int dc,int n, int * c)
{
/***
créer un noeud de quadTree
***/
quadTree *nov=(quadTree*)malloc(sizeof(quadTree));
nov->point[0]=dl;nov->point[1]=dc;
nov->n=n;nov->couleur=c;
nov->sarbre1=NULL;nov->sarbre2=NULL;
nov->sarbre3=NULL;nov->sarbre4=NULL;
return nov;
}
void decompose4(quadTree * noeud)
{
/***
decomposé une image en 4 sous image, et créer les noeuds correspondant
***/
int n=noeud->n/2;
int a=noeud->point[0], b=noeud->point[1], c=noeud->point[0]+n, d=noeud->point[1]+n;
noeud->sarbre1=creat(a, b, n, NULL);
noeud->sarbre2=creat(a, d, n, NULL);
noeud->sarbre3=creat(c, d, n, NULL);
noeud->sarbre1=creat(c, b, n, NULL);
if(oneColor(noeud->sarbre1)==1)noeud->sarbre1->couleur=&image[a][b];
if(oneColor(noeud->sarbre2)==1)noeud->sarbre2->couleur=&image[a][d];
if(oneColor(noeud->sarbre3)==1)noeud->sarbre3->couleur=&image[c][d];
if(oneColor(noeud->sarbre4)==1)noeud->sarbre4->couleur=&image[c][b];
}
///*******************************Creation de quad Tree*****************************
/// fifo pour parcourir quad tree
typedef struct f
{
quadTree *img;
struct f *next;
}fifo;
fifo *debut=NULL;
fifo * fin=NULL;
void add(quadTree *img)
{
///ajout d'un noeud à fifo
fifo * nov=(fifo*)malloc(sizeof(fifo));
nov->img=img;
nov->next=NULL;
if (debut==NULL){debut=nov; fin=nov;}
else
{
fin->next=nov;
fin=nov;
}
fin->next=NULL;
}
quadTree * pop()
{
/****
extraire un élément de la fifo
****/
fifo *d=NULL;
d=debut;
debut=debut->next;
return d->img;
}
///ajouter les sous images à fifo
void add_sarbres(quadTree * img)
{
add(img->sarbre1);
add(img->sarbre2);
add(img->sarbre3);
add(img->sarbre4);
}
void create_tree(int n)
{
racine=NULL;
quadTree * na=NULL;//noeud actuel
racine=creat(0,0,n,NULL);
if (oneColor(racine)==1)racine->couleur=&image[racine->point[0]][racine->point[1]];
else{
add(racine);
while(debut!=NULL)
{
na=pop();
if( na->couleur==NULL)
{
decompose4(na);
add_sarbres(na);
}
}
}
}
///
void TreeInFile(char *fichier)
{
FILE *pf=fopen(fichier,"w");
quadTree * na=NULL;//noeud actuel
debut=NULL;
add(racine);
while(debut!=NULL)
{
na=pop();
if( na!=NULL)
{
fwrite(na,sizeof(quadTree),1,pf);
fflush(pf);
add_sarbres(na);
}
}
fclose(pf);
}
///
int main()
{
int i,j,choix=0, n=512,l,h;
char nomf[100], *nfarbre=NULL, s[]=".quad";
MLV_Image *image_mlv=NULL;
FILE * pf;
do{
printf("(1) ouvrir une image en 256 niveaux de gris au format jpg\n"
"(2) ouvrir un fichier contenant la representation par un arbre quartique dune image carree\n"
"(3) choisir de decouper limage en 16 morceaux\n"
"(4) choisir de d´ecouper limage en 64 morceaux\n"
"(5) quitter\n votre choix :");
scanf("%d",&choix);
switch(choix){
case 1: printf("donnez le chemain du fichier image"); scanf("%s",nomf);
nfarbre=(char*)malloc(sizeof(char)*(strlen(nomf)+2));
strncpy(nfarbre,nomf,strlen(nomf)-4);
strcat(nfarbre,s);//fichier contenant la representation par un arbre quartique
///ouvrire l'image
MLV_create_window( "advanced - 6 - image", "image", 640, 480 );
image_mlv = MLV_load_image( nomf );
/// deretminer la talle de l'image
l = MLV_get_image_width(image_mlv);
h=MLV_get_image_height(image_mlv);
if (l>512 && h>512){
h=512;
l=512;
MLV_resize_image(image_mlv,512,512);}
else if(l>512){
MLV_resize_image(image_mlv,512,h);
l=512;}
else if(h>512){
MLV_resize_image(image_mlv,l,512);
h=512;}
///remplire l'image
for(i=0;i<n;i++){
for(j=0;j<n;j++){
if(i>=h || j>=l)
image[i][j]=255;
else
MLV_get_pixel_on_image(image_mlv, j, i, &image[i][j], NULL, NULL, NULL);
}
}
create_tree(n);
// stocké l'image dans le fichier nfarbre
TreeInFile(nfarbre);
//
break;
case 2:
if(nfarbre!=NULL)
{
pf=fopen(nfarbre,"r");
///
quadTree * na=NULL;//noeud actuel
do
{
fread(na,sizeof(quadTree),1,pf);
if( na!=NULL)
{
if(na->couleur!=NULL)
{
printf("point en haut a droite de la sous image :");
printf("x=%d y=%d \n",na->point[0],na->point[1]);
printf("taille de l'image: %d\n",na->n);
printf("la couleur : %d\n",*(na->couleur));
printf("------------\n");
}
}
}while(na!=NULL);
fclose(pf);
///
}
break;
case 3:
///décopage en 16
if(racine!=NULL){
MLV_Image *sous_image_mlv[16];
int n4=512/4;
int a=racine->point[0], b=racine->point[1];
MLV_clear_window(MLV_COLOR_BLACK);
for(i=0;i<4;i++)
for(j=0;j<4;j++){
MLV_draw_partial_image_on_image(image_mlv,a+i,b+j,n4,n4,sous_image_mlv[i*4+j],0,0);
MLV_draw_image(sous_image_mlv[i*4+j], (i*n4)+3 ,j*n4+3 );
}
}
break;
case 4:
///décopage en 16
if(racine!=NULL){
MLV_Image *sous_image_64[64];
int n8=512/8;
int a=racine->point[0], b=racine->point[1];
MLV_clear_window(MLV_COLOR_BLACK);
for(i=0;i<8;i++)
for(j=0;j<8;j++){
MLV_draw_partial_image_on_image(image_mlv,a+i,b+j,n8,n8,sous_image_64[i*8+j],0,0);
MLV_draw_image(sous_image_64[i*8+j], (i*n8)+3 ,j*n8+3 );
}
}
break;
}
}
while(choix!=5);
//remplissage de la matrice image
//
printf("Hello world!\n");
return 0;
} |
Partager