#ifndef COLOR_H #define COLOR_H #include #include #include "rtweekend.hpp" /* Writes color components as a space-delimited string of numbers in the range [0,255] */ void write_color(FILE *fp, color c, uint32_t samples_per_pixel) { float scale = 1.0 / samples_per_pixel; // Divide the color by the number of samples float r = sqrt(c.x * scale); float g = sqrt(c.y * scale); float b = sqrt(c.z * scale); /* Write output */ fprintf(fp, "%d %d %d\n", (uint8_t) (255 * clamp(r, 0, 1)), (uint8_t) (255 * clamp(g, 0, 1)), (uint8_t) (255 * clamp(b, 0, 1))); } void write_image(color *image, uint64_t n, FILE *fp, uint32_t samples_per_pixel) { for (int64_t i = n-1; i >= 0; --i) { write_color(fp, image[i], samples_per_pixel); } } #endif