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 #include "Component/ModelManager.H"
00040 #include "Devices/FrameGrabberConfigurator.H"
00041 #include "Devices/DeviceOpts.H"
00042 #include "Image/DrawOps.H"
00043 #include "Image/Image.H"
00044 #include "Image/ShapeOps.H"
00045 #include "Image/CutPaste.H"
00046 #include "Image/ImageCache.H"
00047 #include "Image/Pixels.H"
00048 #include "GUI/SDLdisplay.H"
00049 #include "GUI/GUIOpts.H"
00050 #include "Raster/Raster.H"
00051 #include "Transport/FrameIstream.H"
00052 #include "Util/Timer.H"
00053 #include "Util/log.H"
00054 #include "Util/sformat.H"
00055 #include "Util/FileUtil.H"
00056 #include "Video/RgbConversion.H"
00057 #include "Raster/DeBayer.H"
00058
00059 #include <pthread.h>
00060
00061
00062 #define NAVG 20
00063
00064 #define MAXSAVETHREAD 4
00065
00066
00067 enum PreviewType
00068 {
00069 PrevAll,
00070 PrevTopLeft,
00071 PrevTopRight,
00072 PrevBotLeft,
00073 PrevBotRight,
00074 PrevCenter
00075 };
00076
00077
00078 uint fnb = 0;
00079 pthread_mutex_t qmutex_cache, qmutex_cachesv, qmutex_imgShow;
00080 ImageCache<byte> cache, cachesv;
00081 std::vector<std::string> base;
00082 bool saving = false;
00083 Image<PixRGB<byte> > imgShow;
00084 PreviewType prevType = PrevAll;
00085 Dims showDim;
00086
00087
00088
00089 static void* saveframes(void *)
00090 {
00091 while(1) {
00092 Image<byte> img; bool havemore = false;
00093 uint cntTmp = 0;
00094
00095
00096 pthread_mutex_lock(&qmutex_cachesv);
00097 if (cachesv.size()){
00098 img = cachesv.pop_front();
00099 fnb++;
00100 cntTmp = fnb;
00101 }
00102 if (cachesv.size()) havemore = true;
00103 pthread_mutex_unlock(&qmutex_cachesv);
00104
00105
00106 if (img.initialized())
00107 {
00108
00109 const char *b = base[cntTmp % base.size()].c_str();
00110 Raster::WriteGray(img, sformat("%s%06u.pgm", b, cntTmp));
00111 }
00112
00113 if (havemore == false) usleep(1000);
00114 }
00115 return NULL;
00116 }
00117
00118
00119
00120 static void* debayerframes(void *)
00121 {
00122 while(1){
00123 Image<byte> img; bool havemore = false;
00124 Image<PixRGB<byte> > imgRGB;
00125
00126 pthread_mutex_lock(&qmutex_cache);
00127 if(cache.size()) img = cache.pop_front();
00128 if(cache.size()) havemore = true;
00129 pthread_mutex_unlock(&qmutex_cache);
00130
00131 if( img.initialized())
00132 {
00133 imgRGB = deBayer(img, BAYER_GBRG);
00134 pthread_mutex_lock(&qmutex_imgShow);
00135
00136 switch(prevType){
00137 case PrevAll:
00138 imgShow = rescale(imgRGB, showDim.w(), showDim.h(),
00139 RESCALE_SIMPLE_NOINTERP);
00140 break;
00141 case PrevTopLeft:
00142 imgShow = crop(imgRGB, Point2D<int>(0,0), showDim);
00143 break;
00144 case PrevTopRight:
00145 imgShow = crop(imgRGB, Point2D<int>
00146 (imgRGB.getWidth()-showDim.w(),0), showDim);
00147 break;
00148 case PrevBotLeft:
00149 imgShow = crop(imgRGB, Point2D<int>
00150 (0,imgRGB.getHeight()-showDim.h()), showDim);
00151 break;
00152 case PrevBotRight:
00153 imgShow = crop(imgRGB, Point2D<int>
00154 (imgRGB.getWidth()-showDim.w(),
00155 imgRGB.getHeight()-showDim.h()), showDim);
00156 break;
00157 case PrevCenter:
00158 imgShow = crop(imgRGB, Point2D<int>
00159 ((imgRGB.getWidth()-showDim.w())/2,
00160 (imgRGB.getHeight()-showDim.h())/2),showDim);
00161 break;
00162 default:
00163 LFATAL("the preview type should between 0 and 5");
00164 }
00165
00166 pthread_mutex_unlock(&qmutex_imgShow);
00167 }
00168
00169 if(havemore == false) usleep(1000);
00170 }
00171 return NULL;
00172 }
00173
00174
00175
00176
00177
00178
00179
00180
00181
00182 static int submain(const int argc, char** argv)
00183 {
00184
00185 ModelManager manager("Frame Grabber");
00186
00187
00188 nub::soft_ref<FrameGrabberConfigurator>
00189 gbc(new FrameGrabberConfigurator(manager));
00190 manager.addSubComponent(gbc);
00191
00192 nub::soft_ref<SDLdisplay> d(new SDLdisplay(manager));
00193 manager.addSubComponent(d);
00194
00195 manager.setOptionValString(&OPT_SDLdisplayPriority, "0");
00196 manager.setOptionValString(&OPT_FrameGrabberType, "XC");
00197 manager.setOptionValString(&OPT_SDLdisplayFullscreen,"false");
00198 manager.setOptionValString(&OPT_SDLdisplayDims, "960x640");
00199
00200
00201 if (manager.parseCommandLine(argc, argv, "<basename> ... <basename>",
00202 1, MAXSAVETHREAD) == false)
00203 return(1);
00204
00205
00206 nub::soft_ref<FrameIstream> gb = gbc->getFrameGrabber();
00207 if (gb.isInvalid())
00208 LFATAL("You need to have XC camera and XClibrary");
00209
00210
00211 for (uint i = 0; i < manager.numExtraArgs(); i ++)
00212 base.push_back(manager.getExtraArg(i));
00213
00214
00215 manager.start();
00216
00217
00218 Timer tim,timer; uint64 t[NAVG]; int frame = 0;
00219 d->clearScreen(PixRGB<byte>(128)); bool doit = true;
00220
00221 showDim = d->getDims();
00222 int iw = showDim.w(), ih = showDim.h();
00223 int dw = d->getDims().w(), dh = d->getDims().h();
00224
00225 int ovlyoff = (dw - iw) / 2 + dw * ((dh - ih) / 2);
00226 int ovluvoff = (dw - iw) / 4 + dw * ((dh - ih) / 8);
00227 int ystride = (dw - iw), uvstride = (dw - iw) / 2;
00228
00229 pthread_t saver[MAXSAVETHREAD];
00230 for(int ii = 0; ii<(int)base.size(); ii++)
00231 pthread_create(saver+ii, NULL, &saveframes, (void *)NULL);
00232
00233 pthread_t debayer, debayer1;
00234 pthread_create(&debayer, NULL, &debayerframes, (void*) NULL);
00235 pthread_create(&debayer1, NULL, &debayerframes, (void*) NULL);
00236
00237
00238 d->createYUVoverlay(SDL_YV12_OVERLAY);
00239
00240
00241 gb->startStream();
00242
00243
00244
00245
00246
00247 float frate = 0.0f;
00248
00249 while(doit) {
00250 tim.reset();
00251
00252
00253 Image<byte> img = gb->readGray();
00254 pthread_mutex_lock(&qmutex_cache);
00255 cache.push_back(img);
00256 pthread_mutex_unlock(&qmutex_cache);
00257
00258 if(saving)
00259 {
00260 pthread_mutex_lock(&qmutex_cachesv);
00261 cachesv.push_back(img);
00262 pthread_mutex_unlock(&qmutex_cachesv);
00263 }
00264
00265
00266 uint64 t0 = tim.get();
00267
00268
00269 pthread_mutex_lock(&qmutex_imgShow);
00270 if (saving)
00271 {
00272 const std::string msg =
00273 sformat(" %.1ffps [%04d] ", frate, cachesv.size());
00274 writeText(imgShow, Point2D<int>(0, 0), msg.c_str());
00275 }
00276 else
00277 {
00278 const std::string msg =
00279 sformat(" [SPC] to save %.1ffp [%04d] ", frate, cache.size());
00280 writeText(imgShow, Point2D<int>(0, 0), msg.c_str());
00281 }
00282
00283
00284 SDL_Overlay* ovl = d->lockYUVoverlay();
00285 toVideoYUV422(imgShow, ovl->pixels[0] + ovlyoff,
00286 ovl->pixels[2] + ovluvoff,
00287 ovl->pixels[1] + ovluvoff,
00288 ystride, uvstride, uvstride);
00289
00290 d->unlockYUVoverlay();
00291 d->displayYUVoverlay(-1, SDLdisplay::NO_WAIT);
00292 pthread_mutex_unlock(&qmutex_imgShow);
00293
00294
00295
00296 int ii = (d->checkForKey());
00297 if(ii == ' ')
00298 { saving = ! saving;
00299 prevType = PrevAll;
00300 }
00301 else if(ii == 'q')
00302 prevType = PrevTopLeft;
00303 else if(ii == 'e')
00304 prevType = PrevTopRight;
00305 else if(ii == 'a')
00306 prevType = PrevBotLeft;
00307 else if(ii == 'd')
00308 prevType = PrevBotRight;
00309 else if(ii == 's')
00310 prevType = PrevCenter;
00311 else if(ii == 'w')
00312 prevType = PrevAll;
00313
00314 t[frame % NAVG] = tim.get();
00315 t0 = t[frame % NAVG] - t0;
00316 if (t0 > 20000ULL) LINFO("Display took %lluus", t0);
00317
00318
00319 if (frame % NAVG == 0 && frame > 0)
00320 {
00321 uint64 avg = 0ULL; for (int i = 0; i < NAVG; i ++) avg += t[i];
00322 frate = 1000.0F / float(avg) * float(NAVG);
00323 if(saving)
00324 LINFO("Frame rate %f fps, buf size %u, time %f", frate,
00325 cachesv.size(), timer.getSecs());
00326 else
00327 LINFO("Frame rate %f fps, buf size %u, time %f", frate,
00328 cache.size(), timer.getSecs());
00329 }
00330 frame ++;
00331 }
00332
00333 d->destroyYUVoverlay();
00334 manager.stop();
00335
00336
00337 return 0;
00338 }
00339
00340 extern "C" int main(const int argc, char** argv)
00341 {
00342 try
00343 {
00344 return submain(argc, argv);
00345 }
00346 catch (...)
00347 {
00348 REPORT_CURRENT_EXCEPTION;
00349 }
00350
00351 return 1;
00352 }
00353
00354
00355
00356
00357
00358