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

121 lines
2.7 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 <cstdlib>
  22. /**
  23. * Stores basic image data.
  24. */
  25. class image
  26. {
  27. public:
  28. /// Creates an image.
  29. image();
  30. /// Destroys an image.
  31. ~image();
  32. /**
  33. * Changes the format of the image. Existing pixel data will be erased if the format has changed.
  34. *
  35. * @param channels Number of color channels.
  36. * @param hdr Whether or not the image will contain HDR data. HDR pixels are stored as floats. Standard pixels are stored as unsigned chars.
  37. */
  38. void format(unsigned int channels, bool hdr);
  39. /**
  40. * Resizes the image. Existing pixel data will be erased if the size has changed.
  41. *
  42. * @param width New width of the image, in pixels.
  43. * @param height New height of the image, in pixels.
  44. */
  45. void resize(unsigned int width, unsigned int height);
  46. /// Returns whether or not the image contains HDR data.
  47. bool is_hdr() const;
  48. /// Returns the width of the image, in pixels.
  49. unsigned int get_width() const;
  50. /// Returns the height of the image, in pixels.
  51. unsigned int get_height() const;
  52. /// Returns the number of color channels in the image.
  53. unsigned int get_channels() const;
  54. /// Returns a pointer to the pixel data.
  55. const void* get_pixels() const;
  56. /// @copydoc image::get_pixels() const
  57. void* get_pixels();
  58. /// Returns the size of the image, in bytes.
  59. std::size_t get_size() const;
  60. private:
  61. void allocate_pixels();
  62. void free_pixels();
  63. bool hdr;
  64. unsigned int width;
  65. unsigned int height;
  66. unsigned int channels;
  67. void* pixels;
  68. std::size_t size;
  69. };
  70. inline bool image::is_hdr() const
  71. {
  72. return hdr;
  73. }
  74. inline unsigned int image::get_width() const
  75. {
  76. return width;
  77. }
  78. inline unsigned int image::get_height() const
  79. {
  80. return height;
  81. }
  82. inline unsigned int image::get_channels() const
  83. {
  84. return channels;
  85. }
  86. inline const void* image::get_pixels() const
  87. {
  88. return pixels;
  89. }
  90. inline void* image::get_pixels()
  91. {
  92. return pixels;
  93. }
  94. inline std::size_t image::get_size() const
  95. {
  96. return size;
  97. }
  98. #endif // ANTKEEPER_IMAGE_HPP