Blame view

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