Vid.hpp 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. #ifndef VID_HPP
  2. #define VID_HPP
  3. #pragma warning (disable:4786)
  4. #include <windows.h>
  5. #include <cstdlib>
  6. #include <string>
  7. #include <map>
  8. #include <exception>
  9. namespace Vid
  10. {
  11. const enum Format { uyvy }; // format = uyvy;
  12. enum { rate = 2997, scale = 100 };
  13. class FileError : public exception
  14. {
  15. public:
  16. explicit FileError(const char* message);
  17. explicit FileError(DWORD id);
  18. const char* what() const;
  19. private:
  20. std::string message;
  21. };
  22. bool nameIsOK(const char* name);
  23. typedef __int64 offset_type;
  24. typedef unsigned __int64 size_type;
  25. class File
  26. {
  27. public:
  28. static const Format format;
  29. enum mode_t {in, out};
  30. File();
  31. File(const char* name, mode_t mode, int iWidth = 0, int iHeight = 0);
  32. ~File();
  33. void open(
  34. const char* name,
  35. mode_t mode,
  36. DWORD flags = FILE_ATTRIBUTE_NORMAL);
  37. void close();
  38. void reset(int frameNum) const;
  39. void advance(int count) const;
  40. void read(void* frame) const;
  41. void write(const void* frame);
  42. bool isOpen() const;
  43. bool eof() const;
  44. mode_t mode() const;
  45. const char* name() const;
  46. int frameNum() const;
  47. int frameCount() const;
  48. size_type size() const;
  49. void dimensions(int iWidth, int iHeight);
  50. int width() const;
  51. int height() const;
  52. int frameSize() const;
  53. private:
  54. File& operator=(const File&);
  55. File(const File&);
  56. HANDLE m_handle;
  57. // When opening for write with FILE_FLAG_OVERLAPPED
  58. HANDLE m_hEvent;
  59. OVERLAPPED m_overlapped;
  60. mutable int frameNum_;
  61. int frameCount_;
  62. char name_[_MAX_PATH];
  63. mode_t mode_;
  64. int m_iWidth;
  65. int m_iHeight;
  66. int m_iFrameSize;
  67. };
  68. class MappedFile
  69. {
  70. public:
  71. //For now, just implement read-only.
  72. MappedFile();
  73. MappedFile(const char* name, int iWidth = 0, int iHeight = 0);
  74. ~MappedFile();
  75. void open(const char* name);
  76. void close();
  77. const char* name() const;
  78. int frameCount() const;
  79. void* map(int frameNum) const;
  80. void unmap(int frameNum) const;
  81. void unmap() const;
  82. bool isMapped(int frameNum) const;
  83. int mapCount() const;
  84. void dimensions(int iWidth, int iHeight);
  85. int width() const;
  86. int height() const;
  87. int frameSize() const;
  88. private:
  89. MappedFile(const MappedFile&);
  90. MappedFile& operator=(const MappedFile&);
  91. void init(int iWidth = 0, int iHeight = 0);
  92. HANDLE file;
  93. HANDLE mapping;
  94. DWORD allocationGranularity;
  95. int m_frameCount;
  96. struct ViewInfo
  97. {
  98. void* view;
  99. void* frame;
  100. };
  101. typedef std::map<int, ViewInfo> Views;
  102. mutable Views views;
  103. std::string m_name;
  104. int m_iWidth;
  105. int m_iHeight;
  106. int m_iFrameSize;
  107. };
  108. }
  109. #endif