| 12
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
 100
 101
 102
 103
 104
 105
 106
 107
 108
 109
 110
 111
 112
 113
 114
 115
 116
 117
 118
 119
 120
 121
 122
 123
 
 | #include <stdio.h>
#include <string.h>
#include <stdlib.h>
 
#define NB_COM 1
 
typedef struct {unsigned int R, G, B;} RGB;
typedef struct {
	RGB *pixel;
	unsigned int max;
	unsigned int Nx;
	unsigned int Ny;
	char *com[NB_COM];
	int isB;
} image;
 
 
 
void croix(const unsigned int X, const unsigned int Y, image* img) {
	unsigned int i,j;
 
	unsigned int size_x=img->Nx;
 
	i=X;
	for(j=Y-5;j<=Y+5;j++) {
		img->pixel[size_x*j+i].R = 240;
		img->pixel[size_x*j+i].G = 0;
		img->pixel[size_x*j+i].B = 0;
	}
 
	j=Y;//NOC
	for(i=X-5;i<=X+5;i++) {
		img->pixel[size_x*j+i].R = 240;
		img->pixel[size_x*j+i].G = 0;
		img->pixel[size_x*j+i].B = 0;
	}
}
 
static int add_pixel(image *image, unsigned int pos, unsigned int value)
{
	unsigned int real_pos = pos/3;
	if (real_pos >= image->Nx*image->Ny)
		return 1;
	switch (pos%3) {
		case 0: image->pixel[real_pos].R = value; break;
		case 1: image->pixel[real_pos].G = value; break;
		case 2: image->pixel[real_pos].B = value; break;
	}
	return 0;
}
 
static int ppm(image *img)
{
	char buf[256];
	int line_nb = 0;
	int done = 0;
	while ((fgets(buf, sizeof(buf), stdin))) {
		if (line_nb < NB_COM) {
			if (!(img->com[line_nb] = strdup(buf)))
				return 1;
		} if (line_nb == NB_COM) {
			if (sscanf(buf, "%d %d", &img->Nx, &img->Ny) != 2)
				return 2;
			if (!(img->pixel = malloc(sizeof(*img->pixel)*img->Nx*img->Ny)))
				return 3;
		} else if (line_nb == NB_COM+1) {
			if (sscanf(buf, "%u", &img->max) != 1)
				return 4;
		} else if (line_nb > NB_COM+1) {
			unsigned int p;
			if (sscanf(buf, "%u", &p) != 1)
				return 5;
			if (add_pixel(img, line_nb-(NB_COM+2), p))
				return 6;
			done++;
		}
		++line_nb;
	}
	if (done != img->Nx*img->Ny*3) {
		fprintf(stderr, "done=%d, Nx=%d, Ny=%d\n", done, img->Nx, img->Ny);
		return 5;
	}
	croix(30, 30, img);
	return 0;
}
 
static int dump_ppm(image *img)
{
	int i;
	for (i = 0; i < NB_COM; ++i) {
		printf("%s", img->com[i]);
	}
	printf("%u %u\n", img->Nx, img->Ny);
	printf("%u\n", img->max);
	for (i = 0; i < img->Nx*img->Ny; ++i) {
		printf("%u\n", img->pixel[i].R);
		printf("%u\n", img->pixel[i].G);
		printf("%u\n", img->pixel[i].B);
	}
	return 0;
}
 
static void free_ppm(image *img)
{
	int i;
	free(img->pixel);
	for (i = 0; i < NB_COM; ++i)
		free(img->com[i]);
	return ;
}
 
 
int main(void)
{
	image img;
	int rc;
	if ((rc = ppm(&img)))
		return rc;
	if ((rc = dump_ppm(&img)))
		return rc;
	free_ppm(&img);
	return rc;
} | 
Partager