From c94c3930af8c81568be50bdfaaac321b99e296d7 Mon Sep 17 00:00:00 2001 From: Julian T Date: Mon, 9 Mar 2020 18:34:24 +0100 Subject: Colors are floats now --- pgm.c | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) (limited to 'pgm.c') diff --git a/pgm.c b/pgm.c index efc50f7..654f4b7 100644 --- a/pgm.c +++ b/pgm.c @@ -3,20 +3,22 @@ #include #include +#define COLOR_MAX 255 + int pgm_write_header(FILE *fp, unsigned int w, unsigned int h) { - return fprintf(fp, "P3\n%d %d\n255\n", w, h); + return fprintf(fp, "P3\n%d %d\n%d\n", w, h, COLOR_MAX); } int pgm_write_pixel(FILE *fp, color_t *c) { - return fprintf(fp, "%d %d %d\n", c->r, c->g, c->b); + 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, uint8_t r, uint8_t g, uint8_t b) { if (!c) { - c = (color_t *) malloc(sizeof(c)); + c = (color_t *) malloc(sizeof(color_t)); } c->r = r; @@ -32,32 +34,32 @@ color_t *color_add(color_t *dest, color_t *a, color_t *b) dest = (color_t *) malloc(sizeof(dest)); } - unsigned int tmp = a->r + b->r; - dest->r = tmp > 255 ? 255 : tmp; + COORD_T tmp = a->r + b->r; + dest->r = tmp > 1 ? 1 : tmp; tmp = a->g + b->g; - dest->g = tmp > 255 ? 255 : tmp; + dest->g = tmp > 1 ? 1 : tmp; tmp = a->b + b->b; - dest->b = tmp > 255 ? 255 : tmp; + dest->b = tmp > 1 ? 1 : tmp; return dest; } -color_t *color_scale(color_t *dest, color_t *a, float b) +color_t *color_scale(color_t *dest, color_t *a, COORD_T b) { if (!dest) { dest = (color_t *) malloc(sizeof(dest)); } - unsigned int tmp = a->r * b; - dest->r = tmp > 255 ? 255 : tmp; + COORD_T tmp = a->r * b; + dest->r = tmp > 1 ? 1 : tmp; tmp = a->g * b; - dest->g = tmp > 255 ? 255 : tmp; + dest->g = tmp > 1 ? 1 : tmp; tmp = a->b * b; - dest->b = tmp > 255 ? 255 : tmp; + dest->b = tmp > 1 ? 1 : tmp; return dest; } -- cgit v1.2.3