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

141 lines
3.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. #include "image.hpp"
  20. #include <cstring>
  21. #include <stdexcept>
  22. image::image(const image& source)
  23. {
  24. *this = source;
  25. }
  26. image::image():
  27. width(0),
  28. height(0),
  29. component_size(0),
  30. channel_count(0),
  31. pixel_size(0),
  32. pixels(nullptr),
  33. size(0)
  34. {}
  35. image::~image()
  36. {
  37. free_pixels();
  38. }
  39. image& image::operator=(const image& source)
  40. {
  41. format(source.component_size, source.channel_count);
  42. resize(source.width, source.height);
  43. std::memcpy(pixels, source.pixels, size);
  44. return *this;
  45. }
  46. bool image::compatible(const image& other) const
  47. {
  48. return (other.component_size == component_size && other.channel_count == channel_count);
  49. }
  50. void image::copy(const image& source, unsigned int w, unsigned int h, unsigned int from_x, int unsigned from_y, unsigned int to_x, unsigned int to_y)
  51. {
  52. if (!compatible(source))
  53. {
  54. throw std::runtime_error("Cannot copy image with mismatched format");
  55. }
  56. const unsigned char* from_pixels = static_cast<const unsigned char*>(source.pixels);
  57. unsigned char* to_pixels = static_cast<unsigned char*>(pixels);
  58. for (unsigned int i = 0; i < h; ++i)
  59. {
  60. // Calculate vertical pixel offset
  61. unsigned int from_i = from_y + i;
  62. unsigned int to_i = to_y + i;
  63. // Bounds check
  64. if (from_i >= source.height || to_i >= height)
  65. break;
  66. for (unsigned int j = 0; j < w; ++j)
  67. {
  68. // Calculate horizontal pixel offsets
  69. unsigned int from_j = from_x + j;
  70. unsigned int to_j = to_x + j;
  71. // Bounds check
  72. if (from_j >= source.width || to_j >= width)
  73. continue;
  74. // Calculate pixel data offset (in bytes)
  75. std::size_t from_offset = (from_i * source.width + from_j) * pixel_size;
  76. std::size_t to_offset = (to_i * width + to_j) * pixel_size;
  77. // Copy single pixel
  78. std::memcpy(to_pixels + to_offset, from_pixels + from_offset, pixel_size);
  79. }
  80. }
  81. }
  82. void image::format(std::size_t component_size, std::size_t channel_count)
  83. {
  84. if (this->component_size == component_size && this->channel_count == channel_count)
  85. return;
  86. free_pixels();
  87. this->component_size = component_size;
  88. this->channel_count = channel_count;
  89. pixel_size = component_size * channel_count;
  90. size = width * height * pixel_size;
  91. allocate_pixels();
  92. }
  93. void image::resize(unsigned int width, unsigned int height)
  94. {
  95. if (this->width == width && this->height == height)
  96. {
  97. return;
  98. }
  99. free_pixels();
  100. this->width = width;
  101. this->height = height;
  102. size = width * height * pixel_size;
  103. allocate_pixels();
  104. }
  105. void image::allocate_pixels()
  106. {
  107. if (size != 0)
  108. {
  109. pixels = new unsigned char[size];
  110. }
  111. }
  112. void image::free_pixels()
  113. {
  114. if (pixels != nullptr)
  115. {
  116. delete[] reinterpret_cast<unsigned char*>(pixels);
  117. pixels = nullptr;
  118. size = 0;
  119. }
  120. }