camera_feed.h 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /**************************************************************************/
  2. /* camera_feed.h */
  3. /**************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /**************************************************************************/
  8. /* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
  9. /* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
  10. /* */
  11. /* Permission is hereby granted, free of charge, to any person obtaining */
  12. /* a copy of this software and associated documentation files (the */
  13. /* "Software"), to deal in the Software without restriction, including */
  14. /* without limitation the rights to use, copy, modify, merge, publish, */
  15. /* distribute, sublicense, and/or sell copies of the Software, and to */
  16. /* permit persons to whom the Software is furnished to do so, subject to */
  17. /* the following conditions: */
  18. /* */
  19. /* The above copyright notice and this permission notice shall be */
  20. /* included in all copies or substantial portions of the Software. */
  21. /* */
  22. /* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
  23. /* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
  24. /* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
  25. /* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
  26. /* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
  27. /* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
  28. /* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
  29. /**************************************************************************/
  30. #ifndef CAMERA_FEED_H
  31. #define CAMERA_FEED_H
  32. #include "core/io/image.h"
  33. #include "core/math/transform_2d.h"
  34. #include "servers/camera_server.h"
  35. /**
  36. The camera server is a singleton object that gives access to the various
  37. camera feeds that can be used as the background for our environment.
  38. **/
  39. class CameraFeed : public RefCounted {
  40. GDCLASS(CameraFeed, RefCounted);
  41. public:
  42. enum FeedDataType {
  43. FEED_NOIMAGE, // we don't have an image yet
  44. FEED_RGB, // our texture will contain a normal RGB texture that can be used directly
  45. FEED_YCBCR, // our texture will contain a YCbCr texture that needs to be converted to RGB before output
  46. FEED_YCBCR_SEP, // our camera is split into two textures, first plane contains Y data, second plane contains CbCr data
  47. FEED_EXTERNAL, // specific for android atm, camera feed is managed externally, assumed RGB for now
  48. };
  49. enum FeedPosition {
  50. FEED_UNSPECIFIED, // we have no idea
  51. FEED_FRONT, // this is a camera on the front of the device
  52. FEED_BACK // this is a camera on the back of the device
  53. };
  54. private:
  55. int id; // unique id for this, for internal use in case feeds are removed
  56. protected:
  57. struct FeedFormat {
  58. int width = 0;
  59. int height = 0;
  60. String format;
  61. int frame_numerator = 0;
  62. int frame_denominator = 0;
  63. uint32_t pixel_format = 0;
  64. };
  65. String name; // name of our camera feed
  66. FeedDataType datatype; // type of texture data stored
  67. FeedPosition position; // position of camera on the device
  68. Transform2D transform; // display transform
  69. int base_width = 0;
  70. int base_height = 0;
  71. Vector<FeedFormat> formats;
  72. Dictionary parameters;
  73. int selected_format = -1;
  74. bool active; // only when active do we actually update the camera texture each frame
  75. RID texture[CameraServer::FEED_IMAGES]; // texture images needed for this
  76. static void _bind_methods();
  77. public:
  78. int get_id() const;
  79. bool is_active() const;
  80. void set_active(bool p_is_active);
  81. String get_name() const;
  82. void set_name(String p_name);
  83. int get_base_width() const;
  84. int get_base_height() const;
  85. FeedPosition get_position() const;
  86. void set_position(FeedPosition p_position);
  87. Transform2D get_transform() const;
  88. void set_transform(const Transform2D &p_transform);
  89. RID get_texture(CameraServer::FeedImage p_which);
  90. uint64_t get_texture_tex_id(CameraServer::FeedImage p_which);
  91. CameraFeed();
  92. CameraFeed(String p_name, FeedPosition p_position = CameraFeed::FEED_UNSPECIFIED);
  93. virtual ~CameraFeed();
  94. FeedDataType get_datatype() const;
  95. void set_rgb_image(const Ref<Image> &p_rgb_img);
  96. void set_ycbcr_image(const Ref<Image> &p_ycbcr_img);
  97. void set_ycbcr_images(const Ref<Image> &p_y_img, const Ref<Image> &p_cbcr_img);
  98. void set_external(int p_width, int p_height);
  99. virtual bool set_format(int p_index, const Dictionary &p_parameters);
  100. virtual Array get_formats() const;
  101. virtual FeedFormat get_format() const;
  102. virtual bool activate_feed();
  103. virtual void deactivate_feed();
  104. GDVIRTUAL0R(bool, _activate_feed)
  105. GDVIRTUAL0(_deactivate_feed)
  106. };
  107. VARIANT_ENUM_CAST(CameraFeed::FeedDataType);
  108. VARIANT_ENUM_CAST(CameraFeed::FeedPosition);
  109. #endif // CAMERA_FEED_H