-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreadwrite.h
56 lines (42 loc) · 1.25 KB
/
readwrite.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
#ifndef READWRITE_H
#define READWRITE_H
#include <bits/stdc++.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
using namespace std;
vector<uint8_t> readpgm(int &height, int &width, string &filename)
{
stringstream ss;
ifstream infile(filename);
string inputLine = "";
getline(infile, inputLine); // read the first line : P5
if (inputLine.compare("P5") != 0)
cerr << "Version error" << endl;
ss << infile.rdbuf(); //read the third line : width and height
ss >> width >> height;
int max_val; //maximum intensity value : 255
ss >> max_val;
unsigned char pixel;
vector<uint8_t> PixelArray;
for (int x = 0; x < width * height; x++)
{
ss.read((char *)&pixel, 1);
PixelArray.push_back(pixel);
}
infile.close();
return PixelArray;
}
void writepgm(vector<uint8_t> newimg, int width, int height,string writtenfile)
{
ofstream out(writtenfile); //Creating newimg
out << "P5" << endl<< width << " " << height << endl<< 255 << endl; // headers
for (int i = 0; i < newimg.size(); i++)
{
char ch = (char)newimg[i];
out.put(ch);
}
out.close();
}
#endif // READWRITE_H