MatLabWriter.hpp 2.38 KB
Newer Older
incardon's avatar
incardon committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
#ifndef MATLABWRITER_HPP
#define MATLABWRITER_HPP

#include <iostream>
#include "tiffio.h"
#include "map_grid.hpp"
#include <string>

/*! \brief This class is able to save grid into tiff files
 *
 * This class is able to save grid into tiff files
 *
 */

template<unsigned int dim, typename T>
incardon's avatar
incardon committed
16
class MatLabWriter
incardon's avatar
incardon committed
17 18 19 20 21 22 23
{
	/*! \brief Save grid into tiff files
	 *
	 * Save grid into tiff files
	 *
	 */

incardon's avatar
incardon committed
24
	template<typename grid, typename Mem> int operator<<(grid data, std::string file)
incardon's avatar
incardon committed
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 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111
	{
		// Grid can have several properties we can save only scalar fields
		// for each properties save one scalar fields

		// Tiff files can be saved up to 5D

		if (dim > 5)
		{
			std::cerr << "Error Tiff writer support until 5D images" << "\n";
		}

		// Open the tiff image

		uint32 width;
		uint32 height;
		TIFF *tif = TIFFOpen(file.c_str(),"w");

		// if the file is open

		if(tif)
		{
			// set width and height for 2D

			width = data.getGrid().size(0);
			height = data.getGrid().size(1);

			TIFFSetField(tif,TIFFTAG_IMAGEWIDTH, &width);
			TIFFSetField(tif,TIFFTAG_IMAGELENGTH, &height);

			// Create the tiff line, in case the grid is CPU, we have only
			// one property and is a scalar, we can directly copy the line

			typename boost::fusion::result_of::at<T::type,0>::type first_element_type;

			if (typeid(grid).name() == "grid_cpu" && T::num_prop == 1 && boost::is_array<first_element_type>::type::value == true)
			{
				// Get the grid key iterator

				grid_key_dx_iterator<dim> key = data.getIterator();

				// write all lines

				for(int i = 0; i < height ; i++)
				{
					// select the correct lines

					key.set(1,i);

					// we have only one scalar properties, get the buffer pointer
					void * buf = &data.template get<0>(key);

					TIFFWriteScanline(tif,buf,i, 0);
				}
			}
			else
			{
				// we have to create the a scan line for each properties and index array
				// each property and index array became a channel

				// Get the grid key iterator

				grid_key_dx_iterator<dim> key = data.getIterator();

				// count how many properties and how many indexes we have

				const int n_prp = total_prop<T>;

				// write all lines

				for(int i = 0; i < height ; i++)
				{
					// select the correct lines

					key.set(1,i);

					// we have only one scalar properties, get the buffer pointer
					void * buf = &data.template get<0>(key);

					TIFFWriteScanline(tif,buf,i, 0);
				}
			}
		}
	}

};

#endif