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
Gradiente Azul:
$ ./ppmgen -blue imagem.ppm
Gradiente Verde:
$ ./ppmgen -green imagem.ppm
Gradiente Vermelha:
$ ./ppmgen -original imagem.ppm
Gradient Grayscale:
$ ./ppmgen -original imagem.ppm
the Code below, it came out in a bad format
– hadevus
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! :)
– Daniel