$search
00001 /********************************************************************* 00002 * 00003 * Software License Agreement (BSD License) 00004 * 00005 * Copyright (c) 2011, Robert Bosch LLC. 00006 * All rights reserved. 00007 * 00008 * Redistribution and use in source and binary forms, with or without 00009 * modification, are permitted provided that the following conditions 00010 * are met: 00011 * 00012 * * Redistributions of source code must retain the above copyright 00013 * notice, this list of conditions and the following disclaimer. 00014 * * Redistributions in binary form must reproduce the above 00015 * copyright notice, this list of conditions and the following 00016 * disclaimer in the documentation and/or other materials provided 00017 * with the distribution. 00018 * * Neither the name of the Robert Bosch nor the names of its 00019 * contributors may be used to endorse or promote products derived 00020 * from this software without specific prior written permission. 00021 * 00022 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 00023 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 00024 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 00025 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 00026 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 00027 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 00028 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 00029 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00030 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 00031 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 00032 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 00033 * POSSIBILITY OF SUCH DAMAGE. 00034 * 00035 *********************************************************************/ 00036 #include <iostream> 00037 00038 #include "vlr/displayGL.h" 00039 00040 //#include <ippi.h> 00041 //#include <ippcc.h> 00042 #include <typeinfo> 00043 00044 namespace vlr { 00045 00046 const double DisplayGL::GRID_Z=0.001; 00047 const int DisplayGL::crossSize=7; 00048 const double DisplayGL::third = 0.333333333333333; 00049 00050 DisplayGL::DisplayGL() : 00051 GLWidget(QGLFormat(QGL::DoubleBuffer), NULL) { 00052 create(MODE_2D, DEFAULT_FPS); 00053 } 00054 00055 DisplayGL::DisplayGL(QWidget* parent, displayMode_t mode, double frame_rate, QGLFormat gl_format) : 00056 GLWidget(gl_format, parent) { 00057 create(mode, frame_rate); 00058 } 00059 00060 void DisplayGL::create(displayMode_t mode, double frame_rate) { 00061 00062 texType = GL_TEXTURE_RECTANGLE_ARB; 00063 imgBuf = NULL; 00064 texBuf = NULL; 00065 mode_ = mode; 00066 refreshTimeMS = 1000 * std::max(0.00001, frame_rate); 00067 showGridXY = true; 00068 showGridXZ = true; 00069 showGridYZ = true; 00070 bufColorFormat = GL_LUMINANCE; 00071 useColorMap = false; 00072 useTexture = true; 00073 normalize_data_ = true; 00074 gamma = 1.0; 00075 userKeyPressFunc = NULL; // TODO: remove those and make originals virtual..or needed for compatibility?!? 00076 userMousePressFunc = NULL; 00077 userMouseReleaseFunc = NULL; 00078 userMouseMoveFunc = NULL; 00079 heightScale_ = 1.0f; 00080 scale_ = 1.0f; 00081 current_slice_ = 0; 00082 slice_offset_ = 0; 00083 create_snapshot_ = false; 00084 snapshot_buf_ = NULL; 00085 color_map_red_ = gamma_map_red_; 00086 color_map_green_ = gamma_map_green_; 00087 color_map_blue_ = gamma_map_blue_; 00088 color_map_alpha_ = gamma_map_alpha_; 00089 00090 setInitialCameraPos(-90.0, 89.99, 500.0, 0, 0, 0); // TODO: make inititial distance dependend on image 00091 setCameraParams(0.01, 0.3, 0.001, 0.009, 60, 0.4, 200000); 00092 createGammaMap<float> (256, gamma, gamma_map_red_); 00093 memcpy(gamma_map_green_, gamma_map_red_, 256 * sizeof(float)); 00094 memcpy(gamma_map_blue_, gamma_map_red_, 256 * sizeof(float)); 00095 for (unsigned short i = 0; i < 256; i++) { 00096 gamma_map_alpha_[i] = 1.0f; 00097 } 00098 00099 // float phistep = 360.0/256.0; 00100 // float hlsMap[3*256]; 00101 // float phi = 0; 00102 // for(uint32_t i=0; i<3*256; i++) 00103 // { 00104 // hlsMap[i] = (float)i/255.0; i++;///3;i++;//rInt(phi); 00105 // hlsMap[i++] = 0.5; 00106 // hlsMap[i] = 1; 00107 // // phi+=phistep; 00108 // } 00109 // for(uint32_t i=0; i<256; i++) 00110 // { 00111 // hlsMap[3*i] = (float)i/255.0; 00112 // hlsMap[3*i+1] = 0.5; 00113 // hlsMap[3*i+2] = 1; 00114 // // phi+=phistep; 00115 // } 00116 // 00117 // IppiSize roi; 00118 // roi.width=256;roi.height=1; 00119 // 00120 // float tmap [3*256]; 00121 // if (ippiHLSToRGB_32f_C3R(hlsMap, 3 * 256, (float*) tmap, 3 * 256, roi) != ippStsNoErr) { 00122 // std::cout << "Fehler bei der Farbraumkonvertierung.\n"; 00123 // } 00124 // 00125 // printf(" = {\n"); 00126 // for (uint32_t j = 0; j < 8; j++) { 00127 // for (uint32_t i = 0; i < 32; i++) { 00128 // printf("%f, ", tmap[3*(j*32+i)]); 00129 // } 00130 // printf("\n"); 00131 // } 00132 // printf("}\n"); 00133 // 00134 // printf(" = {\n"); 00135 // for (uint32_t j = 0; j < 8; j++) { 00136 // for (uint32_t i = 0; i < 32; i++) { 00137 // printf("%f, ", tmap[3*(j*32+i)+1]); 00138 // } 00139 // printf("\n"); 00140 // } 00141 // printf("}\n"); 00142 // 00143 // printf(" = {\n"); 00144 // for (uint32_t j = 0; j < 8; j++) { 00145 // for (uint32_t i = 0; i < 32; i++) { 00146 // printf("%f, ", tmap[3*(j*32+i)+2]); 00147 // } 00148 // printf("\n"); 00149 // } 00150 // printf("}\n"); 00151 // exit(0); 00152 setFocusPolicy(Qt::StrongFocus); 00153 } 00154 00155 DisplayGL::~DisplayGL() { 00156 } 00157 00158 void DisplayGL::resizeEvent(QResizeEvent* event) { 00159 // correct gl viewport and redraw 00160 resizeGL(event->size().width(), event->size().height()); 00161 } 00162 00163 void DisplayGL::mousePressEvent(QMouseEvent* event) { 00164 printf("%s\n", __PRETTY_FUNCTION__); 00165 uint8_t buttons = 0; 00166 if (event->buttons() & Qt::LeftButton) { 00167 buttons |= LEFT_BUTTON; 00168 } 00169 if (event->buttons() & Qt::MidButton) { 00170 buttons |= MIDDLE_BUTTON; 00171 } 00172 if (event->buttons() & Qt::RightButton) { 00173 buttons |= RIGHT_BUTTON; 00174 } 00175 mousePress(event->x(), event->y(), buttons); 00176 00177 switch (event->modifiers()) { 00178 case Qt::ControlModifier: // selected object is affected 00179 { 00180 if (userMousePressFunc) { 00181 userMousePressFunc(event->x(), event->y(), buttons); 00182 requestRedraw(); 00183 } 00184 else { 00185 // use ctrl + left as replacement for right; useful for Mac Trackpad 00186 QMouseEvent mouse_event(event->type(), QPoint(event->x(), event->y()), Qt::RightButton, Qt::RightButton, Qt::NoModifier); 00187 GLWidget::mousePressEvent(&mouse_event); 00188 } 00189 break; 00190 } 00191 default: // let base class handle other cases 00192 GLWidget::mousePressEvent(event); 00193 break; 00194 } 00195 } 00196 00197 void DisplayGL::mouseReleaseEvent(QMouseEvent* event) { 00198 uint8_t buttons = 0; 00199 if (event->buttons() & Qt::LeftButton) { 00200 buttons |= LEFT_BUTTON; 00201 } 00202 if (event->buttons() & Qt::MidButton) { 00203 buttons |= MIDDLE_BUTTON; 00204 } 00205 if (event->buttons() & Qt::RightButton) { 00206 buttons |= RIGHT_BUTTON; 00207 } 00208 mouseRelease(event->x(), event->y(), buttons); 00209 00210 switch (event->modifiers()) { 00211 case Qt::ControlModifier: // selected object is affected 00212 { 00213 if (userMouseReleaseFunc) { 00214 userMouseReleaseFunc(event->x(), event->y(), buttons); 00215 requestRedraw(); 00216 } 00217 else { 00218 // use ctrl + left as replacement for right; useful for Mac Trackpad 00219 QMouseEvent mouse_event(event->type(), QPoint(event->x(), event->y()), Qt::RightButton, Qt::RightButton, Qt::NoModifier); 00220 GLWidget::mouseReleaseEvent(&mouse_event); 00221 } 00222 break; 00223 } 00224 default: // let base class handle other cases 00225 GLWidget::mouseReleaseEvent(event); 00226 break; 00227 } 00228 } 00229 00230 void DisplayGL::mouseMoveEvent(QMouseEvent *event) { 00231 //double x2, y2, utm_x, utm_y; 00232 //pickPoint(event->x(), event->y(), &x2, &y2); 00233 //utm_x = x2 + gui->rndf_center.x; 00234 //utm_y = y2 + gui->rndf_center.y; 00235 //gui->last_utm_x = utm_x; 00236 //gui->last_utm_y = utm_y; 00237 uint8_t buttons = 0; 00238 if (event->buttons() & Qt::LeftButton) { 00239 buttons |= LEFT_BUTTON; 00240 } 00241 if (event->buttons() & Qt::MidButton) { 00242 buttons |= MIDDLE_BUTTON; 00243 } 00244 if (event->buttons() & Qt::RightButton) { 00245 buttons |= RIGHT_BUTTON; 00246 } 00247 mouseMove(event->x(), event->y(), buttons); 00248 00249 switch (event->modifiers()) { 00250 case Qt::ControlModifier: // selected object is affected 00251 { 00252 if (userMouseMoveFunc) { 00253 userMouseMoveFunc(event->x(), event->y(), buttons); 00254 requestRedraw(); 00255 } 00256 else { 00257 // use ctrl + left as replacement for right; useful for Mac Trackpad 00258 QMouseEvent mouse_event(event->type(), QPoint(event->x(), event->y()), Qt::RightButton, Qt::RightButton, Qt::NoModifier); 00259 GLWidget::mouseMoveEvent(&mouse_event); 00260 } 00261 break; 00262 } 00263 case Qt::NoModifier: 00264 // gui->last_mouse_x = event->x(); 00265 // gui->last_mouse_y = event->y(); 00266 00267 default: // let base class handle other cases 00268 GLWidget::mouseMoveEvent(event); 00269 break; 00270 } 00271 00272 //gui->last_move_utm_x = utm_x; 00273 //gui->last_move_utm_y = utm_y; 00274 } 00275 00276 // Parses keyboard commands 00277 void DisplayGL::keyPressEvent(QKeyEvent* event) { 00278 //double x2, y2, utm_x, utm_y; 00279 00280 uint32_t key = (uint32_t) (*(event->text().toAscii().constData())); 00281 00282 // std::cout << "keyboard: "<< key << " (#"<< (int) key <<") "; 00283 00284 // switch (key) { 00285 // case '2': 00286 // setDisplayMode(MODE_2D); 00287 // std::cout << "2d mode" << std::endl; 00288 // break; 00289 // 00290 // case '3': 00291 // setDisplayMode(MODE_3D); 00292 // std::cout << "3d mode" << std::endl; 00293 // break; 00294 // 00295 // case 'g': 00296 // gamma -= .1; 00297 // gamma = std::max(0.01, gamma); 00298 // createGammaMap<float> (256, gamma, gamma_map_red_); 00299 // memcpy(gamma_map_green_, gamma_map_red_, 256 * sizeof(float)); 00300 // memcpy(gamma_map_blue_, gamma_map_red_, 256 * sizeof(float)); 00301 // color_map_red_ = gamma_map_red_; 00302 // color_map_green_ = gamma_map_green_; 00303 // color_map_blue_ = gamma_map_blue_; 00304 // color_map_alpha_ = gamma_map_alpha_; 00305 // glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 256, color_map_red_); 00306 // glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 256, color_map_green_); 00307 // glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 256, color_map_blue_); 00308 // glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 256, color_map_alpha_); 00309 // useColorMap = false; 00310 // std::cout << "decrease gamma => " << gamma << std::endl; 00311 // break; 00312 // 00313 // case 'G': 00314 // gamma += .1; 00315 // createGammaMap<float> (256, gamma, gamma_map_red_); 00316 // memcpy(gamma_map_green_, gamma_map_red_, 256 * sizeof(float)); 00317 // memcpy(gamma_map_blue_, gamma_map_red_, 256 * sizeof(float)); 00318 // color_map_red_ = gamma_map_red_; 00319 // color_map_green_ = gamma_map_green_; 00320 // color_map_blue_ = gamma_map_blue_; 00321 // color_map_alpha_ = gamma_map_alpha_; 00322 // glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 256, color_map_red_); 00323 // glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 256, color_map_green_); 00324 // glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 256, color_map_blue_); 00325 // glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 256, color_map_alpha_); 00326 // useColorMap = false; 00327 // std::cout << "increase gamma => " << gamma << std::endl; 00328 // break; 00329 // 00330 // case 'c': 00331 // if (!useColorMap) { 00332 // color_map_red_ = cmap_rb1_red_; 00333 // color_map_green_ = cmap_rb1_green_; 00334 // color_map_blue_ = cmap_rb1_blue_; 00335 // color_map_alpha_ = gamma_map_alpha_; 00336 // glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 256, color_map_red_); 00337 // glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 256, color_map_green_); 00338 // glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 256, color_map_blue_); 00339 // glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 256, color_map_alpha_); 00340 // useColorMap = true; 00341 // std::cout << "enabled color map." << std::endl; 00342 // } 00343 // else { 00344 // color_map_red_ = gamma_map_red_; 00345 // color_map_green_ = gamma_map_green_; 00346 // color_map_blue_ = gamma_map_blue_; 00347 // color_map_alpha_ = gamma_map_alpha_; 00348 // glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 256, color_map_red_); 00349 // glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 256, color_map_green_); 00350 // glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 256, color_map_blue_); 00351 // glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 256, color_map_alpha_); 00352 // useColorMap = false; 00353 // std::cout << "disabled color map." << std::endl; 00354 // } 00355 // break; 00356 // 00357 // case '+': { 00358 // scale_ *= 1.5; 00359 // std::cout << "scale factor => " << scale_ * 100 << "\%" << std::endl; 00360 // QResizeEvent* re = new QResizeEvent(QSize(width() * scale_, height() * scale_), size()); 00361 // resizeEvent(re); 00362 // } 00363 // break; 00364 // 00365 // case 'l': { 00366 // initLights(); 00367 // std::cout << "lighting enabled" << std::endl; 00368 // } 00369 // break; 00370 // 00371 // case 't': { 00372 // if(useTexture) { 00373 // useTexture=false; 00374 // std::cout << "texture disabled" << std::endl; 00375 // } 00376 // else { 00377 // useTexture=true; 00378 // std::cout << "texture enabled" << std::endl; 00379 // } 00380 // } 00381 // break; 00382 // 00383 // case '-': { 00384 // scale_ /= 1.5; 00385 // scale_ = std::max(scale_, .001f); 00386 // std::cout << "scale factor => " << scale_ * 100 << "\%" << std::endl; 00387 // QResizeEvent* re = new QResizeEvent(QSize(width() * scale_, height() * scale_), size()); 00388 // resizeEvent(re); 00389 // } 00390 // break; 00391 // 00392 // case '>': { 00393 // uint32_t channel_stride = (imgBuf->colorSpace() == ImageBase::CS_RGB ? 3 : 1); 00394 // current_slice_ = std::min(current_slice_ + 1, imgBuf->channels() / channel_stride - 1); 00395 // slice_offset_ = width() * height() * channel_stride * current_slice_; 00396 // 00397 // std::cout << "displaying slice " << current_slice_ << std::endl; 00398 // } 00399 // break; 00400 // 00401 // case '<': { 00402 // if (current_slice_ == 0) { 00403 // break; 00404 // } 00405 // uint32_t channel_stride = (imgBuf->colorSpace() == ImageBase::CS_RGB ? 3 : 1); 00406 // current_slice_--; 00407 // slice_offset_ = width() * height() * channel_stride * current_slice_; 00408 // 00409 // std::cout << "displaying slice " << current_slice_ << std::endl; 00410 // } 00411 // break; 00412 // 00413 // default: 00414 // // std::cout << "(no command)"<< std::endl; 00415 // break; 00416 // } 00417 00418 if (userKeyPressFunc) { 00419 userKeyPressFunc(key); 00420 } 00421 keyPress(key); 00422 00423 requestRedraw(); 00424 } 00425 00426 void DisplayGL::wheelEvent(QWheelEvent* event) 00427 { 00428 uint8_t buttons = 0; 00429 if (event->buttons() & Qt::LeftButton) { 00430 buttons |= LEFT_BUTTON; 00431 } 00432 if (event->buttons() & Qt::MidButton) { 00433 buttons |= MIDDLE_BUTTON; 00434 } 00435 if (event->buttons() & Qt::RightButton) { 00436 buttons |= RIGHT_BUTTON; 00437 } 00438 mouseWheel(event->x(), event->y(), buttons, event->delta()); 00439 } 00440 00441 void DisplayGL::initLights() { 00442 GLfloat light_ambient[] = { 0.0, 0.0, 0.0, 1.0 }; 00443 GLfloat light_diffuse[] = { .2, .2, .2, 1 }; 00444 GLfloat light_specular[] = { .8, .8, .8, 1 }; 00445 GLfloat light_position[] = { 0.0, 100.0, 30.0, 1.0 }; 00446 00447 GLfloat mat_diffuse[] = { 0.2, 0.2, 0.2, 1.0 }; 00448 GLfloat mat_specular[] = { 0.8, 0.8, 0.8, 1.0 }; 00449 GLfloat mat_emission[] = { 0.0, 0.0, 0.0, 1.0 }; 00450 GLfloat mat_shininess[] = { 50.0 }; 00451 00452 glLightfv(GL_LIGHT0, GL_AMBIENT, light_ambient); 00453 glLightfv(GL_LIGHT0, GL_DIFFUSE, light_diffuse); 00454 glLightfv(GL_LIGHT0, GL_SPECULAR, light_specular); 00455 glLightfv(GL_LIGHT0, GL_POSITION, light_position); 00456 glEnable( GL_LIGHT0); 00457 glEnable( GL_LIGHTING); 00458 00459 glEnable( GL_COLOR_MATERIAL); 00460 glShadeModel( GL_SMOOTH); 00461 glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse); 00462 glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular); 00463 glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess); 00464 glMaterialfv(GL_FRONT_AND_BACK, GL_EMISSION, mat_emission); 00465 glEnable( GL_NORMALIZE); 00466 glEnable( GL_AUTO_NORMAL); 00467 } 00468 00469 void DisplayGL::initializeGL(void) { 00470 glEnable( GL_DEPTH_TEST); 00471 // initLights(); 00472 00473 glHint(GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST); // Really Nice Perspective Calculations 00474 00475 glGenTextures(1, &imgTexture); 00476 glBindTexture(texType, imgTexture); 00477 glTexParameteri(texType, GL_TEXTURE_MAG_FILTER, GL_NEAREST); 00478 glTexParameteri(texType, GL_TEXTURE_MIN_FILTER, GL_NEAREST); 00479 00480 glPixelStorei(GL_UNPACK_ALIGNMENT, 1); 00481 00482 // glMatrixMode(GL_PROJECTION); 00483 // glLoadIdentity(); 00484 // glMatrixMode(GL_MODELVIEW); 00485 // glLoadIdentity(); 00486 // glOrtho(0., width(), 0., height(), -1, 1); 00487 // glViewport(0, 0, width(), height()); 00488 00489 // glClearColor(0.0,0.0,0.0,0.0); 00490 00491 glPixelMapfv(GL_PIXEL_MAP_R_TO_R, 256, color_map_red_); 00492 glPixelMapfv(GL_PIXEL_MAP_G_TO_G, 256, color_map_green_); 00493 glPixelMapfv(GL_PIXEL_MAP_B_TO_B, 256, color_map_blue_); 00494 glPixelMapfv(GL_PIXEL_MAP_A_TO_A, 256, color_map_alpha_); 00495 glPixelTransferi(GL_MAP_COLOR, GL_TRUE); 00496 00497 glClearColor(0.13, 0.17, 0.32, 1.0); 00498 00499 connect(&timer, SIGNAL(timeout()), this, SLOT(redraw(void))); 00500 00501 timer.start(DISPLAY_REFRESH_DELAY_MS); 00502 gl_initialized_ = true; 00503 } 00504 00505 void DisplayGL::paintGL() { 00506 Lock lock(mutex); 00507 00508 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); 00509 00510 if (imgBuf) { 00511 if (imgBuf->width() == 1 || imgBuf->height() == 1) { 00512 internalPaint1d(); 00513 } 00514 else if (mode_ == MODE_2D) { 00515 internalPaint2d(); 00516 } 00517 else if (mode_ == MODE_3D) { 00518 internalPaint3d(); 00519 } 00520 } 00521 else { 00522 // printf("No image buffer :-( (0x%x)\n",(uint32_t)imgBuf); 00523 } 00524 if (create_snapshot_) { 00525 glReadPixels(0, 0, width(), height(), GL_RGB, GL_UNSIGNED_BYTE, snapshot_buf_); 00526 create_snapshot_ = false; 00527 } 00528 00529 refresh_required = false; 00530 } 00531 00532 bool DisplayGL::updateImage(ImageBase& img) { 00533 Lock lock(mutex); 00534 return updateBuffer(img, &imgBuf, bufColorFormat); 00535 } 00536 00537 bool DisplayGL::updateTexture(ImageBase& img) { 00538 Lock lock(mutex); 00539 return updateBuffer(img, &texBuf, texColorFormat); 00540 } 00541 00542 void DisplayGL::removeTexture() { 00543 Lock lock(mutex); 00544 if (texBuf) { 00545 delete texBuf; 00546 texBuf = NULL; 00547 requestRedraw(); 00548 } 00549 } 00550 00551 bool DisplayGL::updateBuffer(ImageBase& img, ImageBase** dest, int& destColorFormat) { 00552 00553 // printf("id for img of type char (0x%x): %s\n", (uint32_t)&typeid(Image<char>), typeid(Image<char>).name()); 00554 // printf("id for img of type uint8_t (0x%x): %s\n", (uint32_t)&typeid(Image<uint8_t>), typeid(Image<uint8_t>).name()); 00555 // printf("id for img of type short (0x%x): %s\n", (uint32_t)&typeid(Image<short>), typeid(Image<short>).name()); 00556 // printf("id for img of type unsigned short (0x%x): %s\n", (uint32_t)&typeid(Image<unsigned short>), typeid(Image<unsigned short>).name()); 00557 // printf("id for img of type int (0x%x): %s\n", (uint32_t)&typeid(Image<int>), typeid(Image<int>).name()); 00558 // printf("id for img of type uint32_t (0x%x): %s\n", (uint32_t)&typeid(Image<uint32_t>), typeid(Image<uint32_t>).name()); 00559 // printf("id for img of type float (0x%x): %s\n", (uint32_t)&typeid(Image<float>), typeid(Image<float>).name()); 00560 // printf("id for img of type double (0x%x): %s\n", (uint32_t)&typeid(Image<double>), typeid(Image<double>).name()); 00561 // printf("id of lovely image (0x%x): %s\n", (uint32_t)&typeid(img), img.typeName().c_str()); 00562 00563 try { 00564 if (img.typeName() == typeid(Image<uint8_t> ).name()) { 00565 makeImageBuffer(*static_cast<Image<uint8_t>*> (&img), (Image<uint8_t>**) (dest), destColorFormat); 00566 data_type_ = TYPE_UCHAR; 00567 } 00568 else if (img.typeName() == typeid(Image<char> ).name()) { 00569 makeImageBuffer(*static_cast<Image<char>*> (&img), (Image<char>**) (dest), destColorFormat); 00570 data_type_ = TYPE_CHAR; 00571 } 00572 else if (img.typeName() == typeid(Image<unsigned short> ).name()) { 00573 makeImageBuffer(*static_cast<Image<unsigned short>*> (&img), (Image<unsigned short>**) (dest), destColorFormat); 00574 data_type_ = TYPE_USHORT; 00575 } 00576 else if (img.typeName() == typeid(Image<short> ).name()) { 00577 makeImageBuffer(*static_cast<Image<short>*> (&img), (Image<short>**) (dest), destColorFormat); 00578 data_type_ = TYPE_SHORT; 00579 } 00580 else if (img.typeName() == typeid(Image<uint32_t> ).name()) { 00581 makeImageBuffer(*static_cast<Image<uint32_t>*> (&img), (Image<uint32_t>**) (dest), destColorFormat); 00582 data_type_ = TYPE_UINT; 00583 } 00584 else if (img.typeName() == typeid(Image<int> ).name()) { 00585 makeImageBuffer(*static_cast<Image<int>*> (&img), (Image<int>**) (dest), destColorFormat); 00586 data_type_ = TYPE_INT; 00587 } 00588 else if (img.typeName() == typeid(Image<float> ).name()) { 00589 makeImageBuffer(*static_cast<Image<float>*> (&img), (Image<float>**) (dest), destColorFormat); 00590 static_cast<Image<float>*> (*dest)->normalize(0, 1); 00591 data_type_ = TYPE_FLOAT; 00592 } 00593 else if (img.typeName() == typeid(Image<double> ).name()) { 00594 Image<float> timg(*static_cast<Image<double>*> (&img)); 00595 makeImageBuffer(timg, (Image<float>**) (dest), destColorFormat); 00596 static_cast<Image<float>*> (*dest)->normalize(0, 1); 00597 // makeImageBuffer(*dynamic_cast<Image<double>*> (&img), (Image<double>**) (dest), destColorFormat); 00598 // dynamic_cast<Image<double>*>(imgBuf)->normalize(0,1); 00599 data_type_ = TYPE_DOUBLE; 00600 } 00601 else { 00602 *dest = NULL; 00603 return false; 00604 } 00605 } 00606 catch (...) { 00607 *dest = NULL; 00608 return false; 00609 } 00610 00611 requestRedraw(); 00612 return true; 00613 } 00614 00615 template<class T> 00616 bool DisplayGL::makeImageBuffer(Image<T>& img, Image<T>** dest, int& destColorFormat) { 00617 Image<T>* res; 00618 00619 try { 00620 res = new Image<T> (img, true, false, true); 00621 } 00622 catch (...) { 00623 return false; 00624 } 00625 switch (img.colorSpace()) { 00626 case ImageBase::CS_RGB: { 00627 cpReorganize<T, COLORORG_RGB> reorg; 00628 reorg.planar2Chunky(img, res->data(), res->paddedWidth()); 00629 destColorFormat = GL_RGB; 00630 } 00631 break; 00632 00633 case ImageBase::CS_RGB_C: { 00634 memcpy(res->data(), img.data(), res->numElements() * sizeof(T)); 00635 destColorFormat = GL_RGB; 00636 } 00637 break; 00638 00639 case ImageBase::CS_GRAY: 00640 memcpy(res->data(), img.data(), res->numElements() * sizeof(T)); 00641 destColorFormat = GL_LUMINANCE; 00642 break; 00643 00644 default: 00645 std::cout << "Display lib currently only supports RGB and gray images.\n"; 00646 if (*dest) { 00647 delete *dest; 00648 *dest = NULL; 00649 } 00650 return false; 00651 } 00652 00653 if (*dest) { 00654 delete *dest; 00655 *dest = NULL; 00656 } 00657 *dest = res; 00658 // calculate min and max value if data should be normalized or is one dimensional 00659 if ((normalize_data_ || (*dest)->width() == 1 || (*dest)->height() == 1) && *dest == imgBuf) { 00660 T minval, maxval; 00661 img.bounds(minval, maxval); 00662 minval_ = (double) minval; 00663 maxval_ = (double) maxval; 00664 if ((*dest)->width() == 1 || (*dest)->height() == 1) { 00665 if (maxval_ - minval_ != 0) { 00666 heightScale_ = std::max(img.width(), img.height()) / (maxval_ - minval_); 00667 } 00668 } 00669 } 00670 00671 return true; 00672 } 00673 00674 bool DisplayGL::snapshot(Image<uint8_t>& res) { 00675 mutex.lock(); 00676 try { 00677 snapshot_buf_ = new uint8_t[width() * height() * 3 * sizeof(uint8_t)]; 00678 } 00679 catch (...) { 00680 return false; 00681 } 00682 00683 if (!res.reformat(width(), height(), 3, width(), ImageBase::CS_RGB)) { 00684 return false; 00685 } 00686 create_snapshot_ = true; 00687 requestRedraw(); 00688 mutex.unlock(); 00689 while (create_snapshot_) { 00690 usleep(10000); 00691 } 00692 00693 cpReorganize<uint8_t, COLORORG_RGB> reorg; 00694 reorg.chunky2Planar(snapshot_buf_, width(), res); 00695 00696 delete[] snapshot_buf_; 00697 snapshot_buf_ = NULL; 00698 00699 return true; 00700 } 00701 00702 template<class T> 00703 bool DisplayGL::createGammaMap(uint32_t mapSize, double gamma, T* map) { 00704 if (!map || gamma == 0.0) { 00705 return false; 00706 } 00707 00708 double base = 1.0 / (mapSize - 1.0); 00709 00710 for (uint32_t i = 0; i < mapSize; i++) { 00711 map[i] = (T) (pow(i * base, 1.0 / gamma)); 00712 } 00713 00714 return true; 00715 } 00716 00717 const float DisplayGL::cmap_rb1_red_[256] = { 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00718 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00719 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00720 1.000000, 1.000000, 0.988235, 0.964706, 0.941176, 0.917647, 0.894118, 0.870588, 0.847059, 0.823530, 0.800000, 0.776470, 0.752941, 0.729412, 0.705882, 00721 0.682353, 0.658824, 0.635294, 0.611765, 0.588235, 0.564706, 0.541176, 0.517647, 0.494118, 0.470588, 0.447059, 0.423529, 0.400000, 0.376470, 0.352941, 00722 0.329412, 0.305882, 0.282353, 0.258823, 0.235294, 0.211765, 0.188235, 0.164706, 0.141176, 0.117647, 0.094118, 0.070588, 0.047059, 0.023529, 0.000000, 00723 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00724 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00725 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00726 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00727 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00728 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.023530, 0.047059, 0.070588, 0.094118, 0.117647, 00729 0.141176, 0.164706, 0.188235, 0.211765, 0.235295, 0.258824, 0.282353, 0.305882, 0.329412, 0.352941, 0.376470, 0.400000, 0.423530, 0.447059, 0.470588, 00730 0.494118, 0.517647, 0.541176, 0.564706, 0.588235, 0.611765, 0.635294, 0.658824, 0.682353, 0.705882, 0.729412, 0.752941, 0.776470, 0.800000, 0.823530, 00731 0.847059, 0.870588, 0.894118, 0.917647, 0.941176, 0.964706, 0.988236, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00732 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00733 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00734 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, }; 00735 00736 const float DisplayGL::cmap_rb1_green_[256] = { 0.000000, 0.023529, 0.047059, 0.070588, 0.094118, 0.117647, 0.141176, 0.164706, 0.188235, 0.211765, 0.235294, 00737 0.258824, 0.282353, 0.305882, 0.329412, 0.352941, 0.376471, 0.400000, 0.423529, 0.447059, 0.470588, 0.494118, 0.517647, 0.541176, 0.564706, 0.588235, 00738 0.611765, 0.635294, 0.658824, 0.682353, 0.705882, 0.729412, 0.752941, 0.776471, 0.800000, 0.823529, 0.847059, 0.870588, 0.894118, 0.917647, 0.941177, 00739 0.964706, 0.988235, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00740 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00741 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00742 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00743 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00744 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 0.988235, 0.964706, 0.941176, 00745 0.917647, 0.894118, 0.870588, 0.847059, 0.823529, 0.800000, 0.776470, 0.752941, 0.729412, 0.705882, 0.682353, 0.658823, 0.635294, 0.611765, 0.588235, 00746 0.564706, 0.541176, 0.517647, 0.494117, 0.470588, 0.447059, 0.423529, 0.400000, 0.376470, 0.352941, 0.329412, 0.305882, 0.282353, 0.258823, 0.235294, 00747 0.211765, 0.188235, 0.164706, 0.141176, 0.117647, 0.094117, 0.070588, 0.047059, 0.023529, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00748 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00749 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00750 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00751 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00752 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00753 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, }; 00754 00755 const float DisplayGL::cmap_rb1_blue_[256] = { 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00756 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00757 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00758 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00759 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00760 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 0.000000, 00761 0.023529, 0.047059, 0.070588, 0.094118, 0.117647, 0.141177, 0.164706, 0.188235, 0.211765, 0.235294, 0.258824, 0.282353, 0.305883, 0.329412, 0.352941, 00762 0.376471, 0.400000, 0.423530, 0.447059, 0.470588, 0.494118, 0.517647, 0.541177, 0.564706, 0.588235, 0.611765, 0.635294, 0.658824, 0.682353, 0.705882, 00763 0.729412, 0.752941, 0.776471, 0.800000, 0.823529, 0.847059, 0.870588, 0.894118, 0.917647, 0.941176, 0.964706, 0.988235, 1.000000, 1.000000, 1.000000, 00764 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00765 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00766 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00767 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00768 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 00769 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 1.000000, 0.988236, 0.964706, 0.941176, 0.917647, 0.894118, 0.870588, 0.847059, 0.823530, 00770 0.800000, 0.776470, 0.752941, 0.729412, 0.705882, 0.682353, 0.658824, 0.635294, 0.611765, 0.588235, 0.564706, 0.541176, 0.517647, 0.494118, 0.470588, 00771 0.447059, 0.423530, 0.400000, 0.376470, 0.352941, 0.329412, 0.305882, 0.282353, 0.258824, 0.235294, 0.211765, 0.188235, 0.164706, 0.141176, 0.117647, 00772 0.094118, 0.070588, 0.047059, 0.023530, 0.000000, }; 00773 00774 //const float DisplayGL::cmap_bone_red_[64] = { 00775 //0.0000, 0.0139, 0.0278, 0.0417, 0.0556, 0.0694, 0.0833, 0.0972, 0.1111, 0.1250, 0.1389, 0.1528, 0.1667, 0.1806, 0.1944, 0.2083, 00776 //0.2222, 0.2361, 0.2500, 0.2639, 0.2778, 0.2917, 0.3056, 0.3194, 0.3333, 0.3472, 0.3611, 0.3750, 0.3889, 0.4028, 0.4167, 0.4306, 00777 //0.4444, 0.4583, 0.4722, 0.4861, 0.5000, 0.5139, 0.5278, 0.5417, 0.5556, 0.5694, 0.5833, 0.5972, 0.6111, 0.6250, 0.6389, 0.6528, 00778 //0.6745, 0.6962, 0.7179, 0.7396, 0.7613, 0.7830, 0.8047, 0.8264, 0.8481, 0.8698, 0.8915, 0.9132, 0.9349, 0.9566, 0.9783, 1.0000 00779 //}; 00780 // 00781 // 00782 //const float DisplayGL::cmap_bone_green_[64] = { 00783 //0.0000, 0.0139, 0.0278, 0.0417, 0.0556, 0.0694, 0.0833, 0.0972, 0.1111, 0.1250, 0.1389, 0.1528, 0.1667, 0.1806, 0.1944, 0.2083, 00784 //0.2222, 0.2361, 0.2500, 0.2639, 0.2778, 0.2917, 0.3056, 0.3194, 0.3385, 0.3576, 0.3767, 0.3958, 0.4149, 0.4340, 0.4531, 0.4722, 00785 //0.4913, 0.5104, 0.5295, 0.5486, 0.5677, 0.5868, 0.6059, 0.6250, 0.6441, 0.6632, 0.6823, 0.7014, 0.7205, 0.7396, 0.7587, 0.7778, 00786 //0.7917, 0.8056, 0.8194, 0.8333, 0.8472, 0.8611, 0.8750, 0.8889, 0.9028, 0.9167, 0.9306, 0.9444, 0.9583, 0.9722, 0.9861, 1.0000 00787 //}; 00788 // 00789 // 00790 //const float DisplayGL::cmap_bone_blue_[64] = { 00791 //0.0052, 0.0243, 0.0434, 0.0625, 0.0816, 0.1007, 0.1198, 0.1389, 0.1580, 0.1771, 0.1962, 0.2153, 0.2344, 0.2535, 0.2726, 0.2917, 00792 //0.3108, 0.3299, 0.3490, 0.3681, 0.3872, 0.4062, 0.4253, 0.4444, 0.4583, 0.4722, 0.4861, 0.5000, 0.5139, 0.5278, 0.5417, 0.5556, 00793 //0.5694, 0.5833, 0.5972, 0.6111, 0.6250, 0.6389, 0.6528, 0.6667, 0.6806, 0.6944, 0.7083, 0.7222, 0.7361, 0.7500, 0.7639, 0.7778, 00794 //0.7917, 0.8056, 0.8194, 0.8333, 0.8472, 0.8611, 0.8750, 0.8889, 0.9028, 0.9167, 0.9306, 0.9444, 0.9583, 0.9722, 0.9861, 1.0000 00795 //}; 00796 00797 } // namespace vlr