rtsUtilities.cpp 4.56 KB
#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;
}