EnviClose.h 1.02 KB
#ifndef ENVICLOSE_H
#define ENVICLOSE_H


#include <fstream>
#include <stdio.h>
#include "EnviHeader.h"
#include "EnviFid.h"
#include "EnviOpen.h"

// this is to close all the opened files and to create a header file for the written files

static void EnviClose(EnviFidClass &fid, int dimX=0)
{

    float* zeroPtr = (float*) malloc(sizeof(float) * fid.header.nF);
    memset(zeroPtr, 0, sizeof(float) * fid.header.nF);

	if(fid.mode=="r")
	{
		fclose(fid.file);
	}

	else
	{
        int totalPixels = fid.header.sX;
        int dimY = totalPixels/dimX;

        if(totalPixels % dimX > 0)
        {
            dimY++;

            int padding = dimY*dimX-totalPixels;

            for(int p=0; p < padding; p++)
                fwrite(zeroPtr, sizeof(float), fid.header.nF, fid.file);
        }

        //write the calculated values to the header object
        fid.header.sX = dimX;
        fid.header.sY = dimY;


        //write the header file to disk
        //TODO


        //close the binary file
		fclose(fid.file);

	}




}
#endif