Image.cpp 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Mustafa Quraish <mustafa@serenityos.org>
  4. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  5. *
  6. * SPDX-License-Identifier: BSD-2-Clause
  7. */
  8. #include "Image.h"
  9. #include "Layer.h"
  10. #include "Selection.h"
  11. #include <AK/Base64.h>
  12. #include <AK/JsonObject.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGfx/BMPWriter.h>
  15. #include <LibGfx/Bitmap.h>
  16. #include <LibGfx/PNGWriter.h>
  17. #include <LibGfx/QOIWriter.h>
  18. #include <LibImageDecoderClient/Client.h>
  19. #include <stdio.h>
  20. namespace PixelPaint {
  21. ErrorOr<NonnullRefPtr<Image>> Image::create_with_size(Gfx::IntSize size)
  22. {
  23. VERIFY(!size.is_empty());
  24. if (size.width() > 16384 || size.height() > 16384)
  25. return Error::from_string_literal("Image size too large");
  26. return adopt_nonnull_ref_or_enomem(new (nothrow) Image(size));
  27. }
  28. Image::Image(Gfx::IntSize size)
  29. : m_size(size)
  30. , m_selection(*this)
  31. {
  32. }
  33. void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect, float scale) const
  34. {
  35. Gfx::PainterStateSaver saver(painter);
  36. painter.add_clip_rect(dest_rect);
  37. for (auto const& layer : m_layers) {
  38. if (!layer->is_visible())
  39. continue;
  40. auto target = dest_rect.to_type<float>().translated(layer->location().x() * scale, layer->location().y() * scale);
  41. target.set_size(layer->size().width() * scale, layer->size().height() * scale);
  42. painter.draw_scaled_bitmap(target.to_type<int>(), layer->display_bitmap(), layer->rect(), (float)layer->opacity_percent() / 100.0f);
  43. }
  44. }
  45. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::decode_bitmap(ReadonlyBytes bitmap_data)
  46. {
  47. // Spawn a new ImageDecoder service process and connect to it.
  48. auto client = TRY(ImageDecoderClient::Client::try_create());
  49. // FIXME: Find a way to avoid the memory copying here.
  50. auto maybe_decoded_image = client->decode_image(bitmap_data);
  51. if (!maybe_decoded_image.has_value())
  52. return Error::from_string_literal("Image decode failed");
  53. // FIXME: Support multi-frame images?
  54. auto decoded_image = maybe_decoded_image.release_value();
  55. if (decoded_image.frames.is_empty())
  56. return Error::from_string_literal("Image decode failed (no frames)");
  57. auto decoded_bitmap = decoded_image.frames.first().bitmap;
  58. if (decoded_bitmap.is_null())
  59. return Error::from_string_literal("Image decode failed (no bitmap for frame)");
  60. return decoded_bitmap.release_nonnull();
  61. }
  62. ErrorOr<NonnullRefPtr<Image>> Image::create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
  63. {
  64. auto image = TRY(create_with_size({ bitmap->width(), bitmap->height() }));
  65. auto layer = TRY(Layer::create_with_bitmap(*image, *bitmap, "Background"));
  66. image->add_layer(move(layer));
  67. return image;
  68. }
  69. ErrorOr<NonnullRefPtr<Image>> Image::create_from_pixel_paint_json(JsonObject const& json)
  70. {
  71. // FIXME: Handle invalid JSON data
  72. auto image = TRY(create_with_size({ json.get_i32("width"sv).value_or(0), json.get_i32("height"sv).value_or(0) }));
  73. auto layers_value = json.get_array("layers"sv).value();
  74. for (auto& layer_value : layers_value.values()) {
  75. auto const& layer_object = layer_value.as_object();
  76. auto name = layer_object.get_deprecated_string("name"sv).value();
  77. auto bitmap_base64_encoded = layer_object.get_deprecated_string("bitmap"sv).value();
  78. auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
  79. auto bitmap = TRY(decode_bitmap(bitmap_data));
  80. auto layer = TRY(Layer::create_with_bitmap(*image, move(bitmap), name));
  81. if (auto const& mask_object = layer_object.get_deprecated_string("mask"sv); mask_object.has_value()) {
  82. auto mask_base64_encoded = mask_object.value();
  83. auto mask_data = TRY(decode_base64(mask_base64_encoded));
  84. auto mask = TRY(decode_bitmap(mask_data));
  85. TRY(layer->set_bitmaps(layer->content_bitmap(), mask));
  86. }
  87. auto width = layer_object.get_i32("width"sv).value_or(0);
  88. auto height = layer_object.get_i32("height"sv).value_or(0);
  89. if (width != layer->size().width() || height != layer->size().height())
  90. return Error::from_string_literal("Decoded layer bitmap has wrong size");
  91. image->add_layer(*layer);
  92. layer->set_location({ layer_object.get_i32("locationx"sv).value_or(0), layer_object.get_i32("locationy"sv).value_or(0) });
  93. layer->set_opacity_percent(layer_object.get_i32("opacity_percent"sv).value());
  94. layer->set_visible(layer_object.get_bool("visible"sv).value());
  95. layer->set_selected(layer_object.get_bool("selected"sv).value());
  96. }
  97. return image;
  98. }
  99. ErrorOr<void> Image::serialize_as_json(JsonObjectSerializer<StringBuilder>& json) const
  100. {
  101. TRY(json.add("width"sv, m_size.width()));
  102. TRY(json.add("height"sv, m_size.height()));
  103. {
  104. auto json_layers = TRY(json.add_array("layers"sv));
  105. for (auto const& layer : m_layers) {
  106. auto json_layer = TRY(json_layers.add_object());
  107. TRY(json_layer.add("width"sv, layer->size().width()));
  108. TRY(json_layer.add("height"sv, layer->size().height()));
  109. TRY(json_layer.add("name"sv, layer->name()));
  110. TRY(json_layer.add("locationx"sv, layer->location().x()));
  111. TRY(json_layer.add("locationy"sv, layer->location().y()));
  112. TRY(json_layer.add("opacity_percent"sv, layer->opacity_percent()));
  113. TRY(json_layer.add("visible"sv, layer->is_visible()));
  114. TRY(json_layer.add("selected"sv, layer->is_selected()));
  115. TRY(json_layer.add("bitmap"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(layer->content_bitmap()))))));
  116. if (layer->is_masked())
  117. TRY(json_layer.add("mask"sv, TRY(encode_base64(TRY(Gfx::PNGWriter::encode(*layer->mask_bitmap()))))));
  118. TRY(json_layer.finish());
  119. }
  120. TRY(json_layers.finish());
  121. }
  122. return {};
  123. }
  124. ErrorOr<NonnullRefPtr<Gfx::Bitmap>> Image::compose_bitmap(Gfx::BitmapFormat format) const
  125. {
  126. auto bitmap = TRY(Gfx::Bitmap::create(format, m_size));
  127. GUI::Painter painter(bitmap);
  128. paint_into(painter, { 0, 0, m_size.width(), m_size.height() }, 1.0f);
  129. return bitmap;
  130. }
  131. RefPtr<Gfx::Bitmap> Image::copy_bitmap(Selection const& selection) const
  132. {
  133. if (selection.is_empty())
  134. return {};
  135. auto selection_rect = selection.bounding_rect();
  136. // FIXME: Add a way to only compose a certain part of the image
  137. auto bitmap_or_error = compose_bitmap(Gfx::BitmapFormat::BGRA8888);
  138. if (bitmap_or_error.is_error())
  139. return {};
  140. auto full_bitmap = bitmap_or_error.release_value();
  141. auto cropped_bitmap_or_error = full_bitmap->cropped(selection_rect);
  142. if (cropped_bitmap_or_error.is_error())
  143. return nullptr;
  144. return cropped_bitmap_or_error.release_value_but_fixme_should_propagate_errors();
  145. }
  146. ErrorOr<void> Image::export_bmp_to_file(NonnullOwnPtr<Stream> stream, bool preserve_alpha_channel) const
  147. {
  148. auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  149. auto bitmap = TRY(compose_bitmap(bitmap_format));
  150. auto encoded_data = TRY(Gfx::BMPWriter::encode(*bitmap));
  151. TRY(stream->write_entire_buffer(encoded_data));
  152. return {};
  153. }
  154. ErrorOr<void> Image::export_png_to_file(NonnullOwnPtr<Stream> stream, bool preserve_alpha_channel) const
  155. {
  156. auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  157. auto bitmap = TRY(compose_bitmap(bitmap_format));
  158. auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
  159. TRY(stream->write_entire_buffer(encoded_data));
  160. return {};
  161. }
  162. ErrorOr<void> Image::export_qoi_to_file(NonnullOwnPtr<Stream> stream) const
  163. {
  164. auto bitmap = TRY(compose_bitmap(Gfx::BitmapFormat::BGRA8888));
  165. auto encoded_data = TRY(Gfx::QOIWriter::encode(bitmap));
  166. TRY(stream->write_entire_buffer(encoded_data));
  167. return {};
  168. }
  169. void Image::add_layer(NonnullRefPtr<Layer> layer)
  170. {
  171. for (auto& existing_layer : m_layers) {
  172. VERIFY(existing_layer != layer);
  173. }
  174. m_layers.append(move(layer));
  175. for (auto* client : m_clients)
  176. client->image_did_add_layer(m_layers.size() - 1);
  177. did_modify_layer_stack();
  178. }
  179. ErrorOr<NonnullRefPtr<Image>> Image::take_snapshot() const
  180. {
  181. auto snapshot = TRY(create_with_size(m_size));
  182. for (auto const& layer : m_layers) {
  183. auto layer_snapshot = TRY(Layer::create_snapshot(*snapshot, layer));
  184. snapshot->add_layer(move(layer_snapshot));
  185. }
  186. snapshot->m_selection.set_mask(m_selection.mask());
  187. return snapshot;
  188. }
  189. ErrorOr<void> Image::restore_snapshot(Image const& snapshot)
  190. {
  191. m_layers.clear();
  192. select_layer(nullptr);
  193. bool layer_selected = false;
  194. for (auto const& snapshot_layer : snapshot.m_layers) {
  195. auto layer = TRY(Layer::create_snapshot(*this, snapshot_layer));
  196. if (layer->is_selected()) {
  197. select_layer(layer.ptr());
  198. layer_selected = true;
  199. }
  200. layer->did_modify_bitmap({}, Layer::NotifyClients::No);
  201. add_layer(*layer);
  202. }
  203. if (!layer_selected)
  204. select_layer(&layer(0));
  205. m_size = snapshot.size();
  206. m_selection.set_mask(snapshot.m_selection.mask());
  207. did_change_rect();
  208. did_modify_layer_stack();
  209. return {};
  210. }
  211. size_t Image::index_of(Layer const& layer) const
  212. {
  213. for (size_t i = 0; i < m_layers.size(); ++i) {
  214. if (m_layers[i] == &layer)
  215. return i;
  216. }
  217. VERIFY_NOT_REACHED();
  218. }
  219. void Image::move_layer_to_back(Layer& layer)
  220. {
  221. NonnullRefPtr<Layer> protector(layer);
  222. auto index = index_of(layer);
  223. m_layers.remove(index);
  224. m_layers.prepend(layer);
  225. did_modify_layer_stack();
  226. }
  227. void Image::move_layer_to_front(Layer& layer)
  228. {
  229. NonnullRefPtr<Layer> protector(layer);
  230. auto index = index_of(layer);
  231. m_layers.remove(index);
  232. m_layers.append(layer);
  233. did_modify_layer_stack();
  234. }
  235. void Image::move_layer_down(Layer& layer)
  236. {
  237. NonnullRefPtr<Layer> protector(layer);
  238. auto index = index_of(layer);
  239. if (!index)
  240. return;
  241. m_layers.remove(index);
  242. m_layers.insert(index - 1, layer);
  243. did_modify_layer_stack();
  244. }
  245. void Image::move_layer_up(Layer& layer)
  246. {
  247. NonnullRefPtr<Layer> protector(layer);
  248. auto index = index_of(layer);
  249. if (index == m_layers.size() - 1)
  250. return;
  251. m_layers.remove(index);
  252. m_layers.insert(index + 1, layer);
  253. did_modify_layer_stack();
  254. }
  255. void Image::change_layer_index(size_t old_index, size_t new_index)
  256. {
  257. VERIFY(old_index < m_layers.size());
  258. VERIFY(new_index < m_layers.size());
  259. auto layer = m_layers.take(old_index);
  260. m_layers.insert(new_index, move(layer));
  261. did_modify_layer_stack();
  262. }
  263. void Image::did_modify_layer_stack()
  264. {
  265. for (auto* client : m_clients)
  266. client->image_did_modify_layer_stack();
  267. did_change();
  268. }
  269. void Image::remove_layer(Layer& layer)
  270. {
  271. NonnullRefPtr<Layer> protector(layer);
  272. auto index = index_of(layer);
  273. m_layers.remove(index);
  274. for (auto* client : m_clients)
  275. client->image_did_remove_layer(index);
  276. did_modify_layer_stack();
  277. }
  278. ErrorOr<void> Image::flatten_all_layers()
  279. {
  280. return merge_layers(LayerMergeMode::All);
  281. }
  282. ErrorOr<void> Image::merge_visible_layers()
  283. {
  284. return merge_layers(LayerMergeMode::VisibleOnly);
  285. }
  286. ErrorOr<void> Image::merge_layers(LayerMergeMode layer_merge_mode)
  287. {
  288. if (m_layers.size() < 2)
  289. return {};
  290. Vector<NonnullRefPtr<Layer>> new_layers;
  291. Gfx::IntRect merged_layer_bounding_rect = {};
  292. size_t bottom_layer_index = 0;
  293. for (auto const& layer : m_layers) {
  294. if (!layer->is_visible()) {
  295. if (layer_merge_mode == LayerMergeMode::VisibleOnly)
  296. TRY(new_layers.try_append(layer));
  297. if (merged_layer_bounding_rect.is_empty())
  298. bottom_layer_index++;
  299. continue;
  300. }
  301. merged_layer_bounding_rect = merged_layer_bounding_rect.united(layer->relative_rect());
  302. }
  303. if (merged_layer_bounding_rect.is_empty())
  304. return {};
  305. NonnullRefPtr<Layer> bottom_layer = m_layers.at(bottom_layer_index);
  306. NonnullRefPtr<Layer> merged_layer = bottom_layer;
  307. if (!merged_layer->relative_rect().contains(merged_layer_bounding_rect)) {
  308. merged_layer = TRY(Layer::create_with_size(*this, merged_layer_bounding_rect.size(), bottom_layer->name()));
  309. merged_layer->set_location(merged_layer_bounding_rect.location());
  310. }
  311. GUI::Painter painter(merged_layer->content_bitmap());
  312. if (merged_layer.ptr() != bottom_layer.ptr())
  313. painter.blit(bottom_layer->location() - merged_layer->location(), bottom_layer->display_bitmap(), bottom_layer->rect(), static_cast<float>(bottom_layer->opacity_percent()) / 100.0f);
  314. for (size_t index = bottom_layer_index + 1; index < m_layers.size(); index++) {
  315. auto& layer = m_layers.at(index);
  316. if (!layer->is_visible())
  317. continue;
  318. painter.blit(layer->location() - merged_layer->location(), layer->display_bitmap(), layer->rect(), static_cast<float>(layer->opacity_percent()) / 100.0f);
  319. }
  320. TRY(new_layers.try_append(merged_layer));
  321. m_layers = move(new_layers);
  322. select_layer(merged_layer.ptr());
  323. did_modify_layer_stack();
  324. return {};
  325. }
  326. ErrorOr<void> Image::merge_active_layer_up(Layer& layer)
  327. {
  328. return merge_active_layer(layer, LayerMergeDirection::Up);
  329. }
  330. ErrorOr<void> Image::merge_active_layer_down(Layer& layer)
  331. {
  332. return merge_active_layer(layer, LayerMergeDirection::Down);
  333. }
  334. ErrorOr<void> Image::merge_active_layer(NonnullRefPtr<Layer> const& layer, LayerMergeDirection layer_merge_direction)
  335. {
  336. if (m_layers.size() < 2)
  337. return {};
  338. if (!layer->is_visible())
  339. return Error::from_string_literal("Layer must be visible");
  340. auto layer_index = index_of(layer);
  341. auto direction = layer_merge_direction == LayerMergeDirection::Up ? 1 : -1;
  342. ssize_t layer_to_merge_index = layer_index + direction;
  343. ssize_t layer_count = m_layers.size();
  344. if (layer_to_merge_index < 0)
  345. return Error::from_string_literal("Layer is already at the bottom");
  346. if (layer_to_merge_index >= layer_count)
  347. return Error::from_string_literal("Layer is already at the top");
  348. Optional<NonnullRefPtr<Layer>> maybe_adjacent_layer;
  349. while (layer_to_merge_index >= 0 && layer_to_merge_index < layer_count) {
  350. auto const& layer = *m_layers[layer_to_merge_index];
  351. if (layer.is_visible()) {
  352. maybe_adjacent_layer = layer;
  353. break;
  354. }
  355. layer_to_merge_index += direction;
  356. }
  357. if (!maybe_adjacent_layer.has_value()) {
  358. auto error_message = layer_merge_direction == LayerMergeDirection::Up ? "No visible layers above this layer"sv : "No visible layers below this layer"sv;
  359. return Error::from_string_view(error_message);
  360. }
  361. auto adjacent_layer = maybe_adjacent_layer.value();
  362. auto bottom_layer = layer_merge_direction == LayerMergeDirection::Down ? adjacent_layer : layer;
  363. auto top_layer = layer_merge_direction == LayerMergeDirection::Down ? layer : adjacent_layer;
  364. auto merged_layer_bounding_rect = bottom_layer->relative_rect().united(top_layer->relative_rect());
  365. auto merged_layer = bottom_layer;
  366. if (!bottom_layer->relative_rect().contains(top_layer->relative_rect())) {
  367. merged_layer = TRY(Layer::create_with_size(*this, merged_layer_bounding_rect.size(), adjacent_layer->name()));
  368. merged_layer->set_location(merged_layer_bounding_rect.location());
  369. } else if (merged_layer.ptr() != adjacent_layer.ptr()) {
  370. merged_layer->set_name(adjacent_layer->name());
  371. }
  372. GUI::Painter painter(merged_layer->content_bitmap());
  373. if (merged_layer.ptr() != bottom_layer.ptr())
  374. painter.blit(bottom_layer->location() - merged_layer->location(), bottom_layer->display_bitmap(), bottom_layer->rect(), static_cast<float>(bottom_layer->opacity_percent()) / 100.0f);
  375. painter.blit(top_layer->location() - merged_layer->location(), top_layer->display_bitmap(), top_layer->rect(), static_cast<float>(top_layer->opacity_percent()) / 100.0f);
  376. auto top_layer_index = max(layer_index, layer_to_merge_index);
  377. auto bottom_layer_index = min(layer_index, layer_to_merge_index);
  378. m_layers.remove(top_layer_index);
  379. m_layers.remove(bottom_layer_index);
  380. m_layers.insert(top_layer_index - 1, merged_layer);
  381. select_layer(merged_layer);
  382. did_modify_layer_stack();
  383. return {};
  384. }
  385. void Image::select_layer(Layer* layer)
  386. {
  387. for (auto* client : m_clients)
  388. client->image_select_layer(layer);
  389. }
  390. void Image::add_client(ImageClient& client)
  391. {
  392. VERIFY(!m_clients.contains(&client));
  393. m_clients.set(&client);
  394. }
  395. void Image::remove_client(ImageClient& client)
  396. {
  397. VERIFY(m_clients.contains(&client));
  398. m_clients.remove(&client);
  399. }
  400. void Image::layer_did_modify_bitmap(Badge<Layer>, Layer const& layer, Gfx::IntRect const& modified_layer_rect)
  401. {
  402. auto layer_index = index_of(layer);
  403. for (auto* client : m_clients)
  404. client->image_did_modify_layer_bitmap(layer_index);
  405. did_change(modified_layer_rect.translated(layer.location()));
  406. }
  407. void Image::layer_did_modify_properties(Badge<Layer>, Layer const& layer)
  408. {
  409. auto layer_index = index_of(layer);
  410. for (auto* client : m_clients)
  411. client->image_did_modify_layer_properties(layer_index);
  412. did_change();
  413. }
  414. void Image::did_change(Gfx::IntRect const& a_modified_rect)
  415. {
  416. auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
  417. for (auto* client : m_clients)
  418. client->image_did_change(modified_rect);
  419. }
  420. void Image::did_change_rect(Gfx::IntRect const& a_modified_rect)
  421. {
  422. auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
  423. for (auto* client : m_clients)
  424. client->image_did_change_rect(modified_rect);
  425. }
  426. ImageUndoCommand::ImageUndoCommand(Image& image, DeprecatedString action_text)
  427. : m_snapshot(image.take_snapshot().release_value_but_fixme_should_propagate_errors())
  428. , m_image(image)
  429. , m_action_text(move(action_text))
  430. {
  431. }
  432. void ImageUndoCommand::undo()
  433. {
  434. // FIXME: Handle errors.
  435. (void)m_image.restore_snapshot(*m_snapshot);
  436. }
  437. void ImageUndoCommand::redo()
  438. {
  439. undo();
  440. }
  441. ErrorOr<void> Image::flip(Gfx::Orientation orientation)
  442. {
  443. Vector<NonnullRefPtr<Layer>> flipped_layers;
  444. TRY(flipped_layers.try_ensure_capacity(m_layers.size()));
  445. VERIFY(m_layers.size() > 0);
  446. size_t selected_layer_index = 0;
  447. for (size_t i = 0; i < m_layers.size(); ++i) {
  448. auto& layer = m_layers[i];
  449. auto new_layer = TRY(Layer::create_snapshot(*this, layer));
  450. if (layer->is_selected())
  451. selected_layer_index = i;
  452. TRY(new_layer->flip(orientation, Layer::NotifyClients::No));
  453. flipped_layers.unchecked_append(new_layer);
  454. }
  455. m_layers = move(flipped_layers);
  456. for (auto& layer : m_layers)
  457. layer->did_modify_bitmap({}, Layer::NotifyClients::No);
  458. select_layer(m_layers[selected_layer_index]);
  459. did_change();
  460. return {};
  461. }
  462. ErrorOr<void> Image::rotate(Gfx::RotationDirection direction)
  463. {
  464. Vector<NonnullRefPtr<Layer>> rotated_layers;
  465. TRY(rotated_layers.try_ensure_capacity(m_layers.size()));
  466. VERIFY(m_layers.size() > 0);
  467. size_t selected_layer_index = 0;
  468. for (size_t i = 0; i < m_layers.size(); ++i) {
  469. auto& layer = m_layers[i];
  470. auto new_layer = TRY(Layer::create_snapshot(*this, layer));
  471. if (layer->is_selected())
  472. selected_layer_index = i;
  473. TRY(new_layer->rotate(direction, Layer::NotifyClients::No));
  474. rotated_layers.unchecked_append(new_layer);
  475. }
  476. m_layers = move(rotated_layers);
  477. for (auto& layer : m_layers)
  478. layer->did_modify_bitmap({}, Layer::NotifyClients::Yes);
  479. select_layer(m_layers[selected_layer_index]);
  480. m_size = { m_size.height(), m_size.width() };
  481. did_change_rect();
  482. return {};
  483. }
  484. ErrorOr<void> Image::crop(Gfx::IntRect const& cropped_rect)
  485. {
  486. Vector<NonnullRefPtr<Layer>> cropped_layers;
  487. TRY(cropped_layers.try_ensure_capacity(m_layers.size()));
  488. VERIFY(m_layers.size() > 0);
  489. size_t selected_layer_index = 0;
  490. for (size_t i = 0; i < m_layers.size(); ++i) {
  491. auto& layer = m_layers[i];
  492. auto new_layer = TRY(Layer::create_snapshot(*this, layer));
  493. if (layer->is_selected())
  494. selected_layer_index = i;
  495. auto layer_location = new_layer->location();
  496. auto layer_local_crop_rect = new_layer->relative_rect().intersected(cropped_rect).translated(-layer_location.x(), -layer_location.y());
  497. TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::No));
  498. auto new_layer_x = max(0, layer_location.x() - cropped_rect.x());
  499. auto new_layer_y = max(0, layer_location.y() - cropped_rect.y());
  500. new_layer->set_location({ new_layer_x, new_layer_y });
  501. cropped_layers.unchecked_append(new_layer);
  502. }
  503. m_layers = move(cropped_layers);
  504. for (auto& layer : m_layers)
  505. layer->did_modify_bitmap({}, Layer::NotifyClients::Yes);
  506. select_layer(m_layers[selected_layer_index]);
  507. m_size = { cropped_rect.width(), cropped_rect.height() };
  508. did_change_rect(cropped_rect);
  509. return {};
  510. }
  511. Optional<Gfx::IntRect> Image::nonempty_content_bounding_rect() const
  512. {
  513. if (m_layers.is_empty())
  514. return {};
  515. Optional<Gfx::IntRect> bounding_rect;
  516. for (auto const& layer : m_layers) {
  517. auto layer_content_rect_in_layer_coordinates = layer->nonempty_content_bounding_rect();
  518. if (!layer_content_rect_in_layer_coordinates.has_value())
  519. continue;
  520. auto layer_content_rect_in_image_coordinates = layer_content_rect_in_layer_coordinates->translated(layer->location());
  521. if (!bounding_rect.has_value())
  522. bounding_rect = layer_content_rect_in_image_coordinates;
  523. else
  524. bounding_rect = bounding_rect->united(layer_content_rect_in_image_coordinates);
  525. }
  526. return bounding_rect;
  527. }
  528. ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode)
  529. {
  530. float scale_x = 1.0f;
  531. float scale_y = 1.0f;
  532. if (size().width() != 0.0f) {
  533. scale_x = new_size.width() / static_cast<float>(size().width());
  534. }
  535. if (size().height() != 0.0f) {
  536. scale_y = new_size.height() / static_cast<float>(size().height());
  537. }
  538. Vector<NonnullRefPtr<Layer>> resized_layers;
  539. TRY(resized_layers.try_ensure_capacity(m_layers.size()));
  540. VERIFY(m_layers.size() > 0);
  541. size_t selected_layer_index = 0;
  542. for (size_t i = 0; i < m_layers.size(); ++i) {
  543. auto& layer = m_layers[i];
  544. auto new_layer = TRY(Layer::create_snapshot(*this, layer));
  545. if (layer->is_selected())
  546. selected_layer_index = i;
  547. Gfx::IntPoint new_location(scale_x * new_layer->location().x(), scale_y * new_layer->location().y());
  548. TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::No));
  549. resized_layers.unchecked_append(new_layer);
  550. }
  551. m_layers = move(resized_layers);
  552. for (auto& layer : m_layers)
  553. layer->did_modify_bitmap({}, Layer::NotifyClients::Yes);
  554. select_layer(m_layers[selected_layer_index]);
  555. m_size = { new_size.width(), new_size.height() };
  556. did_change_rect();
  557. return {};
  558. }
  559. Color Image::color_at(Gfx::IntPoint point) const
  560. {
  561. Color color;
  562. for (auto const& layer : m_layers) {
  563. if (!layer->is_visible() || !layer->rect().contains(point))
  564. continue;
  565. auto layer_color = layer->display_bitmap().get_pixel(point);
  566. float layer_opacity = layer->opacity_percent() / 100.0f;
  567. layer_color.set_alpha((u8)(layer_color.alpha() * layer_opacity));
  568. color = color.blend(layer_color);
  569. }
  570. return color;
  571. }
  572. }