Generate PPM file

2

Hello, I have a code to generate an array of colors and a problem to make a sequence, but unfortunately I can not quite understand the logic behind it.

  

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

  

Thecodewouldbeasfollows:

intmain(void){inti,j;constintdimx=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;
}
    
asked by anonymous 22.06.2017 / 01:17

1 answer

2

Based on your original program, I wrote an experimental program capable of rendering images in .ppm format containing color gradients.

Note that the functions red() , green() , blue() , gray() , and original() are responsible for converting a X,Y coordinate of the image to an RGB color.

Maybe you'll understand the logic behind the original program quite handy.

Follow the program and its outputs:

#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 */

Original Pattern: (posted in the question)

$ ./ppmgen -original imagem.ppm

Bluegradient:

$./ppmgen-blueimagem.ppm

GreenGradient:

$./ppmgen-greenimagem.ppm

Redgradient:

$./ppmgen-originalimagem.ppm

GradientGrayscale:

$./ppmgen-originalimagem.ppm

    
22.06.2017 / 16:10