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

95 lines
1.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. #include "image.hpp"
  20. image::image():
  21. hdr(false),
  22. width(0),
  23. height(0),
  24. channels(4),
  25. pixels(nullptr),
  26. size(0)
  27. {}
  28. image::~image()
  29. {
  30. free_pixels();
  31. }
  32. void image::format(unsigned int channels, bool hdr)
  33. {
  34. if (this->channels == channels && this->hdr == hdr)
  35. {
  36. return;
  37. }
  38. free_pixels();
  39. this->channels = channels;
  40. this->hdr = hdr;
  41. allocate_pixels();
  42. }
  43. void image::resize(unsigned int width, unsigned int height)
  44. {
  45. if (this->width == width && this->height == height)
  46. {
  47. return;
  48. }
  49. free_pixels();
  50. this->width = width;
  51. this->height = height;
  52. allocate_pixels();
  53. }
  54. void image::allocate_pixels()
  55. {
  56. size = width * height * channels;
  57. if (size != 0)
  58. {
  59. if (hdr)
  60. {
  61. pixels = new float[size];
  62. }
  63. else
  64. {
  65. pixels = new unsigned char[size];
  66. }
  67. }
  68. }
  69. void image::free_pixels()
  70. {
  71. if (pixels != nullptr)
  72. {
  73. if (hdr)
  74. {
  75. delete[] reinterpret_cast<float*>(pixels);
  76. }
  77. else
  78. {
  79. delete[] reinterpret_cast<unsigned char*>(pixels);
  80. }
  81. pixels = nullptr;
  82. size = 0;
  83. }
  84. }