-
Notifications
You must be signed in to change notification settings - Fork 0
/
PPMImageLoader.h
77 lines (56 loc) · 1.76 KB
/
PPMImageLoader.h
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
70
71
72
73
74
75
76
77
#ifndef __PPM_IMAGELOADER_H_
#define __PPM_IMAGELOADER_H_
#include "ImageLoader.h"
#include <stdexcept>
#include <iostream>
#include <fstream>
#include <sstream>
/**
* @brief This class is used to load an image in the ASCII PPM format
*
*/
class PPMImageLoader: public ImageLoader {
public:
PPMImageLoader() {
}
void load(std::string filename) {
std::ifstream fp;
std::string str;
int i,j;
int r,g,b;
int factor;
fp.open(filename.c_str());
if (!fp.is_open())
throw std::invalid_argument("File not found!");
std::cout << "Image file opened" << std::endl;
//read line by line and ignore comments
std::stringstream input;
std::string line;
while (std::getline(fp,line)) {
if ((line.length()>0) && (line[0]!='#')) {
input << line << std::endl;
}
}
fp.close();
//read in the word P3
input >> str;
//now read in the width and height
input >> width >> height;
//read in the factor
input >> factor;
image = new GLubyte[3*width*height];
for (i=0;i<height;i++)
{
for (j=0;j<width;j++)
{
input >> r >> g >> b;
image[3*((height-1-i)*width+j)] = r;
image[3*((height-1-i)*width+j)+1] = g;
image[3*((height-1-i)*width+j)+2] = b;
// << "r=" << (int)r << ", g=" << (int)g << ",b=" << (int)b << endl;
}
}
//fp.close();
}
};
#endif