aboutsummaryrefslogtreecommitdiff
path: root/pgm.c
blob: 428048638ce042e0c65ea311aca7b0d0975f2a39 (plain)
1
2
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
#include "pgm.h"

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

#define COLOR_MAX 255

int pgm_write_header(FILE *fp, unsigned int w, unsigned int h)
{
	return fprintf(fp, "P3\n%d %d\n%d\n", w, h, COLOR_MAX);
}

/*
 * Diden't really look that nice. 
void color_clamp(color_t *c)
{
	// Fint the max of the 3 values
	COORD_T max = c->r > c->g ? c->r : c->g;
	max = max > c->b ? max : c->b;

	if (max <= 1.0) {
		return;
	}

	// Scale everything by this
	color_scale(c, c, (COORD_T) 1 / max);
}
*/

#define MAXOUT(v) ( if (v > 1) { v = 1; } )

void color_clamp(color_t *c)
{
	// Just max them out
	if (c->r > 1) { c->r = 1; }
	if (c->g > 1) { c->g = 1; }
	if (c->b > 1) { c->b = 1; }
}

int pgm_write_pixel(FILE *fp, color_t *c)
{
	return fprintf(fp, "%.0lf %.0lf %.0lf\n", c->r * COLOR_MAX, c->g * COLOR_MAX, c->b * COLOR_MAX);
}

color_t *color_set(color_t *c, COORD_T r, COORD_T g, COORD_T b)
{
	c->r = r;
	c->g = g;
	c->b = b;

	return c;
}

color_t *color_add(color_t *dest, color_t *a, color_t *b)
{
	dest->r = a->r + b->r;
	dest->g = a->g + b->g;
	dest->b = a->b + b->b;

	return dest;
}

color_t *color_scale(color_t *dest, color_t *a, COORD_T b)
{
	dest->r = a->r * b;
	dest->g = a->g * b;
	dest->b = a->b * b;

	return dest;
}

color_t *color_scale_vector(color_t *dest, color_t *a, vector_t *v)
{
	dest->r = a->r * v->x;
	dest->g = a->g * v->y;
	dest->b = a->b * v->z;

	return dest;
}