-
Notifications
You must be signed in to change notification settings - Fork 0
/
ppm.cpp
39 lines (32 loc) · 948 Bytes
/
ppm.cpp
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
#include "ppm.h"
#include <stdexcept>
void write_ppm(const char* filename, unsigned char* data, int width, int height)
{
FILE *outfile;
if ((outfile = fopen(filename, "w")) == NULL)
{
throw std::runtime_error("Error: The ppm file cannot be opened for writing.");
}
(void) fprintf(outfile, "P3\n%d %d\n255\n", width, height);
unsigned char color;
for (size_t j = 0, idx = 0; j < height; ++j)
{
for (size_t i = 0; i < width; ++i)
{
for (size_t c = 0; c < 3; ++c, ++idx)
{
color = data[idx];
if (i == width - 1 && c == 2)
{
(void) fprintf(outfile, "%d", color);
}
else
{
(void) fprintf(outfile, "%d ", color);
}
}
}
(void) fprintf(outfile, "\n");
}
(void) fclose(outfile);
}