Skip to content
Snippets Groups Projects
Commit e9ad00de authored by Og's avatar Og
Browse files

test convol + try fix RGB

parent a499098f
No related branches found
No related tags found
No related merge requests found
...@@ -145,19 +145,20 @@ struct gfx_context_t *initWindow(int width,int height,char tab[]) ...@@ -145,19 +145,20 @@ struct gfx_context_t *initWindow(int width,int height,char tab[])
return ctxt; return ctxt;
} }
void dessineTab2D(struct gfx_context_t *context ,int **tab,int width,int height) void dessineTab2D(struct gfx_context_t *context ,uint32_t **tab,int width,int height,int lumMax)
{ {
int min = 0; int min = 0;
int max = 255; uint16_t px;
uint32_t intensity,color; uint32_t intensity,color;
for (int i = 0; i < width ; ++i) for (int i = 0; i < width ; ++i)
for (int j = 0; j < height; ++j) for (int j = 0; j < height; ++j)
{ {
intensity = (tab[i][j] - min)*255 / (max-min); px = tab[i][j];
color = MAKE_COLOR(intensity,intensity,intensity); intensity = (uint32_t)(( px - min)*255) / (255-min); //lumMax
color = MAKE_COLOR(intensity,0,0);
gfx_putpixel(context, i, j, color); gfx_putpixel(context, i, j, color);
} }
} }
......
...@@ -41,7 +41,7 @@ extern SDL_Keycode gfx_keypressed(); ...@@ -41,7 +41,7 @@ extern SDL_Keycode gfx_keypressed();
void drawCarrer(struct gfx_context_t *context,int posX,int posY,int carrer); void drawCarrer(struct gfx_context_t *context,int posX,int posY,int carrer);
void drawGraph(struct gfx_context_t *context,int *tab,int size); void drawGraph(struct gfx_context_t *context,int *tab,int size);
struct gfx_context_t *initWindow(int width,int height,char tab[]); struct gfx_context_t *initWindow(int width,int height,char tab[]);
void dessineTab2D(struct gfx_context_t *context ,int **tab,int width,int height); void dessineTab2D(struct gfx_context_t *context ,uint32_t **tab,int width,int height,int lumMax);
void waitSpacePress(); void waitSpacePress();
#endif #endif
...@@ -46,7 +46,7 @@ void Convolution_filtrage() ...@@ -46,7 +46,7 @@ void Convolution_filtrage()
use only : matrix_create & matrix_free use only : matrix_create & matrix_free
*/ */
char *filname = "part2.pgm"; char *filname = "part3_5.pgm";
pgm img; pgm img;
if (pgm_read_from_file(&img, filname) != ok) if (pgm_read_from_file(&img, filname) != ok)
{ {
...@@ -56,13 +56,32 @@ void Convolution_filtrage() ...@@ -56,13 +56,32 @@ void Convolution_filtrage()
PrintImagePGM(img, filname); PrintImagePGM(img, filname);
pgm_write_to_file(&img,"out.pgm"); pgm_write_to_file(&img,"out.pgm");
matrix kernel = matrix_creat(3,3);
int tab []= {1,1,1,1,1,1,1,1,1};
matrix_from_array(&kernel,3,3,tab);
matrix_print(kernel);
pgm res;
res.max = img.max;
res.pixels = matrix_creat(img.pixels.m,img.pixels.n);
convolve_matrix(&res.pixels,&img.pixels,&kernel,1/(double)9);
PrintImagePGM(res, filname);
pgm_write_to_file(&res,"convolve.pgm");
matrix_destroy(&img.pixels); matrix_destroy(&img.pixels);
//matrix_free(&img.pixels); matrix_destroy(&res.pixels);
matrix_destroy(&kernel);
/*
matrix m1 = matrix_creat(10,10); matrix m1 = matrix_creat(10,10);
matrix m2 = matrix_creat(3,3); matrix m2 = matrix_creat(3,3);
int tab []= {1,1,1,1,1,1,1,1,1}; int tab []= {0,0,0,0,1,0,0,0,0};
matrix_from_array(&m2,3,3,tab); matrix_from_array(&m2,3,3,tab);
...@@ -73,14 +92,16 @@ void Convolution_filtrage() ...@@ -73,14 +92,16 @@ void Convolution_filtrage()
matrix res = matrix_creat(10,10); matrix res = matrix_creat(10,10);
matrix_print(m1); matrix_print(m1);
//res = matrix_padding(*m1,1);
matrix_print(m2); matrix_print(m2);
convolve_matrix(&res,&m1,&m2,1/(double)9); // For create blur kernel
convolve_matrix(&res,&m1,&m2,1); // For create blur kernel
matrix_print(res); matrix_print(res);
matrix_destroy(&m1); matrix_destroy(&m1);
matrix_destroy(&m2); matrix_destroy(&m2);
matrix_destroy(&res); matrix_destroy(&res);
*/
} }
......
...@@ -109,19 +109,21 @@ matrix* convolve(matrix img, matrix kernel,matrix* new,const double factor) ...@@ -109,19 +109,21 @@ matrix* convolve(matrix img, matrix kernel,matrix* new,const double factor)
matrix* convolve_matrix(matrix* conv,matrix* orig, const matrix *const kernel, const double factor) matrix* convolve_matrix(matrix* conv,matrix* orig, const matrix *const kernel, const double factor)
{ {
matrix *tmp; matrix tmp;
//matrix_alloc(&tmp, kernel->col, kernel->row); //matrix_alloc(&tmp, kernel->col, kernel->row);
matrix_init(tmp,kernel->col,kernel->row);
tmp = matrix_creat(kernel->col,kernel->row);
//tmp = matrix_create(kernel->col,kernel->row,0); //tmp = matrix_create(kernel->col,kernel->row,0);
//matrix_alloc(conv,orig->col,orig->row); //matrix_alloc(conv,orig->col,orig->row);
// Browse PGM struct orig and create sub matrix but with default value // Browse PGM struct orig and create sub matrix but with default value
uint32_t res = 0;
for (int i = 0; i < orig->col; i += 1) for (int i = 0; i < orig->col; i += 1)
{ {
for (int j = 0; j < orig->row; j += 1) for (int j = 0; j < orig->row; j += 1)
{ {
int32_t res = 0; res = 0;
matrix_reset(tmp); matrix_reset(&tmp);
for (int k = 0, base_i = i - (int)kernel->col / 2; k < kernel->col; k += 1, base_i += 1) for (int k = 0, base_i = i - (int)kernel->col / 2; k < kernel->col; k += 1, base_i += 1)
{ {
...@@ -129,39 +131,46 @@ matrix* convolve_matrix(matrix* conv,matrix* orig, const matrix *const kernel, c ...@@ -129,39 +131,46 @@ matrix* convolve_matrix(matrix* conv,matrix* orig, const matrix *const kernel, c
{ {
if (base_i < 0 || base_j < 0 || base_i > orig->col - 1 || base_j > orig->row - 1) if (base_i < 0 || base_j < 0 || base_i > orig->col - 1 || base_j > orig->row - 1)
{ {
tmp->data[k][l] = 0; // Default value for out of bounds tmp.data[k][l] = 0; // Default value for out of bounds
} }
else else
{ {
tmp->data[k][l] = orig->data[base_i][base_j]; tmp.data[k][l] = orig->data[base_i][base_j];
} }
} }
} }
uint32_t tm, ker;
// Use matrix tmp with sub matrix // Use matrix tmp with sub matrix
for (int k = 0; k < tmp->col; k += 1) for (int k = 0; k < tmp.col; k += 1)
{ {
for (int l = 0; l < tmp->row; l += 1) for (int l = 0; l < tmp.row; l += 1)
{ {
res += kernel->data[k][l] * tmp->data[k][l]; tm = tmp.data[k][l];
ker = kernel->data[k][l];
res += tm * ker;
} }
} }
printf("Res(before) : %d / %lf\n",res,factor); //printf("Res(before) : %d / %lf\n",res,factor);
res *= factor; // Divide by factor of kernel res = (double)res * factor; // Divide by factor of kernel
printf("Res(after) : %d\n",res); //printf("Res(after) : %d\n",res);
if(res > 65535)
{
res = 65535;
}
if (res < 0) if (res < 0)
res = 4; {
else if (res > 256 - 1) res = 0;
res = 256 - 1; }
conv->data[i][j] = res; conv->data[i][j] = res;
} }
} }
//free(&tmp.data); //free(&tmp.data);
//matrix_destroy(&tmp); // PROBLEME DE FREEE //matrix_destroy(&tmp); // PROBLEME DE FREEE
matrix_destroy(tmp); matrix_destroy(&tmp);
return NULL; return NULL;
} }
......
File added
File added
...@@ -488,7 +488,7 @@ void PrintImagePGM(pgm img, char *filname) ...@@ -488,7 +488,7 @@ void PrintImagePGM(pgm img, char *filname)
{ {
printf("Image sélectionnée '%s' de taille %dx%d lumMax:%d\n", filname, img.pixels.n, img.pixels.m, img.max); printf("Image sélectionnée '%s' de taille %dx%d lumMax:%d\n", filname, img.pixels.n, img.pixels.m, img.max);
struct gfx_context_t *ImagePGM = initWindow(img.pixels.n, img.pixels.m, filname); struct gfx_context_t *ImagePGM = initWindow(img.pixels.n, img.pixels.m, filname);
dessineTab2D(ImagePGM, img.pixels.pM, img.pixels.n, img.pixels.m); dessineTab2D(ImagePGM, img.pixels.pM, img.pixels.n, img.pixels.m,img.max); //
gfx_present(ImagePGM); gfx_present(ImagePGM);
waitSpacePress(); waitSpacePress();
gfx_destroy(ImagePGM); gfx_destroy(ImagePGM);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment