Main Page   Compound List   File List   Compound Members   File Members  

ImageWindow.cpp

00001 #include <glow.h>
00002 
00003 #include "ImageWindow.h"
00004 
00023 void default_mouse_handler(Glow::MouseButton button,
00024                               int x, int y,
00025                               Glow::Modifiers modifiers, MouseActionType action) 
00026 {
00027     if(action == MOUSE_DOWN){
00028         if(button == Glow::leftButton){
00029                 cerr << "Left Mouse (" << x << ',' << y << ')' << endl;
00030         }     
00031         if(button == Glow::middleButton){
00032                 cerr << "Middle Mouse (" << x << ',' << y << ')' << endl;;
00033         }                                                     
00034         if(button == Glow::rightButton){
00035                 cerr << "Right Mouse (" << x << ',' << y << ')' << endl;
00036         }                                                     
00037     }
00038 /*
00039     if(action == MOUSE_UP){
00040         if(button == Glow::leftButton){
00041                 cerr << "Left Up " << x << ' ' << y << ' ';
00042         }
00043         if(button == Glow::middleButton){
00044                 cerr << "Middle Up" << x << ' ' << y << ' ';
00045         }
00046         if(button == Glow::rightButton){
00047                 cerr << "Right Up " << x << ' ' << y << ' ';
00048         }                                                    
00049     }
00050 */
00051 }
00052 
00053 void default_mouse_move_handler(int x, int y){}
00054 
00055 void default_graphics_callback(GlowComponent* parent)
00061 {
00062 }
00063 
00064 
00065 ImageWindow::ImageWindow(int w, int h, const char* title )
00066 {
00067         GlowWindowParams wp;
00068 
00069         grab_display = false ;
00070         grab_display_grey = false ;
00071         graphics_callback = default_graphics_callback ;
00072 
00073         wp.width  = width  = w;
00074         wp.height = height = h;
00075         wp.title  = title;
00076         wp.eventMask = Glow::mouseEvents | Glow::motionEvents;
00077 
00078         scalefactor = 1;
00079         // allocate enough memory for greyscale and colour images
00080         size = width * height;
00081         data = new GLubyte[size * 3];
00082         data_bk = new GLubyte[size * 3];
00083 //        data_grab = new GLubyte[size * 3];
00084         data_grab = new GLubyte[size * 4];
00085 
00086         type = Image::COLOUR;
00087         newContent = false;
00088 
00090         GlowWindow::Init(wp);
00091 
00094         mouse_press = default_mouse_handler ;
00095         mouse_move = default_mouse_move_handler ;
00096 }                               
00097 
00098 ImageWindow::~ImageWindow()
00099 {
00100         delete [] data;
00101         delete [] data_bk;
00102 }
00103 
00107 void ImageWindow::OnMouseDown(Glow::MouseButton button,
00108                               int x, int y,
00109                               Glow::Modifiers modifiers)
00110 {
00111         mouse_press(button,x,y,modifiers,MOUSE_DOWN);
00112 }
00113 
00117 void ImageWindow::OnMouseUp(Glow::MouseButton button,
00118                               int x, int y,
00119                               Glow::Modifiers modifiers)
00120 {
00121         mouse_press(button,x,y,modifiers,MOUSE_UP);
00122 }
00123 
00127 void ImageWindow::OnMouseMotion(int x, int y)
00128 {
00129         mouse_move(x,y);
00130 }
00131 
00132 void ImageWindow::OnEndPaint()
00139 {
00140 #if 0
00141         static int total = 0, framecount = 0;
00142         static int start = Glow::GetMilliseconds(), end;
00143         
00144         end = Glow::GetMilliseconds();
00145         total += end - start;
00146         if (total > 1000) {
00147                 cerr << framecount << " : ";
00148                 framecount = 0;
00149                 total -= 1000;
00150         }               
00151 
00152         start = Glow::GetMilliseconds();
00153         framecount++;
00154 #endif
00155 #if 0
00156         if (newContent) {
00157                 if (type == Image::COLOUR) {
00158                         repackRGB();
00159                 } else {
00160                         repackGrey();
00161                 }
00162 
00163 
00164                 newContent = false;
00165         }
00166 #endif
00167         if(size_changed){
00168           Reshape((int)(width*scalefactor),(int)(height*scalefactor));
00169           glPixelZoom(scalefactor,scalefactor);
00170           size_changed = false;
00171         }
00172         //So a none multiple of 4 width image is displayed properly (CJN)
00173         //Maybe should go in GLOW::Init(), but don't want to touch that! i
00174         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00175 
00176 
00177         glRasterPos2d(-1,-1) ;
00178 
00179         if(type == Image::COLOUR)       
00180             glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
00181         else
00182             glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
00183 
00184 
00185         graphics_callback(this) ;
00186 
00187         Refresh();
00188 
00189 
00190         // Grab display to buffer (if requested)
00191         if(grab_display){
00192             grab_rgb() ;
00193 
00194             grab_display = false ;
00195         }
00196 
00197         // Grab display to grey (if requested)
00198         if(grab_display_grey){
00199             grab_grey() ;
00200 
00201             grab_display_grey = false ;
00202         }
00203 
00204 }
00205 
00206 void ImageWindow::grab_rgb()
00210 {
00211     unsigned int sz, wdth, hgt ;
00212 
00213     wdth = (unsigned int)((double)width*scalefactor) ;
00214     hgt = (unsigned int)((double)height*scalefactor) ;
00215     sz = wdth * hgt ;
00216 
00217     glReadPixels(0,0, wdth, hgt,
00218                  GL_RGB, GL_UNSIGNED_BYTE, data_grab) ;
00219 
00220     GLubyte *d = data_grab + (sz - wdth) * 3 ;
00221     GLint   *p = gd_ptr;
00222 
00223     // GL expects the array to start at the bottom left,
00224     // but the data starts at the top left
00225     for(unsigned int i = 0; i < hgt; i++, d -= 6 * wdth)
00226     {
00227         for (unsigned int j = 0; j < wdth; j++)
00228         {
00229                 *(p++) = *(d++);
00230                 *(p++) = *(d++);
00231                 *(p++) = *(d++);
00232          }
00233     }
00234 
00235 
00236     cerr << "Grabbing RGB" << endl ;
00237 }
00238 
00239 void ImageWindow::grab_grey()
00244 {
00245     unsigned int sz, wdth, hgt ;
00246 
00247     wdth = (unsigned int)((double)width*scalefactor) ;
00248     hgt = (unsigned int)((double)height*scalefactor) ;
00249     sz = wdth * hgt ;
00250 
00251     glReadPixels(0,0, wdth, hgt,
00252                  GL_LUMINANCE, GL_UNSIGNED_BYTE, data_grab) ;
00253 
00254     GLubyte *d = data_grab + (sz - wdth) ;
00255     GLint   *p = gd_ptr;
00256 
00257     // GL expects the array to start at the bottom left,
00258     // but the data starts at the top left
00259     for(unsigned int i = 0; i < hgt; i++, d -= 2 * wdth)
00260     {
00261         for (unsigned int j = 0; j < wdth; j++)
00262         {
00263                 *(p++) = *(d++) ;
00264         }
00265     }
00266 
00267 
00268     cerr << "Grabbing Grey" << endl ;
00269 }
00270 
00271 
00272 void ImageWindow::operator >> (ImageRGB& img)
00273 /*
00274  * Grabs VISIBLE display to an image
00275  */
00276 {
00277     grab_display = true ;
00278     gd_ptr = (int*)img.data ;
00279 
00280     while(grab_display) { }
00281 }
00282 
00283 void ImageWindow::operator >> (ImageGrey& img)
00284 /*
00285  * Grabs VISIBLE display to a greyscale image
00286  */
00287 {
00288     grab_display_grey = true ;
00289     gd_ptr = (int*)img.brightness ;
00290 
00291     while(grab_display_grey) { }
00292 }
00293 
00294 
00295 void ImageWindow::operator << (ImageRGB& img)
00296 {
00297         GLubyte *tmp_ptr ; 
00298 
00299         pixels = (GLint *)img.data;
00300         type = Image::COLOUR;
00301         newContent = true;
00302         repackRGB();
00303 
00304         // Swap back and front buffer
00305         tmp_ptr = data_bk ;
00306         data_bk = data ;
00307         data = tmp_ptr ;
00308 }
00309 
00310 
00311 void ImageWindow::operator << (ImageGrey& img)
00312 {
00313         GLubyte *tmp_ptr ; 
00314 
00315         pixels = (GLint*)img.brightness;
00316         type = Image::GREYSCALE;
00317         newContent = true;
00318         repackGrey();
00319 
00320         // Swap back and front buffer
00321         tmp_ptr = data_bk ;
00322         data_bk = data ;
00323         data = tmp_ptr ;
00324 }
00325 
00326 
00327 void ImageWindow::operator << (Image& img)
00328 {       
00329         (type == Image::COLOUR)? *this << (ImageRGB&)img
00330                                : *this << (ImageGrey&)img;
00331         newContent = true;
00332 }
00333 
00334 
00335 void ImageWindow::repackRGB()
00336 {
00337         GLubyte *d = data_bk + (size - width) * 3 ;
00338         GLint   *p = pixels;
00339 
00340         // GL expects the array to start at the bottom left,
00341         // but the data starts at the top left
00342         for(unsigned int i = 0; i < height; i++, d -= 6 * width)
00343         {
00344                 for (unsigned int j = 0; j < width; j++)
00345                 {
00346                         *(d++) = *(p++);
00347                         *(d++) = *(p++);
00348                         *(d++) = *(p++);
00349                 }
00350         }
00351 }
00352 
00353 
00354 void ImageWindow::repackGrey()
00355 {
00356         GLubyte *d = data_bk + size - width ;
00357         GLint   *p = pixels;
00358 
00359         // GL expects the array to start at the bottom left,
00360         // but the data starts at the top left
00361         for(unsigned int i = 0; i < height; i++, d -= 2 * width)
00362         {
00363                 for (unsigned int j = 0; j < width; j++, d++, p++) 
00364                         *d = *p;
00365         }
00366 }
00367 
00368 void ImageWindow::set_zoom(float zoom)
00369 {
00370         unsigned int sz ;
00371         GLubyte *new_d ;
00372         GLubyte *old_d ;
00373 
00374         scalefactor = zoom;
00375         size_changed = true;
00376 
00377         // Re-allocate memory (for screen grabbing purposes)
00378         sz = (unsigned int)(width*zoom) * (unsigned int)(height*zoom) ;
00379 
00380 //        new_d = new GLubyte[sz * 3] ;
00381         new_d = new GLubyte[sz * 4] ;
00382         old_d = data_grab ;
00383         data_grab = new_d ;
00384         delete old_d ;
00385 }
00386 
00387 
00388 

Generated on Thu Mar 11 11:43:51 2004 for libRTImageDisplay by doxygen1.2.18