Visual Servoing Platform  version 3.2.1 under development (2019-07-18)
vpVideoReader.cpp
1 /****************************************************************************
2  *
3  * ViSP, open source Visual Servoing Platform software.
4  * Copyright (C) 2005 - 2019 by Inria. All rights reserved.
5  *
6  * This software is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  * See the file LICENSE.txt at the root directory of this source
11  * distribution for additional information about the GNU GPL.
12  *
13  * For using ViSP with software that can not be combined with the GNU
14  * GPL, please contact Inria about acquiring a ViSP Professional
15  * Edition License.
16  *
17  * See http://visp.inria.fr for more information.
18  *
19  * This software was developed at:
20  * Inria Rennes - Bretagne Atlantique
21  * Campus Universitaire de Beaulieu
22  * 35042 Rennes Cedex
23  * France
24  *
25  * If you have questions regarding the use of this file, please contact
26  * Inria at visp@inria.fr
27  *
28  * This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE
29  * WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
30  *
31  * Description:
32  * Read videos and image sequences.
33  *
34  * Authors:
35  * Nicolas Melchior
36  * Fabien Spindler
37  *
38  *****************************************************************************/
39 
45 #include <visp3/core/vpDebug.h>
46 #include <visp3/core/vpIoTools.h>
47 #include <visp3/io/vpVideoReader.h>
48 
49 #include <cctype>
50 #include <fstream>
51 #include <iostream>
52 #include <limits> // numeric_limits
53 
58  : vpFrameGrabber(), imSequence(NULL),
59 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
60  capture(), frame(),
61 #endif
62  formatType(FORMAT_UNKNOWN), initFileName(false), isOpen(false), frameCount(0), firstFrame(0), lastFrame(0),
63  firstFrameIndexIsSet(false), lastFrameIndexIsSet(false), frameStep(1), frameRate(0.)
64 {
65 }
66 
71 {
72  if (imSequence != NULL) {
73  delete imSequence;
74  }
75 }
76 
92 void vpVideoReader::setFileName(const char *filename)
93 {
94  if ((!filename) || (*filename == '\0')) {
95  vpERROR_TRACE("filename empty ");
96  throw(vpImageException(vpImageException::noFileNameError, "filename empty "));
97  }
98 
99  if (strlen(filename) >= FILENAME_MAX) {
100  throw(vpException(vpException::memoryAllocationError, "Not enough memory to initialize the file name"));
101  }
102 
103  strcpy(this->fileName, filename);
104 
105  formatType = getFormat(fileName);
106 
107  if (formatType == FORMAT_UNKNOWN) {
108  throw(vpException(vpException::badValue, "Filename extension not supported"));
109  }
110 
111  // checking image name format
112  if (isImageExtensionSupported()) {
113  std::string format = vpIoTools::getName(fileName);
114  if (!checkImageNameFormat(format)) {
115  throw(vpException(vpException::badValue, "Format of image name wasn't recognized: %s", format.c_str()));
116  }
117  }
118 
119  initFileName = true;
120 }
121 
137 void vpVideoReader::setFileName(const std::string &filename) { setFileName(filename.c_str()); }
138 
142 void vpVideoReader::getProperties()
143 {
144  if (!initFileName) {
145  throw(vpImageException(vpImageException::noFileNameError, "The generic filename has to be set"));
146  }
147 
148  if (isImageExtensionSupported()) {
149  imSequence = new vpDiskGrabber;
150  imSequence->setGenericName(fileName);
151  imSequence->setStep(frameStep);
152  if (firstFrameIndexIsSet) {
153  imSequence->setImageNumber(firstFrame);
154  }
155  frameRate = -1.;
156  } else if (isVideoExtensionSupported()) {
157 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
158  capture.open(fileName);
159 
160  if (!capture.isOpened()) {
161  throw(vpException(vpException::ioError, "Could not open the video %s with OpenCV", fileName));
162  }
163 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
164  width = (unsigned int)capture.get(cv::CAP_PROP_FRAME_WIDTH);
165  height = (unsigned int)capture.get(cv::CAP_PROP_FRAME_HEIGHT);
166  frameRate = (double)capture.get(cv::CAP_PROP_FPS);
167 #else
168  width = (unsigned int)capture.get(CV_CAP_PROP_FRAME_WIDTH);
169  height = (unsigned int)capture.get(CV_CAP_PROP_FRAME_HEIGHT);
170  frameRate = capture.get(CV_CAP_PROP_FPS);
171 #endif
172 
173 #else
174  throw(vpException(vpException::fatalError, "To read video files ViSP should be build with opencv "
175  "3rd >= 2.1.0 party libraries."));
176 #endif
177  } else if (formatType == FORMAT_UNKNOWN) {
178  // vpERROR_TRACE("The format of the file does not correspond to a readable
179  // format.");
180  throw(vpException(vpException::fatalError, "The format of the file does "
181  "not correspond to a readable "
182  "format supported by ViSP."));
183  }
184 
185  findFirstFrameIndex();
186  isOpen = true;
187  findLastFrameIndex();
188 }
189 
198 {
199  getProperties();
200 
201  frameCount = firstFrame;
202  if (!getFrame(I, firstFrame)) {
203  throw(vpException(vpException::ioError, "Could not read the video first frame"));
204  }
205 
206  // Rewind to the first frame since open() should not increase the frame
207  // counter
208  frameCount = firstFrame;
209 
210  if (isVideoExtensionSupported()) {
211 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
212 
213 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
214  capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
215 #else
216  capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
217 #endif
218  frameCount--;
219 #endif
220  }
221 }
222 
231 {
232  getProperties();
233 
234  frameCount = firstFrame;
235  if (!getFrame(I, firstFrame)) {
236  throw(vpException(vpException::ioError, "Could not read the video first frame"));
237  }
238 
239  // Rewind to the first frame since open() should not increase the frame
240  // counter
241  frameCount = firstFrame;
242 
243  if (isVideoExtensionSupported()) {
244 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
245 
246 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
247  capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
248 #else
249  capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
250 #endif
251  frameCount--;
252 #endif
253  }
254 }
255 
267 {
268  if (!isOpen) {
269  open(I);
270  }
271 
272  // getFrame(I,frameCount);
273  if (imSequence != NULL) {
274  imSequence->setStep(frameStep);
275  imSequence->acquire(I);
276  frameCount = imSequence->getImageNumber();
277  if (frameCount + frameStep > lastFrame) {
278  imSequence->setImageNumber(frameCount);
279  } else if (frameCount + frameStep < firstFrame) {
280  imSequence->setImageNumber(frameCount);
281  }
282  }
283 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
284  else {
285  capture >> frame;
286  if (frameStep == 1) {
287  frameCount++;
288  } else {
289 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
290  frameCount = (long)capture.get(cv::CAP_PROP_POS_FRAMES);
291  if (frameStep > 0) {
292  if (frameCount + frameStep <= lastFrame) {
293  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
294  } else {
295  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount - 1);
296  }
297  } else if (frameStep < 0) {
298  if (frameCount + frameStep >= firstFrame) {
299  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
300  } else {
301  capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
302  }
303  }
304 #else
305  frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
306  if (frameStep > 0) {
307  if (frameCount + frameStep <= lastFrame) {
308  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
309  } else {
310  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount - 1);
311  }
312  } else if (frameStep < 0) {
313  if (frameCount + frameStep >= firstFrame) {
314  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
315  } else {
316  capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
317  }
318  }
319 #endif
320  }
321 
322  if (frame.empty())
323  setLastFrameIndex(frameCount - frameStep);
324  else
325  vpImageConvert::convert(frame, I);
326  }
327 #endif
328 }
329 
339 {
340  if (!isOpen) {
341  open(I);
342  }
343 
344  if (imSequence != NULL) {
345  imSequence->setStep(frameStep);
346  imSequence->acquire(I);
347  frameCount = imSequence->getImageNumber();
348  if (frameCount + frameStep > lastFrame) {
349  imSequence->setImageNumber(frameCount);
350  } else if (frameCount + frameStep < firstFrame) {
351  imSequence->setImageNumber(frameCount);
352  }
353  }
354 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
355  else {
356  capture >> frame;
357  if (frameStep == 1) {
358  frameCount++;
359  } else {
360 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
361  frameCount = (long)capture.get(cv::CAP_PROP_POS_FRAMES);
362  if (frameStep > 0) {
363  if (frameCount + frameStep <= lastFrame) {
364  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
365  } else {
366  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount - 1);
367  }
368  } else if (frameStep < 0) {
369  if (frameCount + frameStep >= firstFrame) {
370  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
371  } else {
372  capture.set(cv::CAP_PROP_POS_FRAMES, firstFrame - 1);
373  }
374  }
375 #else
376  frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
377  if (frameStep > 0) {
378  if (frameCount + frameStep <= lastFrame) {
379  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
380  } else {
381  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount - 1);
382  }
383  } else if (frameStep < 0) {
384  if (frameCount + frameStep >= firstFrame) {
385  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount + frameStep - 1);
386  } else {
387  capture.set(CV_CAP_PROP_POS_FRAMES, firstFrame - 1);
388  }
389  }
390 #endif
391  }
392 
393  if (frame.empty())
394  setLastFrameIndex(frameCount - frameStep);
395  else
396  vpImageConvert::convert(frame, I);
397  }
398 #endif
399 }
400 
414 bool vpVideoReader::getFrame(vpImage<vpRGBa> &I, long frame_index)
415 {
416  if (imSequence != NULL) {
417  try {
418  imSequence->acquire(I, frame_index);
419  width = I.getWidth();
420  height = I.getHeight();
421  frameCount = imSequence->getImageNumber();
422  imSequence->setImageNumber(frameCount); // to not increment vpDiskGrabber next image
423  if (frameCount + frameStep > lastFrame) {
424  imSequence->setImageNumber(frameCount);
425  } else if (frameCount + frameStep < firstFrame) {
426  imSequence->setImageNumber(frameCount);
427  }
428  } catch (...) {
429  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
430  return false;
431  }
432  } else {
433 #if defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x030000)
434  if (!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
435  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
436  return false;
437  }
438 
439  capture >> frame;
440  frameCount = frame_index + frameStep; // next index
441  capture.set(cv::CAP_PROP_POS_FRAMES, frameCount);
442  if (frame.empty()) {
443  // New trial that makes things working with opencv 3.0.0
444  capture >> frame;
445  if (frame.empty()) {
446  setLastFrameIndex(frameCount - frameStep);
447  return false;
448  } else {
449  vpImageConvert::convert(frame, I);
450  }
451  } else
452  vpImageConvert::convert(frame, I);
453 #elif defined(VISP_HAVE_OPENCV) && (VISP_HAVE_OPENCV_VERSION >= 0x020100)
454  if (!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
455  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
456  return false;
457  }
458 
459  capture >> frame;
460  frameCount = frame_index + frameStep; // next index
461  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
462  if (frame.empty())
463  setLastFrameIndex(frameCount - frameStep);
464  else
465  vpImageConvert::convert(frame, I);
466 #endif
467  }
468  return true;
469 }
470 
485 {
486  if (imSequence != NULL) {
487  try {
488  imSequence->acquire(I, frame_index);
489  width = I.getWidth();
490  height = I.getHeight();
491  frameCount = imSequence->getImageNumber();
492  imSequence->setImageNumber(frameCount); // to not increment vpDiskGrabber next image
493  if (frameCount + frameStep > lastFrame) {
494  imSequence->setImageNumber(frameCount);
495  } else if (frameCount + frameStep < firstFrame) {
496  imSequence->setImageNumber(frameCount);
497  }
498  } catch (...) {
499  vpERROR_TRACE("Couldn't find the %u th frame", frame_index);
500  return false;
501  }
502  } else {
503 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
504  if (!capture.set(cv::CAP_PROP_POS_FRAMES, frame_index)) {
505  vpERROR_TRACE("Couldn't find the %ld th frame", frame_index);
506  return false;
507  }
508  capture >> frame;
509  if (frame.empty()) {
510  // New trial that makes things working with opencv 3.0.0
511  capture >> frame;
512  if (frame.empty()) {
513  setLastFrameIndex(frameCount - frameStep);
514  return false;
515  } else {
516  vpImageConvert::convert(frame, I);
517  }
518  } else {
519  vpImageConvert::convert(frame, I);
520  }
521 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
522  if (!capture.set(CV_CAP_PROP_POS_FRAMES, frame_index)) {
523  vpERROR_TRACE("Couldn't find the %ld th frame",
524  frame_index); // next index
525  return false;
526  }
527  capture >> frame;
528  frameCount = (long)capture.get(CV_CAP_PROP_POS_FRAMES);
529  if (frameStep > 1) {
530  frameCount += frameStep - 1; // next index
531  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
532  } else if (frameStep < -1) {
533  frameCount += frameStep - 1; // next index
534  capture.set(CV_CAP_PROP_POS_FRAMES, frameCount);
535  }
536  if (frame.empty())
537  setLastFrameIndex(frameCount - frameStep);
538  else
539  vpImageConvert::convert(frame, I);
540 #endif
541  }
542  return true;
543 }
544 
550 vpVideoReader::vpVideoFormatType vpVideoReader::getFormat(const char *filename)
551 {
552  std::string sfilename(filename);
553 
554  std::string ext = vpVideoReader::getExtension(sfilename);
555 
556  if (ext.compare(".PGM") == 0)
557  return FORMAT_PGM;
558  else if (ext.compare(".pgm") == 0)
559  return FORMAT_PGM;
560  else if (ext.compare(".PPM") == 0)
561  return FORMAT_PPM;
562  else if (ext.compare(".ppm") == 0)
563  return FORMAT_PPM;
564  else if (ext.compare(".JPG") == 0)
565  return FORMAT_JPEG;
566  else if (ext.compare(".jpg") == 0)
567  return FORMAT_JPEG;
568  else if (ext.compare(".JPEG") == 0)
569  return FORMAT_JPEG;
570  else if (ext.compare(".jpeg") == 0)
571  return FORMAT_JPEG;
572  else if (ext.compare(".PNG") == 0)
573  return FORMAT_PNG;
574  else if (ext.compare(".png") == 0)
575  return FORMAT_PNG;
576  else if (ext.compare(".TIFF") == 0)
577  return FORMAT_TIFF;
578  else if (ext.compare(".tiff") == 0)
579  return FORMAT_TIFF;
580  else if (ext.compare(".BMP") == 0)
581  return FORMAT_BMP;
582  else if (ext.compare(".bmp") == 0)
583  return FORMAT_BMP;
584  else if (ext.compare(".DIB") == 0)
585  return FORMAT_DIB;
586  else if (ext.compare(".dib") == 0)
587  return FORMAT_DIB;
588  else if (ext.compare(".PBM") == 0)
589  return FORMAT_PBM;
590  else if (ext.compare(".pbm") == 0)
591  return FORMAT_PBM;
592  else if (ext.compare(".SR") == 0)
593  return FORMAT_PBM;
594  else if (ext.compare(".sr") == 0)
595  return FORMAT_PBM;
596  else if (ext.compare(".RAS") == 0)
597  return FORMAT_RASTER;
598  else if (ext.compare(".ras") == 0)
599  return FORMAT_RASTER;
600  else if (ext.compare(".JP2") == 0)
601  return FORMAT_JPEG2000;
602  else if (ext.compare(".jp2") == 0)
603  return FORMAT_JPEG2000;
604  else if (ext.compare(".AVI") == 0)
605  return FORMAT_AVI;
606  else if (ext.compare(".avi") == 0)
607  return FORMAT_AVI;
608  else if (ext.compare(".MPEG") == 0)
609  return FORMAT_MPEG;
610  else if (ext.compare(".mpeg") == 0)
611  return FORMAT_MPEG;
612  else if (ext.compare(".MPG") == 0)
613  return FORMAT_MPEG;
614  else if (ext.compare(".mpg") == 0)
615  return FORMAT_MPEG;
616  else if (ext.compare(".MPEG4") == 0)
617  return FORMAT_MPEG4;
618  else if (ext.compare(".mpeg4") == 0)
619  return FORMAT_MPEG4;
620  else if (ext.compare(".MP4") == 0)
621  return FORMAT_MPEG4;
622  else if (ext.compare(".mp4") == 0)
623  return FORMAT_MPEG4;
624  else if (ext.compare(".MOV") == 0)
625  return FORMAT_MOV;
626  else if (ext.compare(".mov") == 0)
627  return FORMAT_MOV;
628  else if (ext.compare(".OGV") == 0)
629  return FORMAT_OGV;
630  else if (ext.compare(".ogv") == 0)
631  return FORMAT_OGV;
632  else if (ext.compare(".WMV") == 0)
633  return FORMAT_WMV;
634  else if (ext.compare(".wmv") == 0)
635  return FORMAT_WMV;
636  else if (ext.compare(".FLV") == 0)
637  return FORMAT_FLV;
638  else if (ext.compare(".flv") == 0)
639  return FORMAT_FLV;
640  else if (ext.compare(".MKV") == 0)
641  return FORMAT_MKV;
642  else if (ext.compare(".mkv") == 0)
643  return FORMAT_MKV;
644  else
645  return FORMAT_UNKNOWN;
646 }
647 
648 // return the extension of the file including the dot
649 std::string vpVideoReader::getExtension(const std::string &filename)
650 {
651  // extract the extension
652  size_t dot = filename.find_last_of(".");
653  std::string ext = filename.substr(dot, filename.size() - 1);
654  return ext;
655 }
656 
660 void vpVideoReader::findLastFrameIndex()
661 {
662  if (!isOpen) {
663  vpERROR_TRACE("Use the open method before");
664  throw(vpException(vpException::notInitialized, "file not yet opened"));
665  }
666 
667  if (imSequence != NULL) {
668  if (!lastFrameIndexIsSet) {
669  std::string imageNameFormat = vpIoTools::getName(std::string(fileName));
670  std::string dirName = vpIoTools::getParent(std::string(fileName));
671  if (dirName == "") {
672  dirName = ".";
673  }
674  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
675  lastFrame = 0;
676  for (size_t i = 0; i < files.size(); i++) {
677  // Checking that file name satisfies image format,
678  // specified by imageNameFormat, and extracting imageIndex
679  long imageIndex = extractImageIndex(files[i], imageNameFormat);
680  if ((imageIndex != -1) && (imageIndex > lastFrame)) {
681  lastFrame = imageIndex;
682  }
683  }
684  }
685  }
686 
687 #if VISP_HAVE_OPENCV_VERSION >= 0x030000
688  else if (!lastFrameIndexIsSet) {
689  lastFrame = (long)capture.get(cv::CAP_PROP_FRAME_COUNT);
690  if (lastFrame <= 2) // with tutorial/matching/video-postcard.mpeg it
691  // return 2 with OpenCV 3.0.0
692  {
693  // std::cout << "Warning: Problem with cv::CAP_PROP_FRAME_COUNT. We set
694  // video last frame to an arbitrary value (1000)." << std::endl;
695  lastFrame = 100000; // Set lastFrame to an arbitrary value
696  }
697  }
698 #elif VISP_HAVE_OPENCV_VERSION >= 0x020100
699  else if (!lastFrameIndexIsSet) {
700  lastFrame = (long)capture.get(CV_CAP_PROP_FRAME_COUNT);
701  if (lastFrame <= 2) // with tutorial/matching/video-postcard.mpeg it
702  // return 2 with OpenCV 2.4.10
703  {
704  // std::cout << "Warning: Problem with CV_CAP_PROP_FRAME_COUNT. We set
705  // video last frame to an arbitrary value (1000)." << std::endl;
706  lastFrame = 100000; // Set lastFrame to an arbitrary value
707  }
708  }
709 #endif
710 }
711 
715 void vpVideoReader::findFirstFrameIndex()
716 {
717  if (imSequence != NULL) {
718  if (!firstFrameIndexIsSet) {
719  std::string imageNameFormat = vpIoTools::getName(std::string(fileName));
720  std::string dirName = vpIoTools::getParent(std::string(fileName));
721  if (dirName == "") {
722  dirName = ".";
723  }
724  std::vector<std::string> files = vpIoTools::getDirFiles(dirName);
725  firstFrame = -1;
726  for (size_t i = 0; i < files.size(); i++) {
727  // Checking that file name satisfies image format, specified by
728  // imageNameFormat, and extracting imageIndex
729  long imageIndex = extractImageIndex(files[i], imageNameFormat);
730  if ((imageIndex != -1) && (imageIndex < firstFrame || firstFrame == -1)) {
731  firstFrame = imageIndex;
732  }
733  }
734  imSequence->setImageNumber(firstFrame);
735  }
736  }
737 #if VISP_HAVE_OPENCV_VERSION >= 0x020100
738  else if (!firstFrameIndexIsSet) {
739  firstFrame = 1L;
740  }
741 #endif
742 }
743 
747 bool vpVideoReader::isImageExtensionSupported()
748 {
749  return (formatType == FORMAT_PGM || formatType == FORMAT_PPM || formatType == FORMAT_JPEG ||
750  formatType == FORMAT_PNG || formatType == FORMAT_TIFF || formatType == FORMAT_BMP ||
751  formatType == FORMAT_DIB || formatType == FORMAT_PBM || formatType == FORMAT_RASTER ||
752  formatType == FORMAT_JPEG2000);
753 }
754 
758 bool vpVideoReader::isVideoExtensionSupported()
759 {
760  return (formatType == FORMAT_AVI || formatType == FORMAT_MPEG || formatType == FORMAT_MPEG4 ||
761  formatType == FORMAT_MOV || formatType == FORMAT_OGV || formatType == FORMAT_WMV ||
762  formatType == FORMAT_FLV || formatType == FORMAT_MKV);
763 }
764 
790 {
791  this->acquire(I);
792  return *this;
793 }
794 
820 {
821  this->acquire(I);
822  return *this;
823 }
824 
836 long vpVideoReader::extractImageIndex(const std::string &imageName, const std::string &format)
837 {
838  size_t indexBegin = format.find_last_of('%');
839  size_t indexEnd = format.find_first_of('d', indexBegin);
840  size_t suffixLength = format.length() - indexEnd - 1;
841 
842  // Extracting index
843  if (imageName.length() <= suffixLength + indexBegin) {
844  return -1;
845  }
846  size_t indexLength = imageName.length() - suffixLength - indexBegin;
847  std::string indexSubstr = imageName.substr(indexBegin, indexLength);
848  std::istringstream ss(indexSubstr);
849  long index = 0;
850  ss >> index;
851  if (ss.fail() || index < 0 || !ss.eof()) {
852  return -1;
853  }
854 
855  // Checking that format with inserted index equals imageName
856  char nameByFormat[FILENAME_MAX];
857  sprintf(nameByFormat, format.c_str(), index);
858  if (std::string(nameByFormat) != imageName) {
859  return -1;
860  }
861  return index;
862 }
863 
868 bool vpVideoReader::checkImageNameFormat(const std::string &format)
869 {
870  size_t indexBegin = format.find_last_of('%');
871  size_t indexEnd = format.find_first_of('d', indexBegin);
872  if (indexBegin == std::string::npos || indexEnd == std::string::npos) {
873  return false;
874  }
875  for (size_t i = indexBegin + 1; i < indexEnd; i++) {
876  if (!std::isdigit(format[i])) {
877  return false;
878  }
879  }
880  return true;
881 }
Used to indicate that a value is not in the allowed range.
Definition: vpException.h:97
void setGenericName(const std::string &genericName)
unsigned int getWidth() const
Definition: vpImage.h:244
static void convert(const vpImage< unsigned char > &src, vpImage< vpRGBa > &dest)
#define vpERROR_TRACE
Definition: vpDebug.h:393
Class that enables to manipulate easily a video file or a sequence of images. As it inherits from the...
void setStep(long step)
error that can be emited by ViSP classes.
Definition: vpException.h:71
Error that can be emited by the vpImage class and its derivates.
static std::vector< std::string > getDirFiles(const std::string &dirname)
Definition: vpIoTools.cpp:1815
static std::string getParent(const std::string &pathname)
Definition: vpIoTools.cpp:1492
void open(vpImage< vpRGBa > &I)
bool getFrame(vpImage< vpRGBa > &I, long frame)
unsigned int height
Number of rows in the image.
Memory allocation error.
Definition: vpException.h:88
void acquire(vpImage< vpRGBa > &I)
void setImageNumber(long number)
void setFileName(const char *filename)
vpVideoReader & operator>>(vpImage< unsigned char > &I)
Class to grab (ie. read) images from the disk.
static std::string getName(const std::string &pathname)
Definition: vpIoTools.cpp:1457
Base class for all video devices. It is designed to provide a front end to video sources.
virtual ~vpVideoReader()
long getImageNumber()
void setLastFrameIndex(const long last_frame)
Used to indicate that a parameter is not initialized.
Definition: vpException.h:98
unsigned int getHeight() const
Definition: vpImage.h:186
void acquire(vpImage< unsigned char > &I)
unsigned int width
Number of columns in the image.