6 #include <fmt/format.h> 
   14 void exportToPGM(
const char* output, 
const uint8_t* data, 
int width, 
int height, 
int maxVal)
 
   16     assert(width > 0 && height > 0);
 
   18         for (
int i = 0; i < width * height; ++i)
 
   20             if (maxVal < data[i]) maxVal = data[i];
 
   23     maxVal     = maxVal < 1 ? 1 : maxVal;
 
   24     int   bpp  = maxVal >= 256 ? 2 : 1;
 
   25     FILE* file = fopen(output, 
"wb");
 
   27         fmt::print(file, 
"P5 {} {} {}\n", width, height, maxVal);
 
   28         fwrite(data, static_cast<size_t>(bpp), static_cast<size_t>(width * height), file);
 
   33 void exportToPGM(
const char* output, ImageView<const uint8_t> image, 
int maxVal)
 
   36     assert(image.width == image.stride);
 
   37     exportToPGM(output, image.buffer, 
int(image.width), 
int(image.height), maxVal);
 
   39 void exportToPPM(
const char* output, 
const uint8_t* data, 
int width, 
int height,
 
   40                  const Palette& palette)
 
   42     assert(width > 0 && height > 0);
 
   43     FILE* file = fopen(output, 
"wb");
 
   45         fmt::print(file, 
"P6 {} {} 255\n", width, height);
 
   46         for (
size_t i = 0; i < static_cast<size_t>(width * height); ++i)
 
   48             const auto& colors = palette.colors;
 
   49             fputc(colors[data[i]].r, file);
 
   50             fputc(colors[data[i]].g, file);
 
   51             fputc(colors[data[i]].b, file);