image.h 1.95 KB
#ifndef STIM_IMAGE_H
#define STIM_IMAGE_H

#include "CImg.h"

#include <iostream>

namespace stim{
//This static class provides the STIM interface for loading images
//	Use this interface for all image management - that way the actual library can be changed without problems

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

	cimg_library::CImg<T> img;


	//default constructor

	//constructor (load an image file)
	image(std::string filename){

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

	//save a file
	void save(std::string filename){;

	//create an image from an interleaved buffer
	void set_interleaved(T* buffer, unsigned int width, unsigned int height, unsigned int channels = 1){

		unsigned char* non_interleaved = (unsigned char*)malloc(width * height * 3);
		unsigned int S = width * height;

		for(unsigned int i = 0; i < S; i++){
			for(unsigned int c = 0; c < channels; c++){
				non_interleaved[i + c * S] = buffer[i * channels + c];

		img = cimg_library::CImg<unsigned char>(non_interleaved, width, height, 1, channels);

	//returns a pointer to the first pixel of the image data
	void data_noninterleaved(T* data){
		memcpy(data,, sizeof(T) * size());

	void data_interleaved(T* data){

		unsigned int C = channels();
		unsigned int X = size();

		T* ptr =;

		//for each channel
		for(unsigned int c = 0; c < C; c++)
			//convert each pixel
			for(unsigned int x = 0; x < X; x++)
				data[x * C + c] = ptr[c * X + x];

	unsigned int channels(){
		return (unsigned int)img.spectrum();

	unsigned int width(){
		return img.width();

	unsigned int height(){
		return img.height();

	//returns the size (number of values) of the image
	unsigned long size(){
		return img.size();



};		//end namespace stim