Skip to content
Snippets Groups Projects
Commit 4ebf7f62 authored by thib's avatar thib
Browse files

main rdy

parent c60b1063
No related branches found
No related tags found
No related merge requests found
File added
......@@ -10,7 +10,6 @@
// matrix pixels;
// } pgm;
pgm_error pmg_read_from_file(pgm *p, char *filename) {
if (NULL == filename || NULL == p) {
return failure;
......@@ -196,7 +195,8 @@ if(NULL== orig || NULL==photomaton){
pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0,
int y1) {
if(ok!=matrix_extract_submatrix(&crop->pixels, orig->pixels, y0, y1, x0, x1)){
if (ok !=
matrix_extract_submatrix(&crop->pixels, orig->pixels, y0, y1, x0, x1)) {
return failure;
}
......@@ -204,62 +204,72 @@ pgm_error pmg_crop(pgm *crop, const pgm *const orig, int x0, int x1, int y0,
return success;
}
pgm_error pmg_conv(pgm *conv, const pgm *const orig,const matrix *const kernel) {
if(NULL== orig || NULL==conv || NULL==kernel){
return failure;
}
int get_divider(const matrix *const mat) {
int divider = 0;
// get divider
for (int y = 0; y < kernel->m; y++) { // ligne
for (int x = 0; x < kernel->n; x++) {
divider += kernel->data[y][x];
for (int y = 0; y < mat->m; y++) {
for (int x = 0; x < mat->n; x++) {
divider += mat->data[y][x];
}
}
if (divider < 1) {
divider = 1;
}
return divider;
}
pgm_error pmg_conv(pgm *conv, const pgm *const orig, const matrix *const kernel) {
if (NULL == orig || NULL == conv || NULL == kernel) {
return failure;
}
matrix_alloc(&(conv->pixels), orig->pixels.m, orig->pixels.n);
conv->max = orig->max;
for (int i = 0; i < orig->pixels.m; i++) { // ligne
for (int j = 0; j < orig->pixels.n; j++) { // col
double sum = 0.0;
int divider = get_divider(kernel);
for (int i = 0; i < orig->pixels.m; i++) {
for (int j = 0; j < orig->pixels.n; j++) {
double pixel = 0.0;
for (int y = 0; y < kernel->m; y++) { // ligne
for (int x = 0; x < kernel->n; x++) { // col
for (int y = 0; y < kernel->m; y++) {
for (int x = 0; x < kernel->n; x++) {
int yi = i - kernel->m / 2 + y;
int xj = j - kernel->n / 2 + x;
if (xj >= 0 && yi >= 0 && xj < orig->pixels.m &&
yi < orig->pixels.n) {
sum += orig->pixels.data[yi][xj] * kernel->data[y][x];
//if pixel is inside matrix
if (xj >= 0 && yi >= 0 && xj < orig->pixels.m && yi < orig->pixels.n) {
pixel += orig->pixels.data[yi][xj] * kernel->data[y][x];
}
}
}
sum /= divider;
// printf("%f ",sum);
if (sum < 0) {
sum = 0;
} else if (sum > conv->max) {
sum = conv->max;
pixel /= divider;
//check pixel's val ->
if (pixel < 0) {
pixel = 0;
} else if (pixel > conv->max) {
pixel = conv->max;
}
// printf("%d\n",(int)sum);
conv->pixels.data[i][j] = (int)sum;
//set pixel
conv->pixels.data[i][j] = (int)pixel;
}
}
return success;
}
pgm_error pgm_display(pgm p) {
struct gfx_context_t *context = gfx_create("SDL Display", p.pixels.m, p.pixels.n);
struct gfx_context_t *context =
gfx_create("SDL Display", p.pixels.m, p.pixels.n);
if (!context) {
fprintf(stderr, "Graphics initialization failed!\n");
return EXIT_FAILURE;
......
......@@ -12,8 +12,29 @@ int main() {
pgm out;
matrix kernel;
pmg_read_from_file(&in, "mandrill.pgm");
pmg_read_from_file(&in, "chien-pasteque.pgm");
// https://en.wikipedia.org/wiki/Kernel_(image_processing)
pmg_negative(&out, &in);
pmg_write_to_file(&out, "neg.pgm");
matrix_destroy(&out.pixels);
pmg_symmetry_hori(&out, &in);
pmg_write_to_file(&out, "symh.pgm");
matrix_destroy(&out.pixels);
pmg_symmetry_vert(&out, &in);
pmg_write_to_file(&out, "symv.pgm");
matrix_destroy(&out.pixels);
pmg_symmetry_cent(&out, &in);
pmg_write_to_file(&out, "symc.pgm");
matrix_destroy(&out.pixels);
pmg_photomaton(&out, &in);
pmg_write_to_file(&out, "photomat.pgm");
matrix_destroy(&out.pixels);
pmg_crop(&out, &in, 50, 200, 50, 200);
pmg_write_to_file(&out, "crop.pgm");
matrix_destroy(&out.pixels);
int sharp[] = {0, -1, 0, -1, 5, -1, 0, -1, 0};
int blur[] = {1, 1, 1, 1, 1, 1, 1, 1, 1};
int edge1[] = {1, 0, -1, 0, 0, 0, -1, 0, 1};
......@@ -21,20 +42,43 @@ int main() {
int edge3[] = {-1, -1, -1, -1, 8, -1, -1, -1, -1};
int blur5x5[] = {1, 4, 6, 4, 1, 4, 16, 24, 16, 4, 6, 24, 36, 24, 6, 4, 16, 24, 16, 4, 1, 4, 6, 4, 1};
matrix_init_from_array(&kernel, 3, 3, sharp);
pmg_conv(&out, &in, &kernel);
// pgm_display(out);
pmg_write_to_file(&out, "sharp.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
//pmg_negative(&out, &in);
matrix_init_from_array(&kernel, 3, 3, blur);
pmg_conv(&out, &in, &kernel);
pmg_write_to_file(&out, "blur.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
pmg_write_to_file(&out, "sharp.pgm");
matrix_init_from_array(&kernel, 3, 3, edge1);
pmg_conv(&out, &in, &kernel);
pmg_write_to_file(&out, "edge1.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
matrix_init_from_array(&kernel, 3, 3, edge2);
pmg_conv(&out, &in, &kernel);
pmg_write_to_file(&out, "edge2.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
//matrix_destroy(&tmp.pixels);
matrix_destroy(&in.pixels);
matrix_init_from_array(&kernel, 3, 3, edge3);
pmg_conv(&out, &in, &kernel);
pmg_write_to_file(&out, "edge3.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
matrix_init_from_array(&kernel, 5, 5, blur5x5);
pmg_conv(&out, &in, &kernel);
pmg_write_to_file(&out, "blur5x5.pgm");
matrix_destroy(&out.pixels);
matrix_destroy(&kernel);
matrix_destroy(&in.pixels);
return EXIT_SUCCESS;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment