💿🐜 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.

169 lines
4.3 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 <cstddef>
  22. /**
  23. * Stores basic image data.
  24. */
  25. class image
  26. {
  27. public:
  28. /**
  29. * Creates a copy of another image.
  30. *
  31. * @param source Image from which to copy.
  32. */
  33. image(const image& source);
  34. /// Creates an image.
  35. image();
  36. /// Destroys an image.
  37. ~image();
  38. /**
  39. * Makes this image a copy of another image.
  40. *
  41. * @param source Image from which to copy.
  42. */
  43. image& operator=(const image& source);
  44. /**
  45. * Checks whether another image has the same number of channels and pixel size as this image.
  46. *
  47. * @param other Image for with which to compare compatibility.
  48. * @return `true` if the image formats are compatible, `false` otherwise.
  49. */
  50. bool compatible(const image& other) const;
  51. /**
  52. * Copies pixel data from another image with a compatible format into this image.
  53. *
  54. * @param source Source image from which to copy pixel data.
  55. * @param w Width of the subimage to copy.
  56. * @param h Height of the subimage to copy.
  57. * @param from_x X-coordinate of the first pixel to copy from the source subimage.
  58. * @param from_y Y-coordinate of the first pixel to copy from the source subimage.
  59. * @param to_x X-coordinate of the first pixel in the destination subimage.
  60. * @param to_y Y-coordinate of the first pixel in the destination subimage.
  61. *
  62. * @except std::runtime_error Cannot copy image with mismatched format.
  63. *
  64. * @see image::compatible(const image&) const
  65. */
  66. 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);
  67. /**
  68. * Changes the format of the image. Existing pixel data will be erased if the format has changed.
  69. *
  70. * @param component_size Size of channel components, in bytes.
  71. * @param channel_count Number of channels in the image.
  72. */
  73. void format(std::size_t component_size, std::size_t channel_count);
  74. /**
  75. * Resizes the image. Existing pixel data will be erased if the size has changed.
  76. *
  77. * @param width New width of the image, in pixels.
  78. * @param height New height of the image, in pixels.
  79. */
  80. void resize(unsigned int width, unsigned int height);
  81. /// Returns the width of the image, in pixels.
  82. unsigned int get_width() const;
  83. /// Returns the height of the image, in pixels.
  84. unsigned int get_height() const;
  85. /// Returns the size of channel components, in bytes.
  86. std::size_t get_component_size() const;
  87. /// Returns the number of color channels in the image.
  88. std::size_t get_channel_count() const;
  89. /// Returns a pointer to the pixel data.
  90. const void* get_pixels() const;
  91. /// @copydoc image::get_pixels() const
  92. void* get_pixels();
  93. /// Returns the size of a single pixel, in bytes.
  94. std::size_t get_pixel_size() const;
  95. /// Returns the size of the image, in bytes.
  96. std::size_t get_size() const;
  97. private:
  98. void allocate_pixels();
  99. void free_pixels();
  100. unsigned int width;
  101. unsigned int height;
  102. std::size_t component_size;
  103. std::size_t channel_count;
  104. void* pixels;
  105. std::size_t pixel_size;
  106. std::size_t size;
  107. };
  108. inline unsigned int image::get_width() const
  109. {
  110. return width;
  111. }
  112. inline unsigned int image::get_height() const
  113. {
  114. return height;
  115. }
  116. inline std::size_t image::get_component_size() const
  117. {
  118. return component_size;
  119. }
  120. inline std::size_t image::get_channel_count() const
  121. {
  122. return channel_count;
  123. }
  124. inline const void* image::get_pixels() const
  125. {
  126. return pixels;
  127. }
  128. inline void* image::get_pixels()
  129. {
  130. return pixels;
  131. }
  132. inline std::size_t image::get_pixel_size() const
  133. {
  134. return pixel_size;
  135. }
  136. inline std::size_t image::get_size() const
  137. {
  138. return size;
  139. }
  140. #endif // ANTKEEPER_IMAGE_HPP