Image.cpp 21 KB

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