Changing integer array to image filter

Asked

Viewed 756 times

1

I’m making a program that applies filters to images .ppm, images without any encoding, the problem is that when I apply the filters, the original image is not changing, where is my error?

The same thing is happening to the other filters.

More about PPM images

  /* 
 * File:   main.c
 * Author: pmargreff
 *
 * Created on 11 de Novembro de 2014, 00:10
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/*
 * 
 */

typedef struct {
    int r;
    int g;
    int b;
} pixel;

pixel imagem[300][300];


void filtro_negativo(pixel im[300][300], int alt, int larg, int max);

int main(int argc, char** argv) {
    int alt, larg;
    char tipo[3];
    char coment[100];
    int max;
    int i, j;

    scanf("%s%d%d%d", tipo, &larg, &alt, &max);

    for (i = 0; i < alt; i++) {
        for (j = 0; j < larg; j++) {
            scanf("%d%d%d", &imagem[i][j].r, &imagem[i][j].g, &imagem[i][j].b);
        }
    }

    filtro_negativo(imagem, alt, larg, max);

    printf("%s\n", tipo);
    printf("%d %d\n", larg, alt);
    printf("%d\n", max);

    for (i = 0; i < alt; i++) {
        for (j = 0; j < larg; j++) {
            printf("%d %d %d\n", 255 - imagem[i][j].r, 255 - imagem[i][j].g, 255 - imagem[i][j].b);
        }
    }
    return (EXIT_SUCCESS);
}

void filtro_negativo(pixel im[300][300], int alt, int larg, int max) {
    int i, j;

    for (i = 0; i < alt; i++) {
        for (j = 0; j < larg; j++) {
            im[i][j].r = max - im[i][j].r;
            im[i][j].g = max - im[i][j].g;
            im[i][j].b = max - im[i][j].b;
        }
    }
}

1 answer

4


Your code works (although there are some potential problems, such as the width and height of the image being passed by parameter but being fixed in the code in the array definition imagem), and your image is changed according to your "filter". The problem is that when it comes to re-entering your image data you undo the filter application by subtracting again the 255 value of each component in the pixel.

I made a shorter example (and that does not read the data of stdin) to facilitate the observation of this problem (can execute the code in Ideone):

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

typedef struct {
    int r;
    int g;
    int b;
} pixel;

pixel imagem[2][2];

void filtro_negativo(pixel im[2][2], int alt, int larg, int max);

int main(void) {

    imagem[0][0].r = 1; imagem[0][0].g = 1; imagem[0][0].b = 1;
    imagem[0][1].r = 2; imagem[0][1].g = 2; imagem[0][1].b = 2;
    imagem[1][0].r = 3; imagem[1][0].g = 3; imagem[1][0].b = 3;
    imagem[1][1].r = 4; imagem[1][1].g = 4; imagem[1][1].b = 4;

    printf("Antes:\n");
    int i, j;
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d %d %d\n", imagem[i][j].r, imagem[i][j].g, imagem[i][j].b);
        }
    }   

    filtro_negativo(imagem, 2, 2, 4);

    printf("Depois:\n");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d %d %d\n", imagem[i][j].r, imagem[i][j].g, imagem[i][j].b);
        }
    }

    printf("Depois (exibindo errado):\n");
    for(i = 0; i < 2; i++) {
        for(j = 0; j < 2; j++) {
            printf("%d %d %d\n", 4 - imagem[i][j].r, 4 - imagem[i][j].g, 4 - imagem[i][j].b);
        }
    }       

    return (EXIT_SUCCESS);  
}

void filtro_negativo(pixel im[2][2], int alt, int larg, int max) {
    int i, j;

    for (i = 0; i < alt; i++) {
        for (j = 0; j < larg; j++) {
            im[i][j].r = max - im[i][j].r;
            im[i][j].g = max - im[i][j].g;
            im[i][j].b = max - im[i][j].b;
        }
    }
}

Resulting in:

Antes:
1 1 1
2 2 2
3 3 3
4 4 4
Depois:
3 3 3
2 2 2
1 1 1
0 0 0
Depois (exibindo errado):
1 1 1
2 2 2
3 3 3
4 4 4

P.S.: Note that in the example I used the value 4 as "maximum" instead of 255 (just to illustrate).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.