Main Page   Compound List   File List   Compound Members   File Members  

ImageSubWindow.cpp

Go to the documentation of this file.
00001 /**************************************************************************
00008  *
00009  * Source code for Real Time Image Display Library (libRTImageDisplay)
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 <glow.h>
00023 
00024 #include "ImageSubWindow.h"
00025 
00026 // Use default mouse handler defined in ImageWindow.cpp
00027 extern void default_mouse_handler(Glow::MouseButton button,
00028                               int x, int y,
00029                               Glow::Modifiers modifiers, 
00030                               MouseActionType action) ;
00031 
00032 // Use default graphics render handler defined in ImageWindow.cpp
00033 extern void default_graphics_callback(GlowComponent* parent) ;
00034 
00035 ImageSubWindow::ImageSubWindow(GlowComponent* parent,
00036                                int x, int y,
00037                                int w, int h)
00038 {
00039         GlowWindowParams wp;
00040 
00041         scalefactor = 1.0 ;
00042         size_changed = false ;
00043 
00044         parent_obj = parent ;
00045         graphics_callback = default_graphics_callback ;
00046 
00047         wp.x = x ;
00048         wp.y = y ;
00049         wp.width  = width  = w;
00050         wp.height = height = h;
00051         wp.eventMask = Glow::mouseEvents;
00052 
00053 
00054         // allocate enough memory for greyscale and colour images
00055         size = width * height;
00056         data = new GLubyte[size * 3];
00057         data_bk = new GLubyte[size * 3];
00058 
00059         type = Image::COLOUR;
00060 
00062         GlowSubwindow::Init(parent,wp);
00063 
00066         mouse_press = default_mouse_handler ;
00067 }                               
00068 
00069 ImageSubWindow::~ImageSubWindow()
00070 {
00071         delete [] data;
00072         delete [] data_bk;
00073 }
00074 
00078 void ImageSubWindow::OnMouseDown(Glow::MouseButton button,
00079                               int x, int y,
00080                               Glow::Modifiers modifiers)
00081 {
00082         mouse_press(button,x,y,modifiers,MOUSE_DOWN);
00083 }
00084 
00088 void ImageSubWindow::OnMouseUp(Glow::MouseButton button,
00089                               int x, int y,
00090                               Glow::Modifiers modifiers)
00091 {
00092         mouse_press(button,x,y,modifiers,MOUSE_UP);
00093 }
00094 
00095 
00099 void ImageSubWindow::OnEndPaint()
00100 {
00101         if(size_changed){
00102           Reshape((int)(width*scalefactor),(int)(height*scalefactor));
00103           glPixelZoom(scalefactor,scalefactor);
00104           size_changed = false;
00105         }
00106 
00107         glRasterPos2f(-1,-1) ;
00108         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
00109         if(type == Image::COLOUR)       
00110             glDrawPixels(width, height, GL_RGB, GL_UNSIGNED_BYTE, data);
00111         else
00112             glDrawPixels(width, height, GL_LUMINANCE, GL_UNSIGNED_BYTE, data);
00113 
00114         graphics_callback(parent_obj) ;
00115 
00116         Refresh();
00117 
00118 }
00119 
00120 
00121 void ImageSubWindow::operator << (ImageRGB& img)
00122 {
00123         GLubyte *tmp_ptr ; 
00124 
00125         pixels = (GLint *)img.data;
00126         type = Image::COLOUR;
00127         repackRGB();
00128 
00129         // Swap back and front buffer
00130         tmp_ptr = data_bk ;
00131         data_bk = data ;
00132         data = tmp_ptr ;
00133 }
00134 
00135 
00136 void ImageSubWindow::operator << (ImageGrey& img)
00137 {
00138         GLubyte *tmp_ptr ; 
00139 
00140         pixels = (GLint*)img.brightness;
00141         type = Image::GREYSCALE;
00142         repackGrey();
00143 
00144         // Swap back and front buffer
00145         tmp_ptr = data_bk ;
00146         data_bk = data ;
00147         data = tmp_ptr ;
00148 }
00149 
00150 
00151 void ImageSubWindow::operator << (Image& img)
00152 {       
00153         (type == Image::COLOUR)? *this << (ImageRGB&)img
00154                                : *this << (ImageGrey&)img;
00155 }
00156 
00157 
00158 void ImageSubWindow::repackRGB()
00159 {
00160         GLubyte *d = data_bk + (size - width) * 3 ;
00161         GLint   *p = pixels;
00162 
00163         // GL expects the array to start at the bottom left,
00164         // but the data starts at the top left
00165         for(unsigned int i = 0; i < height; i++, d -= 6 * width)
00166         {
00167                 for (unsigned int j = 0; j < width; j++)
00168                 {
00169                         *(d++) = *(p++);
00170                         *(d++) = *(p++);
00171                         *(d++) = *(p++);
00172                 }
00173         }
00174 }
00175 
00176 
00177 void ImageSubWindow::repackGrey()
00178 {
00179         GLubyte *d = data_bk + size - width ;
00180         GLint   *p = pixels;
00181 
00182         // GL expects the array to start at the bottom left,
00183         // but the data starts at the top left
00184         for(unsigned int i = 0; i < height; i++, d -= 2 * width)
00185         {
00186                 for (unsigned int j = 0; j < width; j++, d++, p++) 
00187                         *d = *p;
00188         }
00189 }
00190 
00191 void ImageSubWindow::set_zoom(float zoom)
00192 {
00193         scalefactor = zoom;
00194         size_changed = true;
00195 }

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