ProjectLoader.cpp 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
  3. *
  4. * SPDX-License-Identifier: BSD-2-Clause
  5. */
  6. #include "ProjectLoader.h"
  7. #include "Image.h"
  8. #include "Layer.h"
  9. #include <AK/JsonObject.h>
  10. #include <LibCore/MimeData.h>
  11. #include <LibImageDecoderClient/Client.h>
  12. namespace PixelPaint {
  13. ErrorOr<void> ProjectLoader::load_from_file(StringView filename, NonnullOwnPtr<Core::File> file)
  14. {
  15. auto contents = TRY(file->read_until_eof());
  16. auto json_or_error = JsonValue::from_string(contents);
  17. if (json_or_error.is_error()) {
  18. m_is_raw_image = true;
  19. auto guessed_mime_type = Core::guess_mime_type_based_on_filename(filename);
  20. // FIXME: Find a way to avoid the memory copy here.
  21. auto bitmap = TRY(Image::decode_bitmap(contents, guessed_mime_type));
  22. auto image = TRY(Image::create_from_bitmap(move(bitmap)));
  23. m_image = image;
  24. return {};
  25. }
  26. auto& json = json_or_error.value().as_object();
  27. auto image = TRY(Image::create_from_pixel_paint_json(json));
  28. if (json.has_array("guides"sv))
  29. m_json_metadata = json.get_array("guides"sv).value();
  30. m_image = image;
  31. return {};
  32. }
  33. }