我正在尝试通过将每个像素的红色分量乘以10来编辑图像,并保持其他组件(绿色和蓝色)不变。我不确定我应该如何逐个编辑像素。到目前为止我有这个。我认为我的ppm标题也有问题。有没有办法在不指定边界的情况下制作ppm标题,以使其符合您放入的图像?
#include <stdlib.h>
#include <stdio.h>
#define PICTURE_FILE 1
#define MIN_ARGS 2
#define h 768
#define w 1024
void solve(FILE *in_picture, FILE *out_picture)
{
printf("P3 1024 768 255\n");
double r, g, b, row, col;
int ret = fscanf(in_picture, "%lf %lf %lf ", &r, &g, &b);
while(ret != EOF)
{
for(row=1; row <= h; row++)
{
for(col=1; col <= w; col++)
{
fprintf(out_picture, "%lf %lf %lf ", r*10 , g, b);
}
ret = fscanf(in_picture, "%lf %lf %lf ", &r, &g, &b);
}
}
}
FILE *open_file(const char name[], const char mode[])
{
FILE *file = fopen(name, mode);
if(file == NULL)
{
perror(name);
exit(1);
}
return file;
}
void check_args(int argc, char *argv[])
{
if(argc < MIN_ARGS)
{
fprintf(stderr, "%s: not enough arguments \n", argv[0]);
exit(1);
}
}
int main(int argc, char *argv[])
{
check_args(argc, argv);
FILE *in_picture = open_file(argv[PICTURE_FILE], "r");
FILE *out_picture = open_file("hidden.ppm", "w");
solve(in_picture, out_picture);
fclose(in_picture);
fclose(out_picture);
}