Generate PPM file

Asked

Viewed 447 times

2

Hello, I have a code to generate a color matrix and a problem to make a sequence, but unfortunately I can not understand well the logic behind it.

Enunciation:
Create a color palette where each pixel is colored by the value of x and y. The program output should be a palette very close to the palette described below:

padrão de cores a ser utilizado

The code would be as follows:

int main(void)
{    
    int i, j;
    const int dimx = 800, dimy = 600;

    FILE *fp = fopen("first.ppm", "wb"); 
    fprintf(fp, "P6\n%d %d\n255\n", dimx, dimy);

    for (j = 0; j < dimy; ++j)
    {
        for (i = 0; i < dimx; ++i)
        {
            static unsigned char color[3];
            color[0] = i+2*j % 256;  /* red */
            color[1] = i-j % 256;  /* green */
            color[2] = (i+j) % 256;  /* blue */
            fwrite(color, 1, 3, fp);//Escreve no arquivo a cor
        }
    }
    fclose(fp);//Salva o arquivo
    return EXIT_SUCCESS;
}
  • the Code below, it came out in a bad format

  • I’ve edited your question, but I suggest you go back to edit it and try to describe what problem you are having (mistakes, expected result), so that the community can help you! Oh, and do the tour to get to know the site better! :)

1 answer

2

Based on its original program, I wrote an experimental program capable of generating images in the format .ppm containing color gradients.

Note that the functions red(), green(), blue(), gray() and original() are responsible for converting a coordinatedX,Y image in a RGB color.

Maybe, in this way, you understand quite practically the logic behind the original program.

Follow the program and its exits:

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


#define DIMX         (500)
#define DIMY         (400)


void red( int x, int y, char color[] )
{
    color[0] = x % 256; /* red */
    color[1] = 0;       /* green */
    color[2] = 0;       /* blue */
}


void green( int x, int y, char color[] )
{
    color[0] = 0;       /* red */
    color[1] = x % 256; /* green */
    color[2] = 0;       /* blue */
}


void blue( int x, int y, char color[] )
{
    color[0] = 0;       /* red */
    color[1] = 0;       /* green */
    color[2] = x % 256; /* blue */
}


void gray( int x, int y, char color[] )
{
    color[0] = x % 256; /* red */
    color[1] = x % 256; /* green */
    color[2] = x % 256; /* blue */
}


void original( int x, int y, char color[] )
{
    color[0] = x + 2 * y % 256;  /* red */
    color[1] = x - y % 256;      /* green */
    color[2] = (x + y) % 256;    /* blue */
}


int main( int argc, char ** argv )
{
    int x = 0;
    int y = 0;
    FILE * fp = NULL;
    char color[3] = { 0, 0, 0 }; /* r, g, b */
    void (*getcolor)( int, int, char[] );

    /* Verifica argumentos */
    if( argc != 3 )
    {
        fprintf( stderr, "Erro de sintaxe: %s [-original|-gray|-red|-green-|-blue] ARQUIVO_SAIDA\n", argv[0] );
        return EXIT_FAILURE;
    }

    /* Tipo de gradiente */
    if( !strcmp(argv[1],"-gray") )
        getcolor = gray;
    else if( !strcmp(argv[1],"-red") )
        getcolor = red;
    else if( !strcmp(argv[1],"-green") )
        getcolor = green;
    else if( !strcmp(argv[1],"-blue") )
        getcolor = blue;
    else if( !strcmp(argv[1],"-original") )
        getcolor = original;
    else
        getcolor = original;

    /* Abre arquivo para gravacao */
    fp = fopen( argv[2], "wb" );

    /* Verifica se o arquivo foi aberto com sucesso */
    if(!fp)
    {
        fprintf( stderr, "Erro abrindo arquivo '%s' para gravacao: %s\n", argv[2], strerror(errno) );
        return EXIT_FAILURE;
    }

    /*  Grava Cabeçalho (Header) no arquivo PPM  */
    fprintf( fp, "P6\n" );
    fprintf( fp, "%d %d\n", DIMX, DIMY );
    fprintf( fp, "255\n" );

    /* Gera imagem */

    /* Para cada linha... */
    for ( y = 0; y < DIMY; ++y )
    {
        /* Para cada coluna... */
        for ( x = 0; x < DIMX; ++x )
        {
            /* calcula cor a partir da coordenadas */
            getcolor( x, y, color );

            /* Grava pixel RGB no arquivo */
            fwrite( color, sizeof(char), sizeof(color), fp );
        }
    }

    /* fecha arquivo */
    fclose(fp);

    /* Sucesso */
    return EXIT_SUCCESS;
}

/* fim-de-arquivo */

Standard Original: (posted in question)

$ ./ppmgen -original imagem.ppm

Padrão Original

Gradiente Azul:

$ ./ppmgen -blue imagem.ppm

Gradiente Azul

Gradiente Verde:

$ ./ppmgen -green imagem.ppm

Gradiente Verde

Gradiente Vermelha:

$ ./ppmgen -original imagem.ppm

Gradiente Vermelha

Gradient Grayscale:

$ ./ppmgen -original imagem.ppm

Gradiente Cinza

Browser other questions tagged

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