Layer.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2022, Tobias Christiansen <tobyase@serenityos.org>
  4. * Copyright (c) 2022, Timothy Slater <tslater2006@gmail.com>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Layer.h"
  9. #include "Image.h"
  10. #include "Selection.h"
  11. #include <AK/RefPtr.h>
  12. #include <AK/Try.h>
  13. #include <LibGfx/Bitmap.h>
  14. #include <LibGfx/Painter.h>
  15. namespace PixelPaint {
  16. ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_with_size(Image& image, Gfx::IntSize size, DeprecatedString name)
  17. {
  18. VERIFY(!size.is_empty());
  19. if (size.width() > 16384 || size.height() > 16384)
  20. return Error::from_string_literal("Layer size too large");
  21. auto bitmap = TRY(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size));
  22. return adopt_nonnull_ref_or_enomem(new (nothrow) Layer(image, move(bitmap), move(name)));
  23. }
  24. ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_with_bitmap(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, DeprecatedString name)
  25. {
  26. VERIFY(!bitmap->size().is_empty());
  27. if (bitmap->size().width() > 16384 || bitmap->size().height() > 16384)
  28. return Error::from_string_literal("Layer size too large");
  29. return adopt_nonnull_ref_or_enomem(new (nothrow) Layer(image, bitmap, move(name)));
  30. }
  31. ErrorOr<NonnullRefPtr<Layer>> Layer::try_create_snapshot(Image& image, Layer const& layer)
  32. {
  33. auto bitmap = TRY(layer.content_bitmap().clone());
  34. auto snapshot = TRY(try_create_with_bitmap(image, move(bitmap), layer.name()));
  35. /*
  36. We set these properties directly because calling the setters might
  37. notify the image of an update on the newly created layer, but this
  38. layer has not yet been added to the image.
  39. */
  40. snapshot->m_opacity_percent = layer.opacity_percent();
  41. snapshot->m_visible = layer.is_visible();
  42. snapshot->set_selected(layer.is_selected());
  43. snapshot->set_location(layer.location());
  44. return snapshot;
  45. }
  46. Layer::Layer(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, DeprecatedString name)
  47. : m_image(image)
  48. , m_name(move(name))
  49. , m_content_bitmap(move(bitmap))
  50. , m_cached_display_bitmap(m_content_bitmap)
  51. {
  52. }
  53. void Layer::did_modify_bitmap(Gfx::IntRect const& rect)
  54. {
  55. if (!m_scratch_edited_bitmap.is_null()) {
  56. for (int y = 0; y < rect.height(); ++y) {
  57. for (int x = 0; x < rect.width(); ++x) {
  58. Gfx::IntPoint next_point = { rect.left() + x, rect.top() + y };
  59. if (!m_scratch_edited_bitmap->rect().contains(next_point))
  60. continue;
  61. if (this->image().selection().is_selected(next_point.translated(this->location())))
  62. currently_edited_bitmap().set_pixel(next_point, m_scratch_edited_bitmap->get_pixel(next_point));
  63. else
  64. m_scratch_edited_bitmap->set_pixel(next_point, currently_edited_bitmap().get_pixel(next_point));
  65. }
  66. }
  67. }
  68. m_image.layer_did_modify_bitmap({}, *this, rect);
  69. update_cached_bitmap();
  70. }
  71. void Layer::set_visible(bool visible)
  72. {
  73. if (m_visible == visible)
  74. return;
  75. m_visible = visible;
  76. m_image.layer_did_modify_properties({}, *this);
  77. }
  78. void Layer::set_opacity_percent(int opacity_percent)
  79. {
  80. if (m_opacity_percent == opacity_percent)
  81. return;
  82. m_opacity_percent = opacity_percent;
  83. m_image.layer_did_modify_properties({}, *this);
  84. }
  85. void Layer::set_name(DeprecatedString name)
  86. {
  87. if (m_name == name)
  88. return;
  89. m_name = move(name);
  90. m_image.layer_did_modify_properties({}, *this);
  91. }
  92. Gfx::Bitmap& Layer::get_scratch_edited_bitmap()
  93. {
  94. if (this->image().selection().is_empty()) {
  95. m_scratch_edited_bitmap = nullptr;
  96. return currently_edited_bitmap();
  97. }
  98. if (!m_scratch_edited_bitmap.is_null())
  99. return *m_scratch_edited_bitmap;
  100. m_scratch_edited_bitmap = MUST(currently_edited_bitmap().clone());
  101. return *m_scratch_edited_bitmap;
  102. }
  103. RefPtr<Gfx::Bitmap> Layer::try_copy_bitmap(Selection const& selection) const
  104. {
  105. if (selection.is_empty()) {
  106. return {};
  107. }
  108. auto selection_rect = selection.bounding_rect();
  109. auto bitmap_or_error = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, selection_rect.size());
  110. if (bitmap_or_error.is_error())
  111. return nullptr;
  112. auto result = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  113. VERIFY(result->has_alpha_channel());
  114. for (int y = selection_rect.top(); y <= selection_rect.bottom(); y++) {
  115. for (int x = selection_rect.left(); x <= selection_rect.right(); x++) {
  116. Gfx::IntPoint image_point { x, y };
  117. auto layer_point = image_point - m_location;
  118. auto result_point = image_point - selection_rect.top_left();
  119. if (!m_content_bitmap->physical_rect().contains(layer_point)) {
  120. result->set_pixel(result_point, Gfx::Color::Transparent);
  121. continue;
  122. }
  123. auto pixel = m_content_bitmap->get_pixel(layer_point);
  124. // Widen to int before multiplying to avoid overflow issues
  125. auto pixel_alpha = static_cast<int>(pixel.alpha());
  126. auto selection_alpha = static_cast<int>(selection.get_selection_alpha(image_point));
  127. auto new_alpha = (pixel_alpha * selection_alpha) / 0xFF;
  128. pixel.set_alpha(static_cast<u8>(clamp(new_alpha, 0, 0xFF)));
  129. result->set_pixel(result_point, pixel);
  130. }
  131. }
  132. return result;
  133. }
  134. void Layer::erase_selection(Selection const& selection)
  135. {
  136. auto const image_and_selection_intersection = m_image.rect().intersected(selection.bounding_rect());
  137. auto const translated_to_layer_space = image_and_selection_intersection.translated(-location());
  138. for (int y = translated_to_layer_space.top(); y < translated_to_layer_space.top() + translated_to_layer_space.height(); ++y) {
  139. for (int x = translated_to_layer_space.left(); x < translated_to_layer_space.left() + translated_to_layer_space.width(); ++x) {
  140. // Selection is still in pre-translated coordinates, account for this by adding the layer's relative location
  141. if (content_bitmap().rect().contains(x, y) && selection.is_selected(x + location().x(), y + location().y())) {
  142. content_bitmap().set_pixel(x, y, Color::Transparent);
  143. }
  144. }
  145. }
  146. did_modify_bitmap(translated_to_layer_space);
  147. }
  148. ErrorOr<void> Layer::try_set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask)
  149. {
  150. if (mask && content->size() != mask->size())
  151. return Error::from_string_literal("Layer content and mask must be same size");
  152. m_content_bitmap = move(content);
  153. m_mask_bitmap = move(mask);
  154. m_scratch_edited_bitmap = nullptr;
  155. update_cached_bitmap();
  156. return {};
  157. }
  158. void Layer::flip(Gfx::Orientation orientation)
  159. {
  160. m_content_bitmap = *m_content_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
  161. if (m_mask_bitmap)
  162. m_mask_bitmap = *m_mask_bitmap->flipped(orientation).release_value_but_fixme_should_propagate_errors();
  163. did_modify_bitmap();
  164. }
  165. void Layer::rotate(Gfx::RotationDirection direction)
  166. {
  167. m_content_bitmap = *m_content_bitmap->rotated(direction).release_value_but_fixme_should_propagate_errors();
  168. if (m_mask_bitmap)
  169. m_mask_bitmap = *m_mask_bitmap->rotated(direction).release_value_but_fixme_should_propagate_errors();
  170. did_modify_bitmap();
  171. }
  172. void Layer::crop(Gfx::IntRect const& rect)
  173. {
  174. m_content_bitmap = *m_content_bitmap->cropped(rect).release_value_but_fixme_should_propagate_errors();
  175. if (m_mask_bitmap)
  176. m_mask_bitmap = *m_mask_bitmap->cropped(rect).release_value_but_fixme_should_propagate_errors();
  177. did_modify_bitmap();
  178. }
  179. void Layer::resize(Gfx::IntSize new_size, Gfx::IntPoint new_location, Gfx::Painter::ScalingMode scaling_mode)
  180. {
  181. auto src_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), size());
  182. auto dst_rect = Gfx::IntRect(Gfx::IntPoint(0, 0), new_size);
  183. {
  184. auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
  185. Gfx::Painter painter(dst);
  186. if (scaling_mode == Gfx::Painter::ScalingMode::None) {
  187. painter.blit(src_rect.top_left(), *m_content_bitmap, src_rect, 1.0f);
  188. } else {
  189. painter.draw_scaled_bitmap(dst_rect, *m_content_bitmap, src_rect, 1.0f, scaling_mode);
  190. }
  191. m_content_bitmap = move(dst);
  192. }
  193. if (m_mask_bitmap) {
  194. auto dst = Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, new_size).release_value_but_fixme_should_propagate_errors();
  195. Gfx::Painter painter(dst);
  196. if (scaling_mode == Gfx::Painter::ScalingMode::None) {
  197. painter.blit(src_rect.top_left(), *m_content_bitmap, src_rect, 1.0f);
  198. } else {
  199. painter.draw_scaled_bitmap(dst_rect, *m_mask_bitmap, src_rect, 1.0f, scaling_mode);
  200. }
  201. m_mask_bitmap = move(dst);
  202. }
  203. set_location(new_location);
  204. did_modify_bitmap();
  205. }
  206. void Layer::resize(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode)
  207. {
  208. resize(new_rect.size(), new_rect.location(), scaling_mode);
  209. }
  210. void Layer::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode)
  211. {
  212. resize(new_size, location(), scaling_mode);
  213. }
  214. void Layer::update_cached_bitmap()
  215. {
  216. if (!is_masked()) {
  217. if (m_content_bitmap.ptr() == m_cached_display_bitmap.ptr())
  218. return;
  219. m_cached_display_bitmap = m_content_bitmap;
  220. return;
  221. }
  222. if (m_cached_display_bitmap.ptr() == m_content_bitmap.ptr() || m_cached_display_bitmap->size() != size()) {
  223. m_cached_display_bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRA8888, size()));
  224. }
  225. // FIXME: This can probably be done nicer
  226. m_cached_display_bitmap->fill(Color::Transparent);
  227. for (int y = 0; y < size().height(); ++y) {
  228. for (int x = 0; x < size().width(); ++x) {
  229. auto opacity_multiplier = (float)m_mask_bitmap->get_pixel(x, y).to_grayscale().red() / 255;
  230. auto content_color = m_content_bitmap->get_pixel(x, y);
  231. content_color.set_alpha(content_color.alpha() * opacity_multiplier);
  232. m_cached_display_bitmap->set_pixel(x, y, content_color);
  233. }
  234. }
  235. }
  236. void Layer::create_mask()
  237. {
  238. m_mask_bitmap = MUST(Gfx::Bitmap::try_create(Gfx::BitmapFormat::BGRx8888, size()));
  239. m_mask_bitmap->fill(Gfx::Color::White);
  240. update_cached_bitmap();
  241. }
  242. Gfx::Bitmap& Layer::currently_edited_bitmap()
  243. {
  244. switch (edit_mode()) {
  245. case EditMode::Mask:
  246. if (is_masked())
  247. return *mask_bitmap();
  248. [[fallthrough]];
  249. case EditMode::Content:
  250. return content_bitmap();
  251. }
  252. VERIFY_NOT_REACHED();
  253. }
  254. void Layer::set_edit_mode(Layer::EditMode mode)
  255. {
  256. if (m_edit_mode == mode)
  257. return;
  258. m_scratch_edited_bitmap = nullptr;
  259. m_edit_mode = mode;
  260. }
  261. Optional<Gfx::IntRect> Layer::nonempty_content_bounding_rect() const
  262. {
  263. Optional<int> min_content_y;
  264. Optional<int> min_content_x;
  265. Optional<int> max_content_y;
  266. Optional<int> max_content_x;
  267. for (int y = 0; y < m_content_bitmap->height(); ++y) {
  268. for (int x = 0; x < m_content_bitmap->width(); ++x) {
  269. auto color = m_content_bitmap->get_pixel(x, y);
  270. if (color.alpha() == 0)
  271. continue;
  272. min_content_x = min(min_content_x.value_or(x), x);
  273. min_content_y = min(min_content_y.value_or(y), y);
  274. max_content_x = max(max_content_x.value_or(x), x);
  275. max_content_y = max(max_content_y.value_or(y), y);
  276. }
  277. }
  278. if (!min_content_x.has_value())
  279. return {};
  280. return Gfx::IntRect {
  281. *min_content_x,
  282. *min_content_y,
  283. *max_content_x - *min_content_x + 1,
  284. *max_content_y - *min_content_y + 1
  285. };
  286. }
  287. }