Viewer
display a stack of images
gl_image_panel.cpp
Go to the documentation of this file.
00001 /*
00002  * This work is licensed under a Creative Commons 
00003  * Attribution-Noncommercial-Share Alike 3.0 United States License.
00004  * 
00005  *    http://creativecommons.org/licenses/by-nc-sa/3.0/us/
00006  * 
00007  * You are free:
00008  * 
00009  *    to Share - to copy, distribute, display, and perform the work
00010  *    to Remix - to make derivative works
00011  * 
00012  * Under the following conditions:
00013  * 
00014  *    Attribution. You must attribute the work in the manner specified by the
00015  *    author or licensor (but not in any way that suggests that they endorse you
00016  *    or your use of the work).
00017  * 
00018  *    Noncommercial. You may not use this work for commercial purposes.
00019  * 
00020  *    Share Alike. If you alter, transform, or build upon this work, you may
00021  *    distribute the resulting work only under the same or similar license to
00022  *    this one.
00023  * 
00024  * For any reuse or distribution, you must make clear to others the license
00025  * terms of this work. The best way to do this is by including this header.
00026  * 
00027  * Any of the above conditions can be waived if you get permission from the
00028  * copyright holder.
00029  * 
00030  * Apart from the remix rights granted under this license, nothing in this
00031  * license impairs or restricts the author's moral rights.
00032  */
00033 
00034 
00046 #include <config.h>
00047 
00048 #include <cassert>
00049 
00050 #include <qgl.h>
00051 
00052 #include "gl_image_panel.h"
00053 
00054 using namespace jwsc;
00055 
00056 GLImagePanel::GLImagePanel(QWidget* parent, const char* name)
00057     : QGLWidget(parent, name)
00058 {
00059     img_blk = NULL;
00060     current_x_index = 0;
00061     current_y_index = 0;
00062     current_z_index = 0;
00063 
00064     setMouseTracking(TRUE);
00065 
00066     draw_line = true;
00067 }
00068 
00069 void GLImagePanel::set_current_x_index(int index)
00070 {
00071     current_x_index = index;
00072     updateGL();
00073 }
00074 
00075 void GLImagePanel::set_current_y_index(int index)
00076 {
00077     current_y_index = index;
00078     updateGL();
00079 }
00080 
00081 void GLImagePanel::set_current_z_index(int index)
00082 {
00083     current_z_index = index;
00084     updateGL();
00085 }
00086 
00087 void GLImagePanel::set_draw_line(bool b)
00088 {
00089     draw_line = b;
00090     updateGL();
00091 }
00092 
00093 void GLImagePanel::set_imgblock(Imgblock_f* img_blk)
00094 {
00095     this->img_blk = img_blk;
00096     resizeGL(0, 0);
00097 }
00098 
00099 void GLImagePanel::initializeGL()
00100 {
00101     qglClearColor(black);
00102 }
00103 
00104 GLImagePanelXY::GLImagePanelXY(QWidget* parent, const char* name)
00105     : GLImagePanel(parent, name)
00106 {
00107     xy_img = NULL;
00108 
00109     default_width = 256;
00110     default_height = 256;
00111 }
00112 
00113 void GLImagePanelXY::set_current_z_index(int index)
00114 {
00115     update_xy_img = 1;
00116 
00117     GLImagePanel::set_current_z_index(index);
00118 }
00119 
00120 void GLImagePanelXY::set_draw_x_line(bool b)
00121 {
00122     draw_x_line = b;
00123 }
00124 
00125 void GLImagePanelXY::set_draw_y_line(bool b)
00126 {
00127     draw_y_line = b;
00128 }
00129 
00130 void GLImagePanelXY::set_imgblock(Imgblock_f* img_blk)
00131 {
00132     update_xy_img = 1;
00133 
00134     if (img_blk == NULL)
00135     {
00136         free(xy_img);
00137         xy_img = NULL;
00138     }
00139     else
00140     {
00141         int num_rows = img_blk->num_rows;
00142         int num_cols = img_blk->num_cols;
00143         xy_img = (float*)realloc(xy_img, 3*num_rows*num_cols*sizeof(float));
00144     }
00145 
00146     GLImagePanel::set_imgblock(img_blk);
00147 }
00148 
00149 void GLImagePanelXY::paintGL()
00150 {
00151     glClear(GL_COLOR_BUFFER_BIT);
00152 
00153     if (img_blk == NULL) return;
00154 
00155     int num_rows = img_blk->num_rows;
00156     int num_cols = img_blk->num_cols;
00157 
00158     if (update_xy_img)
00159     {
00160         update_xy_img = 0;
00161 
00162         int img = current_z_index;
00163         int xy_img_px;
00164 
00165         for (int row = 0; row < num_rows; row++)
00166         {
00167             xy_img_px = (num_cols*3)*(num_rows-row-1);
00168 
00169             for (int col = 0; col < num_cols; col++)
00170             {
00171                 xy_img[ xy_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].r;
00172                 xy_img[ xy_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].g;
00173                 xy_img[ xy_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].b;
00174             }
00175         }
00176     }
00177 
00178     glRasterPos2i(0, 0);
00179     glDrawPixels((GLsizei)num_cols, (GLsizei)num_rows, GL_RGB, GL_FLOAT,xy_img);
00180 
00181     glColor3f(1.0f, 0, 0);
00182     if (draw_line)
00183     {
00184         if (draw_x_line)
00185         {
00186             glBegin(GL_LINES);
00187             glVertex2i(0, num_rows-current_y_index-1);
00188             glVertex2i(num_cols, num_rows-current_y_index-1);
00189             glEnd();
00190         }
00191         if (draw_y_line)
00192         {
00193             glBegin(GL_LINES);
00194             glVertex2i(current_x_index, 0);
00195             glVertex2i(current_x_index, num_rows);
00196             glEnd();
00197         }
00198     }
00199 
00200     glFlush();
00201 
00202     assert(glGetError()==GL_NO_ERROR);
00203 }
00204 
00205 void GLImagePanelXY::resizeGL(int w, int h)
00206 {
00207     int width = default_width;
00208     int height = default_height;
00209 
00210     if (img_blk != NULL)
00211     {
00212         width = img_blk->num_cols;
00213         height = img_blk->num_rows;
00214 
00215     }
00216 
00217     setFixedSize(width, height);
00218 
00219     glMatrixMode(GL_PROJECTION);
00220     glLoadIdentity();
00221     gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
00222     glMatrixMode(GL_MODELVIEW);
00223     glLoadIdentity();
00224     glViewport(0, 0, (GLsizei)width, (GLsizei)height);
00225 }
00226 
00227 void GLImagePanelXY::mouseMoveEvent(QMouseEvent* event)
00228 {
00229     uint32_t x = event->x();
00230     uint32_t y = event->y();
00231     uint32_t z = current_z_index;
00232 
00233     if (x >= 0 && y >= 0)
00234     {
00235         if (img_blk != NULL && x < img_blk->num_cols && y < img_blk->num_rows)
00236         {
00237             emit mouse_position_changed(x, y, z, &(img_blk->pxls[ z ][ y ][ x ]));
00238         }
00239     }
00240 }
00241 
00242 GLImagePanelXZ::GLImagePanelXZ(QWidget* parent, const char* name)
00243     : GLImagePanel(parent, name)
00244 {
00245     xz_img = NULL;
00246 
00247     default_width = 256;
00248     default_height = 32;
00249 }
00250 
00251 void GLImagePanelXZ::set_current_y_index(int index)
00252 {
00253     update_xz_img = 1;
00254 
00255     GLImagePanel::set_current_y_index(index);
00256 }
00257 
00258 void GLImagePanelXZ::set_imgblock(Imgblock_f* img_blk)
00259 {
00260     update_xz_img = 1;
00261 
00262     if (img_blk == NULL)
00263     {
00264         free(xz_img);
00265         xz_img = NULL;
00266     }
00267     else
00268     {
00269         int num_cols = img_blk->num_cols;
00270         int num_imgs = img_blk->num_imgs;
00271         xz_img = (float*)realloc(xz_img, 3*num_cols*num_imgs*sizeof(float));
00272     }
00273 
00274     GLImagePanel::set_imgblock(img_blk);
00275 }
00276 
00277 void GLImagePanelXZ::paintGL()
00278 {
00279     glClear(GL_COLOR_BUFFER_BIT);
00280 
00281     if (img_blk == NULL) return;
00282 
00283     int num_imgs = img_blk->num_imgs;
00284     int num_cols = img_blk->num_cols;
00285 
00286     if (update_xz_img)
00287     {
00288         update_xz_img = 0;
00289 
00290         int row = current_y_index;
00291         int xz_img_px;
00292 
00293         for (int img = 0; img < num_imgs; img++)
00294         {
00295             xz_img_px = (num_cols*3)*(num_imgs-img-1);
00296 
00297             for (int col = 0; col < num_cols; col++)
00298             {
00299                 xz_img[ xz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].r;
00300                 xz_img[ xz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].g;
00301                 xz_img[ xz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].b;
00302             }
00303         }
00304     }
00305 
00306     glRasterPos2i(0, 0);
00307     glDrawPixels((GLsizei)num_cols, (GLsizei)num_imgs, GL_RGB, GL_FLOAT,xz_img);
00308 
00309     glColor3f(1.0f, 0, 0);
00310     if (draw_line)
00311     {
00312         glBegin(GL_LINES);
00313         glVertex2i(0, num_imgs-current_z_index-1);
00314         glVertex2i(num_cols, num_imgs-current_z_index-1);
00315         glEnd();
00316     }
00317 
00318     glFlush();
00319 }
00320 
00321 void GLImagePanelXZ::resizeGL(int w, int h)
00322 {
00323     int width = default_width;
00324     int height = default_height;
00325 
00326     if (img_blk != NULL)
00327     {
00328         width = img_blk->num_cols;
00329         height = img_blk->num_imgs;
00330     }
00331 
00332     setFixedSize(width, height);
00333 
00334     glMatrixMode(GL_PROJECTION);
00335     glLoadIdentity();
00336     gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
00337     glMatrixMode(GL_MODELVIEW);
00338     glLoadIdentity();
00339     glViewport(0, 0, (GLsizei)width, (GLsizei)height);
00340 }
00341 
00342 void GLImagePanelXZ::mouseMoveEvent(QMouseEvent* event)
00343 {
00344     uint32_t x = event->x();
00345     uint32_t y = current_y_index;
00346     uint32_t z = event->y();
00347     
00348     if (x >= 0 && z >= 0)
00349     {
00350         if (img_blk != NULL && x < img_blk->num_cols && z < img_blk->num_imgs)
00351         {
00352             emit mouse_position_changed(x, y, z, &(img_blk->pxls[ z ][ y ][ x ]));
00353         }
00354     }
00355 }
00356 
00357 GLImagePanelYZ::GLImagePanelYZ(QWidget* parent, const char* name)
00358     : GLImagePanel(parent, name)
00359 {
00360     yz_img = NULL;
00361 
00362     default_width = 32;
00363     default_height = 256;
00364 }
00365 
00366 void GLImagePanelYZ::set_current_x_index(int index)
00367 {
00368     update_yz_img = 1;
00369 
00370     GLImagePanel::set_current_x_index(index);
00371 }
00372 
00373 void GLImagePanelYZ::set_imgblock(Imgblock_f* img_blk)
00374 {
00375     update_yz_img = 1;
00376 
00377     if (img_blk == NULL)
00378     {
00379         free(yz_img);
00380         yz_img = NULL;
00381     }
00382     else
00383     {
00384         int num_rows = img_blk->num_rows;
00385         int num_imgs = img_blk->num_imgs;
00386         yz_img = (float*)realloc(yz_img, 3*num_rows*num_imgs*sizeof(float));
00387     }
00388 
00389     GLImagePanel::set_imgblock(img_blk);
00390 }
00391 
00392 void GLImagePanelYZ::paintGL()
00393 {
00394     glClear(GL_COLOR_BUFFER_BIT);
00395 
00396     if (img_blk == NULL) return;
00397 
00398     int num_imgs = img_blk->num_imgs;
00399     int num_rows = img_blk->num_rows;
00400 
00401     if (update_yz_img)
00402     {
00403         update_yz_img = 0;
00404 
00405         int col = current_x_index;
00406         int yz_img_px;
00407 
00408         for (int row = 0; row < num_rows; row++)
00409         {
00410             yz_img_px = (num_imgs*3)*(num_rows-row-1);
00411 
00412             for (int img = 0; img < num_imgs; img++)
00413             {
00414                 yz_img[ yz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].r;
00415                 yz_img[ yz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].g;
00416                 yz_img[ yz_img_px++ ] = img_blk->pxls[ img ][ row ][ col ].b;
00417             }
00418         }
00419     }
00420 
00421     glRasterPos2i(0, 0);
00422     glDrawPixels((GLsizei)num_imgs, (GLsizei)num_rows, GL_RGB, GL_FLOAT,yz_img);
00423 
00424     glColor3f(1.0f, 0, 0);
00425     if (draw_line)
00426     {
00427         glBegin(GL_LINES);
00428         glVertex2i(current_z_index, 0);
00429         glVertex2i(current_z_index, num_rows);
00430         glEnd();
00431     }
00432 
00433     glFlush();
00434 }
00435 
00436 void GLImagePanelYZ::resizeGL(int w, int h)
00437 {
00438     int width = default_width;
00439     int height = default_height;
00440 
00441     if (img_blk != NULL)
00442     {
00443         width = img_blk->num_imgs;
00444         height = img_blk->num_rows;
00445     }
00446 
00447     setFixedSize(width, height);
00448 
00449     glMatrixMode(GL_PROJECTION);
00450     glLoadIdentity();
00451     gluOrtho2D(0.0, (GLdouble)width, 0.0, (GLdouble)height);
00452     glMatrixMode(GL_MODELVIEW);
00453     glLoadIdentity();
00454     glViewport(0, 0, (GLsizei)width, (GLsizei)height);
00455 }
00456 
00457 void GLImagePanelYZ::mouseMoveEvent(QMouseEvent* event)
00458 {
00459     uint32_t x = current_x_index;
00460     uint32_t y = event->y();
00461     uint32_t z = event->x();
00462 
00463     if (y >= 0 && z >= 0)
00464     {
00465         if (img_blk != NULL && y < img_blk->num_rows && z < img_blk->num_imgs)
00466         {
00467             emit mouse_position_changed(x, y, z, &(img_blk->pxls[ z ][ y ][ x ]));
00468         }
00469     }
00470 }