Image.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673
  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::try_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::try_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::try_create_from_bitmap(NonnullRefPtr<Gfx::Bitmap> const& bitmap)
  64. {
  65. auto image = TRY(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::try_create_from_pixel_paint_json(JsonObject const& json)
  71. {
  72. auto image = TRY(try_create_with_size({ json.get("width"sv).to_i32(), json.get("height"sv).to_i32() }));
  73. auto layers_value = json.get("layers"sv);
  74. for (auto const& layer_value : layers_value.as_array().values()) {
  75. auto const& layer_object = layer_value.as_object();
  76. auto name = layer_object.get("name"sv).as_string();
  77. auto bitmap_base64_encoded = layer_object.get("bitmap"sv).as_string();
  78. auto bitmap_data = TRY(decode_base64(bitmap_base64_encoded));
  79. auto bitmap = TRY(try_decode_bitmap(bitmap_data));
  80. auto layer = TRY(Layer::try_create_with_bitmap(*image, move(bitmap), name));
  81. if (auto const& mask_object = layer_object.get("mask"sv); !mask_object.is_null()) {
  82. auto mask_base64_encoded = mask_object.as_string();
  83. auto mask_data = TRY(decode_base64(mask_base64_encoded));
  84. auto mask = TRY(try_decode_bitmap(mask_data));
  85. TRY(layer->try_set_bitmaps(layer->content_bitmap(), mask));
  86. }
  87. auto width = layer_object.get("width"sv).to_i32();
  88. auto height = layer_object.get("height"sv).to_i32();
  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("locationx"sv).to_i32(), layer_object.get("locationy"sv).to_i32() });
  93. layer->set_opacity_percent(layer_object.get("opacity_percent"sv).to_i32());
  94. layer->set_visible(layer_object.get("visible"sv).as_bool());
  95. layer->set_selected(layer_object.get("selected"sv).as_bool());
  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::try_compose_bitmap(Gfx::BitmapFormat format) const
  125. {
  126. auto bitmap = TRY(Gfx::Bitmap::try_create(format, m_size));
  127. GUI::Painter painter(bitmap);
  128. paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
  129. return bitmap;
  130. }
  131. RefPtr<Gfx::Bitmap> Image::try_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 = try_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(Core::File& file, bool preserve_alpha_channel) const
  147. {
  148. auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  149. auto bitmap = TRY(try_compose_bitmap(bitmap_format));
  150. Gfx::BMPWriter dumper;
  151. auto encoded_data = dumper.dump(bitmap);
  152. if (!file.write(encoded_data.data(), encoded_data.size()))
  153. return Error::from_errno(file.error());
  154. return {};
  155. }
  156. ErrorOr<void> Image::export_png_to_file(Core::File& file, bool preserve_alpha_channel) const
  157. {
  158. auto bitmap_format = preserve_alpha_channel ? Gfx::BitmapFormat::BGRA8888 : Gfx::BitmapFormat::BGRx8888;
  159. auto bitmap = TRY(try_compose_bitmap(bitmap_format));
  160. auto encoded_data = TRY(Gfx::PNGWriter::encode(*bitmap));
  161. if (!file.write(encoded_data.data(), encoded_data.size()))
  162. return Error::from_errno(file.error());
  163. return {};
  164. }
  165. ErrorOr<void> Image::export_qoi_to_file(Core::File& file) const
  166. {
  167. auto bitmap = TRY(try_compose_bitmap(Gfx::BitmapFormat::BGRA8888));
  168. auto encoded_data = Gfx::QOIWriter::encode(bitmap);
  169. if (!file.write(encoded_data.data(), encoded_data.size()))
  170. return Error::from_errno(file.error());
  171. return {};
  172. }
  173. void Image::add_layer(NonnullRefPtr<Layer> layer)
  174. {
  175. for (auto& existing_layer : m_layers) {
  176. VERIFY(&existing_layer != layer.ptr());
  177. }
  178. m_layers.append(move(layer));
  179. for (auto* client : m_clients)
  180. client->image_did_add_layer(m_layers.size() - 1);
  181. did_modify_layer_stack();
  182. }
  183. ErrorOr<NonnullRefPtr<Image>> Image::take_snapshot() const
  184. {
  185. auto snapshot = TRY(try_create_with_size(m_size));
  186. for (auto const& layer : m_layers) {
  187. auto layer_snapshot = TRY(Layer::try_create_snapshot(*snapshot, layer));
  188. snapshot->add_layer(move(layer_snapshot));
  189. }
  190. snapshot->m_selection.set_mask(m_selection.mask());
  191. return snapshot;
  192. }
  193. ErrorOr<void> Image::restore_snapshot(Image const& snapshot)
  194. {
  195. m_layers.clear();
  196. select_layer(nullptr);
  197. bool layer_selected = false;
  198. for (auto const& snapshot_layer : snapshot.m_layers) {
  199. auto layer = TRY(Layer::try_create_snapshot(*this, snapshot_layer));
  200. if (layer->is_selected()) {
  201. select_layer(layer.ptr());
  202. layer_selected = true;
  203. }
  204. add_layer(*layer);
  205. }
  206. if (!layer_selected)
  207. select_layer(&layer(0));
  208. m_size = snapshot.size();
  209. m_selection.set_mask(snapshot.m_selection.mask());
  210. did_change_rect();
  211. did_modify_layer_stack();
  212. return {};
  213. }
  214. size_t Image::index_of(Layer const& layer) const
  215. {
  216. for (size_t i = 0; i < m_layers.size(); ++i) {
  217. if (&m_layers.at(i) == &layer)
  218. return i;
  219. }
  220. VERIFY_NOT_REACHED();
  221. }
  222. void Image::move_layer_to_back(Layer& layer)
  223. {
  224. NonnullRefPtr<Layer> protector(layer);
  225. auto index = index_of(layer);
  226. m_layers.remove(index);
  227. m_layers.prepend(layer);
  228. did_modify_layer_stack();
  229. }
  230. void Image::move_layer_to_front(Layer& layer)
  231. {
  232. NonnullRefPtr<Layer> protector(layer);
  233. auto index = index_of(layer);
  234. m_layers.remove(index);
  235. m_layers.append(layer);
  236. did_modify_layer_stack();
  237. }
  238. void Image::move_layer_down(Layer& layer)
  239. {
  240. NonnullRefPtr<Layer> protector(layer);
  241. auto index = index_of(layer);
  242. if (!index)
  243. return;
  244. m_layers.remove(index);
  245. m_layers.insert(index - 1, layer);
  246. did_modify_layer_stack();
  247. }
  248. void Image::move_layer_up(Layer& layer)
  249. {
  250. NonnullRefPtr<Layer> protector(layer);
  251. auto index = index_of(layer);
  252. if (index == m_layers.size() - 1)
  253. return;
  254. m_layers.remove(index);
  255. m_layers.insert(index + 1, layer);
  256. did_modify_layer_stack();
  257. }
  258. void Image::change_layer_index(size_t old_index, size_t new_index)
  259. {
  260. VERIFY(old_index < m_layers.size());
  261. VERIFY(new_index < m_layers.size());
  262. auto layer = m_layers.take(old_index);
  263. m_layers.insert(new_index, move(layer));
  264. did_modify_layer_stack();
  265. }
  266. void Image::did_modify_layer_stack()
  267. {
  268. for (auto* client : m_clients)
  269. client->image_did_modify_layer_stack();
  270. did_change();
  271. }
  272. void Image::remove_layer(Layer& layer)
  273. {
  274. NonnullRefPtr<Layer> protector(layer);
  275. auto index = index_of(layer);
  276. m_layers.remove(index);
  277. for (auto* client : m_clients)
  278. client->image_did_remove_layer(index);
  279. did_modify_layer_stack();
  280. }
  281. void Image::flatten_all_layers()
  282. {
  283. if (m_layers.size() < 2)
  284. return;
  285. auto& bottom_layer = m_layers.at(0);
  286. GUI::Painter painter(bottom_layer.content_bitmap());
  287. paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
  288. for (size_t index = m_layers.size() - 1; index > 0; index--) {
  289. auto& layer = m_layers.at(index);
  290. remove_layer(layer);
  291. }
  292. bottom_layer.set_name("Background");
  293. select_layer(&bottom_layer);
  294. }
  295. void Image::merge_visible_layers()
  296. {
  297. if (m_layers.size() < 2)
  298. return;
  299. size_t index = 0;
  300. while (index < m_layers.size()) {
  301. if (m_layers.at(index).is_visible()) {
  302. auto& bottom_layer = m_layers.at(index);
  303. GUI::Painter painter(bottom_layer.content_bitmap());
  304. paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
  305. select_layer(&bottom_layer);
  306. index++;
  307. break;
  308. }
  309. index++;
  310. }
  311. while (index < m_layers.size()) {
  312. if (m_layers.at(index).is_visible()) {
  313. auto& layer = m_layers.at(index);
  314. remove_layer(layer);
  315. } else {
  316. index++;
  317. }
  318. }
  319. }
  320. void Image::merge_active_layer_up(Layer& layer)
  321. {
  322. if (m_layers.size() < 2)
  323. return;
  324. size_t layer_index = this->index_of(layer);
  325. if ((layer_index + 1) == m_layers.size()) {
  326. dbgln("Cannot merge layer up: layer is already at the top");
  327. return; // FIXME: Notify user of error properly.
  328. }
  329. auto& layer_above = m_layers.at(layer_index + 1);
  330. GUI::Painter painter(layer_above.content_bitmap());
  331. painter.draw_scaled_bitmap(rect(), layer.display_bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
  332. remove_layer(layer);
  333. select_layer(&layer_above);
  334. }
  335. void Image::merge_active_layer_down(Layer& layer)
  336. {
  337. if (m_layers.size() < 2)
  338. return;
  339. int layer_index = this->index_of(layer);
  340. if (layer_index == 0) {
  341. dbgln("Cannot merge layer down: layer is already at the bottom");
  342. return; // FIXME: Notify user of error properly.
  343. }
  344. auto& layer_below = m_layers.at(layer_index - 1);
  345. GUI::Painter painter(layer_below.content_bitmap());
  346. painter.draw_scaled_bitmap(rect(), layer.display_bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
  347. remove_layer(layer);
  348. select_layer(&layer_below);
  349. }
  350. void Image::select_layer(Layer* layer)
  351. {
  352. for (auto* client : m_clients)
  353. client->image_select_layer(layer);
  354. }
  355. void Image::add_client(ImageClient& client)
  356. {
  357. VERIFY(!m_clients.contains(&client));
  358. m_clients.set(&client);
  359. }
  360. void Image::remove_client(ImageClient& client)
  361. {
  362. VERIFY(m_clients.contains(&client));
  363. m_clients.remove(&client);
  364. }
  365. void Image::layer_did_modify_bitmap(Badge<Layer>, Layer const& layer, Gfx::IntRect const& modified_layer_rect)
  366. {
  367. auto layer_index = index_of(layer);
  368. for (auto* client : m_clients)
  369. client->image_did_modify_layer_bitmap(layer_index);
  370. did_change(modified_layer_rect.translated(layer.location()));
  371. }
  372. void Image::layer_did_modify_properties(Badge<Layer>, Layer const& layer)
  373. {
  374. auto layer_index = index_of(layer);
  375. for (auto* client : m_clients)
  376. client->image_did_modify_layer_properties(layer_index);
  377. did_change();
  378. }
  379. void Image::did_change(Gfx::IntRect const& a_modified_rect)
  380. {
  381. auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
  382. for (auto* client : m_clients)
  383. client->image_did_change(modified_rect);
  384. }
  385. void Image::did_change_rect(Gfx::IntRect const& a_modified_rect)
  386. {
  387. auto modified_rect = a_modified_rect.is_empty() ? this->rect() : a_modified_rect;
  388. for (auto* client : m_clients)
  389. client->image_did_change_rect(modified_rect);
  390. }
  391. ImageUndoCommand::ImageUndoCommand(Image& image, DeprecatedString action_text)
  392. : m_snapshot(image.take_snapshot().release_value_but_fixme_should_propagate_errors())
  393. , m_image(image)
  394. , m_action_text(move(action_text))
  395. {
  396. }
  397. void ImageUndoCommand::undo()
  398. {
  399. // FIXME: Handle errors.
  400. (void)m_image.restore_snapshot(*m_snapshot);
  401. }
  402. void ImageUndoCommand::redo()
  403. {
  404. undo();
  405. }
  406. ErrorOr<void> Image::flip(Gfx::Orientation orientation)
  407. {
  408. Vector<NonnullRefPtr<Layer>> flipped_layers;
  409. TRY(flipped_layers.try_ensure_capacity(m_layers.size()));
  410. VERIFY(m_layers.size() > 0);
  411. size_t selected_layer_index = 0;
  412. for (size_t i = 0; i < m_layers.size(); ++i) {
  413. auto& layer = m_layers[i];
  414. auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
  415. if (layer.is_selected())
  416. selected_layer_index = i;
  417. TRY(new_layer->flip(orientation, Layer::NotifyClients::No));
  418. flipped_layers.unchecked_append(new_layer);
  419. }
  420. m_layers = move(flipped_layers);
  421. for (auto& layer : m_layers)
  422. layer.did_modify_bitmap({}, Layer::NotifyClients::No);
  423. select_layer(&m_layers[selected_layer_index]);
  424. did_change();
  425. return {};
  426. }
  427. ErrorOr<void> Image::rotate(Gfx::RotationDirection direction)
  428. {
  429. Vector<NonnullRefPtr<Layer>> rotated_layers;
  430. TRY(rotated_layers.try_ensure_capacity(m_layers.size()));
  431. VERIFY(m_layers.size() > 0);
  432. size_t selected_layer_index = 0;
  433. for (size_t i = 0; i < m_layers.size(); ++i) {
  434. auto& layer = m_layers[i];
  435. auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
  436. if (layer.is_selected())
  437. selected_layer_index = i;
  438. TRY(new_layer->rotate(direction, Layer::NotifyClients::No));
  439. rotated_layers.unchecked_append(new_layer);
  440. }
  441. m_layers = move(rotated_layers);
  442. for (auto& layer : m_layers)
  443. layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
  444. select_layer(&m_layers[selected_layer_index]);
  445. m_size = { m_size.height(), m_size.width() };
  446. did_change_rect();
  447. return {};
  448. }
  449. ErrorOr<void> Image::crop(Gfx::IntRect const& cropped_rect)
  450. {
  451. Vector<NonnullRefPtr<Layer>> cropped_layers;
  452. TRY(cropped_layers.try_ensure_capacity(m_layers.size()));
  453. VERIFY(m_layers.size() > 0);
  454. size_t selected_layer_index = 0;
  455. for (size_t i = 0; i < m_layers.size(); ++i) {
  456. auto& layer = m_layers[i];
  457. auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
  458. if (layer.is_selected())
  459. selected_layer_index = i;
  460. auto layer_location = new_layer->location();
  461. auto layer_local_crop_rect = new_layer->relative_rect().intersected(cropped_rect).translated(-layer_location.x(), -layer_location.y());
  462. TRY(new_layer->crop(layer_local_crop_rect, Layer::NotifyClients::No));
  463. auto new_layer_x = max(0, layer_location.x() - cropped_rect.x());
  464. auto new_layer_y = max(0, layer_location.y() - cropped_rect.y());
  465. new_layer->set_location({ new_layer_x, new_layer_y });
  466. cropped_layers.unchecked_append(new_layer);
  467. }
  468. m_layers = move(cropped_layers);
  469. for (auto& layer : m_layers)
  470. layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
  471. select_layer(&m_layers[selected_layer_index]);
  472. m_size = { cropped_rect.width(), cropped_rect.height() };
  473. did_change_rect(cropped_rect);
  474. return {};
  475. }
  476. Optional<Gfx::IntRect> Image::nonempty_content_bounding_rect() const
  477. {
  478. if (m_layers.is_empty())
  479. return {};
  480. Optional<Gfx::IntRect> bounding_rect;
  481. for (auto const& layer : m_layers) {
  482. auto layer_content_rect_in_layer_coordinates = layer.nonempty_content_bounding_rect();
  483. if (!layer_content_rect_in_layer_coordinates.has_value())
  484. continue;
  485. auto layer_content_rect_in_image_coordinates = layer_content_rect_in_layer_coordinates->translated(layer.location());
  486. if (!bounding_rect.has_value())
  487. bounding_rect = layer_content_rect_in_image_coordinates;
  488. else
  489. bounding_rect = bounding_rect->united(layer_content_rect_in_image_coordinates);
  490. }
  491. return bounding_rect;
  492. }
  493. ErrorOr<void> Image::resize(Gfx::IntSize new_size, Gfx::Painter::ScalingMode scaling_mode)
  494. {
  495. float scale_x = 1.0f;
  496. float scale_y = 1.0f;
  497. if (size().width() != 0.0f) {
  498. scale_x = new_size.width() / static_cast<float>(size().width());
  499. }
  500. if (size().height() != 0.0f) {
  501. scale_y = new_size.height() / static_cast<float>(size().height());
  502. }
  503. Vector<NonnullRefPtr<Layer>> resized_layers;
  504. TRY(resized_layers.try_ensure_capacity(m_layers.size()));
  505. VERIFY(m_layers.size() > 0);
  506. size_t selected_layer_index = 0;
  507. for (size_t i = 0; i < m_layers.size(); ++i) {
  508. auto& layer = m_layers[i];
  509. auto new_layer = TRY(Layer::try_create_snapshot(*this, layer));
  510. if (layer.is_selected())
  511. selected_layer_index = i;
  512. Gfx::IntPoint new_location(scale_x * new_layer->location().x(), scale_y * new_layer->location().y());
  513. TRY(new_layer->resize(new_size, new_location, scaling_mode, Layer::NotifyClients::No));
  514. resized_layers.unchecked_append(new_layer);
  515. }
  516. m_layers = move(resized_layers);
  517. for (auto& layer : m_layers)
  518. layer.did_modify_bitmap({}, Layer::NotifyClients::Yes);
  519. select_layer(&m_layers[selected_layer_index]);
  520. m_size = { new_size.width(), new_size.height() };
  521. did_change_rect();
  522. return {};
  523. }
  524. Color Image::color_at(Gfx::IntPoint point) const
  525. {
  526. Color color;
  527. for (auto const& layer : m_layers) {
  528. if (!layer.is_visible() || !layer.rect().contains(point))
  529. continue;
  530. auto layer_color = layer.display_bitmap().get_pixel(point);
  531. float layer_opacity = layer.opacity_percent() / 100.0f;
  532. layer_color.set_alpha((u8)(layer_color.alpha() * layer_opacity));
  533. color = color.blend(layer_color);
  534. }
  535. return color;
  536. }
  537. }