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