-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPM.cpp
executable file
·70 lines (53 loc) · 1.41 KB
/
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "PPM.hpp"
namespace PPM {
unsigned char *Read(const std::string &filename, int &width, int &height)
{
FILE *ifp;
char buffer[80];
int i;
int header[3]; // width, height, maxval
int tmp;
// Warning - Number of references to this class might cause problems when reading images
ifp = fopen(filename.c_str(), "rb");
if( !ifp ) {
throw std::string("Error opening file \"") + filename + std::string("\"");
}
i = 0;
fgets(buffer, 80, ifp);
if( strncmp(buffer, "P6", 2) ) {
fclose(ifp);
throw std::string("File is not in valid PPM format");
}
while( (i < 3) ) {
if( (tmp=fgetc(ifp)) == '#' ) {
fgets(buffer, 80, ifp); // read out comment
continue;
} else {
ungetc(tmp, ifp);
fscanf(ifp, "%d", &header[i++]);
}
}
fgets(buffer, 80, ifp); // read to newline
// Renew image
width = header[0];
height = header[1];
unsigned char *img = new unsigned char[width*height*3];
fread(img, 3, width * height, ifp);
fclose(ifp);
return img;
}
void Write(const std::string &filename, const unsigned char *img, const int width, const int height)
{
FILE *ifp;
ifp = fopen(filename.c_str(), "wb");
if( !ifp ) {
throw std::string("Error opening file \"") + filename + std::string("\"");
}
fprintf(ifp, "P6\n%d %d\n255\n", width, height);
fwrite(img, 3, width * height, ifp);
fclose(ifp);
}
} // namespace PPM