ProjectLoader.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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 <AK/Result.h>
  11. #include <AK/String.h>
  12. #include <LibCore/File.h>
  13. #include <LibCore/MappedFile.h>
  14. #include <LibImageDecoderClient/Client.h>
  15. namespace PixelPaint {
  16. ErrorOr<void> ProjectLoader::try_load_from_file(Core::File& file)
  17. {
  18. auto contents = file.read_all();
  19. auto json_or_error = JsonValue::from_string(contents);
  20. if (json_or_error.is_error()) {
  21. m_is_raw_image = true;
  22. // FIXME: Find a way to avoid the memory copy here.
  23. auto bitmap = TRY(Image::try_decode_bitmap(contents));
  24. auto image = TRY(Image::try_create_from_bitmap(move(bitmap)));
  25. m_image = image;
  26. return {};
  27. }
  28. auto& json = json_or_error.value().as_object();
  29. auto image = TRY(Image::try_create_from_pixel_paint_json(json));
  30. if (json.has("guides"))
  31. m_json_metadata = json.get("guides").as_array();
  32. m_image = image;
  33. return {};
  34. }
  35. }