ImageFileListReader.h 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154
  1. #ifndef _IMAGE_IMAGEFILELISTREADER_H
  2. #define _IMAGE_IMAGEFILELISTREADER_H
  3. #include <string>
  4. #include <vector>
  5. #include "core/basics/FileName.h"
  6. #include "core/image/ImageInputStream.h"
  7. namespace NICE {
  8. /**
  9. * An ImageInputStream reading a list of image files as defined by a textfile.
  10. *
  11. * The sequence is defined by a textfile which lists the filenames
  12. * of the sequence. The filenames are defined relative to the textfile
  13. * (absolute paths are also allowed). Filenames are separated by
  14. * a whitespace, usually one filename per line.
  15. *
  16. * If the filename given to a constructor is actually a directory 'something/foo',
  17. * it tries to read the file 'something/foo/foo.txt'.
  18. */
  19. class ImageFileListReader : public ImageInputStream {
  20. public:
  21. /**
  22. * Constructor.
  23. * @param fileName Name of the textfile defining the sequence of image files.
  24. * @param _preload Preload the whole sequence into memory?
  25. */
  26. ImageFileListReader(const char* fileName, bool _preload = false);
  27. /**
  28. * Constructor.
  29. * @param fileName Name of the textfile defining the sequence of image files.
  30. * @param _preload Preload the whole sequence into memory?
  31. */
  32. ImageFileListReader(const std::string& fileName, bool _preload = false);
  33. virtual ~ImageFileListReader();
  34. virtual bool endOfStream();
  35. virtual void readColorImage(ColorImage& buffer);
  36. virtual void readGrayImage(Image& buffer);
  37. //! random access
  38. virtual void readColorImage(ColorImage& buffer, unsigned int index);
  39. //! random access
  40. virtual void readGrayImage(Image& buffer, unsigned int index);
  41. virtual int imageWidth();
  42. virtual int imageHeight();
  43. /**
  44. * Skip the next image, but do provide the image file name via
  45. * getPreviousImageFileName().
  46. */
  47. inline void skipNextImage() {
  48. ++currentFrame;
  49. }
  50. /**
  51. * Total number of frames (=images).
  52. * @return number of frames
  53. */
  54. virtual unsigned int numberOfFrames() const;
  55. /**
  56. * Start reading the sequence from the beginning.
  57. */
  58. virtual void reset();
  59. /**
  60. * Skip frames.
  61. * @param frames Number of frames to skip
  62. */
  63. virtual void ignoreFrames(int frames);
  64. /**
  65. * Get the file name of the image file list
  66. * (as given to the constructor).
  67. * @return file name
  68. */
  69. inline const FileName& getInputFileName() const {
  70. return inputFileName;
  71. }
  72. /**
  73. * Get the file name of the previously read image,
  74. * empty string if not available.
  75. */
  76. inline std::string getPreviousImageFileName() const {
  77. if (currentFrame == 0 || currentFrame > numberOfFrames()) {
  78. return "";
  79. } else {
  80. return *fileList.at(getPreviousImageIndex());
  81. }
  82. }
  83. /**
  84. * Get the file name of the image with index i
  85. */
  86. inline std::string getImageFileName(unsigned int i) const {
  87. if (i >= numberOfFrames()) {
  88. return "";
  89. } else {
  90. return *fileList.at(i);
  91. }
  92. }
  93. /**
  94. * The index of the previously read image.
  95. */
  96. inline uint getPreviousImageIndex() const {
  97. return currentFrame - 1;
  98. }
  99. private:
  100. //! List of filenames read from the textfile.
  101. std::vector<std::string*> fileList;
  102. //! The file list definition file.
  103. FileName inputFileName;
  104. //! Current index into \c fileList.
  105. unsigned int currentFrame;
  106. //! Width of the images.
  107. int width;
  108. //! Height of the images.
  109. int height;
  110. //! Preload flag.
  111. bool preload;
  112. //! Preloaded images.
  113. std::vector<ColorImage*> images;
  114. /**
  115. * Initialize.
  116. * @param fileName Input file name
  117. * @param preload Preload?
  118. */
  119. void init(const std::string& fileName, bool preload);
  120. /**
  121. * Read the next image in the sequence.
  122. */
  123. ColorImage* doReadColorImage(ColorImage* buffer = NULL);
  124. };
  125. } // namespace
  126. #endif // _IMAGE_IMAGEFILELISTREADER_H