diff --git a/grids/grid_data.h b/grids/grid_data.h index 449416e..82eef7c 100644 --- a/grids/grid_data.h +++ b/grids/grid_data.h @@ -4,6 +4,7 @@ #include #include #include +#include #include "../cuda/threads.h" #include "../cuda/error.h" @@ -14,7 +15,6 @@ namespace stim{ //This object describes a generic D-dimensional grid containing data of type T - // data can be stored on the GPU or CPU (and transferred between the two) // data can be loaded in the form of images // data can be saved in the form of binary files template @@ -22,9 +22,8 @@ class grid_data{ protected: - stim::vector R; //elements in each dimension + stim::vec R; //elements in each dimension T* ptr; //pointer to the data (on the GPU or CPU) - bool gpu; //true if the data is on the GPU //return the total number of values in the binary file unsigned long samples(){ @@ -42,19 +41,23 @@ public: //write data to disk void write(std::string filename){ + std::fstream file; + //open the file as binary for reading file.open(filename.c_str(), std::ios::out | std::ios::binary); //write file to disk - file.write((char *)p, samples() * sizeof(T)); + file.write((char *)ptr, samples() * sizeof(T)); } //load a binary file from disk // header size is in bytes - void read(std::string filename, stim::vector S, unsigned long header = 0){ + void read(std::string filename, stim::vec S, unsigned long header = 0){ R = S; //set the sample resolution + std::fstream file; + //open the file as binary for writing file.open(filename.c_str(), std::ios::in | std::ios::binary); @@ -62,7 +65,7 @@ public: file.seekg(header, std::ios::beg); //read the data - file.read((char *)p, samples() * sizeof(T)); + file.read((char *)ptr, samples() * sizeof(T)); } @@ -72,4 +75,4 @@ public: } -#endif \ No newline at end of file +#endif diff --git a/grids/image_stack.h b/grids/image_stack.h index 43929d6..498719d 100644 --- a/grids/image_stack.h +++ b/grids/image_stack.h @@ -1,16 +1,90 @@ #ifndef STIM_IMAGE_STACK_H #define STIM_IMAGE_STACK_H +#include "../parser/wildcards.h" +#include "../parser/filename.h" +#include "../grids/grid_data.h" +#include "../image/image.h" namespace stim{ //this creates a class that can be used to load 3D grid data from stacks of images +// The class uses a 4D grid_data object, where the first dimension is color template -class image_stack : public virtual grid_data{ +class image_stack : public virtual grid_data{ + + enum image_type {stimAuto, stimMono, stimRGB, stimRGBA}; + +protected: + using grid_data::R; + using grid_data::ptr; + using grid_data::samples; public: - void load(std::string wild_name){ + void load_images(std::string file_mask){ + + stim::filename file_path(file_mask); + + //if the file path is relative, update it with the current working directory + if(file_path.is_relative()){ + stim::filename wd = stim::filename::cwd(); + file_path = wd.get_relative(file_mask); + } + + //get the list of files + std::vector file_list = file_path.get_list(); + + //load the first image and set all of the image_stack properties + stim::image I(file_list[0].str()); + + //set the image resolution and number of channels + R[0] = I.channels(); + R[1] = I.width(); + R[2] = I.height(); + R[3] = file_list.size(); + + //allocate storage space + ptr = (T*)malloc(sizeof(T) * samples()); + + //load and copy each image into the grid + for(unsigned int i = 0; i I(file_list[i].str()); + + //retrieve the interlaced data from the image - store it in the grid + I.data_interleaved(&ptr[ i * R[0] * R[1] * R[2] ]); + } + } + + void save_images(std::string file_mask){ + + stim::filename file_path(file_mask); + + //if the file path is relative, update it with the current working directory + if(file_path.is_relative()){ + stim::filename wd = stim::filename::cwd(); + file_path = wd.get_relative(file_mask); + } + + //create a list of file names + std::vector file_list = stim::wildcards::increment(file_path.str(), 0, R[3]-1, 1); + + for(int i=0; i I; + + //retrieve the interlaced data from the image - store it in the grid + I.set_interleaved(&ptr[ i * R[0] * R[1] * R[2] ], R[1], R[2], R[0]); + + I.save(file_list[i]); + + + std::cout< result_path(begin, end); //create a new path to be returned - stim::filename result; + stim::filename result = *this; result.path = result_path; result.set_name(rel_path.back()); return result; } + bool is_relative(){ + return !absolute; + } + + bool is_absolute(){ + return absolute; + } + + -- libgit2 0.21.4