EnviWrite.h
1.6 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
66
67
68
69
70
71
#ifndef ENVIWRITE_H
#define ENVIWRITE_H
#include <fstream>
#include <stdio.h>
#include "EnviHeader.h"
#include "EnviFid.h"
#include "EnviOpen.h"
// Take a memory pointer, and copy all of that data to disk.
static int EnviWrite(float* ptr, int npixels, EnviFidClass &fid)
{
if(fid.mode == "r") return 0;
//open a binary file for writing
if (fid.file == NULL)
{
cout<<"Binary file isn't open."<<endl;
return 1;
}
//allocate an array of zeros equal to the size of a single pixel
float* zeroPtr = (float*) malloc(sizeof(float) * fid.header.nF);
memset(zeroPtr, 0, sizeof(float) * fid.header.nF);
int j = 0;
//for each pixel send to the EnviRead function
for(int p = 0; p<npixels; p++)
{
//while the mask is zero
while(fid.mask[fid.filePos] == 0)
{
//increment filePos
fid.filePos++;
fid.header.sX++;
j++;
//write a sequence of zeros
size_t results= fwrite(zeroPtr, sizeof(float), fid.header.nF, fid.file);
//check for errors
if (results != fid.header.nF)
{
cout<<"Error during read # "<<j<<endl;
return 0;
}
}
//write a pixel
size_t results= fwrite(&ptr[fid.header.nF * p], sizeof(float), fid.header.nF, fid.file);
if (results != fid.header.nF)
{
cout<<"Error during read # "<<j<<endl;
return 0;
}
//increment filePos
fid.filePos++;
fid.header.sX++;
j++;
}
return j;
}
#endif