iLab Neuromorphic Robotics Toolkit  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
tests/test-stream.C
/*! @file
@author
@copyright GNU Public License (GPL v3)
@section License
@verbatim
// ////////////////////////////////////////////////////////////////////////
// The iLab Neuromorphic Robotics Toolkit (NRT) //
// Copyright 2010-2012 by the University of Southern California (USC) //
// and the iLab at USC. //
// //
// iLab - University of Southern California //
// Hedco Neurociences Building, Room HNB-10 //
// Los Angeles, Ca 90089-2520 - USA //
// //
// See http://ilab.usc.edu for information about this project. //
// ////////////////////////////////////////////////////////////////////////
// This file is part of The iLab Neuromorphic Robotics Toolkit. //
// //
// The iLab Neuromorphic Robotics Toolkit is free software: you can //
// redistribute it and/or modify it under the terms of the GNU General //
// Public License as published by the Free Software Foundation, either //
// version 3 of the License, or (at your option) any later version. //
// //
// The iLab Neuromorphic Robotics Toolkit is distributed in the hope //
// that it will be useful, but WITHOUT ANY WARRANTY; without even the //
// implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR //
// PURPOSE. See the GNU General Public License for more details. //
// //
// You should have received a copy of the GNU General Public License //
// along with The iLab Neuromorphic Robotics Toolkit. If not, see //
// <http://www.gnu.org/licenses/>. //
// ////////////////////////////////////////////////////////////////////////
@endverbatim */
#include <chrono>
NRT_DECLARE_PARAMETER(fps, double, "The frames / second", 30);
NRT_DECLARE_PARAMETER(loop, bool, "Keep looping the input?", false);
NRT_DECLARE_PARAMETER(showframe, bool, "Show the frame number in the top left corner (only available for "
"seekable sources)", false);
NRT_DECLARE_PARAMETER(rotate, bool, "rotate the image 90 degrees: TODO rotate to other degrees", false);
int main(int argc, char const* argv[])
{
try
{
nrt::Manager mgr(argc, argv);
auto mySink = mgr.addComponent<nrt::ImageSink>("out");
auto mySource = mgr.addComponent<nrt::ImageSource>("in");
// Add a custom frames per second parameter for this application
class MyParameters : public nrt::Component, public nrt::Parameter<fps, loop, showframe, rotate>
auto params = mgr.addComponent<MyParameters>("params");
mgr.launch();
std::shared_ptr<nrt::SeekableImageSourceType> seekableSource =
std::dynamic_pointer_cast<nrt::SeekableImageSourceType>(mySource->actualSource());
while(true)
{
auto startTime = std::chrono::steady_clock::now();
if (!mySource->ok())
{
if (params->loop::get())
{
if(seekableSource)
if(!seekableSource->seek(seekableSource->frameRange().first))
NRT_FATAL("Seek failed!");
}
else
break;
}
int framenumber=0;
if(seekableSource)
{
framenumber = seekableSource->nextFrameNumber();
}
nrt::Image<nrt::PixRGB<nrt::byte> > img = mySource->in().convertTo<nrt::PixRGB<nrt::byte> >();
if (params->showframe::get() && seekableSource)
if (params->rotate::get())
{
int width = img.width();
int height = img.height();
nrt::Image<nrt::PixRGB<nrt::byte> > out(height, width);
for (nrt::int32 y = 0; y < height; ++y)
{
for (nrt::int32 x = 0; x < width; ++x)
{
out(y, width - x - 1) = img(x, y);
}
}
mySink->out(nrt::GenericImage(out));
} else {
mySink->out(nrt::GenericImage(img));
}
std::chrono::duration<double> duration
= (std::chrono::steady_clock::now() - startTime);
usleep( std::max(0.0, ((1.0/params->fps::get()) - duration.count()) * 1000000));
}
}
{ NRT_WARNING(" Received Parameter exception:\n" << e.what()); }
return 0;
}