UsTK : Ultrasound ToolKit  version 2.0.1 under development (2023-12-07)
usSequenceReader3D.h
1 /****************************************************************************
2  *
3  * This file is part of the ustk software.
4  * Copyright (C) 2016 - 2017 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * ("GPL") version 2 as published by the Free Software Foundation.
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 ustk 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  * This software was developed at:
17  * Inria Rennes - Bretagne Atlantique
18  * Campus Universitaire de Beaulieu
19  * 35042 Rennes Cedex
20  * France
21  *
22  * If you have questions regarding the use of this file, please contact
23  * Inria at ustk@inria.fr
24  *
25  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
26  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
27  *
28  * Authors:
29  * Marc Pouliquen
30  *
31  *****************************************************************************/
32 
40 #ifndef __usSequenceReader3D_h_
41 #define __usSequenceReader3D_h_
42 
43 #include <algorithm>
44 #include <cstring>
45 #include <iostream>
46 #include <vector>
47 
48 #include <visp3/core/vpDebug.h>
49 #include <visp3/core/vpException.h>
50 
51 #include <visp3/ustk_core/usImageIo.h>
52 
63 template <class ImageType> class usSequenceReader3D
64 {
65 private:
66  void open(ImageType &image);
67 
70  long m_frameCount;
71 
74  std::string m_fileName;
75  bool m_fileNameIsSet;
76 
78  bool is_open;
79 
81  usImageIo::VolHeader m_volHeader;
82 
84  std::ifstream m_volFile;
85 
86 public:
88 
89  virtual ~usSequenceReader3D();
90 
91  // get images in grabber style
92  void acquire(ImageType &image);
93 
94  // get image with its number in the sequence
95  void getVolume(usImagePreScan3D<unsigned char> &preScanImage, int volumeNumberInSequence);
96 
100  inline bool end()
101  {
102  if (!is_open) {
103  ImageType image;
104  open(image);
105  m_frameCount--;
106  }
107  if (m_frameCount > m_volHeader.volumes)
108  return true;
109  return false;
110  }
111 
112  long getImageNumber() const { return m_frameCount; }
113 
114  void setSequenceFileName(const std::string &sequenceFileName);
115 };
116 
117 /****************************************************************************
118 * Template implementations.
119 ****************************************************************************/
120 
124 template <class ImageType>
126  : m_frameCount(0), m_fileName(""), m_fileNameIsSet(false), is_open(false), m_volHeader()
127 {
128 }
129 
134 
139 template <class ImageType> void usSequenceReader3D<ImageType>::setSequenceFileName(const std::string &fileName)
140 {
141  m_fileName = fileName;
142  m_fileNameIsSet = true;
143 }
144 
149 template <class ImageType> void usSequenceReader3D<ImageType>::open(ImageType &image)
150 {
151  (void)image;
152  throw(vpException(vpException::notImplementedError));
153 }
154 /*
155 template<>
156 void usSequenceReader3D<usImageRF2D<short> >::open(usImageRF2D<short> &image)
157 {
158  if(!m_fileNameIsSet) {
159  throw(vpException(vpException::badValue, "Sequence settings file name not set"));
160  }
161  usImageSettingsXmlParser xmlParser;
162  xmlParser.parse(m_sequenceFileName);
163 
164  setFirstFrameIndex(xmlParser.getSequenceStartNumber());
165  setLastFrameIndex(xmlParser.getSequenceStopNumber());
166  m_genericImageFileName = xmlParser.getImageFileName();
167 
168  //saving the settings for all the rf sequence
169  m_frame.setTransducerRadius(xmlParser.getTransducerSettings().getTransducerRadius());
170  m_frame.setScanLinePitch(xmlParser.getTransducerSettings().getScanLinePitch());
171  m_frame.setScanLineNumber(xmlParser.getTransducerSettings().getScanLineNumber());
172  m_frame.setTransducerConvexity(xmlParser.getTransducerSettings().isTransducerConvex());
173  m_frame.setAxialResolution(xmlParser.getAxialResolution());
174 
175  //Reading image
176  char buffer[FILENAME_MAX];
177  sprintf(buffer, m_genericImageFileName.c_str(),m_firstFrame);
178  std::string parentName = vpIoTools::getParent(m_sequenceFileName);
179  if(!parentName.empty()) {
180  parentName = parentName + vpIoTools::path("/");
181  }
182  std::string imageFileName = parentName + buffer;
183  vpImageIo::read(image,imageFileName);
184  image.setImagePreScanSettings(m_frame);
185 
186  m_frameCount = m_firstFrame + 1;
187  is_open = true;
188 }*/
189 
191 {
192  if (!m_fileNameIsSet) {
193  throw(vpException(vpException::badValue, "Sequence file name not set"));
194  }
195 
197  throw(vpException(vpException::ioError, "only .vol files are supported for 3D sequence reading"));
198 
199  // INIT
200  int szHeader = sizeof(usImageIo::VolHeader);
201  int n = 0;
202  char byte;
203 
204  // FILE OPENING
205  m_volFile.open(m_fileName.c_str(), std::ios::in | std::ios::binary);
206 
207  // READING HEADER
208  while (n < szHeader) {
209  m_volFile.read(&byte, 1);
210  ((char *)&m_volHeader)[n] = byte;
211  n++;
212  }
213  std::cout << "fpv " << m_volHeader.fpv << std::endl;
214  std::cout << "w " << m_volHeader.w << std::endl;
215  std::cout << "h " << m_volHeader.h << std::endl;
216 
217  // CHECK IMAGE TYPE
218  if (m_volHeader.type != 0)
219  throw(vpException(vpException::badValue, "trying to read non-prescan in .vol file"));
220 
221  // CHECK DATA TYPE
222  if (m_volHeader.ss != 8)
223  throw(vpException(vpException::badValue, ".vol file doesn't contain unsigned char data"));
224 
225  // READING DATA
226  image.resize(m_volHeader.h, m_volHeader.w, m_volHeader.fpv);
227  n = 0;
228  unsigned char voxel;
229  for (int k = 0; k < m_volHeader.fpv; k++) {
230  for (int j = 0; j < m_volHeader.w; j++) {
231  for (int i = 0; i < m_volHeader.h; i++) {
232  m_volFile.read((char *)&voxel, 1);
233  image(j, i, k, voxel);
234  }
235  }
236  }
237 
238  m_frameCount = 1;
239  is_open = true;
240 }
241 
243 {
244  if (!m_fileNameIsSet) {
245  throw(vpException(vpException::badValue, "Sequence file name not set"));
246  }
247 
249  throw(vpException(vpException::ioError, "only .vol files are supported for 3D sequence reading"));
250 
251  // INIT
252  int szHeader = sizeof(usImageIo::VolHeader);
253  int n = 0;
254  char byte;
255 
256  // FILE OPENING
257  m_volFile.open(m_fileName.c_str(), std::ios::in | std::ios::binary);
258 
259  // READING HEADER
260  while (n < szHeader) {
261  m_volFile.read(&byte, 1);
262  ((char *)&m_volHeader)[n] = byte;
263  n++;
264  }
265  std::cout << "fpv " << m_volHeader.fpv << std::endl;
266  std::cout << "w " << m_volHeader.w << std::endl;
267  std::cout << "h " << m_volHeader.h << std::endl;
268 
269  // CHECK IMAGE TYPE
270  if (m_volHeader.type != 1)
271  throw(vpException(vpException::badValue, "trying to read non-postscan in .vol file"));
272 
273  // CHECK DATA TYPE
274  if (m_volHeader.ss != 8)
275  throw(vpException(vpException::badValue, ".vol file doesn't contain unsigned char data"));
276 
277  // READING DATA
278  image.resize(m_volHeader.h, m_volHeader.w, m_volHeader.fpv);
279  n = 0;
280  unsigned char voxel;
281  for (int k = 0; k < m_volHeader.fpv; k++) {
282  for (int i = 0; i < m_volHeader.h; i++) {
283  for (int j = 0; j < m_volHeader.w; j++) {
284  m_volFile.read((char *)&voxel, 1);
285  image(j, i, k, voxel);
286  }
287  }
288  }
289 
290  m_frameCount = 1;
291  is_open = true;
292 }
293 
299 {
300  if (!is_open) {
301  this->open(image);
302  return;
303  }
304  // READING DATA
305  image.resize(m_volHeader.h, m_volHeader.w, m_volHeader.fpv);
306  unsigned char voxel;
307  for (int k = 0; k < m_volHeader.fpv; k++) {
308  for (int j = 0; j < m_volHeader.w; j++) {
309  for (int i = 0; i < m_volHeader.h; i++) {
310  m_volFile.read((char *)&voxel, 1);
311  image(j, i, k, voxel);
312  }
313  }
314  }
315 
316  m_frameCount++;
317 }
318 
324 {
325  if (!is_open) {
326  this->open(image);
327  return;
328  }
329  // READING DATA
330  image.resize(m_volHeader.h, m_volHeader.w, m_volHeader.fpv);
331  unsigned char voxel;
332  for (int k = 0; k < m_volHeader.fpv; k++) {
333  for (int i = 0; i < m_volHeader.h; i++) {
334  for (int j = 0; j < m_volHeader.w; j++) {
335  m_volFile.read((char *)&voxel, 1);
336  image(j, i, k, voxel);
337  }
338  }
339  }
340 
341  m_frameCount++;
342 }
343 
353 template <>
355  int volumeNumberInSequence)
356 {
357  if (!is_open) {
358  this->open(preScanImage);
359  return;
360  }
361 
362  // CHECK IMAGE TYPE
363  if (m_volHeader.type != 0)
364  throw(vpException(vpException::badValue, "trying to read non-prescan in .vol file"));
365 
366  // CHECK DATA TYPE
367  if (m_volHeader.ss != 8)
368  throw(vpException(vpException::badValue, ".vol file doesn't contain unsigned char data"));
369 
370  // OFFSET
371  // to select volume to read (in .vol files multiple volumes can be stacked after the header part)
372  int szHeader = sizeof(usImageIo::VolHeader);
373  int offset = szHeader + volumeNumberInSequence * m_volHeader.fpv * m_volHeader.w * m_volHeader.h;
374  m_volFile.seekg(offset);
375 
376  // READING DATA
377  preScanImage.resize(m_volHeader.h, m_volHeader.w, m_volHeader.fpv);
378  unsigned char voxel;
379  for (int k = 0; k < m_volHeader.fpv; k++) {
380  for (int j = 0; j < m_volHeader.w; j++) {
381  for (int i = 0; i < m_volHeader.h; i++) {
382  m_volFile.read((char *)&voxel, 1);
383  preScanImage(j, i, k, voxel);
384  }
385  }
386  }
387 }
388 
389 #endif // __usSequenceReader3D_h_
void resize(unsigned int height, unsigned int width, unsigned int numberOfFrames)
Definition: usImage3D.h:376
static usHeaderFormatType getHeaderFormat(const std::string &headerfilename)
Definition: usImageIo.cpp:50
@ FORMAT_VOL
Definition: usImageIo.h:152
void resize(unsigned int height, unsigned int width, unsigned int numberOfFrames)
Reading of sequences of ultrasound images.
void setSequenceFileName(const std::string &sequenceFileName)
long getImageNumber() const
void acquire(ImageType &image)
void getVolume(usImagePreScan3D< unsigned char > &preScanImage, int volumeNumberInSequence)