Visual Servoing Platform  version 3.6.1 under development (2024-12-17)
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 #if (VISP_CXX_STANDARD >= VISP_CXX_STANDARD_11) && defined(VISP_HAVE_THREADS)
40 
41 #include <visp3/io/vpImageIo.h>
42 #include <visp3/io/vpImageQueue.h>
43 
44 BEGIN_VISP_NAMESPACE
53 template <class Type> class vpImageStorageWorker
54 {
55 public:
60  vpImageStorageWorker(vpImageQueue<Type> &queue)
61  : m_queue(queue), m_dataname(""), m_cpt(1), m_ofs_data(), m_data_file_created(false)
62  {
63  m_seqname = queue.getSeqName();
64  m_record_mode = queue.getRecordingMode();
65  }
66 
70  void run()
71  {
72  try {
73  vpImage<Type> I;
74  std::string data;
75  char filename[FILENAME_MAX];
76 
77  for (;;) {
78  m_queue.pop(I, data);
79 
80  // Save image
81  snprintf(filename, FILENAME_MAX, m_seqname.c_str(), m_cpt);
82 
83  if (m_record_mode > 0) { // Single image
84  std::cout << "Save image: " << filename << std::endl;
85  }
86  else if (m_cpt == 1) {
87  std::cout << "Started sequence saving: " << m_seqname << std::endl;
88  }
89  vpImageIo::write(I, filename);
90 
91  if (!data.empty()) {
92  if (!m_data_file_created) {
93  std::string parent = vpIoTools::getParent(m_seqname);
94  if (!parent.empty()) {
95  m_dataname = vpIoTools::getParent(m_seqname) + "/";
96  }
97  m_dataname += vpIoTools::getNameWE(m_seqname);
98  m_dataname += ".txt";
99 
100  std::cout << "Create data file: " << m_dataname << std::endl;
101  m_ofs_data.open(m_dataname);
102 
103  m_data_file_created = true;
104  }
105  m_ofs_data << vpIoTools::getName(filename) << " " << data << std::endl;
106  }
107 
108  m_cpt++;
109  }
110  }
111  catch (const vpImageQueue<vpRGBa>::vpCancelled_t &) {
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  }
118  catch (const vpImageQueue<unsigned char>::vpCancelled_t &) {
119  std::cout << "Receive cancel during gray image saving." << std::endl;
120  if (m_data_file_created) {
121  std::cout << "Close data file: " << m_dataname << std::endl;
122  m_ofs_data.close();
123  }
124  }
125  }
126 
127 private:
128  vpImageQueue<Type> &m_queue;
129  std::string m_seqname;
130  std::string m_dataname;
131  int m_record_mode;
132  unsigned int m_cpt;
133  std::ofstream m_ofs_data;
134  bool m_data_file_created;
135 };
136 END_VISP_NAMESPACE
137 #endif
138 #endif
static void write(const vpImage< unsigned char > &I, const std::string &filename, int backend=IO_DEFAULT_BACKEND)
Definition: vpImageIo.cpp:291
vpImageStorageWorker(vpImageQueue< Type > &queue)
Definition of the vpImage class member functions.
Definition: vpImage.h:131
static std::string getNameWE(const std::string &pathname)
Definition: vpIoTools.cpp:1227
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1314
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1205