00001 /************************************************************************** 00008 * 00009 * Source code for Real Time Image Library (libRTImage) 00010 * 00011 * Leeds Vision Group give permission for this code to be copied, modified 00012 * and distributed within the University of Leeds subject to the following 00013 * conditions:- 00014 * 00015 * - The code is not to be used for commercial gain. 00016 * - The code and use thereof will be attributed to the author where 00017 * appropriate (inluding demonstrations which rely on it's use). 00018 * - All modified, distributions of the source files will retain this header. 00019 * 00020 ****************************************************************************/ 00021 00022 #include <time.h> // time() 00023 #include <unistd.h> // usleep() 00024 #include <stdlib.h> // rand() 00025 00026 #include "RandomSource.h" 00027 00028 00029 RandomSource::RandomSource(unsigned int w, unsigned int h, unsigned int fps) 00038 { 00039 // initialise random sunber generator 00040 srand(time(NULL)); 00041 sleeptime = 1000000 / fps; 00042 00043 // default palette for this ImageSource is colour 00044 type = Image::COLOUR; 00045 } 00046 00047 00056 ImageSource& RandomSource::operator >> (ImageRGB& img) 00057 { 00058 update(img); 00059 usleep(sleeptime); 00060 00061 return *this; 00062 } 00063 00064 00073 ImageSource& RandomSource::operator >> (ImageGrey& img) 00074 { 00075 update(img); 00076 usleep(sleeptime); 00077 00078 return *this; 00079 } 00080 00081 00093 ImageSource& RandomSource::operator >> (Image& img) 00094 { 00095 (type == Image::COLOUR) ? update((ImageRGB&) img) 00096 : update((ImageGrey&) img); 00097 usleep(sleeptime); 00098 00099 return *this; 00100 } 00101 00102 00112 void RandomSource::update(ImageRGB& img) 00113 { 00114 int *d = img.data[0]; 00115 unsigned int size = img.get_width() * img.get_height(); 00116 00117 // generate random values 00118 int r = rand() % 255; 00119 int g = rand() % 255; 00120 int b = rand() % 255; 00121 00122 // set all the pixels in the Image 00123 for (unsigned int i = 0; i < size; i++, d+=3) { 00124 *d = r; 00125 *(d+1) = g; 00126 *(d+2) = b; 00127 } 00128 } 00129 00130 00140 void RandomSource::update(ImageGrey& img) 00141 { 00142 int *d = img.brightness; 00143 unsigned int size = img.get_width() * img.get_height(); 00144 00145 // generate the random value 00146 int b = rand() % 255; 00147 00148 // set all the pixels in the image 00149 for (unsigned int i = 0; i < size; i++, d++) *d = b; 00150 } 00151
1.2.8.1 written by Dimitri van Heesch,
© 1997-2001