iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
test-stream.C
Go to the documentation of this file.
1 /*! @file
2  @author
3  @copyright GNU Public License (GPL v3)
4  @section License
5  @verbatim
6  // ////////////////////////////////////////////////////////////////////////
7  // The iLab Neuromorphic Robotics Toolkit (NRT) //
8  // Copyright 2010-2012 by the University of Southern California (USC) //
9  // and the iLab at USC. //
10  // //
11  // iLab - University of Southern California //
12  // Hedco Neurociences Building, Room HNB-10 //
13  // Los Angeles, Ca 90089-2520 - USA //
14  // //
15  // See http://ilab.usc.edu for information about this project. //
16  // ////////////////////////////////////////////////////////////////////////
17  // This file is part of The iLab Neuromorphic Robotics Toolkit. //
18  // //
19  // The iLab Neuromorphic Robotics Toolkit is free software: you can //
20  // redistribute it and/or modify it under the terms of the GNU General //
21  // Public License as published by the Free Software Foundation, either //
22  // version 3 of the License, or (at your option) any later version. //
23  // //
24  // The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
25  // that it will be useful, but WITHOUT ANY WARRANTY; without even the //
26  // implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
27  // PURPOSE. See the GNU General Public License for more details. //
28  // //
29  // You should have received a copy of the GNU General Public License //
30  // along with The iLab Neuromorphic Robotics Toolkit. If not, see //
31  // <http://www.gnu.org/licenses/>. //
32  // ////////////////////////////////////////////////////////////////////////
33  @endverbatim */
34 
35 
36 
37 #include <nrt/Core/Image/Image.H>
39 #include <nrt/Core/Model/Manager.H>
44 #include <chrono>
45 
46 NRT_DECLARE_PARAMETER(fps, double, "The frames / second", 30);
47 NRT_DECLARE_PARAMETER(loop, bool, "Keep looping the input?", false);
48 NRT_DECLARE_PARAMETER(showframe, bool, "Show the frame number in the top left corner (only available for "
49  "seekable sources)", false);
50 NRT_DECLARE_PARAMETER(rotate, bool, "rotate the image 90 degrees: TODO rotate to other degrees", false);
51 
52 
53 int main(int argc, char const* argv[])
54 {
55  try
56  {
57  nrt::Manager mgr(argc, argv);
58 
59  auto mySink = mgr.addComponent<nrt::ImageSink>("out");
60  auto mySource = mgr.addComponent<nrt::ImageSource>("in");
61 
62  // Add a custom frames per second parameter for this application
63  class MyParameters : public nrt::Component, public nrt::Parameter<fps, loop, showframe, rotate>
64  { using nrt::Component::Component; };
65 
66  auto params = mgr.addComponent<MyParameters>("params");
67 
68  mgr.launch();
69 
70  std::shared_ptr<nrt::SeekableImageSourceType> seekableSource =
71  std::dynamic_pointer_cast<nrt::SeekableImageSourceType>(mySource->actualSource());
72 
73  while(true)
74  {
75  auto startTime = std::chrono::steady_clock::now();
76 
77  if (!mySource->ok())
78  {
79  if (params->loop::get())
80  {
81  if(seekableSource)
82  if(!seekableSource->seek(seekableSource->frameRange().first))
83  NRT_FATAL("Seek failed!");
84  }
85  else
86  break;
87  }
88 
89  int framenumber=0;
90  if(seekableSource)
91  {
92  framenumber = seekableSource->nextFrameNumber();
93  }
94 
95  nrt::Image<nrt::PixRGB<nrt::byte> > img = mySource->in().convertTo<nrt::PixRGB<nrt::byte> >();
96  if (params->showframe::get() && seekableSource)
97  nrt::drawText(img, nrt::Point2D<int>(0,0), nrt::sformat("%d", framenumber), nrt::PixRGB<nrt::byte>(255),
98  nrt::PixRGBA<nrt::byte>(0,0,0,200));
99 
100 
101  if (params->rotate::get())
102  {
103  int width = img.width();
104  int height = img.height();
105 
106  nrt::Image<nrt::PixRGB<nrt::byte> > out(height, width);
107  for (nrt::int32 y = 0; y < height; ++y)
108  {
109  for (nrt::int32 x = 0; x < width; ++x)
110  {
111  out(y, width - x - 1) = img(x, y);
112  }
113  }
114  mySink->out(nrt::GenericImage(out));
115  } else {
116  mySink->out(nrt::GenericImage(img));
117  }
118 
119 
120  std::chrono::duration<double> duration
121  = (std::chrono::steady_clock::now() - startTime);
122  usleep( std::max(0.0, ((1.0/params->fps::get()) - duration.count()) * 1000000));
123  }
124 
125  }
127  { NRT_WARNING(" Received Parameter exception:\n" << e.what()); }
128 
129 
130  return 0;
131 }
132