Image.cpp 8.9 KB

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