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