rtsImage.h 5.46 KB
#include "rtsFunction3D.h"
#include "cimg/cimg.h"
#include <string>
#include <sstream>
#include <iomanip>

using namespace std;
using namespace cimg_library;


///This file contains a series of functions useful for loading images into other structures.  These function use the CImg header file.

void rts_cimgLoadImage(rtsFunction3D<unsigned char>& result, const char* filename)	///<This function loads an image and store it in an Implicit3D object.
{
	CImg<unsigned char> image(filename), visu(500,400,1,3,0);

	result = rtsFunction3D<unsigned char>(image.width, image.height, 1);
	unsigned int x, y;
	for(x=0; x<image.width; x++)
		for(y=0; y<image.height; y++)
		{
			result(x, y, 0) = image(x, y);
		}
}

void rts_cimgSaveImage(rtsFunction3D<unsigned char>& result, int z, const char* filename)
{
	CImg<unsigned char> image(result.DimX(), result.DimY());
	unsigned int x, y;
	for(x=0; x<image.width; x++)
		for(y=0; y<image.height; y++)
			image(x, y) = result(x, y, z);

	image.save(filename);
}

void rts_cimgLoadImageSequence(rtsFunction3D<unsigned char>& result, const char* filename, unsigned int min, unsigned int max, unsigned int color = 0)
{
	/**
	This function loads a sequence of images into a 3D implicit function.  The filename is passed using the '?' wild
	card.  The wild cards are then replaced with the given min through max values in order to construct the names
	for the image files to be loaded.  The files are then loaded and placed in sequential order along the z-axis
	of the implicit function.
	**/
	string sequence_name = filename;							//turn the filename into a string
	unsigned int wild_card_begin = sequence_name.find_first_of('?');		//find the start and end indices of the wild card
	unsigned int wild_card_end = sequence_name.find_last_of('?');

	unsigned int max_number_length = wild_card_end - wild_card_begin + 1;	//find the maximum number of characters in the image number
	unsigned int max_number = (unsigned int)pow((double)10, (double)max_number_length) - 1;				//find the maximum possible number

	//make sure that the data is given correctly
	if(max < min) return;
	if(max > max_number) max = max_number;

	//create an array of file names
	int num_images = max - min + 1;					//compute the number of images
	bool implicit_created = false;
	for(int i=0; i<num_images; i++)
	{
		ostringstream format_stream;			//create a stream for number formatting
		format_stream.fill('0');				//the frame name will be right justified with proceeding zeroes
		format_stream<<setw(max_number_length);	//specify the length of the frame name (based on the wild cards)
		format_stream<<i+min;							//put the number in the stream, convert it to a string
		string frame_name = format_stream.str();

		string file_name = sequence_name;		//create the file name for the current frame
		file_name.replace(wild_card_begin, max_number_length, frame_name);

		CImg<unsigned char> image(file_name.c_str());	//load the frame
		if(implicit_created == false)			//create the implicit function for the first frame
		{
			result = rtsFunction3D<unsigned char>(image.width, image.height, num_images);
			implicit_created = true;
		}

		//place the frame in the implicit function
		unsigned int x, y;
		for(x=0; x<image.width; x++)
		for(y=0; y<image.height; y++)
			result(x, y, i) = image(x, y, color);
	}	
}

void rts_cimgSaveImageSequence(rtsFunction3D<unsigned char>& result, const char* filename)
{
	string sequence_name = filename;							//turn the filename into a string
	unsigned int wild_card_begin = sequence_name.find_first_of('?');		//find the start and end indices of the wild card
	unsigned int wild_card_end = sequence_name.find_last_of('?');

	unsigned int max_number_length = wild_card_end - wild_card_begin + 1;	//find the maximum number of characters in the image number
	unsigned int max_number = (unsigned int)pow((double)10, (double)max_number_length) - 1;				//find the maximum possible number

	//make sure that the data is given correctly
	unsigned int max = result.DimZ();
	if(max > max_number) max = max_number;

	//save each individual file
	int num_images = max;					//compute the number of images
	CImg<unsigned char> image(result.DimX(), result.DimY());	//create an image for saving
	for(int i=0; i<num_images; i++)			//determine the filename and save the image
	{
		ostringstream format_stream;			//create a stream for number formatting
		format_stream.fill('0');				//the frame name will be right justified with proceeding zeroes
		format_stream<<setw(max_number_length);	//specify the length of the frame name (based on the wild cards)
		format_stream<<i;							//put the number in the stream, convert it to a string
		string frame_name = format_stream.str();

		string file_name = sequence_name;		//create the file name for the current frame
		file_name.replace(wild_card_begin, max_number_length, frame_name);

		//fill the image with the implicit data at the current z coordinate
		unsigned int x, y;
		for(x=0; x<image.width; x++)
			for(y=0; y<image.height; y++)
				image(x, y) = result(x, y, i);

		//save the file
		image.save(file_name.c_str());
	}

}

void rts_cimgDisplayFunction3D(rtsFunction3D<unsigned char> source, unsigned int z)
{
	CImg<unsigned char> image(source.DimX(), source.DimY());

	int x, y;
	for(x=0; x<image.width; x++)
		for(y=0; y<image.height; y++)
			image(x, y) = source(x, y, z);

	CImgDisplay main_disp(image,"loaded image");
	while (!main_disp.is_closed)
      main_disp.wait();
}