EnviRead.h 1.55 KB
#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