ProjectLoader.cpp 1.1 KB

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