💿🐜 Antkeeper source code https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

229 lines
6.4 KiB

  1. /*
  2. * Copyright (C) 2021 Christopher J. Howard
  3. *
  4. * This file is part of Antkeeper source code.
  5. *
  6. * Antkeeper source code 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 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Antkeeper source code is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #ifndef ANTKEEPER_IMAGE_HPP
  20. #define ANTKEEPER_IMAGE_HPP
  21. #include "math/vector.hpp"
  22. #include <cstddef>
  23. #include <type_traits>
  24. /**
  25. * Stores basic image data.
  26. */
  27. class image
  28. {
  29. public:
  30. /**
  31. * Creates a copy of another image.
  32. *
  33. * @param source Image from which to copy.
  34. */
  35. image(const image& source);
  36. /// Creates an image.
  37. image();
  38. /// Destroys an image.
  39. ~image();
  40. /**
  41. * Makes this image a copy of another image.
  42. *
  43. * @param source Image from which to copy.
  44. */
  45. image& operator=(const image& source);
  46. /**
  47. * Returns an iterator to the first pixel.
  48. *
  49. * @tparam T Pixel data type.
  50. */
  51. /// @{
  52. template <class T>
  53. constexpr inline T* begin() noexcept
  54. {
  55. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  56. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  57. return static_cast<T*>(pixels);
  58. }
  59. template <class T>
  60. constexpr inline const T* begin() const noexcept
  61. {
  62. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  63. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  64. return static_cast<const T*>(pixels);
  65. }
  66. template <class T>
  67. constexpr inline const T* cbegin() const noexcept
  68. {
  69. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  70. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  71. return static_cast<const T*>(pixels);
  72. }
  73. /// @}
  74. /**
  75. * Returns an iterator to the pixel following the last pixel.
  76. *
  77. * @tparam T Pixel data type.
  78. */
  79. /// @{
  80. template <class T>
  81. constexpr inline T* end() noexcept
  82. {
  83. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  84. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  85. return static_cast<T*>(static_cast<unsigned char*>(pixels) + size);
  86. }
  87. template <class T>
  88. constexpr inline const T* end() const noexcept
  89. {
  90. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  91. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  92. return static_cast<const T*>(static_cast<const unsigned char*>(pixels) + size);
  93. }
  94. template <class T>
  95. constexpr inline const T* cend() const noexcept
  96. {
  97. static_assert(std::is_standard_layout<T>::value, "Pixel iterator type is not standard-layout.");
  98. static_assert(std::is_trivial<T>::value, "Pixel iterator type is not trivial.");
  99. return static_cast<const T*>(static_cast<const unsigned char*>(pixels) + size);
  100. }
  101. /// @}
  102. /**
  103. * Checks whether another image has the same number of channels and pixel size as this image.
  104. *
  105. * @param other Image for with which to compare compatibility.
  106. * @return `true` if the image formats are compatible, `false` otherwise.
  107. */
  108. bool compatible(const image& other) const;
  109. /**
  110. * Copies pixel data from another image with a compatible format into this image.
  111. *
  112. * @param source Source image from which to copy pixel data.
  113. * @param w Width of the subimage to copy.
  114. * @param h Height of the subimage to copy.
  115. * @param from_x X-coordinate of the first pixel to copy from the source subimage.
  116. * @param from_y Y-coordinate of the first pixel to copy from the source subimage.
  117. * @param to_x X-coordinate of the first pixel in the destination subimage.
  118. * @param to_y Y-coordinate of the first pixel in the destination subimage.
  119. *
  120. * @except std::runtime_error Cannot copy image with mismatched format.
  121. *
  122. * @see image::compatible(const image&) const
  123. */
  124. void copy(const image& source, unsigned int w, unsigned int h, unsigned int from_x = 0, int unsigned from_y = 0, unsigned int to_x = 0, unsigned int to_y = 0);
  125. /**
  126. * Changes the format of the image. Existing pixel data will be erased if the format has changed.
  127. *
  128. * @param component_size Size of channel components, in bytes.
  129. * @param channel_count Number of channels in the image.
  130. */
  131. void format(std::size_t component_size, std::size_t channel_count);
  132. /**
  133. * Resizes the image. Existing pixel data will be erased if the size has changed.
  134. *
  135. * @param width New width of the image, in pixels.
  136. * @param height New height of the image, in pixels.
  137. */
  138. void resize(unsigned int width, unsigned int height);
  139. /// Returns the width of the image, in pixels.
  140. unsigned int get_width() const;
  141. /// Returns the height of the image, in pixels.
  142. unsigned int get_height() const;
  143. /// Returns the size of channel components, in bytes.
  144. std::size_t get_component_size() const;
  145. /// Returns the number of color channels in the image.
  146. std::size_t get_channel_count() const;
  147. /// Returns a pointer to the pixel data.
  148. /// @{
  149. void* data() noexcept;
  150. const void* data() const noexcept;
  151. /// @}
  152. /// Returns the size of a single pixel, in bytes.
  153. std::size_t get_pixel_size() const;
  154. /// Returns the size of the image, in bytes.
  155. std::size_t get_size() const;
  156. private:
  157. void allocate_pixels();
  158. void free_pixels();
  159. unsigned int width;
  160. unsigned int height;
  161. std::size_t component_size;
  162. std::size_t channel_count;
  163. void* pixels;
  164. std::size_t pixel_size;
  165. std::size_t size;
  166. };
  167. inline unsigned int image::get_width() const
  168. {
  169. return width;
  170. }
  171. inline unsigned int image::get_height() const
  172. {
  173. return height;
  174. }
  175. inline std::size_t image::get_component_size() const
  176. {
  177. return component_size;
  178. }
  179. inline std::size_t image::get_channel_count() const
  180. {
  181. return channel_count;
  182. }
  183. inline void* image::data() noexcept
  184. {
  185. return pixels;
  186. }
  187. inline const void* image::data() const noexcept
  188. {
  189. return pixels;
  190. }
  191. inline std::size_t image::get_pixel_size() const
  192. {
  193. return pixel_size;
  194. }
  195. inline std::size_t image::get_size() const
  196. {
  197. return size;
  198. }
  199. #endif // ANTKEEPER_IMAGE_HPP