#include "render/frame_buffer.h" #include namespace kumo { uint64_t hash_pixels(const FrameBuffer& framebuffer) { uint64_t hash = 0xcbf29ce484222325ull; // FNV-1a offset basis // Feeds the four bytes of a value, least significant first, whatever the // byte order of this machine is. auto mix = [&hash](uint32_t value) { for (int shift = 0; shift < 32; shift += 8) { hash ^= (value >> shift) & 0xFF; hash *= 0x100000001b3ull; // FNV-1a prime; unsigned, so wraps defined } }; mix(static_cast(framebuffer.width)); mix(static_cast(framebuffer.height)); for (Color pixel : framebuffer.pixels) mix(pixel); return hash; } bool write_tga(const FrameBuffer& framebuffer, const char* path) { const int width = framebuffer.width; const int height = framebuffer.height; if (width < 1 || height < 1 || width > 65535 || height > 65535) return false; std::FILE* file = std::fopen(path, "wb"); if (file == nullptr) return false; // The 18 byte header; every field not set below stays zero. uint8_t header[18] = {}; header[2] = 2; // image type: uncompressed true color header[12] = static_cast(width & 0xFF); // width, little endian header[13] = static_cast(width >> 8); header[14] = static_cast(height & 0xFF); // height, little endian header[15] = static_cast(height >> 8); header[16] = 32; // bits per pixel header[17] = 0x28; // 8 alpha bits, and the first row is the top one bool ok = std::fwrite(header, 1, sizeof(header), file) == sizeof(header); // Bytes are written as blue, green, red, alpha, which is what the format // wants, one by one so the file does not depend on this machine's byte order. std::vector bytes; bytes.reserve(framebuffer.pixels.size() * 4); for (Color pixel : framebuffer.pixels) { bytes.push_back(static_cast(pixel & 0xFF)); bytes.push_back(static_cast((pixel >> 8) & 0xFF)); bytes.push_back(static_cast((pixel >> 16) & 0xFF)); bytes.push_back(static_cast((pixel >> 24) & 0xFF)); } ok = ok && std::fwrite(bytes.data(), 1, bytes.size(), file) == bytes.size(); // Close in every case, and count a failed close (a failed flush) as an error. ok = (std::fclose(file) == 0) && ok; return ok; } } // namespace kumo