Blame view

stim/image/image.h 14.5 KB
d32a1854   David Mayerich   added framework f...
1
2
  #ifndef STIM_IMAGE_H
  #define STIM_IMAGE_H
fbf0ab02   David Mayerich   added support for...
3
  
2e5e3a26   David Mayerich   added 2D convolut...
4
5
6
7
  #ifdef USING_OPENCV
  	#include <opencv2/core/core.hpp>
  	#include <opencv2/highgui/highgui.hpp>
  #endif
912d9073   David Mayerich   added vector.h to...
8
  #include <vector>
d32a1854   David Mayerich   added framework f...
9
  #include <iostream>
a2bf1d08   David Mayerich   general bug fixes...
10
  #include <limits>
e252a6d6   David Mayerich   somehow the C++11...
11
  #include <typeinfo>
cfcf8619   David Mayerich   changes to filena...
12
  #include <fstream>
6d30a707   Tianshu Cheng   add cuda/array_ad...
13
  
7b3948ab   David Mayerich   added support for...
14
  namespace stim{
c8c976a9   David Mayerich   replaced CImg wit...
15
16
  /// 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).
d32a1854   David Mayerich   added framework f...
17
18
19
20
21
22
  
  //currently this interface uses CImg
  //	T = data type (usually unsigned char)
  template <class T>
  class image{
  
cfcf8619   David Mayerich   changes to filena...
23
  	T* img;										//pointer to the image data (interleaved RGB for color)
c8c976a9   David Mayerich   replaced CImg wit...
24
25
  	size_t R[3];
  
dbeb83f2   David Mayerich   added separable c...
26
27
28
  	inline size_t X() const { return R[1]; }
  	inline size_t Y() const { return R[2]; }
  	inline size_t C() const { return R[0]; }
c8c976a9   David Mayerich   replaced CImg wit...
29
  
c8c976a9   David Mayerich   replaced CImg wit...
30
31
32
33
34
35
36
  	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
c8c976a9   David Mayerich   replaced CImg wit...
37
  	}
1a224b6a   David Mayerich   fixed linux compa...
38
  
c8c976a9   David Mayerich   replaced CImg wit...
39
40
41
42
43
44
45
  
  	void clear(){								//clears all image data
  		unalloc();								//unallocate previous memory
  		init();									//re-initialize the variables
  	}
  
  	void allocate(){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
46
  		unalloc();
814eb271   David Mayerich   fixed problems wi...
47
  		img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] );	//allocate memory
c8c976a9   David Mayerich   replaced CImg wit...
48
49
50
  	}
  
  	void allocate(size_t x, size_t y, size_t c){	//allocate memory based on the resolution
c8c976a9   David Mayerich   replaced CImg wit...
51
52
53
54
  		R[0] = c; R[1] = x; R[2] = y;				//set the resolution
  		allocate();									//allocate memory
  	}
  
814eb271   David Mayerich   fixed problems wi...
55
  	size_t bytes(){ return size() * sizeof(T); }
c8c976a9   David Mayerich   replaced CImg wit...
56
  
dbeb83f2   David Mayerich   added separable c...
57
58
  	inline size_t idx(size_t x, size_t y, size_t c = 0){
  		return y * R[0] * R[1] + x * R[0] + c;
c8c976a9   David Mayerich   replaced CImg wit...
59
  	}
1a224b6a   David Mayerich   fixed linux compa...
60
  
2e5e3a26   David Mayerich   added 2D convolut...
61
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
62
  	int cv_type(){
e252a6d6   David Mayerich   somehow the C++11...
63
64
65
66
67
68
69
  		if(typeid(T) == typeid(unsigned char))		return CV_MAKETYPE(CV_8U, (int)C());
  		if(typeid(T) == typeid(char))				return CV_MAKETYPE(CV_8S, (int)C());
  		if(typeid(T) == typeid(unsigned short))		return CV_MAKETYPE(CV_16U, (int)C());
  		if(typeid(T) == typeid(short))				return CV_MAKETYPE(CV_16S, (int)C());
  		if(typeid(T) == typeid(int))				return CV_MAKETYPE(CV_32S, (int)C());
  		if(typeid(T) == typeid(float))				return CV_MAKETYPE(CV_32F, (int)C());
  		if(typeid(T) == typeid(double))				return CV_MAKETYPE(CV_64F, (int)C());
1a224b6a   David Mayerich   fixed linux compa...
70
  
c8c976a9   David Mayerich   replaced CImg wit...
71
  		std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
3ff136b3   David Mayerich   replaced C++11 co...
72
  		exit(1);
c8c976a9   David Mayerich   replaced CImg wit...
73
  	}
2e5e3a26   David Mayerich   added 2D convolut...
74
  #endif
a2bf1d08   David Mayerich   general bug fixes...
75
76
  	/// Returns the value for "white" based on the dynamic range (assumes white is 1.0 for floating point images)
  	T white(){
e252a6d6   David Mayerich   somehow the C++11...
77
78
79
80
81
82
83
84
  
  		if(typeid(T) == typeid(unsigned char))		return UCHAR_MAX;
  		if(typeid(T) == typeid(unsigned short))		return SHRT_MAX;
  		if(typeid(T) == typeid(unsigned))			return UINT_MAX;
  		if(typeid(T) == typeid(unsigned long))		return ULONG_MAX;
  		if(typeid(T) == typeid(unsigned long long))	return ULLONG_MAX;
  		if(typeid(T) == typeid(float))				return 1.0f;
  		if(typeid(T) == typeid(double))				return 1.0;
a2bf1d08   David Mayerich   general bug fixes...
85
86
  
  		std::cout<<"ERROR in stim::image::white - no white value known for this data type"<<std::endl;
e252a6d6   David Mayerich   somehow the C++11...
87
  		exit(1);
814eb271   David Mayerich   fixed problems wi...
88
  
a2bf1d08   David Mayerich   general bug fixes...
89
90
  	}
  
d32a1854   David Mayerich   added framework f...
91
92
93
  
  public:
  
a2bf1d08   David Mayerich   general bug fixes...
94
  	/// Default constructor - creates an empty image object
814eb271   David Mayerich   fixed problems wi...
95
  	image(){ init(); }							//initialize all variables to zero, don't allocate any memory
7b3948ab   David Mayerich   added support for...
96
  
a2bf1d08   David Mayerich   general bug fixes...
97
  	/// Constructor with a filename - loads the specified file
c8c976a9   David Mayerich   replaced CImg wit...
98
  	image(std::string filename){				//constructor initialize the image with an image file
fba6e7e3   Pavel Govyadinov   fixed image.h bug...
99
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
100
  		load(filename);
7b3948ab   David Mayerich   added support for...
101
102
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
103
  	/// Create a new image from scratch given a number of samples and channels
1a224b6a   David Mayerich   fixed linux compa...
104
  	image(size_t x, size_t y = 1, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
105
  		init();
a2bf1d08   David Mayerich   general bug fixes...
106
  		allocate(x, y, c);
c8c976a9   David Mayerich   replaced CImg wit...
107
  	}
f186dbda   Tianshu Cheng   header file for b...
108
  
a2bf1d08   David Mayerich   general bug fixes...
109
  	/// Create a new image with the data given in 'data'
c8c976a9   David Mayerich   replaced CImg wit...
110
  	image(T* data, size_t x, size_t y, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
111
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
112
113
  		allocate(x, y, c);
  		memcpy(img, data, bytes());
41acaf5d   David Mayerich   added a CUDA Gaus...
114
115
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
116
  	/// Copy constructor - duplicates an image object
814eb271   David Mayerich   fixed problems wi...
117
  	image(const stim::image<T>& I){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
118
  		init();
a2bf1d08   David Mayerich   general bug fixes...
119
  		allocate(I.X(), I.Y(), I.C());
a2bf1d08   David Mayerich   general bug fixes...
120
121
122
123
124
125
126
127
  		memcpy(img, I.img, bytes());
  	}
  
  	/// Destructor - clear memory
  	~image(){
  		free(img);
  	}
  
735a2a24   David Mayerich   started testing o...
128
  	stim::image<T>& operator=(const stim::image<T>& I){
814eb271   David Mayerich   fixed problems wi...
129
  		init();
a2bf1d08   David Mayerich   general bug fixes...
130
131
132
133
134
135
136
  		if(&I == this)									//handle self-assignment
  			return *this;
  		allocate(I.X(), I.Y(), I.C());
  		memcpy(img, I.img, bytes());
  		return *this;
  	}
  
cfcf8619   David Mayerich   changes to filena...
137
138
139
140
141
142
143
144
145
146
147
148
  	//save a Netpbm file
  	void load_netpbm(std::string filename) {
  		std::ifstream infile(filename, std::ios::in | std::ios::binary);		//open an output file
  		if (!infile) {
  			std::cout << "Error opening input file in image::load_netpbm()" << std::endl;
  			exit(1);
  		}
  		if (sizeof(T) != 1) {
  			std::cout << "Error in image::load_netpbm() - data type must be 8-bit integer." << std::endl;
  			exit(1);
  		}
  
2e5e3a26   David Mayerich   added 2D convolut...
149
150
  		size_t nc;													//allocate space for the number of channels
  		char format[2];												//allocate space to hold the image format tag
cfcf8619   David Mayerich   changes to filena...
151
  		infile.read(format, 2);										//read the image format tag
2e5e3a26   David Mayerich   added 2D convolut...
152
  		infile.seekg(1, std::ios::cur);								//skip the newline character
cfcf8619   David Mayerich   changes to filena...
153
154
155
156
157
  
  		if (format[0] != 'P') {
  			std::cout << "Error in image::load_netpbm() - file format tag is invalid: " << format[0] << format[1] << std::endl;
  			exit(1);
  		}
2e5e3a26   David Mayerich   added 2D convolut...
158
159
  		if (format[1] == '5') nc = 1;								//get the number of channels from the format flag
  		else if (format[1] == '6') nc = 3;
cfcf8619   David Mayerich   changes to filena...
160
161
162
163
  		else {
  			std::cout << "Error in image::load_netpbm() - file format tag is invalid: " << format[0] << format[1] << std::endl;
  			exit(1);
  		}
2e5e3a26   David Mayerich   added 2D convolut...
164
165
166
167
168
169
170
171
172
  
  		unsigned char c;								//stores a character
  		while (infile.peek() == '#') {					//if the next character indicates the start of a comment
  			while (true) {
  				c = infile.get();
  				if (c == 0x0A) break;
  			}
  		}
  
cfcf8619   David Mayerich   changes to filena...
173
  		std::string sw;									//create a string to store the width of the image
cfcf8619   David Mayerich   changes to filena...
174
175
176
177
178
179
180
181
182
183
184
185
186
  		while(true){
  			c = infile.get();							//get a single character
  			if (c == ' ') break;						//exit if we've encountered a space
  			sw.push_back(c);							//push the character on to the string
  		}
  		size_t w = atoi(sw.c_str());					//convert the string into an integer
  
  		std::string sh;
  		while (true) {
  			c = infile.get();
  			if (c == 0x0A) break;
  			sh.push_back(c);
  		}
cfcf8619   David Mayerich   changes to filena...
187
  
2e5e3a26   David Mayerich   added 2D convolut...
188
189
190
191
192
193
194
195
  		while (true) {									//skip the maximum value
  			c = infile.get();
  			if (c == 0x0A) break;
  		}
  		size_t h = atoi(sh.c_str());					//convert the string into an integer
  
  		allocate(w, h, nc);								//allocate space for the image
  		infile.read((char*)img, size());						//copy the binary data from the file to the image
cfcf8619   David Mayerich   changes to filena...
196
197
198
199
  		infile.close();
  	}
  	
  
2e5e3a26   David Mayerich   added 2D convolut...
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
  #ifdef USING_OPENCV
  	void from_opencv(unsigned char* buffer, size_t width, size_t height) {
  		allocate(width, height, 3);
  		T value;
  		size_t i;
  		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++) {
  					i = y * X() * C() + x * C() + (2 - c);
  					value = buffer[i];
  					img[idx(x, y, c)] = value;
  				}
  			}
  		}
  	}
  #endif
c8c976a9   David Mayerich   replaced CImg wit...
216
  	/// Load an image from a file
d32a1854   David Mayerich   added framework f...
217
  	void load(std::string filename){
2e5e3a26   David Mayerich   added 2D convolut...
218
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
219
  		cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED);	//use OpenCV to open the image file
a2bf1d08   David Mayerich   general bug fixes...
220
  		if(!cvImage.data){
814eb271   David Mayerich   fixed problems wi...
221
  			std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<std::endl;
a2bf1d08   David Mayerich   general bug fixes...
222
223
  			exit(1);
  		}
26da82c8   David Mayerich   finalized stim::g...
224
225
226
227
  		int cols = cvImage.cols;
  		int rows = cvImage.rows;
  		int channels = cvImage.channels();
  		allocate(cols, rows, channels);			//allocate space for the image
ca99f951   David Mayerich   faster implementa...
228
  		unsigned char* cv_ptr = (unsigned char*)cvImage.data;
814eb271   David Mayerich   fixed problems wi...
229
230
231
  		if(C() == 1)														//if this is a single-color image, just copy the data
  			memcpy(img, cv_ptr, bytes());
  		if(C() == 3)														//if this is a 3-color image, OpenCV uses BGR interleaving
ca99f951   David Mayerich   faster implementa...
232
  			from_opencv(cv_ptr, X(), Y());
2e5e3a26   David Mayerich   added 2D convolut...
233
234
235
  #else
  		load_netpbm(filename);
  #endif
ca99f951   David Mayerich   faster implementa...
236
237
  	}
  
2e5e3a26   David Mayerich   added 2D convolut...
238
  
d32a1854   David Mayerich   added framework f...
239
  
cfcf8619   David Mayerich   changes to filena...
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
  	//save a Netpbm file
  	void save_netpbm(std::string filename) {
  		std::ofstream outfile(filename, std::ios::out | std::ios::binary);		//open an output file
  		if(!outfile) {
  			std::cout << "Error generating output file in image::save_netpbm()" << std::endl;
  			exit(1);
  		}
  		if (sizeof(T) != 1) {
  			std::cout << "Error in image::save_netpbm() - data type must be 8-bit integer." << std::endl;
  			exit(1);
  		}
  		std::string format;
  		if (channels() == 1) outfile << "P5" << (char)0x0A;			//output P5 if the file is grayscale
  		else if (channels() == 3) outfile << "P6" << (char)0x0A;		//output P6 if the file is color
  		else {
  			std::cout << "Error in image::save_netpbm() - data must be grayscale or RGB." << std::endl;
  			exit(1);
  		}
2e5e3a26   David Mayerich   added 2D convolut...
258
259
260
  		size_t w = width();
  		size_t h = height();
  		outfile << w << " " << h << (char)0x0A;			//save the width and height
cfcf8619   David Mayerich   changes to filena...
261
262
263
264
265
  		outfile << "255" << (char)0x0A;								//output the maximum value
  		outfile.write((const char*)img, size());			//write the binary data
  		outfile.close();
  	}
  
d32a1854   David Mayerich   added framework f...
266
267
  	//save a file
  	void save(std::string filename){
2e5e3a26   David Mayerich   added 2D convolut...
268
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
269
270
  		//OpenCV uses an interleaved format, so convert first and then output
  		T* buffer = (T*) malloc(bytes());
a2bf1d08   David Mayerich   general bug fixes...
271
272
273
274
  
  		if(C() == 1)
  			memcpy(buffer, img, bytes());
  		else if(C() == 3)
9b766f1f   Pavel Govyadinov   completed merge f...
275
  			get_interleaved_bgr(buffer);
c8c976a9   David Mayerich   replaced CImg wit...
276
277
  		cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
  		cv::imwrite(filename, cvImage);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
278
  		free(buffer);
2e5e3a26   David Mayerich   added 2D convolut...
279
280
281
  #else
  		save_netpbm(filename);
  #endif
d32a1854   David Mayerich   added framework f...
282
283
  	}
  
ca99f951   David Mayerich   faster implementa...
284
285
286
287
288
  	void set_interleaved(T* buffer, size_t width, size_t height, size_t channels){
  		allocate(width, height, channels);
  		memcpy(img, buffer, bytes());
  	}
  
8b7be670   David Mayerich   implemented savin...
289
  	//create an image from an interleaved buffer
814eb271   David Mayerich   fixed problems wi...
290
  	void set_interleaved_rgb(T* buffer, size_t width, size_t height){
ca99f951   David Mayerich   faster implementa...
291
  		set_interleaved(buffer, width, height, 3);
c8c976a9   David Mayerich   replaced CImg wit...
292
  	}
fbf0ab02   David Mayerich   added support for...
293
  
814eb271   David Mayerich   fixed problems wi...
294
295
  	void set_interleaved_bgr(T* buffer, size_t width, size_t height){
  		allocate(width, height, 3);
ca99f951   David Mayerich   faster implementa...
296
297
  		T value;
  		size_t i;
c8c976a9   David Mayerich   replaced CImg wit...
298
299
300
  		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++){
ca99f951   David Mayerich   faster implementa...
301
302
303
  					i = y * X() * C() + x * C() + (2-c);
  					value = buffer[i];
  					img[idx(x, y, c)] = value;
c8c976a9   David Mayerich   replaced CImg wit...
304
  				}
fbf0ab02   David Mayerich   added support for...
305
306
  			}
  		}
8b7be670   David Mayerich   implemented savin...
307
308
  	}
  
ca99f951   David Mayerich   faster implementa...
309
310
311
312
  	void set_interleaved(T* buffer, size_t width, size_t height){
  		set_interleaved_rgb(buffer, width, height);
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
313
  	void get_interleaved_bgr(T* data){
8b7be670   David Mayerich   implemented savin...
314
315
  
  		//for each channel
c8c976a9   David Mayerich   replaced CImg wit...
316
317
318
319
320
321
322
  		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)];
  				}
  			}
  		}
8b7be670   David Mayerich   implemented savin...
323
324
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
325
326
327
328
  	void get_interleaved_rgb(T* data){
  		memcpy(data, img, bytes());
  	}
  
904614c3   David Mayerich   added normalizati...
329
330
331
332
333
334
335
336
337
338
339
340
  	//copies data in the given channel order as a non-interleaved image
  	void get_noninterleaved(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[c * Y() * X() + y * X() + x] = img[idx(x, y, c)];
  				}
  			}
  		}
  	}
  
dbeb83f2   David Mayerich   added separable c...
341
342
343
344
  	/// Return an image representing a specified channel
  	/// @param c is the channel to be returned
  	image<T> channel(size_t c){		
  		image<T> r(X(), Y(), 1);				//create a new image
c8c976a9   David Mayerich   replaced CImg wit...
345
346
  		for(size_t x = 0; x < X(); x++){
  			for(size_t y = 0; y < Y(); y++){
8e2fb2b4   David Mayerich   fixed a segmentat...
347
  				r.img[r.idx(x, y, 0)] = img[idx(x, y, c)];
c8c976a9   David Mayerich   replaced CImg wit...
348
349
  			}
  		}
c8c976a9   David Mayerich   replaced CImg wit...
350
  		return r;
dbeb83f2   David Mayerich   added separable c...
351
352
353
354
355
356
  	}
  
  	/// Returns an std::vector containing each channel as a separate image
  	std::vector<image<T>> split() {
  		std::vector<image<T>> r;			//create an image array
  		r.resize(C());						//create images for each channel
41acaf5d   David Mayerich   added a CUDA Gaus...
357
  
dbeb83f2   David Mayerich   added separable c...
358
359
360
361
  		for (size_t c = 0; c < C(); c++) {	//for each channel
  			r[c] = channel(c);				//copy the channel image to the array
  		}
  		return r;
41acaf5d   David Mayerich   added a CUDA Gaus...
362
363
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
364
365
  	T& operator()(size_t x, size_t y, size_t c = 0){
  		return img[idx(x, y, c)];
3d0b6243   David Mayerich   fixed hsiproc bug...
366
367
368
369
370
371
372
  	}
  
  	/// 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){
  
c8c976a9   David Mayerich   replaced CImg wit...
373
  		size_t N = size();
3d0b6243   David Mayerich   fixed hsiproc bug...
374
  
c8c976a9   David Mayerich   replaced CImg wit...
375
376
  		for(size_t n = 0; n < N; n++)
  			img[n] = v;
3d0b6243   David Mayerich   fixed hsiproc bug...
377
378
379
380
381
  
  		return *this;
  
  	}
  
f186dbda   Tianshu Cheng   header file for b...
382
383
384
385
386
  	/// 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
  
c8c976a9   David Mayerich   replaced CImg wit...
387
  	void set_channel(T* buffer, size_t c){
6d30a707   Tianshu Cheng   add cuda/array_ad...
388
  
c8c976a9   David Mayerich   replaced CImg wit...
389
390
391
  		size_t x, y;
  		for(y = 0; y < Y(); y++){
  			for(x = 0; x < X(); x++){
2e5e3a26   David Mayerich   added 2D convolut...
392
  				img[idx(x, y, c)] = buffer[y * X() + x];
c8c976a9   David Mayerich   replaced CImg wit...
393
394
  			}
  		}
6d30a707   Tianshu Cheng   add cuda/array_ad...
395
396
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
397
  	size_t channels(){
c8c976a9   David Mayerich   replaced CImg wit...
398
  		return C();
8b7be670   David Mayerich   implemented savin...
399
400
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
401
  	size_t width(){
c8c976a9   David Mayerich   replaced CImg wit...
402
  		return X();
8b7be670   David Mayerich   implemented savin...
403
404
  	}
  
9d3ba0b1   David Mayerich   added stim::hsi a...
405
  	size_t height(){
c8c976a9   David Mayerich   replaced CImg wit...
406
  		return Y();
3d0b6243   David Mayerich   fixed hsiproc bug...
407
408
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
409
  	T* data(){
c8c976a9   David Mayerich   replaced CImg wit...
410
  		return img;
6d30a707   Tianshu Cheng   add cuda/array_ad...
411
412
  	}
  
7b3948ab   David Mayerich   added support for...
413
  	//returns the size (number of values) of the image
c8c976a9   David Mayerich   replaced CImg wit...
414
  	size_t size(){ return C() * X() * Y(); }
d32a1854   David Mayerich   added framework f...
415
  
faef7718   David Mayerich   updates to stim::...
416
  	/// Returns the number of nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
417
  	size_t nnz(){
faef7718   David Mayerich   updates to stim::...
418
  
c8c976a9   David Mayerich   replaced CImg wit...
419
  		size_t N = X() * Y() * C();
faef7718   David Mayerich   updates to stim::...
420
  
c8c976a9   David Mayerich   replaced CImg wit...
421
422
423
  		size_t nz = 0;
  		for(size_t n = 0; n < N; n++)
  			if(img[n] != 0) nz++;
faef7718   David Mayerich   updates to stim::...
424
  
c8c976a9   David Mayerich   replaced CImg wit...
425
  		return nz;	//return the number of nonzero pixels
faef7718   David Mayerich   updates to stim::...
426
427
428
429
  
  	}
  
  	//this function returns indices of pixels that have nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
430
  	std::vector<size_t> sparse_idx(){
faef7718   David Mayerich   updates to stim::...
431
  
c8c976a9   David Mayerich   replaced CImg wit...
432
  		std::vector<size_t> s;				//allocate an array
faef7718   David Mayerich   updates to stim::...
433
434
  		s.resize(nnz());					//allocate space in the array
  
c8c976a9   David Mayerich   replaced CImg wit...
435
436
  		size_t N = size();
  		//size_t C = channels();
faef7718   David Mayerich   updates to stim::...
437
  
c8c976a9   David Mayerich   replaced CImg wit...
438
  		//T* ptr = img.data();				//get a pointer to the image data
faef7718   David Mayerich   updates to stim::...
439
  
9d3ba0b1   David Mayerich   added stim::hsi a...
440
  		size_t i = 0;
c8c976a9   David Mayerich   replaced CImg wit...
441
442
443
444
  		for(size_t n = 0; n < N; n++){
  			if(img[n] != 0){
  				s[i] = n;
  				i++;
faef7718   David Mayerich   updates to stim::...
445
446
447
448
449
  			}
  		}
  
  		return s;			//return the index list
  	}
6d30a707   Tianshu Cheng   add cuda/array_ad...
450
  
1a224b6a   David Mayerich   fixed linux compa...
451
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
452
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
453
  	T maxv(){
c8c976a9   David Mayerich   replaced CImg wit...
454
455
  		T max_val = img[0];				//initialize the maximum value to the first one
  		size_t N = size();	//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
456
  
c8c976a9   David Mayerich   replaced CImg wit...
457
  		for (size_t n=0; n<N; n++){		//for every value
6d30a707   Tianshu Cheng   add cuda/array_ad...
458
  
c8c976a9   David Mayerich   replaced CImg wit...
459
460
  			if (img[n] > max_val){			//if the value is higher than the current max
  				max_val = img[n];
1a224b6a   David Mayerich   fixed linux compa...
461
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
462
463
  		}
  
1a224b6a   David Mayerich   fixed linux compa...
464
  		return max_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
465
  	}
d32a1854   David Mayerich   added framework f...
466
  
c8c976a9   David Mayerich   replaced CImg wit...
467
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
468
  	T minv(){
c8c976a9   David Mayerich   replaced CImg wit...
469
470
  		T min_val = img[0];				//initialize the maximum value to the first one
  		size_t N = size();	//get the number of pixels
6d30a707   Tianshu Cheng   add cuda/array_ad...
471
  
c8c976a9   David Mayerich   replaced CImg wit...
472
473
474
  		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];
1a224b6a   David Mayerich   fixed linux compa...
475
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
476
477
  		}
  
1a224b6a   David Mayerich   fixed linux compa...
478
  		return min_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
479
480
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
481
482
483
484
485
486
  	/// Invert an image by calculating I1 = alpha - I0, where alpha is the maximum image value
  	image<T> invert(T white_val){
  		size_t N = size();						//calculate the total number of values in the image
  		image<T> r(X(), Y(), C());				//allocate space for the resulting image
  		for(size_t n = 0; n < N; n++)
  			r.img[n] = white_val - img[n];		//perform the inversion
1a224b6a   David Mayerich   fixed linux compa...
487
  
a2bf1d08   David Mayerich   general bug fixes...
488
489
  		return r;								//return the inverted image
  	}
a2bf1d08   David Mayerich   general bug fixes...
490
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
491
  	image<T> srgb2lab(){
c8c976a9   David Mayerich   replaced CImg wit...
492
493
  		std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl;
  		exit(1);
6d30a707   Tianshu Cheng   add cuda/array_ad...
494
495
496
  	}
  
  	image<T> convolve2(image<T> mask){
1a224b6a   David Mayerich   fixed linux compa...
497
  
c8c976a9   David Mayerich   replaced CImg wit...
498
499
  		std::cout<<"ERROR stim::image::convolve2 - function has been broken, and shouldn't really be in here."<<std::endl;
  		exit(1);
6d30a707   Tianshu Cheng   add cuda/array_ad...
500
  	}
d32a1854   David Mayerich   added framework f...
501
502
  
  
f186dbda   Tianshu Cheng   header file for b...
503
  	image<T> rotate(float angle, float cx, float cy){
c8c976a9   David Mayerich   replaced CImg wit...
504
505
  		std::cout<<"ERROR stim::image::rotate - function has been broken, and shouldn't really be in here."<<std::endl;
  		exit(1);
f186dbda   Tianshu Cheng   header file for b...
506
  	}
945ee13c   Laila Saadatifard   the get_list func...
507
  
945ee13c   Laila Saadatifard   the get_list func...
508
509
  	// leila's code for non_interleaving data in 3D
  	//create an data set from an interleaved buffer
9d3ba0b1   David Mayerich   added stim::hsi a...
510
  	void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
c8c976a9   David Mayerich   replaced CImg wit...
511
512
  		std::cout<<"ERROR stim::image::set_interleaved3 - stim::image no longer supports 3D images."<<std::endl;
  		exit(1);
945ee13c   Laila Saadatifard   the get_list func...
513
  	}
1a224b6a   David Mayerich   fixed linux compa...
514
  
dbeb83f2   David Mayerich   added separable c...
515
  	/// Casting operator, casts every value in an image to a different data type V
2e5e3a26   David Mayerich   added 2D convolut...
516
517
  	template<typename V>
  	operator image<V>() {
dbeb83f2   David Mayerich   added separable c...
518
519
520
  		image<V> r(X(), Y(), C());					//create a new image
  		std::copy(img, img + size(), r.data());		//copy and cast the data
  		return r;									//return the new image
2e5e3a26   David Mayerich   added 2D convolut...
521
522
  	}
  
d32a1854   David Mayerich   added framework f...
523
524
  };
  
7b3948ab   David Mayerich   added support for...
525
526
  };		//end namespace stim
  
d32a1854   David Mayerich   added framework f...
527
528
  
  #endif