/* * CERT_Loader.cpp * * Copyright (c) 2008 Machine Perception Laboratory * University of California San Diego. * * Authors: Andrew Salamon * * Please read the disclaimer and notes about redistribution * at the end of this file. * */ #include "CERT_Loader.h" #include #include #include #include "ImageDirIterator.h" namespace CERT { Loader::Loader( const std::vector< std::string > &_paths, unsigned int _max ) : paths(_paths), max(_max), _interrupt(false), sourceFinished(false), index(0) { VideoIterator tmp; // This will initialize QTMovie on the main thread. } Loader::~Loader() { } void Loader::start() { boost::thread thr( boost::ref( *this ) ); } void Loader::operator()() { unsigned int pathCount = paths.size(); sourceFinished = false; // These need to be created here so that the autorelease pool they use (if on a Mac) // will be created on the correct thread. loader = new ImageLoader; videos = new VideoIterator; if( pathCount > 0 ) { for( unsigned int index = 0; !_interrupt && (index < pathCount); ++index ) { processPath( paths[index] ); } } else { do { std::string path = getimagepath(); if( _interrupt || ("quit" == path) ) { break; } processPath( path ); } while( true ); } sourceFinished = true; Task task; task.error = "Exiting..."; queue.push( task ); delete loader; delete videos; } bool Loader::finished() { return (sourceFinished && queue.empty()); } void Loader::getNextTask( Task &task ) { queue.wait_and_pop( task ); } void Loader::interrupt() { _interrupt = true; } void Loader::processPath( const std::string &path ) { bf::path bfPath( path, bf::native ); if( bf::exists( bfPath ) ) { if( bf::is_directory( bfPath ) ) processDir( path ); else { processImage( path ); } } else { Task task; task.path = path; task.error = "File does not exist"; queue.push_if_not_full( task, max ); } } void Loader::processDir( const std::string &path ) { ImageDirIterator images( path ); int count = 0; while( ++images ) { if( _interrupt ) return; std::string fileName = *images; processImage( fileName ); ++count; } if( 0 == count ) { Task task; task.path = path; task.error = "Directory is empty"; queue.push_if_not_full( task, max ); } } void Loader::processImage( const std::string &path ) { Task task; if( loader->loadRImage( task.rimage, path ) ) // handles loading an image from file into an RImage { task.path = path; queue.push_if_not_full( task, max ); } else { videos->setPath( path ); if( videos->isVideo() ) { while( videos->nextFrame( task.rimage ) ) { if( _interrupt ) { videos->setPath( "" ); return; } std::ostringstream frameStream; frameStream << path << ":" << videos->getFrameNumber(); task.path = frameStream.str(); queue.push_if_not_full( task, max ); } } else { task.path = path; task.error = "Unable to read file"; queue.push_if_not_full( task, max ); } videos->setPath( "" ); // This should release the movie, so it doesn't stick around in memory. } } std::string Loader::getimagepath() { std::string path; std::cout << "Enter path to image"; #ifdef WIN32 std::cout << " (Windows demo only accepts 24 bit bitmaps)"; #endif std::cout << " or ctrl-c to exit" << std::endl; std::cout << "--> "; // std::cin >> path; std::getline( std::cin, path ); if( std::cin.eof() ) path = "quit"; return(path); } } // End namespace CERT /* * * Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: * * 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. * 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. * 3. The name of the author may not be used to endorse or promote products derived from this software without specific prior written permission. * * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */