-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathReadImageHeader.cpp
More file actions
62 lines (47 loc) · 1.23 KB
/
ReadImageHeader.cpp
File metadata and controls
62 lines (47 loc) · 1.23 KB
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
#include <iostream>
#include <fstream>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
#include "image.h"
void readImageHeader(char fname[], int& N, int& M, int& Q, bool& type, bool& fileRead)
{
int i, j;
unsigned char *charImage;
char header [100], *ptr;
ifstream ifp;
ifp.open(fname, ios::in | ios::binary);
if (!ifp) {
cout << "Can't read image: " << fname << endl;
//exit(1); <---- Gives the user a chance to re-enter their filename if this is commented out.
fileRead = false;
return;
}
// read header
type = false; // PGM
ifp.getline(header,100,'\n');
if ( (header[0] == 80) && /* 'P' */
(header[1]== 53) ) { /* '5' */
type = false;
fileRead = true;
}
else if ( (header[0] == 80) && /* 'P' */
(header[1] == 54) ) { /* '6' */
type = true;
fileRead = true;
}
else {
cout << "Image " << fname << " is not PGM or PPM" << endl;
// exit(1); <---- Gives the user a chance to re-enter their filename if this is commented out.
fileRead = false;
return;
}
ifp.getline(header,100,'\n');
while(header[0]=='#')
ifp.getline(header,100,'\n');
M=strtol(header,&ptr,0);
N=atoi(ptr);
ifp.getline(header,100,'\n');
Q=strtol(header,&ptr,0);
ifp.close();
}