Permalink
Browse files

Programmation C - TP7

Bug fixed, everyhting works fine.
I love my grilfriend, everfing works fine.
My roommate is childish, still to be fixed.
  • Loading branch information...
Malphaet committed Mar 15, 2012
1 parent 7f6abc1 commit 122106b2b640c8e7f9171cbb2aa0f110fb849b90
@@ -27,33 +27,30 @@
#include "types_image.h"
/* ========= Defines ==========*/
-
+#define im(i,j) *(image->pixels+sub2ind((i),(j),image->width))
/* ========= Functions ========*/
-void effect_binarization(Image *image){
+void effect_binarization(Image *image,float threshold){
int i,j;
- float val;
+ if (threshold<-1||threshold>1) threshold=0.5;
for (i=0;i<image->width;i+=1)
- for (j=0;j<image->height;j+=1){
- val=*(image->pixels+sub2ind(i,j,image->width));
- val=val>0.5?1:0;
- }
+ for (j=0;j<image->height;j+=1)
+ im(i,j)=(im(i,j)>threshold)?1:0;
}
void effect_negative(Image *image){
int i,j;
- float val;
for (i=0;i<image->width;i+=1)
- for (j=0;j<image->height;j+=1){
- val=*(image->pixels+sub2ind(i,j,image->width));
- val=1-val;
- }
+ for (j=0;j<image->height;j+=1)
+ im(i,j)=1-im(i,j);
}
-void effect_noise(Image *image, int level){
+void effect_noise(Image *image, float percent){
int i,j;
+ int level=(int)(percent*100);
srand(time(NULL));
for (i=0;i<image->width;i+=1)
for (j=0;j<image->height;j+=1)
- if (rand()%level) *(image->pixels+sub2ind(i,j,image->width))=0;
+ if ((rand()%100)<level) im(i,j)=0;
}
+#undef im
@@ -25,8 +25,8 @@
/* ========= Typedef ==========*/
/* ======== Prototype =========*/
-void effect_binarization(Image *); /* */
-void effect_negative(Image *); /* */
-void effect_noise(Image *, int); /* */
+void effect_binarization(Image *,float); /* */
+void effect_negative(Image *); /* Tested */
+void effect_noise(Image *, float); /* Tested */
#endif /* __IMAGE_EFFECTS_H__ */
@@ -38,6 +38,7 @@ Image *pgm_read(char *nom){
fscanf(file,"%d ",&val);
*(image->pixels+sub2ind(i,j,width))=(float) val/max_intens;
}
+ fclose(file);
return image;
}
@@ -50,4 +51,22 @@ void pgm_write(char* nom,Image* image){
fprintf(file,"%d ",(int) (INTENSITY_MAX**(image->pixels+sub2ind(i,j,image->width))));
fprintf(file,"\n");
}
+ fclose(file);
+}
+
+char colors[]={5,' ','+','%','#','@'};
+char color(float value){
+ if (value==1) return *(colors+*colors);
+ return *(colors + (int) (value**colors)+1);
+}
+
+void print_image(Image* image,int maxw,int maxh){
+ int i,j;
+ if (maxh<0) maxh=image->height;
+ if (maxw<0) maxw=image->width;
+ for (i=0;i<maxw;i+=1){
+ for (j=0;j<maxh;j+=1)
+ printf("%c ",color(*(image->pixels+sub2ind(i,j,image->width))));
+ printf("\n");
+ }
}
@@ -24,8 +24,10 @@
/* ========= Typedef ==========*/
/* ======== Prototype =========*/
-Image *pgm_read(char*); /* Seems working */
-void pgm_write(char*,Image*); /* */
+Image *pgm_read(char*); /* Tested */
+void pgm_write(char*,Image*); /* Tested */
+char color(float); /* Tested */
+void print_image(Image*,int,int); /* Tested */
#endif /* __IMAGE_IO_H__ */
@@ -33,21 +33,16 @@ Image *create_image(int width,int height){
float *table=malloc(sizeof(float)*width*height);
Image *image=malloc(sizeof(Image));
P_TEST(image); P_TEST(table);
- image->width=width;
- image->height=height;
- image->pixels=table;
+ image->width=width; image->height=height; image->pixels=table;
return image;
}
Image *copy_image(Image* image){
- Image *image_copy=malloc(sizeof(Image));
- float *table_copy=malloc(sizeof(image->width*image->height));
+ Image *image_copy;
int size=image->width*image->height,i;
- P_TEST(image_copy);P_TEST(table_copy);
- image_copy->width=image->width;
- image_copy->height=image->height;
- for (i=0;i<size;i+=1) table_copy[i]=image->pixels[i];
- image_copy->pixels=table_copy;
+ image_copy=create_image(image->width,image->height);
+ image_copy->width=image->width; image_copy->height=image->height;
+ for (i=0;i<size;i+=1) image_copy->pixels[i]=image->pixels[i];
return image_copy;
}
void free_image(Image *image){
@@ -60,16 +55,9 @@ int sub2ind(int i,int j,int width){
}
int ind2row(int k,int width){
- return width/k;
+ return k/width;
}
int ind2col(int k,int width){
return k%width;
}
-
-char colors[]={4,'#','%','+',' '};
-char color(float value){
- return *(colors + (int) value**colors);
-}
-
-
@@ -28,12 +28,11 @@
/* ========= Typedef ==========*/
/* ======== Prototype =========*/
-Image *create_image(int,int); /* */
-Image *copy_image(Image*); /* */
-void free_image(Image*); /* */
-int sub2ind(int,int,int); /* */
-int ind2row(int,int); /* */
-int ind2col(int,int); /* */
-char color(float); /* */
+Image *create_image(int,int); /* Tested */
+Image *copy_image(Image*); /* Tested */
+void free_image(Image*); /* Tested */
+int sub2ind(int,int,int); /* Tested */
+int ind2row(int,int); /* Seems working*/
+int ind2col(int,int); /* Seems working */
#endif /* __IMAGE_UTILS_H__ */
@@ -1,7 +1,7 @@
CC = gcc
CFLAGS = -W -Wall -ansi -pedantic
LDFLAGS =
-EXEC=executable
+EXEC=test
all: $(EXEC)
@@ -21,8 +21,9 @@
#include <stdio.h>
#include <stdlib.h>
#include "types_image.h"
-#include "image_utils.h"
-#include "image_io.h"
+#include "image_utils.h" /* Tested */
+#include "image_effects.h" /* Tested */
+#include "image_io.h" /* Tested */
/* ========= Defines ==========*/
@@ -31,7 +32,33 @@
int main(void){
Image *image=pgm_read("test.pgm");
- pgm_write("image.pgm",image);
+ Image *image2=copy_image(image);
+ Image *effect_image;
+ free_image(image);
+ pgm_write("image.pgm",image2);
+ /*printf("(%d,%d)=>(%d)=>(%d,%d)\n",4,5,sub2ind(4,5,10),ind2col(54,10),ind2row(54,10));*/
+ print_image(image2,10,10);
+
+ printf("Effect:binarization(0.5)\n");
+ effect_image=copy_image(image2);
+ effect_binarization(effect_image,0.5);
+ print_image(effect_image,-1,-1);
+ printf("Effect:binarization(0.7)\n");
+ effect_image=copy_image(image2);
+ effect_binarization(effect_image,0.7);
+ print_image(effect_image,-1,-1);
+ printf("Effect:negative\n");
+ effect_image=copy_image(image2);
+ effect_negative(effect_image);
+ print_image(effect_image,-1,-1);
+ printf("Effect:noise (50/100)\n");
+ effect_image=copy_image(image2);
+ effect_noise(effect_image,0.5);
+ print_image(effect_image,-1,-1);
+ printf("Effect:noise (20/100)\n");
+ effect_image=copy_image(image2);
+ effect_noise(effect_image,0.2);
+ print_image(effect_image,-1,-1);
return 0;
}

0 comments on commit 122106b

Please sign in to comment.