00001 #ifndef Impala_Visualization_ImagesWindow_h
00002 #define Impala_Visualization_ImagesWindow_h
00003
00004 #include "Core/Geometry/VxRectangle.h"
00005 #include "Core/ImageSet/ImageSet.h"
00006 #include "Core/Table/TableIds.h"
00007 #include "Visualization/Window.h"
00008 #include "Visualization/ImagesListener.h"
00009 #include "Visualization/RgbImage.h"
00010
00011 namespace Impala
00012 {
00013 namespace Visualization
00014 {
00015
00016
00017 class ImagesWindow : public Window
00018 {
00019 public:
00020
00021 ImagesWindow(int x, int y, int width, int height, bool is2d=true) :
00022 Window(x, y, width, height, is2d)
00023 {
00024 mImagesListener = 0;
00025 mViewListener = 0;
00026 mViewWidth = -1;
00027 }
00028
00029 ImagesWindow(OglGui::Window* parent, int width, int height, bool is2d=true) :
00030 Window(parent, width, height, is2d)
00031 {
00032 mImagesListener = 0;
00033 mViewListener = 0;
00034 mViewWidth = -1;
00035 }
00036
00037 ImagesWindow(OglGui::Window* parent, int x, int y, int width, int height,
00038 bool is2d=true) :
00039 Window(parent, x, y, width, height, is2d)
00040 {
00041 mImagesListener = 0;
00042 mViewListener = 0;
00043 mViewWidth = -1;
00044 }
00045
00046 virtual
00047 ~ImagesWindow()
00048 {
00049 #ifdef DESTRUCT_VERBOSE
00050 printf("ImagesWindow destructor\n");
00051 #endif
00052 RemoveImages();
00053 }
00054
00055 void
00056 SetImagesListener(ImagesListener* listener, void* listenerData = 0)
00057 {
00058 mImagesListener = listener;
00059 mImagesListenerData = listenerData;
00060 }
00061 void
00062 SetImagesListener(ImagesListener* listener, int listenerData)
00063 {
00064 SetImagesListener(listener, (void*) listenerData);
00065 }
00066
00067 void
00068 SetViewListener(OglGui::ViewListener* listener)
00069 {
00070 mViewListener = listener;
00071 }
00072
00073 void SetViewSize(int w, int h)
00074 {
00075 mViewWidth = w;
00076 mViewHeight = h;
00077 }
00078
00079 void GetDefaultViewSizes(int& w, int& h)
00080 {
00081 w = mViewWidth;
00082 h = mViewHeight;
00083 }
00084
00085
00086
00087
00088 template <class ArrayT>
00089 void
00090 AddImage(ArrayT* im, std::string displayMode, std::string caption,
00091 bool ownImage, bool intelliScroll = false, int userIdx = -1)
00092 {
00093 if (!im) {
00094 std::cout << "ERROR: invalid image to ImagesWindow::AddImage" << std::endl;
00095 return;
00096 }
00097
00098 int idx = mImages.size();
00099 if (userIdx < 0)
00100 userIdx = idx;
00101
00102
00103
00104
00105
00106
00107 double scale = 1.0;
00108 if (mViewWidth != -1)
00109 {
00110 double scaleX = ((double) mViewWidth) / im->CW();
00111 double scaleY = ((double) mViewHeight) / im->CH();
00112 scale = Min(scaleX, scaleY);
00113 }
00114 RgbImage* rgbIm = 0;
00115 if (displayMode == std::string("Direct"))
00116 {
00117
00118
00119 rgbIm = new RgbImage((Array2dVec3UInt8*) im, caption, scale,
00120 ownImage, userIdx);
00121 }
00122 else
00123 {
00124 Array2dVec3UInt8* dispIm =
00125 Core::Array::ArrayCreate<Array2dVec3UInt8>(im->CW(), im->CH(),
00126 0, 0);
00127 Core::Array::GetRgbPixels(im, dispIm->CPB(0, 0), displayMode);
00128 if (ownImage)
00129 delete im;
00130 rgbIm = new RgbImage(dispIm, caption, scale, true, userIdx);
00131 }
00132 mImages.push_back(rgbIm);
00133 ImageAdded(idx, intelliScroll);
00134 }
00135
00136 void
00137 AddImage(int id, Core::ImageSet::ImageSet* imSet)
00138 {
00139 AddImage(imSet->GetImage(id), std::string("Direct"),
00140 FileNameBase(imSet->GetFile(id)), true);
00141 }
00142
00143 void
00144 ReplaceImages(Core::Table::TableIds* ids, Core::ImageSet::ImageSet* imSet)
00145 {
00146 RemoveImages();
00147 for (int i=0 ; i<ids->Size() ; i++)
00148 AddImage(ids->Get1(i), imSet);
00149 }
00150
00151 void
00152 MoveImageTo(int id1, int id2)
00153 {
00154 if (id1 < 0 || id1 >= mImages.size())
00155 return;
00156
00157 if (id2 < 0)
00158 id2 = 0;
00159
00160 if (id2 > mImages.size())
00161 id2 = mImages.size();
00162
00163 if (id1 == id2)
00164 return;
00165
00166 RgbImage* im = mImages[id1];
00167 mImages.erase(mImages.begin() + id1);
00168 if (id1 < id2)
00169 id2--;
00170 mImages.insert(mImages.begin() + id2, im);
00171 }
00172
00173 void
00174 RemoveImages()
00175 {
00176 HideViewers();
00177 for (int i=0 ; i<mImages.size() ; i++)
00178 delete mImages[i];
00179 mImages.clear();
00180 ImagesRemoved();
00181 }
00182
00183 int
00184 GetNrImages()
00185 {
00186 return mImages.size();
00187 }
00188
00189
00190 virtual void
00191 MouseFunc(int msg, int but, int state, int x, int y)
00192 {
00193 Window::MouseFunc(msg, but, state, x, y);
00194
00195
00196 if (mImagesListener && (msg==oglMouseDown) && (but==oglLeftButton))
00197 {
00198 View* view = FindView(x, y);
00199 if (view)
00200 {
00201 int imIdx = GetViewIdx(view);
00202 mImagesListener->ImageSelectionEvent(this, imIdx,
00203 mImagesListenerData);
00204 }
00205 }
00206 }
00207
00208 protected:
00209
00210 virtual void
00211 ImageAdded(int idx, bool intelliScroll)
00212 {
00213 SetViewVisible(idx, true);
00214 UpdateViewWithImage(idx, idx);
00215 }
00216
00217 virtual void
00218 ImagesRemoved()
00219 {
00220 }
00221
00222 virtual void
00223 HideViewers()
00224 {
00225 for (int i=0 ; i<NrViewers() ; i++)
00226 SetViewVisible(i, false);
00227 }
00228
00229 void
00230 UpdateViewWithImage(int viewIdx, int imIdx)
00231 {
00232 if (mImages.size() == 0)
00233 return;
00234 RgbImage* im = mImages[imIdx];
00235 UpdateView(viewIdx, im->Data(), "Direct", im->Scale(), mViewListener);
00236 SetViewText(viewIdx, im->Caption());
00237 }
00238
00239 ImagesListener* mImagesListener;
00240 void* mImagesListenerData;
00241 OglGui::ViewListener* mViewListener;
00242
00243 int mViewWidth;
00244 int mViewHeight;
00245
00246 std::vector<RgbImage*> mImages;
00247
00248 };
00249
00250 }
00251 }
00252
00253 #endif