Blame view

stim/image/image.h 21.2 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
  #ifdef USING_OPENCV
dd5aab2f   David Mayerich   fixed errors in s...
5
6
7
  	//#include <opencv2/core/core.hpp>
  	//#include <opencv2/highgui/highgui.hpp>
  	#include <opencv2/opencv.hpp>
2e5e3a26   David Mayerich   added 2D convolut...
8
  #endif
912d9073   David Mayerich   added vector.h to...
9
  #include <vector>
d32a1854   David Mayerich   added framework f...
10
  #include <iostream>
a2bf1d08   David Mayerich   general bug fixes...
11
  #include <limits>
e252a6d6   David Mayerich   somehow the C++11...
12
  #include <typeinfo>
cfcf8619   David Mayerich   changes to filena...
13
  #include <fstream>
b4cea01a   David Mayerich   removed automated...
14
  #include <cstring>
6d30a707   Tianshu Cheng   add cuda/array_ad...
15
  
d9b2b2a8   David Mayerich   enabled basic rea...
16
17
18
  #include <stim/image/bmp.h>
  #include <stim/parser/filename.h>
  
7b3948ab   David Mayerich   added support for...
19
  namespace stim{
c8c976a9   David Mayerich   replaced CImg wit...
20
21
  /// 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...
22
23
24
25
26
27
  
  //currently this interface uses CImg
  //	T = data type (usually unsigned char)
  template <class T>
  class image{
  
cfcf8619   David Mayerich   changes to filena...
28
  	T* img;										//pointer to the image data (interleaved RGB for color)
c8c976a9   David Mayerich   replaced CImg wit...
29
30
  	size_t R[3];
  
dbeb83f2   David Mayerich   added separable c...
31
32
33
  	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...
34
  
c8c976a9   David Mayerich   replaced CImg wit...
35
36
37
38
39
40
41
  	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...
42
  	}
1a224b6a   David Mayerich   fixed linux compa...
43
  
c8c976a9   David Mayerich   replaced CImg wit...
44
45
46
47
48
49
50
  
  	void clear(){								//clears all image data
  		unalloc();								//unallocate previous memory
  		init();									//re-initialize the variables
  	}
  
  	void allocate(){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
51
  		unalloc();
814eb271   David Mayerich   fixed problems wi...
52
  		img = (T*) malloc( sizeof(T) * R[0] * R[1] * R[2] );	//allocate memory
c8c976a9   David Mayerich   replaced CImg wit...
53
54
55
  	}
  
  	void allocate(size_t x, size_t y, size_t c){	//allocate memory based on the resolution
c8c976a9   David Mayerich   replaced CImg wit...
56
57
58
59
  		R[0] = c; R[1] = x; R[2] = y;				//set the resolution
  		allocate();									//allocate memory
  	}
  
86dfa80b   David Mayerich   added 2D gaussian...
60
  	inline size_t idx(size_t x, size_t y, size_t c = 0) const {
dbeb83f2   David Mayerich   added separable c...
61
  		return y * R[0] * R[1] + x * R[0] + c;
c8c976a9   David Mayerich   replaced CImg wit...
62
  	}
1a224b6a   David Mayerich   fixed linux compa...
63
  
2e5e3a26   David Mayerich   added 2D convolut...
64
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
65
  	int cv_type(){
e252a6d6   David Mayerich   somehow the C++11...
66
67
68
69
70
71
72
  		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...
73
  
c8c976a9   David Mayerich   replaced CImg wit...
74
  		std::cout<<"ERROR in stim::image::cv_type - no valid data type found"<<std::endl;
3ff136b3   David Mayerich   replaced C++11 co...
75
  		exit(1);
c8c976a9   David Mayerich   replaced CImg wit...
76
  	}
2e5e3a26   David Mayerich   added 2D convolut...
77
  #endif
a2bf1d08   David Mayerich   general bug fixes...
78
79
  	/// Returns the value for "white" based on the dynamic range (assumes white is 1.0 for floating point images)
  	T white(){
0129eeb3   David Mayerich   updated type chec...
80
  		return std::numeric_limits<T>::max();
a2bf1d08   David Mayerich   general bug fixes...
81
82
  	}
  
d32a1854   David Mayerich   added framework f...
83
84
85
  
  public:
  
a7ed4b2e   David Mayerich   ivote updates
86
87
  	size_t bytes() { return size() * sizeof(T); }
  
a2bf1d08   David Mayerich   general bug fixes...
88
  	/// Default constructor - creates an empty image object
814eb271   David Mayerich   fixed problems wi...
89
  	image(){ init(); }							//initialize all variables to zero, don't allocate any memory
7b3948ab   David Mayerich   added support for...
90
  
a2bf1d08   David Mayerich   general bug fixes...
91
  	/// Constructor with a filename - loads the specified file
c8c976a9   David Mayerich   replaced CImg wit...
92
  	image(std::string filename){				//constructor initialize the image with an image file
fba6e7e3   Pavel Govyadinov   fixed image.h bug...
93
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
94
  		load(filename);
7b3948ab   David Mayerich   added support for...
95
96
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
97
  	/// Create a new image from scratch given a number of samples and channels
1a224b6a   David Mayerich   fixed linux compa...
98
  	image(size_t x, size_t y = 1, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
99
  		init();
a2bf1d08   David Mayerich   general bug fixes...
100
  		allocate(x, y, c);
c8c976a9   David Mayerich   replaced CImg wit...
101
  	}
f186dbda   Tianshu Cheng   header file for b...
102
  
a2bf1d08   David Mayerich   general bug fixes...
103
  	/// Create a new image with the data given in 'data'
c8c976a9   David Mayerich   replaced CImg wit...
104
  	image(T* data, size_t x, size_t y, size_t c = 1){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
105
  		init();
c8c976a9   David Mayerich   replaced CImg wit...
106
107
  		allocate(x, y, c);
  		memcpy(img, data, bytes());
41acaf5d   David Mayerich   added a CUDA Gaus...
108
109
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
110
  	/// Copy constructor - duplicates an image object
814eb271   David Mayerich   fixed problems wi...
111
  	image(const stim::image<T>& I){
e9a1b974   Pavel Govyadinov   imlemented Dr. Ma...
112
  		init();
a2bf1d08   David Mayerich   general bug fixes...
113
  		allocate(I.X(), I.Y(), I.C());
a2bf1d08   David Mayerich   general bug fixes...
114
115
116
117
118
119
120
121
  		memcpy(img, I.img, bytes());
  	}
  
  	/// Destructor - clear memory
  	~image(){
  		free(img);
  	}
  
a7ed4b2e   David Mayerich   ivote updates
122
  	///Resize an image - this function looks like it hasn't been implemented
86dfa80b   David Mayerich   added 2D gaussian...
123
124
125
126
  	void resize(size_t x, size_t y, size_t c = 1) {
  		allocate(x, y, c);
  	}
  
735a2a24   David Mayerich   started testing o...
127
  	stim::image<T>& operator=(const stim::image<T>& I){
a2bf1d08   David Mayerich   general bug fixes...
128
129
  		if(&I == this)									//handle self-assignment
  			return *this;
276f9b23   David Mayerich   forced matching t...
130
  		init();
a2bf1d08   David Mayerich   general bug fixes...
131
132
133
134
135
  		allocate(I.X(), I.Y(), I.C());
  		memcpy(img, I.img, bytes());
  		return *this;
  	}
  
d9b2b2a8   David Mayerich   enabled basic rea...
136
137
138
139
140
141
142
143
144
145
146
  	void load_bmp(std::string filename) {
  		stim::bmp bitmap;
  		bitmap.open(filename);											//load the bitmap and read the headers
  		resize(bitmap.width, bitmap.height, 3);							//resize the current image to match the bitmap
  		if (!bitmap.read((char*)img)) {										//read the bits from file
  			std::cout << "stim::image ERROR: problem loading bitmap image." << std::endl;
  			exit(1);
  		}
  		bitmap.close();													//close the bitmap file
  	}
  
cfcf8619   David Mayerich   changes to filena...
147
148
  	//save a Netpbm file
  	void load_netpbm(std::string filename) {
683f216a   David Mayerich   fixed? Linux comp...
149
  		std::ifstream infile(filename.c_str(), std::ios::in | std::ios::binary);		//open an output file
cfcf8619   David Mayerich   changes to filena...
150
151
152
153
  		if (!infile) {
  			std::cout << "Error opening input file in image::load_netpbm()" << std::endl;
  			exit(1);
  		}
cfcf8619   David Mayerich   changes to filena...
154
  
2e5e3a26   David Mayerich   added 2D convolut...
155
156
  		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...
157
  		infile.read(format, 2);										//read the image format tag
2e5e3a26   David Mayerich   added 2D convolut...
158
  		infile.seekg(1, std::ios::cur);								//skip the newline character
cfcf8619   David Mayerich   changes to filena...
159
160
161
162
163
  
  		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...
164
165
  		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...
166
167
168
169
  		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...
170
171
172
173
174
175
176
177
178
  
  		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...
179
  		std::string sw;									//create a string to store the width of the image
cfcf8619   David Mayerich   changes to filena...
180
181
182
183
184
185
186
187
188
189
190
191
192
  		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...
193
  
2e5e3a26   David Mayerich   added 2D convolut...
194
195
196
197
198
199
  		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
  
01707489   David Mayerich   implemented a new...
200
201
202
203
204
205
  		allocate(w, h, nc);													//allocate space for the image
  		unsigned char* buffer = (unsigned char*)malloc(w * h * nc);			//create a buffer to store the read data
  		infile.read((char*)buffer, size());									//copy the binary data from the file to the image
  		infile.close();														//close the file
  		for (size_t n = 0; n < size(); n++) img[n] = (T)buffer[n];			//copy the buffer data into the image
  		free(buffer);														//free the buffer array
cfcf8619   David Mayerich   changes to filena...
206
207
208
  	}
  	
  
2e5e3a26   David Mayerich   added 2D convolut...
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
  #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...
225
  	/// Load an image from a file
d32a1854   David Mayerich   added framework f...
226
  	void load(std::string filename){
2e5e3a26   David Mayerich   added 2D convolut...
227
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
228
  		cv::Mat cvImage = cv::imread(filename, CV_LOAD_IMAGE_UNCHANGED);	//use OpenCV to open the image file
a2bf1d08   David Mayerich   general bug fixes...
229
  		if(!cvImage.data){
814eb271   David Mayerich   fixed problems wi...
230
  			std::cout<<"ERROR stim::image::load() - unable to find image "<<filename<<std::endl;
a2bf1d08   David Mayerich   general bug fixes...
231
232
  			exit(1);
  		}
26da82c8   David Mayerich   finalized stim::g...
233
234
235
236
  		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...
237
  		unsigned char* cv_ptr = (unsigned char*)cvImage.data;
814eb271   David Mayerich   fixed problems wi...
238
239
240
  		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...
241
  			from_opencv(cv_ptr, X(), Y());
2e5e3a26   David Mayerich   added 2D convolut...
242
  #else
d9b2b2a8   David Mayerich   enabled basic rea...
243
244
245
246
247
  		stim::filename file(filename);
  		if (file.extension() == "ppm")
  			load_netpbm(filename);
  		else if (file.extension() == "bmp")
  			load_bmp(filename);
2e5e3a26   David Mayerich   added 2D convolut...
248
  #endif
ca99f951   David Mayerich   faster implementa...
249
250
  	}
  
2e5e3a26   David Mayerich   added 2D convolut...
251
  
d32a1854   David Mayerich   added framework f...
252
  
cfcf8619   David Mayerich   changes to filena...
253
254
  	//save a Netpbm file
  	void save_netpbm(std::string filename) {
683f216a   David Mayerich   fixed? Linux comp...
255
  		std::ofstream outfile(filename.c_str(), std::ios::out | std::ios::binary);		//open an output file
cfcf8619   David Mayerich   changes to filena...
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
  		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...
271
272
273
  		size_t w = width();
  		size_t h = height();
  		outfile << w << " " << h << (char)0x0A;			//save the width and height
cfcf8619   David Mayerich   changes to filena...
274
275
276
277
278
  		outfile << "255" << (char)0x0A;								//output the maximum value
  		outfile.write((const char*)img, size());			//write the binary data
  		outfile.close();
  	}
  
d9b2b2a8   David Mayerich   enabled basic rea...
279
280
281
282
  	void save_bmp(std::string filename) {
  		stim::save_bmp(filename, (char*)img, width(), height());
  	}
  
d32a1854   David Mayerich   added framework f...
283
284
  	//save a file
  	void save(std::string filename){
2e5e3a26   David Mayerich   added 2D convolut...
285
  #ifdef USING_OPENCV
c8c976a9   David Mayerich   replaced CImg wit...
286
287
  		//OpenCV uses an interleaved format, so convert first and then output
  		T* buffer = (T*) malloc(bytes());
a2bf1d08   David Mayerich   general bug fixes...
288
289
290
291
  
  		if(C() == 1)
  			memcpy(buffer, img, bytes());
  		else if(C() == 3)
9b766f1f   Pavel Govyadinov   completed merge f...
292
  			get_interleaved_bgr(buffer);
c8c976a9   David Mayerich   replaced CImg wit...
293
294
  		cv::Mat cvImage((int)Y(), (int)X(), cv_type(), buffer);
  		cv::imwrite(filename, cvImage);
efe7b7cc   Pavel Govyadinov   Added a detailed ...
295
  		free(buffer);
2e5e3a26   David Mayerich   added 2D convolut...
296
  #else
d9b2b2a8   David Mayerich   enabled basic rea...
297
298
299
300
301
302
303
304
305
  		stim::filename file(filename);
  		if (file.extension() == "ppm")
  			save_netpbm(filename);
  		else if (file.extension() == "bmp")
  			save_bmp(filename);
  		else {
  			std::cout << "stim::image ERROR: File type not supported without OpenCV. Make sure to link OpenCV and define USING_OPENCV" << std::endl;
  			exit(1);
  		}
2e5e3a26   David Mayerich   added 2D convolut...
306
  #endif
d32a1854   David Mayerich   added framework f...
307
308
  	}
  
ca99f951   David Mayerich   faster implementa...
309
310
311
312
313
  	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...
314
  	//create an image from an interleaved buffer
814eb271   David Mayerich   fixed problems wi...
315
  	void set_interleaved_rgb(T* buffer, size_t width, size_t height){
ca99f951   David Mayerich   faster implementa...
316
  		set_interleaved(buffer, width, height, 3);
c8c976a9   David Mayerich   replaced CImg wit...
317
  	}
fbf0ab02   David Mayerich   added support for...
318
  
814eb271   David Mayerich   fixed problems wi...
319
320
  	void set_interleaved_bgr(T* buffer, size_t width, size_t height){
  		allocate(width, height, 3);
ca99f951   David Mayerich   faster implementa...
321
322
  		T value;
  		size_t i;
c8c976a9   David Mayerich   replaced CImg wit...
323
324
325
  		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...
326
327
328
  					i = y * X() * C() + x * C() + (2-c);
  					value = buffer[i];
  					img[idx(x, y, c)] = value;
c8c976a9   David Mayerich   replaced CImg wit...
329
  				}
fbf0ab02   David Mayerich   added support for...
330
331
  			}
  		}
8b7be670   David Mayerich   implemented savin...
332
333
  	}
  
ca99f951   David Mayerich   faster implementa...
334
335
336
337
  	void set_interleaved(T* buffer, size_t width, size_t height){
  		set_interleaved_rgb(buffer, width, height);
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
338
  	void get_interleaved_bgr(T* data){
8b7be670   David Mayerich   implemented savin...
339
340
  
  		//for each channel
c8c976a9   David Mayerich   replaced CImg wit...
341
342
343
344
345
346
347
  		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...
348
349
  	}
  
9b766f1f   Pavel Govyadinov   completed merge f...
350
351
352
353
  	void get_interleaved_rgb(T* data){
  		memcpy(data, img, bytes());
  	}
  
904614c3   David Mayerich   added normalizati...
354
355
356
357
358
359
360
361
362
363
364
365
  	//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...
366
367
  	/// Return an image representing a specified channel
  	/// @param c is the channel to be returned
86dfa80b   David Mayerich   added 2D gaussian...
368
  	image<T> channel(size_t c) const {		
dbeb83f2   David Mayerich   added separable c...
369
  		image<T> r(X(), Y(), 1);				//create a new image
c8c976a9   David Mayerich   replaced CImg wit...
370
371
  		for(size_t x = 0; x < X(); x++){
  			for(size_t y = 0; y < Y(); y++){
8e2fb2b4   David Mayerich   fixed a segmentat...
372
  				r.img[r.idx(x, y, 0)] = img[idx(x, y, c)];
c8c976a9   David Mayerich   replaced CImg wit...
373
374
  			}
  		}
c8c976a9   David Mayerich   replaced CImg wit...
375
  		return r;
dbeb83f2   David Mayerich   added separable c...
376
377
378
  	}
  
  	/// Returns an std::vector containing each channel as a separate image
683f216a   David Mayerich   fixed? Linux comp...
379
380
  	std::vector< image<T> > split() const {
  		std::vector< image<T> > r;			//create an image array
dbeb83f2   David Mayerich   added separable c...
381
  		r.resize(C());						//create images for each channel
41acaf5d   David Mayerich   added a CUDA Gaus...
382
  
dbeb83f2   David Mayerich   added separable c...
383
384
385
386
  		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...
387
388
  	}
  
86dfa80b   David Mayerich   added 2D gaussian...
389
  	/// Merge a series of single-channel images into a multi-channel image
683f216a   David Mayerich   fixed? Linux comp...
390
  	void merge(std::vector< image<T> >& list) {
86dfa80b   David Mayerich   added 2D gaussian...
391
392
393
394
395
396
397
  		size_t x = list[0].width();				//calculate the size of the image
  		size_t y = list[0].height();
  		allocate(x, y, list.size());			//re-allocate the image
  		for (size_t c = 0; c < list.size(); c++)		//for each channel
  			set_channel(list[c].channel(0).data(), c);	//insert the channel into the output image
  	}
  
c8c976a9   David Mayerich   replaced CImg wit...
398
399
  	T& operator()(size_t x, size_t y, size_t c = 0){
  		return img[idx(x, y, c)];
3d0b6243   David Mayerich   fixed hsiproc bug...
400
401
402
403
404
  	}
  
  	/// Set all elements in the image to a given scalar value
  
  	/// @param v is the value used to set all values in the image
a8ad9192   David Mayerich   removed warnings ...
405
  	void set_all(T v) {														//set all elements of the image to a given value v
c8c976a9   David Mayerich   replaced CImg wit...
406
  		size_t N = size();
a8ad9192   David Mayerich   removed warnings ...
407
408
409
410
  		for (size_t n = 0; n < N; n++) img[n] = v;
  	}
  	image<T> operator=(T v){
  		set_all(v);
3d0b6243   David Mayerich   fixed hsiproc bug...
411
  		return *this;
3d0b6243   David Mayerich   fixed hsiproc bug...
412
413
  	}
  
a7ed4b2e   David Mayerich   ivote updates
414
415
416
417
418
419
420
421
422
  	/// invert the image, given a specified maximum value (ex. maxval = 255, I' = 255 - I)
  	/*image<T> invert(T maxval) {
  		image<T> result(width(), height(), channels());		//create a new image
  		size_t N = size();									//get the number of elements in the image
  		for (size_t n = 0; n < N; n++)
  			result.data()[n] = maxval - img[n];				//perform the inversion and save the result to the new image
  		return result;
  	}*/
  
5bd6c411   David Mayerich   added contrast st...
423
424
425
426
  	/// Stretch the contrast of the image such that the minimum and maximum intensity match the given values
  	image<T> stretch(T low, T high) {
  		T maxval = maxv();
  		T minval = minv();
5bd6c411   David Mayerich   added contrast st...
427
  		image<T> result = *this;				//create a new image for output
a8ad9192   David Mayerich   removed warnings ...
428
429
430
431
432
  		if (maxval == minval) {					//if the minimum and maximum values are the same, return an image composed of low
  			result = low;
  			return result;
  		}	
  		
5bd6c411   David Mayerich   added contrast st...
433
  		size_t N = size();						//get the number of values in the image
d9b2b2a8   David Mayerich   enabled basic rea...
434
435
  		T range = maxval - minval;			//calculate the current range of the image
  		T desired_range = high - low;		//calculate the desired range of the image
5bd6c411   David Mayerich   added contrast st...
436
  		for (size_t n = 0; n < N; n++) {		//for each element in the image
ec0fdb47   Mahsa Lotfollahi   changes in stretc...
437
  			result.data()[n] = desired_range * (img[n] - minval) / range + low;
5bd6c411   David Mayerich   added contrast st...
438
439
440
441
  		}
  		return result;
  	}
  
70f0baaf   Laila Saadatifard   update the image....
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
  	/// Add a border of width w with the given value around the image
  	/// @param w specifies the total size of the border
  	/// @param T is the pixel value (all channels will be the same)
  	image<T> border(size_t w, T value = 0) {
  		image<T> result(width() + w * 2, height() + w * 2, channels());						//create an output image
  		result = value;														//assign the border value to all pixels in the new image
  		for (size_t y = 0; y < height(); y++) {								//for each pixel in the original image
  			for (size_t x = 0; x < width(); x++) {
  				size_t n = (y + w) * (width() + w * 2) + x + w;				//calculate the index of the corresponding pixel in the result image
  				size_t n0 = idx(x,y);										//calculate the index for this pixel in the original image
  				result.data()[n] = img[n0];									// copy the original image to the result image afer the border area
  			}
  		}
  		return result;
  	}
  
276f9b23   David Mayerich   forced matching t...
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
  	/// Adds curcular padding for the specified number of pixels - in this case replicating the boundary pixels
  	image<T> pad_replicate(size_t p) {
  		image<T> result(width() + p * 2, height() + p * 2, channels());						//create an output image
  		result = 0;
  		//result = value;														//assign the border value to all pixels in the new image
  		for (size_t y = 0; y < height(); y++) {								//for each pixel in the original image
  			for (size_t x = 0; x < width(); x++) {
  				size_t n = (y + p) * (width() + p * 2) + x + p;				//calculate the index of the corresponding pixel in the result image
  				size_t n0 = idx(x, y);										//calculate the index for this pixel in the original image
  				result.data()[n] = img[n0];									// copy the original image to the result image afer the border area
  			}
  		}
  		size_t l = p;
  		size_t r = p + width() - 1;
  		size_t t = p;
  		size_t b = p + height() - 1;
  		for (size_t y = 0; y < p; y++) for (size_t x = l; x <= r; x++) result(x, y) = result(x, t);						//pad the top
  		for (size_t y = b + 1; y < result.height(); y++) for (size_t x = l; x <= r; x++) result(x, y) = result(x, b);	//pad the bottom
  		for (size_t y = t; y <= b; y++) for (size_t x = 0; x < l; x++) result(x, y) = result(l, y);						//pad the left
  		for (size_t y = t; y <= b; y++) for (size_t x = r+1; x < result.width(); x++) result(x, y) = result(r, y);		//pad the right
  		for (size_t y = 0; y < t; y++) for (size_t x = 0; x < l; x++) result(x, y) = result(l, t);						//pad the top left
  		for (size_t y = 0; y < t; y++) for (size_t x = r+1; x < result.width(); x++) result(x, y) = result(r, t);		//pad the top right
  		for (size_t y = b+1; y < result.height(); y++) for (size_t x = 0; x < l; x++) result(x, y) = result(l, b);		//pad the bottom left
  		for (size_t y = b+1; y < result.height(); y++) for (size_t x = r + 1; x < result.width(); x++) result(x, y) = result(r, b);		//pad the bottom right
  		return result;
  	}
  
f186dbda   Tianshu Cheng   header file for b...
485
486
487
488
489
  	/// 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...
490
  	void set_channel(T* buffer, size_t c){
6d30a707   Tianshu Cheng   add cuda/array_ad...
491
  
c8c976a9   David Mayerich   replaced CImg wit...
492
493
494
  		size_t x, y;
  		for(y = 0; y < Y(); y++){
  			for(x = 0; x < X(); x++){
2e5e3a26   David Mayerich   added 2D convolut...
495
  				img[idx(x, y, c)] = buffer[y * X() + x];
c8c976a9   David Mayerich   replaced CImg wit...
496
497
  			}
  		}
6d30a707   Tianshu Cheng   add cuda/array_ad...
498
499
  	}
  
86dfa80b   David Mayerich   added 2D gaussian...
500
  	size_t channels() const{
c8c976a9   David Mayerich   replaced CImg wit...
501
  		return C();
8b7be670   David Mayerich   implemented savin...
502
503
  	}
  
86dfa80b   David Mayerich   added 2D gaussian...
504
  	size_t width() const{
c8c976a9   David Mayerich   replaced CImg wit...
505
  		return X();
8b7be670   David Mayerich   implemented savin...
506
507
  	}
  
86dfa80b   David Mayerich   added 2D gaussian...
508
  	size_t height() const{
c8c976a9   David Mayerich   replaced CImg wit...
509
  		return Y();
3d0b6243   David Mayerich   fixed hsiproc bug...
510
511
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
512
  	T* data(){
c8c976a9   David Mayerich   replaced CImg wit...
513
  		return img;
6d30a707   Tianshu Cheng   add cuda/array_ad...
514
515
  	}
  
7b3948ab   David Mayerich   added support for...
516
  	//returns the size (number of values) of the image
c8c976a9   David Mayerich   replaced CImg wit...
517
  	size_t size(){ return C() * X() * Y(); }
d32a1854   David Mayerich   added framework f...
518
  
faef7718   David Mayerich   updates to stim::...
519
  	/// Returns the number of nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
520
  	size_t nnz(){
faef7718   David Mayerich   updates to stim::...
521
  
c8c976a9   David Mayerich   replaced CImg wit...
522
  		size_t N = X() * Y() * C();
faef7718   David Mayerich   updates to stim::...
523
  
c8c976a9   David Mayerich   replaced CImg wit...
524
525
526
  		size_t nz = 0;
  		for(size_t n = 0; n < N; n++)
  			if(img[n] != 0) nz++;
faef7718   David Mayerich   updates to stim::...
527
  
c8c976a9   David Mayerich   replaced CImg wit...
528
  		return nz;	//return the number of nonzero pixels
faef7718   David Mayerich   updates to stim::...
529
530
531
532
  
  	}
  
  	//this function returns indices of pixels that have nonzero values
9d3ba0b1   David Mayerich   added stim::hsi a...
533
  	std::vector<size_t> sparse_idx(){
faef7718   David Mayerich   updates to stim::...
534
  
c8c976a9   David Mayerich   replaced CImg wit...
535
  		std::vector<size_t> s;				//allocate an array
faef7718   David Mayerich   updates to stim::...
536
537
  		s.resize(nnz());					//allocate space in the array
  
c8c976a9   David Mayerich   replaced CImg wit...
538
539
  		size_t N = size();
  		//size_t C = channels();
faef7718   David Mayerich   updates to stim::...
540
  
c8c976a9   David Mayerich   replaced CImg wit...
541
  		//T* ptr = img.data();				//get a pointer to the image data
faef7718   David Mayerich   updates to stim::...
542
  
9d3ba0b1   David Mayerich   added stim::hsi a...
543
  		size_t i = 0;
c8c976a9   David Mayerich   replaced CImg wit...
544
545
546
547
  		for(size_t n = 0; n < N; n++){
  			if(img[n] != 0){
  				s[i] = n;
  				i++;
faef7718   David Mayerich   updates to stim::...
548
549
550
551
552
  			}
  		}
  
  		return s;			//return the index list
  	}
6d30a707   Tianshu Cheng   add cuda/array_ad...
553
  
1a224b6a   David Mayerich   fixed linux compa...
554
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
555
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
556
  	T maxv(){
c8c976a9   David Mayerich   replaced CImg wit...
557
558
  		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...
559
  
c8c976a9   David Mayerich   replaced CImg wit...
560
  		for (size_t n=0; n<N; n++){		//for every value
6d30a707   Tianshu Cheng   add cuda/array_ad...
561
  
c8c976a9   David Mayerich   replaced CImg wit...
562
563
  			if (img[n] > max_val){			//if the value is higher than the current max
  				max_val = img[n];
1a224b6a   David Mayerich   fixed linux compa...
564
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
565
  		}
1a224b6a   David Mayerich   fixed linux compa...
566
  		return max_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
567
  	}
d32a1854   David Mayerich   added framework f...
568
  
c8c976a9   David Mayerich   replaced CImg wit...
569
  	/// Returns the maximum pixel value in the image
96f9b10f   Laila Saadatifard   change the header...
570
  	T minv(){
c8c976a9   David Mayerich   replaced CImg wit...
571
572
  		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...
573
  
c8c976a9   David Mayerich   replaced CImg wit...
574
575
576
  		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...
577
  			}
6d30a707   Tianshu Cheng   add cuda/array_ad...
578
579
  		}
  
1a224b6a   David Mayerich   fixed linux compa...
580
  		return min_val;
6d30a707   Tianshu Cheng   add cuda/array_ad...
581
582
  	}
  
a2bf1d08   David Mayerich   general bug fixes...
583
584
585
586
587
588
  	/// 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...
589
  
a2bf1d08   David Mayerich   general bug fixes...
590
591
  		return r;								//return the inverted image
  	}
a2bf1d08   David Mayerich   general bug fixes...
592
  
3c17c706   David Mayerich   added a crop func...
593
594
595
596
597
598
599
600
601
602
603
604
605
606
  	image<T> crop(size_t x0, size_t y0, size_t w, size_t h){
  		image<T> result(w, h, C());								//create the output cropped image
  
  		size_t srci;
  		size_t dsti;
  		size_t line_bytes = w * C();							//calculate the number of bytes in a line
  		for (size_t yi = 0; yi < h; yi++) {						//for each row in the cropped image
  			srci = (y0 + yi) * X() * C() + x0 * C();			//calculate the source index
  			dsti = yi * w * C();								//calculate the destination index
  			memcpy(&result.img[dsti], &img[srci], line_bytes);	//copy the data
  		}
  		return result;
  	}
  
e702aa4e   David Mayerich   implemented array...
607
  	//crop regions given by an array of 1D index values
276f9b23   David Mayerich   forced matching t...
608
  	std::vector< image<T> > crop_idx(size_t w, size_t h, std::vector<size_t> idx) {
c01958ad   Pavel Govyadinov   merged the two st...
609
  		std::vector<image<T> > result(idx.size());										//create an array of image files to return
e702aa4e   David Mayerich   implemented array...
610
611
612
613
614
615
616
617
618
619
  		for (size_t i = 0; i < idx.size(); i++) {										//for each specified index point
  			size_t y = idx[i] / X();													//calculate the y coordinate from the 1D index (center of ROI)
  			size_t x = idx[i] - y * X();												//calculate the x coordinate (center of ROI)
  			y -= w / 2;																	//update x and y values to reflect the lower corner of the ROI
  			x -= h / 2;
  			result[i] = crop(x, y, w, h);												//get the cropped image and store it in the result array
  		}
  		return result;
  	}
  
6d30a707   Tianshu Cheng   add cuda/array_ad...
620
  	image<T> srgb2lab(){
c8c976a9   David Mayerich   replaced CImg wit...
621
622
  		std::cout<<"ERROR stim::image::srgb2lab - function has been broken, re-implement."<<std::endl;
  		exit(1);
6d30a707   Tianshu Cheng   add cuda/array_ad...
623
624
625
  	}
  
  	image<T> convolve2(image<T> mask){
c8c976a9   David Mayerich   replaced CImg wit...
626
627
  		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...
628
  	}
d32a1854   David Mayerich   added framework f...
629
630
  
  
f186dbda   Tianshu Cheng   header file for b...
631
  	image<T> rotate(float angle, float cx, float cy){
c8c976a9   David Mayerich   replaced CImg wit...
632
633
  		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...
634
  	}
945ee13c   Laila Saadatifard   the get_list func...
635
  
945ee13c   Laila Saadatifard   the get_list func...
636
637
  	// leila's code for non_interleaving data in 3D
  	//create an data set from an interleaved buffer
9d3ba0b1   David Mayerich   added stim::hsi a...
638
  	void set_interleaved3(T* buffer, size_t width, size_t height, size_t depth, size_t channels = 3){
c8c976a9   David Mayerich   replaced CImg wit...
639
640
  		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...
641
  	}
1a224b6a   David Mayerich   fixed linux compa...
642
  
dbeb83f2   David Mayerich   added separable c...
643
  	/// Casting operator, casts every value in an image to a different data type V
2e5e3a26   David Mayerich   added 2D convolut...
644
645
  	template<typename V>
  	operator image<V>() {
dbeb83f2   David Mayerich   added separable c...
646
647
648
  		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...
649
650
  	}
  
d32a1854   David Mayerich   added framework f...
651
652
  };
  
7b3948ab   David Mayerich   added support for...
653
654
  };		//end namespace stim
  
d32a1854   David Mayerich   added framework f...
655
656
  
  #endif