Layer.cpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413
  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::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::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::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::create_snapshot(Image& image, Layer const& layer)
  32. {
  33. auto bitmap = TRY(layer.content_bitmap().clone());
  34. auto snapshot = TRY(create_with_bitmap(image, move(bitmap), layer.name()));
  35. if (layer.is_masked()) {
  36. snapshot->m_mask_bitmap = TRY(layer.mask_bitmap()->clone());
  37. snapshot->m_edit_mode = layer.m_edit_mode;
  38. }
  39. /*
  40. We set these properties directly because calling the setters might
  41. notify the image of an update on the newly created layer, but this
  42. layer has not yet been added to the image.
  43. */
  44. snapshot->m_opacity_percent = layer.opacity_percent();
  45. snapshot->m_visible = layer.is_visible();
  46. snapshot->set_selected(layer.is_selected());
  47. snapshot->set_location(layer.location());
  48. return snapshot;
  49. }
  50. Layer::Layer(Image& image, NonnullRefPtr<Gfx::Bitmap> bitmap, DeprecatedString name)
  51. : m_image(image)
  52. , m_name(move(name))
  53. , m_content_bitmap(move(bitmap))
  54. , m_cached_display_bitmap(m_content_bitmap)
  55. {
  56. }
  57. void Layer::did_modify_bitmap(Gfx::IntRect const& rect, NotifyClients notify_clients)
  58. {
  59. if (!m_scratch_edited_bitmap.is_null()) {
  60. for (int y = 0; y < rect.height(); ++y) {
  61. for (int x = 0; x < rect.width(); ++x) {
  62. Gfx::IntPoint next_point = { rect.left() + x, rect.top() + y };
  63. if (!m_scratch_edited_bitmap->rect().contains(next_point))
  64. continue;
  65. if (this->image().selection().is_selected(next_point.translated(this->location())))
  66. currently_edited_bitmap().set_pixel(next_point, m_scratch_edited_bitmap->get_pixel(next_point));
  67. else
  68. m_scratch_edited_bitmap->set_pixel(next_point, currently_edited_bitmap().get_pixel(next_point));
  69. }
  70. }
  71. }
  72. // NOTE: If NotifyClients::NO is passed to this function the caller should handle notifying
  73. // the clients of any bitmap changes.
  74. if (notify_clients == NotifyClients::Yes)
  75. m_image.layer_did_modify_bitmap({}, *this, rect);
  76. update_cached_bitmap();
  77. }
  78. void Layer::set_visible(bool visible)
  79. {
  80. if (m_visible == visible)
  81. return;
  82. m_visible = visible;
  83. m_image.layer_did_modify_properties({}, *this);
  84. }
  85. void Layer::set_opacity_percent(int opacity_percent)
  86. {
  87. if (m_opacity_percent == opacity_percent)
  88. return;
  89. m_opacity_percent = opacity_percent;
  90. m_image.layer_did_modify_properties({}, *this);
  91. }
  92. void Layer::set_name(DeprecatedString name)
  93. {
  94. if (m_name == name)
  95. return;
  96. m_name = move(name);
  97. m_image.layer_did_modify_properties({}, *this);
  98. }
  99. Gfx::Bitmap& Layer::get_scratch_edited_bitmap()
  100. {
  101. if (this->image().selection().is_empty()) {
  102. m_scratch_edited_bitmap = nullptr;
  103. return currently_edited_bitmap();
  104. }
  105. if (!m_scratch_edited_bitmap.is_null())
  106. return *m_scratch_edited_bitmap;
  107. m_scratch_edited_bitmap = MUST(currently_edited_bitmap().clone());
  108. return *m_scratch_edited_bitmap;
  109. }
  110. RefPtr<Gfx::Bitmap> Layer::copy_bitmap(Selection const& selection) const
  111. {
  112. if (selection.is_empty())
  113. return {};
  114. auto selection_rect = selection.bounding_rect();
  115. auto bitmap_or_error = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, selection_rect.size());
  116. if (bitmap_or_error.is_error())
  117. return nullptr;
  118. auto result = bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  119. VERIFY(result->has_alpha_channel());
  120. for (int y = selection_rect.top(); y < selection_rect.bottom(); y++) {
  121. for (int x = selection_rect.left(); x < selection_rect.right(); x++) {
  122. Gfx::IntPoint image_point { x, y };
  123. auto layer_point = image_point - m_location;
  124. auto result_point = image_point - selection_rect.top_left();
  125. if (!m_content_bitmap->physical_rect().contains(layer_point)) {
  126. result->set_pixel(result_point, Gfx::Color::Transparent);
  127. continue;
  128. }
  129. auto pixel = m_content_bitmap->get_pixel(layer_point);
  130. // Widen to int before multiplying to avoid overflow issues
  131. auto pixel_alpha = static_cast<int>(pixel.alpha());
  132. auto selection_alpha = static_cast<int>(selection.get_selection_alpha(image_point));
  133. auto new_alpha = (pixel_alpha * selection_alpha) / 0xFF;
  134. pixel.set_alpha(static_cast<u8>(clamp(new_alpha, 0, 0xFF)));
  135. result->set_pixel(result_point, pixel);
  136. }
  137. }
  138. return result;
  139. }
  140. void Layer::erase_selection(Selection const& selection)
  141. {
  142. auto const image_and_selection_intersection = m_image.rect().intersected(selection.bounding_rect());
  143. auto const translated_to_layer_space = image_and_selection_intersection.translated(-location());
  144. for (int y = translated_to_layer_space.top(); y < translated_to_layer_space.top() + translated_to_layer_space.height(); ++y) {
  145. for (int x = translated_to_layer_space.left(); x < translated_to_layer_space.left() + translated_to_layer_space.width(); ++x) {
  146. // Selection is still in pre-translated coordinates, account for this by adding the layer's relative location
  147. if (content_bitmap().rect().contains(x, y) && selection.is_selected(x + location().x(), y + location().y()))
  148. content_bitmap().set_pixel(x, y, Color::Transparent);
  149. }
  150. }
  151. did_modify_bitmap(translated_to_layer_space);
  152. }
  153. ErrorOr<void> Layer::set_bitmaps(NonnullRefPtr<Gfx::Bitmap> content, RefPtr<Gfx::Bitmap> mask)
  154. {
  155. if (mask && content->size() != mask->size())
  156. return Error::from_string_literal("Layer content and mask must be same size");
  157. m_content_bitmap = move(content);
  158. m_mask_bitmap = move(mask);
  159. m_scratch_edited_bitmap = nullptr;
  160. update_cached_bitmap();
  161. return {};
  162. }
  163. ErrorOr<void> Layer::flip(Gfx::Orientation orientation, NotifyClients notify_clients)
  164. {
  165. auto flipped_content_bitmap = TRY(m_content_bitmap->flipped(orientation));
  166. if (m_mask_bitmap)
  167. m_mask_bitmap = *TRY(m_mask_bitmap->flipped(orientation));
  168. m_content_bitmap = move(flipped_content_bitmap);
  169. did_modify_bitmap({}, notify_clients);
  170. return {};
  171. }
  172. ErrorOr<void> Layer::rotate(Gfx::RotationDirection direction, NotifyClients notify_clients)
  173. {
  174. auto rotated_content_bitmap = TRY(m_content_bitmap->rotated(direction));
  175. if (m_mask_bitmap)
  176. m_mask_bitmap = *TRY(m_mask_bitmap->rotated(direction));
  177. m_content_bitmap = move(rotated_content_bitmap);
  178. did_modify_bitmap({}, notify_clients);
  179. return {};
  180. }
  181. ErrorOr<void> Layer::crop(Gfx::IntRect const& rect, NotifyClients notify_clients)
  182. {
  183. auto cropped_content_bitmap = TRY(m_content_bitmap->cropped(rect));
  184. if (m_mask_bitmap)
  185. m_mask_bitmap = *TRY(m_mask_bitmap->cropped(rect));
  186. m_content_bitmap = move(cropped_content_bitmap);
  187. did_modify_bitmap({}, notify_clients);
  188. return {};
  189. }
  190. ErrorOr<void> Layer::scale(Gfx::IntRect const& new_rect, Gfx::Painter::ScalingMode scaling_mode, NotifyClients notify_clients)
  191. {
  192. auto src_rect = Gfx::IntRect({}, size());
  193. auto dst_rect = Gfx::IntRect({}, new_rect.size());
  194. auto scaled_content_bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, new_rect.size()));
  195. {
  196. Gfx::Painter painter(scaled_content_bitmap);
  197. if (scaling_mode == Gfx::Painter::ScalingMode::None) {
  198. painter.blit(src_rect.top_left(), *m_content_bitmap, src_rect, 1.0f);
  199. } else {
  200. painter.draw_scaled_bitmap(dst_rect, *m_content_bitmap, src_rect, 1.0f, scaling_mode);
  201. }
  202. }
  203. if (m_mask_bitmap) {
  204. auto dst = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, new_rect.size()));
  205. Gfx::Painter painter(dst);
  206. if (scaling_mode == Gfx::Painter::ScalingMode::None) {
  207. painter.blit(src_rect.top_left(), *m_content_bitmap, src_rect, 1.0f);
  208. } else {
  209. painter.draw_scaled_bitmap(dst_rect, *m_mask_bitmap, src_rect, 1.0f, scaling_mode);
  210. }
  211. m_mask_bitmap = move(dst);
  212. }
  213. m_content_bitmap = move(scaled_content_bitmap);
  214. set_location(new_rect.location());
  215. did_modify_bitmap({}, notify_clients);
  216. return {};
  217. }
  218. void Layer::update_cached_bitmap()
  219. {
  220. if (!is_masked()) {
  221. if (m_content_bitmap.ptr() == m_cached_display_bitmap.ptr())
  222. return;
  223. m_cached_display_bitmap = m_content_bitmap;
  224. return;
  225. }
  226. if (m_cached_display_bitmap.ptr() == m_content_bitmap.ptr() || m_cached_display_bitmap->size() != size()) {
  227. m_cached_display_bitmap = MUST(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, size()));
  228. }
  229. // FIXME: This can probably be done nicer
  230. m_cached_display_bitmap->fill(Color::Transparent);
  231. for (int y = 0; y < size().height(); ++y) {
  232. for (int x = 0; x < size().width(); ++x) {
  233. auto opacity_multiplier = (float)m_mask_bitmap->get_pixel(x, y).to_grayscale().red() / 255;
  234. auto content_color = m_content_bitmap->get_pixel(x, y);
  235. content_color.set_alpha(content_color.alpha() * opacity_multiplier);
  236. m_cached_display_bitmap->set_pixel(x, y, content_color);
  237. }
  238. }
  239. }
  240. ErrorOr<void> Layer::create_mask()
  241. {
  242. m_mask_bitmap = TRY(Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, size()));
  243. m_mask_bitmap->fill(Gfx::Color::White);
  244. update_cached_bitmap();
  245. return {};
  246. }
  247. void Layer::delete_mask()
  248. {
  249. m_mask_bitmap = nullptr;
  250. set_edit_mode(EditMode::Content);
  251. update_cached_bitmap();
  252. }
  253. void Layer::apply_mask()
  254. {
  255. m_content_bitmap->fill(Color::Transparent);
  256. Gfx::Painter painter(m_content_bitmap);
  257. painter.blit({}, m_cached_display_bitmap, m_cached_display_bitmap->rect());
  258. delete_mask();
  259. }
  260. Gfx::Bitmap& Layer::currently_edited_bitmap()
  261. {
  262. switch (edit_mode()) {
  263. case EditMode::Mask:
  264. if (is_masked())
  265. return *mask_bitmap();
  266. [[fallthrough]];
  267. case EditMode::Content:
  268. return content_bitmap();
  269. }
  270. VERIFY_NOT_REACHED();
  271. }
  272. void Layer::set_edit_mode(Layer::EditMode mode)
  273. {
  274. if (m_edit_mode == mode)
  275. return;
  276. m_scratch_edited_bitmap = nullptr;
  277. m_edit_mode = mode;
  278. }
  279. Optional<Gfx::IntRect> Layer::nonempty_content_bounding_rect() const
  280. {
  281. auto determine_background_color = [](NonnullRefPtr<Gfx::Bitmap> const& bitmap) -> Optional<Gfx::Color> {
  282. auto bitmap_size = bitmap->size();
  283. auto top_left_pixel = bitmap->get_pixel(0, 0);
  284. auto top_right_pixel = bitmap->get_pixel(bitmap_size.width() - 1, 0);
  285. auto bottom_left_pixel = bitmap->get_pixel(0, bitmap_size.height() - 1);
  286. auto bottom_right_pixel = bitmap->get_pixel(bitmap_size.width() - 1, bitmap_size.height() - 1);
  287. if (top_left_pixel == top_right_pixel || top_left_pixel == bottom_left_pixel)
  288. return top_left_pixel;
  289. if (bottom_right_pixel == bottom_left_pixel || bottom_right_pixel == top_right_pixel)
  290. return top_right_pixel;
  291. return {};
  292. };
  293. enum class ShrinkMode {
  294. Alpha,
  295. BackgroundColor
  296. };
  297. Optional<int> min_content_y;
  298. Optional<int> min_content_x;
  299. Optional<int> max_content_y;
  300. Optional<int> max_content_x;
  301. auto background_color = determine_background_color(m_content_bitmap);
  302. auto shrink_mode = background_color.has_value() ? ShrinkMode::BackgroundColor : ShrinkMode::Alpha;
  303. for (int y = 0; y < m_content_bitmap->height(); ++y) {
  304. for (int x = 0; x < m_content_bitmap->width(); ++x) {
  305. auto color = m_content_bitmap->get_pixel(x, y);
  306. switch (shrink_mode) {
  307. case ShrinkMode::BackgroundColor:
  308. if (color == background_color)
  309. continue;
  310. break;
  311. case ShrinkMode::Alpha:
  312. if (color.alpha() == 0)
  313. continue;
  314. break;
  315. default:
  316. VERIFY_NOT_REACHED();
  317. }
  318. min_content_x = min(min_content_x.value_or(x), x);
  319. min_content_y = min(min_content_y.value_or(y), y);
  320. max_content_x = max(max_content_x.value_or(x), x);
  321. max_content_y = max(max_content_y.value_or(y), y);
  322. }
  323. }
  324. if (!min_content_x.has_value())
  325. return {};
  326. return Gfx::IntRect {
  327. *min_content_x,
  328. *min_content_y,
  329. *max_content_x - *min_content_x + 1,
  330. *max_content_y - *min_content_y + 1
  331. };
  332. }
  333. ErrorOr<NonnullRefPtr<Layer>> Layer::duplicate(DeprecatedString name)
  334. {
  335. auto duplicated_layer = TRY(Layer::create_snapshot(m_image, *this));
  336. duplicated_layer->m_name = move(name);
  337. duplicated_layer->m_selected = false;
  338. return duplicated_layer;
  339. }
  340. }