EnviRead.h
1.55 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
63
64
65
#ifndef ENVIREAD_H
#define ENVIREAD_H
#include <string>
#include <iostream>
#include <fstream>
#include <ctime>
#include <sstream>
#include <stdio.h>
#include "EnviHeader.h"
#include "EnviFid.h"
#include "EnviOpen.h"
using namespace std;
static int EnviRead(float* ptr, int nPixels, EnviFidClass &fid)
{
//number of values to read from the file
int totalPixels = fid.header.sX * fid.header.sY;
//ERROR TEST: make sure that fid.file is a valid file pointer
if (fid.file == NULL)
{
cout<<"Binary file isn't open."<<endl;
return 0;
}
//---------MASK implementation
int i=0;
unsigned int sizeRead;
//iterate through every pixel in the image
//while we have not encountered the end of the file AND we have not read nPixels pixels
while(fid.filePos < totalPixels && i < nPixels)
{
//if there is no mask OR the current pixel is valid, read it
if(fid.mask == NULL || fid.mask[fid.filePos] == 1)
{
//read the pixel directly from disk
sizeRead = fread(&ptr[fid.header.nF * i], sizeof(float), fid.header.nF, fid.file);
//catch an error if one occurs
if (sizeRead != fid.header.nF)
{
cout<<"Error during read # "<<i<<endl;
return 0;
}
i++;
}
//otherwise, skip it
else
fseek(fid.file, sizeof(float) * fid.header.nF, SEEK_CUR);
//increment the file position (used to index into the mask)
fid.filePos++;
}
return i;
}
#endif