1 /**************************************************************************** 2 * 3 * ViSP, open source Visual Servoing Platform software. 4 * Copyright (C) 2005 - 2019 by Inria. All rights reserved. 5 * 6 * This software is free software; you can redistribute it and/or modify 7 * it under the terms of the GNU General Public License as published by 8 * the Free Software Foundation; either version 2 of the License, or 9 * (at your option) any later version. 10 * See the file LICENSE.txt at the root directory of this source 11 * distribution for additional information about the GNU GPL. 12 * 13 * For using ViSP with software that can not be combined with the GNU 14 * GPL, please contact Inria about acquiring a ViSP Professional 15 * Edition License. 16 * 17 * See http://visp.inria.fr for more information. 18 * 19 * This software was developed at: 20 * Inria Rennes - Bretagne Atlantique 21 * Campus Universitaire de Beaulieu 22 * 35042 Rennes Cedex 23 * France 24 * 25 * If you have questions regarding the use of this file, please contact 26 * Inria at visp@inria.fr 27 * 28 * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE 29 * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. 30 * 31 * Description: 32 * Image storage helper. 33 * 34 *****************************************************************************/ 35 36 #ifndef vpImageStorageWorker_h 37 #define vpImageStorageWorker_h 38 39 #include <visp3/core/vpConfig.h> 40 41 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) 42 43 #include <visp3/io/vpImageQueue.h> 44 #include <visp3/io/vpImageIo.h> 45 46 /*! 47 \class vpImageStorageWorker 48 49 \ingroup group_io_image 50 51 Save data contained in an vpImageQueue. 52 53 */ 54 template <class Type> 55 class vpImageStorageWorker { 56 public: 57 /*! 58 * Constructor. 59 * \param[in] queue : A reference to a queue. 60 */ vpImageStorageWorker(vpImageQueue<Type> & queue)61 vpImageStorageWorker(vpImageQueue<Type> &queue) 62 : m_queue(queue), m_dataname(""), m_cpt(1), m_ofs_data(), m_data_file_created(false) 63 { 64 m_seqname = queue.getSeqName(); 65 m_record_mode = queue.getRecordingMode(); 66 } 67 68 /*! 69 * Thread main loop that save the images and additional data. 70 */ run()71 void run() 72 { 73 try { 74 vpImage<Type> I; 75 std::string data; 76 char filename[FILENAME_MAX]; 77 78 for (;;) { 79 m_queue.pop(I, data); 80 81 // Save image 82 sprintf(filename, m_seqname.c_str(), m_cpt); 83 84 if (m_record_mode > 0) { // Single image 85 std::cout << "Save image: " << filename << std::endl; 86 } 87 else if (m_cpt == 1) { 88 std::cout << "Started sequence saving: " << m_seqname << std::endl; 89 } 90 vpImageIo::write(I, filename); 91 92 if (! data.empty()) { 93 if (! m_data_file_created) { 94 std::string parent = vpIoTools::getParent(m_seqname); 95 if (! parent.empty()) { 96 m_dataname = vpIoTools::getParent(m_seqname) + "/"; 97 } 98 m_dataname += vpIoTools::getNameWE(m_seqname); 99 m_dataname += ".txt"; 100 101 std::cout << "Create data file: " << m_dataname<< std::endl; 102 m_ofs_data.open(m_dataname); 103 104 m_data_file_created = true; 105 } 106 m_ofs_data << vpIoTools::getName(filename) << " " << data << std::endl; 107 } 108 109 m_cpt ++; 110 } 111 } catch (const vpImageQueue<vpRGBa>::cancelled &) { 112 std::cout << "Receive cancel during color image saving." << std::endl; 113 if (m_data_file_created) { 114 std::cout << "Close data file: " << m_dataname << std::endl; 115 m_ofs_data.close(); 116 } 117 } catch (const vpImageQueue<unsigned char>::cancelled &) { 118 std::cout << "Receive cancel during gray image saving." << std::endl; 119 if (m_data_file_created) { 120 std::cout << "Close data file: " << m_dataname << std::endl; 121 m_ofs_data.close(); 122 } 123 } 124 } 125 126 private: 127 vpImageQueue<Type> &m_queue; 128 std::string m_seqname; 129 std::string m_dataname; 130 int m_record_mode; 131 unsigned int m_cpt; 132 std::ofstream m_ofs_data; 133 bool m_data_file_created; 134 }; 135 136 #endif 137 #endif 138