image.h 7.32 KB
#ifndef STIM_IMAGE_H
#define STIM_IMAGE_H

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <vector>
#include <iostream>

namespace stim{
/// This static class provides the STIM interface for loading, saving, and storing 2D images.
/// Data is stored in an interleaved (BIP) format (default for saving and loading is RGB).

//currently this interface uses CImg
//	T = data type (usually unsigned char)
template <class T>
class image{

	//cimg_library::CImg<T> img;
	T* img;										//pointer to the image data (assumes RGB for loading/saving)
	size_t R[3];

	size_t X(){ return R[1]; }
	size_t Y(){ return R[2]; }
	size_t C(){ return R[0]; }

	void init(){								//initializes all variables, assumes no memory is allocated
		memset(R, 0, sizeof(size_t) * 3);		//set the resolution and number of channels to zero
		img = NULL;
	}

	void unalloc(){								//frees any resources associated with the image
		if(img)	free(img);						//if memory has been allocated, free it
	}
			

	void clear(){								//clears all image data
		unalloc();								//unallocate previous memory
		init();									//re-initialize the variables
	}

	void allocate(){
		img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] );	//allocate memory
	}

	void allocate(size_t x, size_t y, size_t c){	//allocate memory based on the resolution
		R[0] = c; R[1] = x; R[2] = y;				//set the resolution
		allocate();									//allocate memory
	}

	size_t bytes(){ return size() * sizeof(T); }

	size_t idx(size_t x, size_t y, size_t c = 0){
		return y * C() * X() + x * C() + c;
	}
	

	int cv_type(){
		if(std::is_same<T, unsigned char>::value)	return CV_MAKETYPE(CV_8U, (int)C());
		if(std::is_same<T, char>::value)			return CV_MAKETYPE(CV_8S, (int)C());
		if(std::is_same<T, unsigned short>::value)	return CV_MAKETYPE(CV_16U, (int)C());
		if(std::is_same<T, short>::value)			return CV_MAKETYPE(CV_16S, (int)C());
		if(std::is_same<T, int>::value)				return CV_MAKETYPE(CV_32S, (int)C());
		if(std::is_same<T, float>::value)			return CV_MAKETYPE(CV_32F, (int)C());
		if(std::is_same<T, double>::value)			return CV_MAKETYPE(CV_64F, (int)C());
		
		std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
		exit(1);
	}


public:

	//default constructor
	image(){ init(); }							//initialize all variables to zero, don't allocate any memory

	image(std::string filename){				//constructor initialize the image with an image file
		load(filename);
	}

	/// Create a new image from scratch given a number of samples and channels
	image(size_t x, size_t y = 1, size_t c = 1){			
		img = (T*) malloc( sizeof(T) * x * y * c );
	}

	image(T* data, size_t x, size_t y, size_t c = 1){
		allocate(x, y, c);
		memcpy(img, data, bytes());
	}

	/// Load an image from a file
	void load(std::string filename){

		cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED);	//use OpenCV to open the image file
		allocate(cvImage.cols, cvImage.rows, cvImage.channels());			//allocate space for the image
		T* cv_ptr = (T*)cvImage.data;
		set_interleaved_bgr(cv_ptr, X(), Y());
	}

	//save a file
	void save(std::string filename){
		//OpenCV uses an interleaved format, so convert first and then output
		T* buffer = (T*) malloc(bytes());
		data_interleaved_bgr(buffer);
		cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
		cv::imwrite(filename, cvImage);
	}

	//create an image from an interleaved buffer
	void set_interleaved_rgb(T* buffer, size_t width, size_t height){
		allocate(width, height, 3);
		memcpy(img, buffer, bytes());
	}

	void set_interleaved_bgr(T* buffer, size_t width, size_t height){
		allocate(width, height, 3);
		for(size_t c = 0; c < C(); c++){								//copy directly
			for(size_t y = 0; y < Y(); y++){
				for(size_t x = 0; x < X(); x++){
					img[idx(x, y, c)] = buffer[y * X() * C() + x * C() + (2-c)];
				}
			}
		}
	}

	void data_interleaved_bgr(T* data){

		//for each channel
		for(size_t y = 0; y < Y(); y++){
			for(size_t x = 0; x < X(); x++){
				for(size_t c = 0; c < C(); c++){
					data[y * X() * C() + x * C() + (2-c)] = img[idx(x, y, c)];
				}
			}
		}
	}

	image<T> channel(size_t c){

		//create a new image
		image<T> r(R[0], R[1], 1);

		for(size_t x = 0; x < X(); x++){
			for(size_t y = 0; y < Y(); y++){
				r.img[r.idx(x, y, c)] = img[idx(x, y, c)];
			}
		}

		return r;

	}

	T& operator()(size_t x, size_t y, size_t c = 0){
		return img[idx(x, y, c)];
	}

	/// Set all elements in the image to a given scalar value

	/// @param v is the value used to set all values in the image
	image<T> operator=(T v){

		size_t N = size();

		for(size_t n = 0; n < N; n++)
			img[n] = v;

		return *this;

	}

	/// Copy the given data to the specified channel

	/// @param c is the channel number that the data will be copied to
	/// @param buffer is a pointer to the image to be copied to channel c

	void set_channel(T* buffer, size_t c){

		size_t x, y;
		for(y = 0; y < Y(); y++){
			for(x = 0; x < X(); x++){
				img[idx(x, y, c)] = buffer[c];
			}
		}
	}

	size_t channels(){
		return C();
	}

	size_t width(){
		return X();
	}

	size_t height(){
		return Y();
	}

	T* data(){
		return img;
	}

	//returns the size (number of values) of the image
	size_t size(){ return C() * X() * Y(); }

	/// Returns the number of nonzero values
	size_t nnz(){

		size_t N = X() * Y() * C();

		size_t nz = 0;
		for(size_t n = 0; n < N; n++)
			if(img[n] != 0) nz++;

		return nz;	//return the number of nonzero pixels

	}

	//this function returns indices of pixels that have nonzero values
	std::vector<size_t> sparse_idx(){

		std::vector<size_t> s;				//allocate an array
		s.resize(nnz());					//allocate space in the array

		size_t N = size();
		//size_t C = channels();

		//T* ptr = img.data();				//get a pointer to the image data

		size_t i = 0;
		for(size_t n = 0; n < N; n++){
			if(img[n] != 0){
				s[i] = n;
				i++;
			}
		}

		return s;			//return the index list
	}

	
	/// Returns the maximum pixel value in the image
	T maxv(){
		T max_val = img[0];				//initialize the maximum value to the first one
		size_t N = size();	//get the number of pixels

		for (size_t n=0; n<N; n++){		//for every value

			if (img[n] > max_val){			//if the value is higher than the current max
				max_val = img[n];
			}	
		}

		return max;	
	}

	/// Returns the maximum pixel value in the image
	T minv(){
		T min_val = img[0];				//initialize the maximum value to the first one
		size_t N = size();	//get the number of pixels

		for (size_t n=0; n<N; n++){		//for every value
			if (img[n] < min_val){			//if the value is higher than the current max
				min_val = img[n];
			}	
		}

		return max;	
	}

	image<T> srgb2lab(){
		std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl;
		exit(1);
	}

	image<T> convolve2(image<T> mask){
		
		std::cout<<"ERROR stim::image::convolve2 - function has been broken, and shouldn't really be in here."<<std::endl;
		exit(1);
	}


	image<T> rotate(float angle, float cx, float cy){
		std::cout<<"ERROR stim::image::rotate - function has been broken, and shouldn't really be in here."<<std::endl;
		exit(1);
	}

	// leila's code for non_interleaving data in 3D
	//create an data set from an interleaved buffer
	void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
		std::cout<<"ERROR stim::image::set_interleaved3 - stim::image no longer supports 3D images."<<std::endl;
		exit(1);
	}
	
};

};		//end namespace stim


#endif