2021-09-03 18:46:11 +02:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2021, Tobias Christiansen <tobyase@serenityos.org>
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "ProjectLoader.h"
|
|
|
|
#include "Image.h"
|
|
|
|
#include "Layer.h"
|
|
|
|
#include <AK/JsonObject.h>
|
2023-08-08 20:22:54 +02:00
|
|
|
#include <LibCore/MimeData.h>
|
2021-09-03 18:46:11 +02:00
|
|
|
#include <LibImageDecoderClient/Client.h>
|
|
|
|
|
|
|
|
namespace PixelPaint {
|
|
|
|
|
2023-08-08 20:22:54 +02:00
|
|
|
ErrorOr<void> ProjectLoader::load_from_file(StringView filename, NonnullOwnPtr<Core::File> file)
|
2021-09-03 18:46:11 +02:00
|
|
|
{
|
2022-12-17 17:51:26 +01:00
|
|
|
auto contents = TRY(file->read_until_eof());
|
2021-09-03 18:46:11 +02:00
|
|
|
|
|
|
|
auto json_or_error = JsonValue::from_string(contents);
|
2021-11-15 01:46:51 +01:00
|
|
|
if (json_or_error.is_error()) {
|
2021-09-03 18:46:11 +02:00
|
|
|
m_is_raw_image = true;
|
2023-08-08 20:22:54 +02:00
|
|
|
auto guessed_mime_type = Core::guess_mime_type_based_on_filename(filename);
|
2021-09-03 18:46:11 +02:00
|
|
|
|
|
|
|
// FIXME: Find a way to avoid the memory copy here.
|
2023-08-08 20:22:54 +02:00
|
|
|
auto bitmap = TRY(Image::decode_bitmap(contents, guessed_mime_type));
|
2023-01-28 20:12:17 +00:00
|
|
|
auto image = TRY(Image::create_from_bitmap(move(bitmap)));
|
2021-09-03 18:46:11 +02:00
|
|
|
|
|
|
|
m_image = image;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
auto& json = json_or_error.value().as_object();
|
2023-01-28 20:12:17 +00:00
|
|
|
auto image = TRY(Image::create_from_pixel_paint_json(json));
|
2021-09-03 18:46:11 +02:00
|
|
|
|
2022-12-21 19:34:37 +00:00
|
|
|
if (json.has_array("guides"sv))
|
|
|
|
m_json_metadata = json.get_array("guides"sv).value();
|
2021-09-03 18:46:11 +02:00
|
|
|
|
|
|
|
m_image = image;
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|