Changing array of integers for image filter

1

I'm doing 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 for 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;
        }
    }
}
    
asked by anonymous 11.11.2014 / 14:02

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 fixed in the code in the imagem array definition), and your image is changed accordingly with your "filter". The problem is that when you unhide the data from your image you undo the application of the filter by subtracting again the value 255 of the value of each component in the pixel.

I made a shorter example (and it does not read the stdin data) to make it easier to observe this problem ( you can execute the code in the 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

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

    
11.11.2014 / 15:31