ImageFileListWriter.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  1. #include "core/image/ImageFileListWriter.h"
  2. #include "core/image/Convert.h"
  3. #include "core/basics/FileName.h"
  4. #include <sstream>
  5. namespace NICE {
  6. ImageFileListWriter::ImageFileListWriter(const std::string& path,
  7. const std::string& extension,
  8. const bool _keepImagesInMemory)
  9. : nameExtension(extension), currentFrame(0),
  10. keepImagesInMemory(_keepImagesInMemory) {
  11. init(path);
  12. }
  13. ImageFileListWriter::~ImageFileListWriter() {
  14. close();
  15. }
  16. void ImageFileListWriter::writeColorImage(const ColorImage& image) {
  17. m_lastImageFileName = makeCurrentFileName();
  18. if (keepImagesInMemory) {
  19. ColorImage* imageCopy = new ColorImage(image);
  20. images.push_back(imageCopy);
  21. fileNames.push_back(new std::string(m_lastImageFileName));
  22. } else {
  23. //image.writePPM(fileName);
  24. image.write(ImageFile(m_lastImageFileName));
  25. addToFileList(m_lastImageFileName);
  26. }
  27. currentFrame++;
  28. }
  29. void ImageFileListWriter::writeGrayImage(const Image& image) {
  30. m_lastImageFileName = makeCurrentFileName();
  31. if (keepImagesInMemory) {
  32. ColorImage* imageCopy = grayToRGB(image);
  33. images.push_back(imageCopy);
  34. fileNames.push_back(new std::string(m_lastImageFileName));
  35. } else {
  36. //std::auto_ptr<ColorImage> imageCopy(grayToRGB(image));
  37. //imageCopy->writePPM(fileName);
  38. image.write(ImageFile(m_lastImageFileName));
  39. addToFileList(m_lastImageFileName);
  40. }
  41. currentFrame++;
  42. }
  43. void ImageFileListWriter::close() {
  44. flushMemoryBuffer();
  45. /*if (keepImagesInMemory) {
  46. for (unsigned int i = 0; i < images.size(); i++) {
  47. std::string* fileName = fileNames.at(i);
  48. ColorImage* image = images.at(i);
  49. //image->writePPM(*fileName);
  50. image->write(ImageFile(*fileName));
  51. addToFileList(*fileName);
  52. delete image;
  53. delete fileName;
  54. }
  55. }*/
  56. fileList.close();
  57. }
  58. void ImageFileListWriter::init(const std::string& path) {
  59. FileName thePath(path);
  60. thePath.removeSlash();
  61. FileName subdir = thePath.extractFileName();
  62. thePath.createDirectory();
  63. thePath.addSlash();
  64. namePrefix = thePath.str() + subdir.str();
  65. fileListFileName = namePrefix + ".txt";
  66. fileList.open(fileListFileName.c_str());
  67. if (!fileList.good()) {
  68. throw ImageException(
  69. std::string("ImageFileListWriter: Error creating filelist ")
  70. + fileListFileName);
  71. }
  72. }
  73. std::string ImageFileListWriter::makeCurrentFileName() {
  74. std::ostringstream counterStream;
  75. counterStream << currentFrame;
  76. std::string counter = counterStream.str();
  77. while (counter.length() < 5) {
  78. counter = "0" + counter;
  79. }
  80. return namePrefix + "_" + counter + nameExtension;//".ppm";
  81. }
  82. void ImageFileListWriter::addToFileList(const std::string& fileName) {
  83. FileName name(fileName);
  84. fileList << name.extractFileName() << std::endl;
  85. }
  86. void ImageFileListWriter::insertMissingFrames(int frames) {
  87. currentFrame += frames;
  88. }
  89. void ImageFileListWriter::flushMemoryBuffer() {
  90. if (keepImagesInMemory) {
  91. for (unsigned int i = 0; i < images.size(); i++) {
  92. std::string* fileName = fileNames.at(i);
  93. ColorImage* image = images.at(i);
  94. //image->writePPM(*fileName);
  95. image->write(ImageFile(*fileName));
  96. addToFileList(*fileName);
  97. delete image;
  98. delete fileName;
  99. }
  100. images.clear();
  101. fileNames.clear();
  102. }
  103. }
  104. } // namespace