Blame view

legacy/rtsUtilities.cpp 4.56 KB
f1402849   dmayerich   renewed commit
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
  #include "rtsUtilities.h"

  

  char* rtsInt_to_String(int num, int min_chars)

  {

  	//returns a null-terminated string representing the passed parameter

  	

  	//find out if the number is negative

  	bool negative = false;

  	if(num < 0)

  	{

  		negative = true;

  		num *= -1;

  	}

  

  	//first, count the number of digits that will be in the string

  	int num_digits = 0;

  	int dividend = 1;

  	while((num/dividend) != 0)

  	{

  		dividend*=10;

  		num_digits++;

  	}

  

  

  	//calculate the number of characters required by number

  

  	int num_chars = 0;

  

  	if(negative)

  		num_chars = num_digits+1;

  	else

  		num_chars = num_digits;

  	

  	//allocate memory for the string (add 1 for the NULL character)

  	char* result;	

  	int total_chars = 0;

  	if(num_chars > min_chars)

  		total_chars = num_chars;

  	else

  		total_chars = min_chars;

  	result =  new char[total_chars + 1];

  	//initialize the string to 0's

  	for(int i=0; i<total_chars; i++)

  		result[i] = '0';

  

  	//calculate the starting point for the character string

  	int start_char = total_chars - num_digits;

  	if(negative)

  		result[0] = '-';

  

  	//finally, calculate each character and assign it to the string

  	dividend = pow(10.0, num_digits-1);	//calculate the character from biggest to smallest

  	unsigned char digit = 0;

  	int i;

  	for(i=0; i < num_digits; i++)

  	{

  		digit = num/dividend;

  		num -= digit*dividend;

  		dividend /= 10;

  		result[start_char + i] = '0' + digit;

  	}

  

  	result[start_char + i] = NULL;

  

  	return result;

  }

  

  char** rtsGetFileList(const char* mask, unsigned int start, unsigned int end)

  {

  	//retrieves a list of names based on the supplied mask "?" is a number and the passed integers

  

  	//first, find the length of the string

  	int length = strlen(mask);

  	int num_length = 0;

  	int start_index = -1;

  	//get the number of number characters in the mask by counting "?"

  	for(int i=0; i<length; i++)

  	{

  		if(mask[i] == '?')

  		{

  			if(start_index == -1)

  				start_index = i;

  

  			num_length++;

  		}

  	}

  

  	//allocate space for a list of filenames

  	char** result = new char*[end - start + 1];

  

  	//now, for each string name

  	for(int i=0; i<(end - start) + 1; i++)

  	{

  		//create a new slot for a filename

  		char* filename = new char[length+1];

  		//find the string version of the current number

  		char* number = rtsInt_to_String(start+i, num_length);

  		//now, for each character in the filename

  		for(int j=0; j<length; j++)

  		{

  			//if we are at the mask, output the number

  			if(j >= start_index && (j < start_index + num_length))

  			{

  				filename[j] = number[j - start_index];

  			}

  			//otherwise output the mask value

  			else

  				filename[j] = mask[j];

  		}

  		filename[length] = 0;

  		result[i] = filename;

  	}

  	return result;

  }

  

  

  unsigned char* rtsCalculateDifferenceImage(int length, unsigned char* a, unsigned char* b)

  {

  	//calculates the difference between two images and returns the absolute value

  	unsigned char* result = new unsigned char[length];

  	for(int i=0; i<length; i++)

  		result[i] = (unsigned char)(abs((int)a[i]-(int)b[i]));

  	return result;

  }

  

  unsigned int rtsSumPixels(int length, unsigned char* pixels)

  {

  	//calculates the sum of all pixels in the array

  	unsigned int result = 0;

  	for(int i=0; i<length; i++)

  		result+=(unsigned int)pixels[i];

  

  	return result;

  }

  

  unsigned char* rts2DGaussian(int width, int height, double std_dev)

  {

  	//this function creates a 2D gaussian with a mean at the center of the image

  	//and a standard deviation as specified

  	unsigned char* gaussian = new unsigned char[width*height];

  	double high_value = (1.0/(2.0*RTS_PI*std_dev*std_dev))*exp(0.0);

  	for(int x = 0; x<width; x++)

  		for(int y=0; y<height; y++)

  		{

  			double exponent = exp(-((x-width/2.0)*(x-width/2.0) + (y-height/2.0)*(y-height/2.0))/(2.0*std_dev*std_dev));

  			gaussian[y*width+x] = (255.0/high_value)*(1.0/(2.0*RTS_PI*std_dev*std_dev))*exponent;

  		}

  	return gaussian;

  }

  

  unsigned char* rtsInvertImage(int width, int height, unsigned char* pixels)

  {

  	//this function inverts an image

  	unsigned char* result = new unsigned char[width*height];

  	for(int x = 0; x<width; x++)

  		for(int y=0; y<height; y++)

  		{

  			result[y*width+x] = 255 - pixels[y*width+x];

  		}

  	return result;

  }

  

  unsigned char* rtsMultiplyImages(int width, int height, unsigned char* pixels_a, unsigned char* pixels_b)

  {

  	//this function multiplies one image by another

  	unsigned char* result = new unsigned char[width*height];

  	for(int x = 0; x<width; x++)

  		for(int y=0; y<height; y++)

  		{

  			result[y*width+x] = (unsigned char)255*((pixels_a[y*width+x]/255.0) * (pixels_b[y*width+x]/255.0));

  		}

  	return result;

  }