00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046 #include "Image/ByteImage.h"
00047
00048 #include <string.h>
00049
00050
00051
00052 #ifdef USE_QTGUI
00053
00054 #include "gui/Qt/QtApplicationHandler.h"
00055 #include "gui/Qt/QtMainWindow.h"
00056
00057 CApplicationHandlerInterface *CreateApplicationHandler(int argc, char **argv)
00058 {
00059 return new CQtApplicationHandler(argc, argv);
00060 }
00061
00062 CMainWindowInterface *CreateMainWindow(int x, int y, int width, int height, const char *title)
00063 {
00064 return new CQtMainWindow(x, y, width, height, title);
00065 }
00066
00067 #include <qapplication.h>
00068 #include <qfiledialog.h>
00069
00070 #if QT_VERSION >= 0x040000
00071 #define GET_ASCII toAscii
00072 #else
00073 #define GET_ASCII ascii
00074 #endif
00075
00076 static char app_name[] = "IVT_APPLICATION\0";
00077 static int my_argc = 1;
00078 static char *my_argv[2] = { app_name, NULL};
00079
00080
00081 bool FileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length)
00082 {
00083 QApplication *pApp = NULL;
00084 if (qApp == NULL)
00085 {
00086 pApp = new QApplication(my_argc, my_argv);
00087 }
00088
00089 char filter_str[1024];
00090 char *fs = filter_str;
00091 *fs = 0;
00092
00093 for (int i = 0; i < num_filter; i++)
00094 {
00095 if (i == num_filter - 1)
00096 {
00097 sprintf(fs, "%s (%s)", filter[(2*i)], filter[(2*i) + 1]);
00098 fs += strlen(filter[(2*i)]) + strlen(filter[(2*i) + 1]) + 3;
00099 }
00100 else
00101 {
00102 sprintf(fs, "%s (%s);;", filter[(2*i)], filter[(2*i) + 1]);
00103 fs += strlen(filter[(2*i)]) + strlen(filter[(2*i) + 1]) + 5;
00104 }
00105 }
00106 *fs++ = 0;
00107
00108 if (save_dialog)
00109 {
00110 #if QT_VERSION >= 0x040000
00111 QString s = QFileDialog::getSaveFileName(0, caption, NULL, filter_str);
00112 #else
00113 QString s = QFileDialog::getSaveFileName(QString(filename), filter_str, 0, "Save File Dialog", caption);
00114 #endif
00115
00116 if (s.length() > 0)
00117 {
00118 strncpy(filename, s.GET_ASCII(), max_length);
00119
00120 if (pApp)
00121 {
00122 delete pApp;
00123 pApp = NULL;
00124 }
00125
00126 return true;
00127 }
00128 }
00129 else
00130 {
00131 #if QT_VERSION >= 0x040000
00132 QString s = QFileDialog::getOpenFileName(0, caption, NULL, filter_str);
00133 #else
00134 QString s = QFileDialog::getOpenFileName(QString(filename), filter_str, 0, "Open File Dialog", caption);
00135 #endif
00136
00137 if (s.length() > 0)
00138 {
00139 strncpy(filename, s.GET_ASCII(), max_length);
00140
00141 if (pApp)
00142 {
00143 delete pApp;
00144 pApp = NULL;
00145 }
00146
00147 return true;
00148 }
00149 }
00150
00151 if (pApp)
00152 {
00153 delete pApp;
00154 pApp = NULL;
00155 }
00156
00157 return false;
00158 }
00159
00160 #include <qimage.h>
00161
00162
00163 bool LoadImageFromFile(const char *filename, CByteImage *pImage)
00164 {
00165 QApplication *pApp = NULL;
00166 if (qApp == NULL)
00167 {
00168 int argc = 1;
00169 char *argv[] = {app_name, NULL};
00170
00171 pApp = new QApplication(argc, argv);
00172 }
00173
00174 QImage img(filename, 0);
00175
00176 if (!img.isNull())
00177 {
00178 if (img.depth() != 8 && img.depth() != 24 && img.depth() != 32)
00179 {
00180 printf("error: LoadImageFromFile: image '%s' has an unsupported bit depth of %d bpp\n", filename, img.depth());
00181 return false;
00182 }
00183
00184 int width = img.width();
00185 int height = img.height();
00186
00187 if (pImage->m_bOwnMemory)
00188 delete [] pImage->pixels;
00189
00190 pImage->width = width;
00191 pImage->height = height;
00192 pImage->bytesPerPixel = (img.depth() == 8 ? 1 : 3);
00193 pImage->type = (img.depth() <= 8 ? CByteImage::eGrayScale : CByteImage::eRGB24);
00194 pImage->pixels = new unsigned char[pImage->bytesPerPixel * width*height];
00195 pImage->m_bOwnMemory = true;
00196
00197 uchar *ptr = img.bits();
00198 if (img.depth() == 32)
00199 {
00200 unsigned char *output = pImage->pixels;
00201
00202 int size = width * height;
00203 for (int i = 0; i < size; i++)
00204 {
00205 output[3*i ] = ptr[4*i + 2];
00206 output[3*i + 1] = ptr[4*i + 1];
00207 output[3*i + 2] = ptr[4*i ];
00208 }
00209 }
00210 else
00211 {
00212 memcpy(pImage->pixels, ptr, pImage->bytesPerPixel * width*height);
00213 }
00214
00215 if (pApp)
00216 {
00217 delete pApp;
00218 pApp = NULL;
00219 }
00220
00221 return true;
00222 }
00223
00224 if (pApp)
00225 {
00226 delete pApp;
00227 pApp = NULL;
00228 }
00229
00230 return false;
00231 }
00232
00233 #else
00234
00235
00236 #if defined(USE_GTKGUI) && !defined(USE_REMOTEGUI)
00237
00238 #include "gui/GTK/GTKApplicationHandler.h"
00239 #include "gui/GTK/GTKMainWindow.h"
00240
00241 CApplicationHandlerInterface *CreateApplicationHandler(int argc, char **argv)
00242 {
00243 return new CGTKApplicationHandler(argc, argv);
00244 }
00245
00246 CMainWindowInterface *CreateMainWindow(int x, int y, int width, int height, const char *title)
00247 {
00248 return new CGTKMainWindow(x, y, width, height, title);
00249 }
00250
00251
00252 bool FileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length)
00253 {
00254
00255 GtkWidget *dialog;
00256
00257 if (save_dialog)
00258 {
00259 dialog = gtk_file_chooser_dialog_new (caption,
00260 NULL,
00261 GTK_FILE_CHOOSER_ACTION_SAVE,
00262 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00263 GTK_STOCK_SAVE, GTK_RESPONSE_ACCEPT,
00264 NULL);
00265 }
00266 else
00267 {
00268 dialog = gtk_file_chooser_dialog_new (caption,
00269 NULL,
00270 GTK_FILE_CHOOSER_ACTION_OPEN,
00271 GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
00272 GTK_STOCK_OPEN, GTK_RESPONSE_ACCEPT,
00273 NULL);
00274 }
00275
00276 gtk_file_chooser_set_filename(GTK_FILE_CHOOSER(dialog), filename);
00277
00278 for (int i = 0; i < num_filter; i++)
00279 {
00280 GtkFileFilter *f = gtk_file_filter_new();
00281 gtk_file_filter_set_name(f, filter[2*i]);
00282 gtk_file_filter_add_pattern(f, filter[2*i+1]);
00283 gtk_file_chooser_add_filter(GTK_FILE_CHOOSER(dialog), f);
00284 }
00285
00286 if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT)
00287 {
00288 char *file;
00289 file = gtk_file_chooser_get_filename(GTK_FILE_CHOOSER(dialog));
00290 strncpy(filename, file, max_length);
00291 g_free (file);
00292
00293 gtk_widget_destroy (dialog);
00294
00295 return true;
00296 }
00297
00298 gtk_widget_destroy (dialog);
00299
00300 return false;
00301 }
00302
00303
00304
00305 bool LoadImageFromFile(const char *filename, CByteImage *pImage)
00306 {
00307 bool success = false;
00308
00309 GdkPixbuf *pixbuf = gdk_pixbuf_new_from_file(filename, NULL);
00310 if (pixbuf)
00311 {
00312 int channels = gdk_pixbuf_get_n_channels(pixbuf);
00313 int bits_per_sample = gdk_pixbuf_get_bits_per_sample(pixbuf);
00314 unsigned char *pixels = gdk_pixbuf_get_pixels(pixbuf);
00315 int width = gdk_pixbuf_get_width(pixbuf);
00316 int height = gdk_pixbuf_get_height(pixbuf);
00317 int rowstride = gdk_pixbuf_get_rowstride(pixbuf);
00318
00319 if (bits_per_sample == 8 && (channels == 1 || channels == 3 || channels == 4))
00320 {
00321 if (pImage->m_bOwnMemory)
00322 delete [] pImage->pixels;
00323
00324 pImage->width = width;
00325 pImage->height = height;
00326 pImage->bytesPerPixel = channels;
00327 pImage->type = (channels == 3 ? CByteImage::eGrayScale : CByteImage::eRGB24);
00328 pImage->pixels = new unsigned char[pImage->bytesPerPixel * width * height];
00329 pImage->m_bOwnMemory = true;
00330
00331 if (channels == 1)
00332 {
00333 for (int y = 0; y < height; y++)
00334 {
00335 memcpy(&pImage->pixels[y*width], pixels, width);
00336 pixels += rowstride;
00337 }
00338 }
00339 else
00340 {
00341 if (channels == 3)
00342 {
00343 for (int y = 0; y < height; y++)
00344 {
00345 memcpy(&pImage->pixels[3*y*width], pixels, 3*width);
00346 pixels += rowstride;
00347 }
00348 }
00349 else
00350 {
00351
00352 for (int y = 0; y < height; y++)
00353 {
00354 for (int x = 0; x < width; x++)
00355 {
00356 pImage->pixels[3*(y*width + x)] = pixels[4*x];
00357 pImage->pixels[3*(y*width + x)+1] = pixels[4*x+1];
00358 pImage->pixels[3*(y*width + x)+2] = pixels[4*x+2];
00359 }
00360
00361 pixels += rowstride;
00362 }
00363 }
00364 }
00365
00366 success = true;
00367 }
00368
00369 }
00370
00371 return success;
00372 }
00373
00374 #else
00375
00376
00377 #if defined(WIN32) && !defined(USE_REMOTEGUI)
00378
00379 #include "gui/win32/Win32ApplicationHandler.h"
00380 #include "gui/win32/Win32MainWindow.h"
00381
00382 CApplicationHandlerInterface *CreateApplicationHandler(int argc, char **argv)
00383 {
00384 return new CWin32ApplicationHandler();
00385 }
00386
00387 CMainWindowInterface *CreateMainWindow(int x, int y, int width, int height, const char *title)
00388 {
00389 return new CWin32MainWindow(x, y, width, height, title);
00390 }
00391
00392 #include <windows.h>
00393
00394
00395 bool FileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length)
00396 {
00397 char filter_str[1024];
00398 char *fs = filter_str;
00399 *fs = 0;
00400
00401 for (int i = 0; i < num_filter; i++)
00402 {
00403 sprintf(fs, "%s", filter[(2*i)]);
00404 fs += strlen(filter[(2*i)]) + 1;
00405
00406 sprintf(fs, "%s", filter[(2*i) + 1]);
00407 fs += strlen(filter[(2*i) + 1]) + 1;
00408 }
00409 *fs++ = 0;
00410
00411 OPENFILENAME ofn;
00412 memset(&ofn, 0, sizeof(OPENFILENAME));
00413
00414 ofn.lStructSize = sizeof(OPENFILENAME);
00415 ofn.hwndOwner = NULL;
00416 ofn.hInstance = NULL;
00417 ofn.lpstrFilter = filter_str;
00418 ofn.lpstrCustomFilter = NULL;
00419 ofn.nMaxCustFilter = 0;
00420 ofn.nFilterIndex = 1;
00421 ofn.lpstrFile = filename;
00422 ofn.nMaxFile = max_length;
00423 ofn.lpstrFileTitle = NULL;
00424 ofn.nMaxFileTitle = 0;
00425 ofn.lpstrInitialDir = NULL;
00426 ofn.lpstrTitle = caption;
00427 ofn.Flags = 0;
00428 ofn.nFileOffset = 0;
00429 ofn.nFileExtension = 0;
00430 ofn.lpstrDefExt = NULL;
00431 ofn.lCustData = 0;
00432 ofn.lpfnHook = NULL;
00433 ofn.lpTemplateName = NULL;
00434
00435 if (save_dialog)
00436 {
00437 if (GetSaveFileName(&ofn))
00438 {
00439 return true;
00440 }
00441 }
00442 else
00443 {
00444 if (GetOpenFileName(&ofn))
00445 {
00446 return true;
00447 }
00448 }
00449
00450 return false;
00451 }
00452
00453 #include <olectl.h>
00454
00455
00456 bool LoadImageFromFile(const char *filename, CByteImage *pImage)
00457 {
00458 HDC hdcTemp;
00459 HBITMAP hbmpTemp;
00460 IPicture *pPicture;
00461 OLECHAR wszPath[MAX_PATH+1];
00462 long lWidth;
00463 long lHeight;
00464 long lWidthPixels;
00465 long lHeightPixels;
00466
00467 MultiByteToWideChar(CP_ACP, 0, filename, -1, wszPath, MAX_PATH);
00468 HRESULT hr = OleLoadPicturePath(wszPath, 0, 0, 0, IID_IPicture, (void**)&pPicture);
00469
00470 if(FAILED(hr))
00471 return false;
00472
00473 hdcTemp = CreateCompatibleDC(GetDC(0));
00474 if(!hdcTemp)
00475 {
00476 pPicture->Release();
00477 return false;
00478 }
00479
00480 pPicture->get_Width(&lWidth);
00481 lWidthPixels = MulDiv(lWidth, GetDeviceCaps(hdcTemp, LOGPIXELSX), 2540);
00482 pPicture->get_Height(&lHeight);
00483 lHeightPixels = MulDiv(lHeight, GetDeviceCaps(hdcTemp, LOGPIXELSY), 2540);
00484
00485
00486 BITMAPINFO bi = {0};
00487 DWORD *pBits = 0;
00488
00489 bi.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
00490 bi.bmiHeader.biBitCount = 32;
00491 bi.bmiHeader.biWidth = lWidthPixels;
00492 bi.bmiHeader.biHeight = lHeightPixels;
00493 bi.bmiHeader.biCompression = BI_RGB;
00494 bi.bmiHeader.biPlanes = 1;
00495
00496
00497 hbmpTemp = CreateDIBSection(hdcTemp, &bi, DIB_RGB_COLORS, (void**)&pBits, 0, 0);
00498
00499 if(!hbmpTemp)
00500 {
00501 DeleteDC(hdcTemp);
00502 pPicture->Release();
00503 return false;
00504 }
00505
00506 SelectObject(hdcTemp, hbmpTemp);
00507
00508
00509 pPicture->Render(hdcTemp, 0, 0, lWidthPixels, lHeightPixels, 0, 0, lWidth, lHeight, 0);
00510
00511
00512 if (pImage->m_bOwnMemory)
00513 delete [] pImage->pixels;
00514
00515 pImage->width = lWidthPixels;
00516 pImage->height = lHeightPixels;
00517 pImage->bytesPerPixel = 3;
00518 pImage->type = CByteImage::eRGB24;
00519 pImage->pixels = new unsigned char[pImage->bytesPerPixel * lWidthPixels*lHeightPixels];
00520 pImage->m_bOwnMemory = true;
00521
00522 unsigned char *output = pImage->pixels;
00523
00524
00525 for(long i = 0; i < lWidthPixels * lHeightPixels; i++)
00526 {
00527 BYTE* pPixel = (BYTE*)(&pBits[i]);
00528 output[3*i] = pPixel[2];
00529 output[3*i + 1] = pPixel[1];
00530 output[3*i + 2] = pPixel[0];
00531 }
00532
00533 DeleteObject(hbmpTemp);
00534 DeleteDC(hdcTemp);
00535
00536 pPicture->Release();
00537
00538 return true;
00539 }
00540
00541 #else
00542
00543
00544 #ifdef USE_COCOAGUI
00545
00546 #include "gui/Cocoa/CocoaApplicationHandler.h"
00547 #include "gui/Cocoa/CocoaMainWindow.h"
00548
00549 CApplicationHandlerInterface *CreateApplicationHandler(int argc, char **argv)
00550 {
00551 return new CCocoaApplicationHandler();
00552 }
00553
00554 CMainWindowInterface *CreateMainWindow(int x, int y, int width, int height, const char *title)
00555 {
00556 return new CCocoaMainWindow(x, y, width, height, title);
00557 }
00558
00559
00560 extern "C"
00561 {
00562 bool CocoaFileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length);
00563 bool CocoaLoadImage(const char *filename, unsigned char **ptr, int *width, int *height, int *type);
00564 void CocoaFreeImageMem(unsigned char *ptr);
00565 }
00566
00567
00568 bool FileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length)
00569 {
00570 char buf[64];
00571
00572 char **cocoa_filters = NULL;
00573 if (filter != NULL) {
00574 cocoa_filters = new char*[num_filter];
00575 for (int i = 0; i < num_filter; i++)
00576 {
00577 sscanf(filter[2*i + 1], "*.%s", buf);
00578 cocoa_filters[i] = new char[strlen(buf) + 1];
00579 strcpy(cocoa_filters[i], buf);
00580 }
00581 }
00582
00583 bool result = CocoaFileDialog(save_dialog, (const char**)cocoa_filters, num_filter, caption, filename, max_length);
00584
00585 if (cocoa_filters != NULL)
00586 {
00587 for (int i = 0; i < num_filter; i++)
00588 {
00589 delete [] cocoa_filters[i];
00590 }
00591 delete [] cocoa_filters;
00592 }
00593
00594 return result;
00595 }
00596
00597
00598 bool LoadImageFromFile(const char *filename, CByteImage *pImage)
00599 {
00600 unsigned char *ptr = NULL;
00601 int width = -1;
00602 int height = -1;
00603 int type = -1;
00604
00605 if (CocoaLoadImage(filename, &ptr, &width, &height, &type))
00606 {
00607 if (pImage->m_bOwnMemory)
00608 delete [] pImage->pixels;
00609
00610 pImage->width = width;
00611 pImage->height = height;
00612 pImage->bytesPerPixel = (type == 1 ? 1 : 3);
00613 pImage->type = (type == 1 ? CByteImage::eGrayScale : CByteImage::eRGB24);
00614 pImage->pixels = new unsigned char[pImage->bytesPerPixel * width * height];
00615 pImage->m_bOwnMemory = true;
00616
00617 memcpy(pImage->pixels, ptr, pImage->bytesPerPixel * width * height);
00618
00619 CocoaFreeImageMem(ptr);
00620
00621 return true;
00622 }
00623
00624 return false;
00625 }
00626
00627 #else
00628
00629
00630 #ifdef USE_REMOTEGUI
00631
00632 #include "gui/Remote/RemoteApplicationHandler.h"
00633 #include "gui/Remote/RemoteMainWindow.h"
00634
00635 CApplicationHandlerInterface *CreateApplicationHandler(int argc, char **argv)
00636 {
00637 return new CRemoteApplicationHandler(argc, argv);
00638 }
00639
00640 CMainWindowInterface *CreateMainWindow(int x, int y, int width, int height, const char *title)
00641 {
00642 CRemoteApplicationHandler *app_handler = CRemoteApplicationHandler::GetApplicationHandler();
00643
00644 if (!app_handler)
00645 return NULL;
00646
00647 return app_handler->CreateMainWindow(x, y, width, height, title);
00648 }
00649
00650
00651 bool FileDialog(bool save_dialog, const char **filter, int num_filter, const char *caption, char *filename, int max_length)
00652 {
00653
00654 return false;
00655 }
00656
00657
00658 bool LoadImageFromFile(const char *filename, CByteImage *pImage)
00659 {
00660
00661 return false;
00662 }
00663
00664 #endif
00665
00666
00667 #endif
00668
00669
00670 #endif
00671
00672
00673 #endif
00674
00675
00676 #endif
00677
00678
00679