ImageFileListWriter.h 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. #ifndef _IMAGE_IMAGEFILELISTWRITER_H
  2. #define _IMAGE_IMAGEFILELISTWRITER_H
  3. #include "core/image/ImageOutputStream.h"
  4. #include <string>
  5. #include <fstream>
  6. #include <vector>
  7. namespace NICE {
  8. /**
  9. * Writes a sequence of image files and a filelist.
  10. *
  11. * The filenames of the image files are formed as follows:
  12. * Let path = toppath/subdir.
  13. * Then the filenames are toppath/subdir/subdir_ + counter + extension.
  14. * The filename of the filelist text file is
  15. * toppath/subdir/subdir + '.txt'.
  16. * The directory toppath/subdir will be created if it doesn't exist.
  17. *
  18. * The filelist written by this class can be read by ImageFileListReader.
  19. * The filename of that filelist can be retrieved via getSequenceFileName().
  20. *
  21. * @note Any existing files will be overwritten!
  22. */
  23. class ImageFileListWriter: public ImageOutputStream {
  24. public:
  25. /**
  26. * Constructor.
  27. * @param path Output path (see class description).
  28. * @param extension target output extension
  29. * @param _keepImagesInMemory Write all files in \c close().
  30. * No disk access until then.
  31. */
  32. ImageFileListWriter(const std::string& path, const std::string& extension = ".ppm", const bool _keepImagesInMemory =
  33. false);
  34. /**
  35. * Destructor. Will close the writer.
  36. */
  37. virtual ~ImageFileListWriter();
  38. //! write a ColorImage
  39. virtual void writeColorImage(const ColorImage& image);
  40. /**
  41. * Write a Image.
  42. * Note: If in buffered mode (\c _keepImagesInMemory), GrayImages will be
  43. * buffered as ColorImages.
  44. */
  45. virtual void writeGrayImage(const Image& image);
  46. /**
  47. * If in buffered mode (\c _keepImagesInMemory), write all images to
  48. disk.
  49. * Otherwise, do nothing.
  50. * NEW SINCE NICE
  51. */
  52. virtual void flushMemoryBuffer();
  53. virtual void close();
  54. virtual void insertMissingFrames(int frames);
  55. /**
  56. * Get the name of the output file (as can be passed to ImageFileListReader)
  57. * @return filename of the output file list
  58. */
  59. inline const std::string& getSequenceFileName() const {
  60. return fileListFileName;
  61. }
  62. /**
  63. * Current frame index. The index of the NEXT image to be written.
  64. */
  65. inline int currentFrameIndex() const {
  66. return currentFrame;
  67. }
  68. /**
  69. * The filename of the last image which has been written.
  70. */
  71. inline const std::string& lastImageFileName() const {
  72. return m_lastImageFileName;
  73. }
  74. private:
  75. //! The output name prefix.
  76. std::string namePrefix;
  77. //! The output name extension and thus file format.
  78. std::string nameExtension;
  79. //! The current frame number.
  80. int currentFrame;
  81. //! The file name of the output file list.
  82. std::string fileListFileName;
  83. //! Stream to the output file list.
  84. std::ofstream fileList;
  85. /**
  86. * Initialize.
  87. * @param path Output path
  88. */
  89. void init(const std::string& path);
  90. /**
  91. * Construct the file name for the current frame.
  92. * @return new file name
  93. */
  94. std::string makeCurrentFileName();
  95. /**
  96. * Add a file name to the file list.
  97. * @param fileName File name to be added.
  98. */
  99. void addToFileList(const std::string& fileName);
  100. //! Keep images in memory until destruction?
  101. bool keepImagesInMemory;
  102. //! Buffered images.
  103. std::vector<ColorImage*> images;
  104. //! Buffered file names.
  105. std::vector<std::string*> fileNames;
  106. std::string m_lastImageFileName;
  107. };
  108. } // namespace
  109. #endif // _IMAGE_IMAGEFILELISTWRITER_H