Image.cpp 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357
  1. /*
  2. * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "Image.h"
  7. #include "Layer.h"
  8. #include <AK/Base64.h>
  9. #include <AK/JsonObject.h>
  10. #include <AK/JsonObjectSerializer.h>
  11. #include <AK/JsonValue.h>
  12. #include <AK/StringBuilder.h>
  13. #include <LibGUI/Painter.h>
  14. #include <LibGfx/BMPLoader.h>
  15. #include <LibGfx/BMPWriter.h>
  16. #include <LibGfx/Bitmap.h>
  17. #include <LibGfx/ImageDecoder.h>
  18. #include <LibGfx/PNGLoader.h>
  19. #include <LibGfx/PNGWriter.h>
  20. #include <stdio.h>
  21. namespace PixelPaint {
  22. RefPtr<Image> Image::try_create_with_size(Gfx::IntSize const& size)
  23. {
  24. if (size.is_empty())
  25. return nullptr;
  26. if (size.width() > 16384 || size.height() > 16384)
  27. return nullptr;
  28. return adopt_ref(*new Image(size));
  29. }
  30. Image::Image(Gfx::IntSize const& size)
  31. : m_size(size)
  32. {
  33. }
  34. void Image::paint_into(GUI::Painter& painter, Gfx::IntRect const& dest_rect)
  35. {
  36. float scale = (float)dest_rect.width() / (float)rect().width();
  37. Gfx::PainterStateSaver saver(painter);
  38. painter.add_clip_rect(dest_rect);
  39. for (auto& layer : m_layers) {
  40. if (!layer.is_visible())
  41. continue;
  42. auto target = dest_rect.translated(layer.location().x() * scale, layer.location().y() * scale);
  43. target.set_size(layer.size().width() * scale, layer.size().height() * scale);
  44. painter.draw_scaled_bitmap(target, layer.bitmap(), layer.rect(), (float)layer.opacity_percent() / 100.0f);
  45. }
  46. }
  47. RefPtr<Image> Image::try_create_from_bitmap(RefPtr<Gfx::Bitmap> bitmap)
  48. {
  49. auto image = try_create_with_size({ bitmap->width(), bitmap->height() });
  50. if (!image)
  51. return nullptr;
  52. auto layer = Layer::try_create_with_bitmap(*image, *bitmap, "Background");
  53. if (!layer)
  54. return nullptr;
  55. image->add_layer(layer.release_nonnull());
  56. return image;
  57. }
  58. RefPtr<Image> Image::try_create_from_pixel_paint_file(String const& file_path)
  59. {
  60. auto file = fopen(file_path.characters(), "r");
  61. fseek(file, 0L, SEEK_END);
  62. auto length = ftell(file);
  63. rewind(file);
  64. auto buffer = ByteBuffer::create_uninitialized(length);
  65. fread(buffer.data(), sizeof(u8), length, file);
  66. fclose(file);
  67. auto json_or_error = JsonValue::from_string(String::copy(buffer));
  68. if (!json_or_error.has_value())
  69. return nullptr;
  70. auto json = json_or_error.value().as_object();
  71. auto image = try_create_with_size({ json.get("width").to_i32(), json.get("height").to_i32() });
  72. json.get("layers").as_array().for_each([&](JsonValue json_layer) {
  73. auto json_layer_object = json_layer.as_object();
  74. auto width = json_layer_object.get("width").to_i32();
  75. auto height = json_layer_object.get("height").to_i32();
  76. auto name = json_layer_object.get("name").as_string();
  77. auto layer = Layer::try_create_with_size(*image, { width, height }, name);
  78. VERIFY(layer);
  79. layer->set_location({ json_layer_object.get("locationx").to_i32(), json_layer_object.get("locationy").to_i32() });
  80. layer->set_opacity_percent(json_layer_object.get("opacity_percent").to_i32());
  81. layer->set_visible(json_layer_object.get("visible").as_bool());
  82. layer->set_selected(json_layer_object.get("selected").as_bool());
  83. auto bitmap_base64_encoded = json_layer_object.get("bitmap").as_string();
  84. auto bitmap_data = decode_base64(bitmap_base64_encoded);
  85. auto image_decoder = Gfx::ImageDecoder::create(bitmap_data);
  86. auto bitmap = image_decoder->bitmap();
  87. VERIFY(bitmap);
  88. layer->set_bitmap(bitmap.release_nonnull());
  89. image->add_layer(*layer);
  90. });
  91. return image;
  92. }
  93. RefPtr<Image> Image::try_create_from_file(String const& file_path)
  94. {
  95. if (auto bitmap = Gfx::Bitmap::load_from_file(file_path))
  96. return try_create_from_bitmap(bitmap);
  97. return try_create_from_pixel_paint_file(file_path);
  98. }
  99. void Image::save(String const& file_path) const
  100. {
  101. // Build json file
  102. StringBuilder builder;
  103. JsonObjectSerializer json(builder);
  104. json.add("width", m_size.width());
  105. json.add("height", m_size.height());
  106. {
  107. auto json_layers = json.add_array("layers");
  108. for (const auto& layer : m_layers) {
  109. Gfx::BMPWriter bmp_dumber;
  110. auto json_layer = json_layers.add_object();
  111. json_layer.add("width", layer.size().width());
  112. json_layer.add("height", layer.size().height());
  113. json_layer.add("name", layer.name());
  114. json_layer.add("locationx", layer.location().x());
  115. json_layer.add("locationy", layer.location().y());
  116. json_layer.add("opacity_percent", layer.opacity_percent());
  117. json_layer.add("visible", layer.is_visible());
  118. json_layer.add("selected", layer.is_selected());
  119. json_layer.add("bitmap", encode_base64(bmp_dumber.dump(layer.bitmap())));
  120. }
  121. }
  122. json.finish();
  123. // Write json to disk
  124. auto file = fopen(file_path.characters(), "w");
  125. auto byte_buffer = builder.to_byte_buffer();
  126. fwrite(byte_buffer.data(), sizeof(u8), byte_buffer.size(), file);
  127. fclose(file);
  128. }
  129. void Image::export_bmp(String const& file_path)
  130. {
  131. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRx8888, m_size);
  132. GUI::Painter painter(*bitmap);
  133. paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
  134. Gfx::BMPWriter dumper;
  135. auto bmp = dumper.dump(bitmap);
  136. auto file = fopen(file_path.characters(), "wb");
  137. fwrite(bmp.data(), sizeof(u8), bmp.size(), file);
  138. fclose(file);
  139. }
  140. void Image::export_png(String const& file_path)
  141. {
  142. auto bitmap = Gfx::Bitmap::create(Gfx::BitmapFormat::BGRA8888, m_size);
  143. VERIFY(bitmap);
  144. GUI::Painter painter(*bitmap);
  145. paint_into(painter, { 0, 0, m_size.width(), m_size.height() });
  146. auto png = Gfx::PNGWriter::encode(*bitmap);
  147. auto file = fopen(file_path.characters(), "wb");
  148. fwrite(png.data(), sizeof(u8), png.size(), file);
  149. fclose(file);
  150. }
  151. void Image::add_layer(NonnullRefPtr<Layer> layer)
  152. {
  153. for (auto& existing_layer : m_layers) {
  154. VERIFY(&existing_layer != layer.ptr());
  155. }
  156. m_layers.append(move(layer));
  157. for (auto* client : m_clients)
  158. client->image_did_add_layer(m_layers.size() - 1);
  159. did_modify_layer_stack();
  160. }
  161. RefPtr<Image> Image::take_snapshot() const
  162. {
  163. auto snapshot = try_create_with_size(m_size);
  164. if (!snapshot)
  165. return nullptr;
  166. for (const auto& layer : m_layers) {
  167. auto layer_snapshot = Layer::try_create_snapshot(*snapshot, layer);
  168. if (!layer_snapshot)
  169. return nullptr;
  170. snapshot->add_layer(layer_snapshot.release_nonnull());
  171. }
  172. return snapshot;
  173. }
  174. void Image::restore_snapshot(Image const& snapshot)
  175. {
  176. m_layers.clear();
  177. select_layer(nullptr);
  178. for (const auto& snapshot_layer : snapshot.m_layers) {
  179. auto layer = Layer::try_create_snapshot(*this, snapshot_layer);
  180. VERIFY(layer);
  181. if (layer->is_selected())
  182. select_layer(layer.ptr());
  183. add_layer(*layer);
  184. }
  185. did_modify_layer_stack();
  186. }
  187. size_t Image::index_of(Layer const& layer) const
  188. {
  189. for (size_t i = 0; i < m_layers.size(); ++i) {
  190. if (&m_layers.at(i) == &layer)
  191. return i;
  192. }
  193. VERIFY_NOT_REACHED();
  194. }
  195. void Image::move_layer_to_back(Layer& layer)
  196. {
  197. NonnullRefPtr<Layer> protector(layer);
  198. auto index = index_of(layer);
  199. m_layers.remove(index);
  200. m_layers.prepend(layer);
  201. did_modify_layer_stack();
  202. }
  203. void Image::move_layer_to_front(Layer& layer)
  204. {
  205. NonnullRefPtr<Layer> protector(layer);
  206. auto index = index_of(layer);
  207. m_layers.remove(index);
  208. m_layers.append(layer);
  209. did_modify_layer_stack();
  210. }
  211. void Image::move_layer_down(Layer& layer)
  212. {
  213. NonnullRefPtr<Layer> protector(layer);
  214. auto index = index_of(layer);
  215. if (!index)
  216. return;
  217. m_layers.remove(index);
  218. m_layers.insert(index - 1, layer);
  219. did_modify_layer_stack();
  220. }
  221. void Image::move_layer_up(Layer& layer)
  222. {
  223. NonnullRefPtr<Layer> protector(layer);
  224. auto index = index_of(layer);
  225. if (index == m_layers.size() - 1)
  226. return;
  227. m_layers.remove(index);
  228. m_layers.insert(index + 1, layer);
  229. did_modify_layer_stack();
  230. }
  231. void Image::change_layer_index(size_t old_index, size_t new_index)
  232. {
  233. VERIFY(old_index < m_layers.size());
  234. VERIFY(new_index < m_layers.size());
  235. auto layer = m_layers.take(old_index);
  236. m_layers.insert(new_index, move(layer));
  237. did_modify_layer_stack();
  238. }
  239. void Image::did_modify_layer_stack()
  240. {
  241. for (auto* client : m_clients)
  242. client->image_did_modify_layer_stack();
  243. did_change();
  244. }
  245. void Image::remove_layer(Layer& layer)
  246. {
  247. NonnullRefPtr<Layer> protector(layer);
  248. auto index = index_of(layer);
  249. m_layers.remove(index);
  250. for (auto* client : m_clients)
  251. client->image_did_remove_layer(index);
  252. did_modify_layer_stack();
  253. }
  254. void Image::select_layer(Layer* layer)
  255. {
  256. for (auto* client : m_clients)
  257. client->image_select_layer(layer);
  258. }
  259. void Image::add_client(ImageClient& client)
  260. {
  261. VERIFY(!m_clients.contains(&client));
  262. m_clients.set(&client);
  263. }
  264. void Image::remove_client(ImageClient& client)
  265. {
  266. VERIFY(m_clients.contains(&client));
  267. m_clients.remove(&client);
  268. }
  269. void Image::layer_did_modify_bitmap(Badge<Layer>, Layer const& layer)
  270. {
  271. auto layer_index = index_of(layer);
  272. for (auto* client : m_clients)
  273. client->image_did_modify_layer(layer_index);
  274. did_change();
  275. }
  276. void Image::layer_did_modify_properties(Badge<Layer>, Layer const& layer)
  277. {
  278. auto layer_index = index_of(layer);
  279. for (auto* client : m_clients)
  280. client->image_did_modify_layer(layer_index);
  281. did_change();
  282. }
  283. void Image::did_change()
  284. {
  285. for (auto* client : m_clients)
  286. client->image_did_change();
  287. }
  288. ImageUndoCommand::ImageUndoCommand(Image& image)
  289. : m_snapshot(image.take_snapshot())
  290. , m_image(image)
  291. {
  292. }
  293. void ImageUndoCommand::undo()
  294. {
  295. m_image.restore_snapshot(*m_snapshot);
  296. }
  297. void ImageUndoCommand::redo()
  298. {
  299. undo();
  300. }
  301. }