summaryrefslogtreecommitdiff
path: root/Userland/Applications/PixelPaint/ProjectLoader.cpp
blob: 020beb5423fd9fa3e10aa1d4ebb2102b36be340d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/*
 * 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/DeprecatedString.h>
#include <AK/JsonObject.h>
#include <AK/Result.h>
#include <LibCore/MappedFile.h>
#include <LibImageDecoderClient/Client.h>

namespace PixelPaint {

ErrorOr<void> ProjectLoader::load_from_file(NonnullOwnPtr<Core::Stream::File> file)
{
    auto contents = TRY(file->read_until_eof());

    auto json_or_error = JsonValue::from_string(contents);
    if (json_or_error.is_error()) {
        m_is_raw_image = true;

        // FIXME: Find a way to avoid the memory copy here.
        auto bitmap = TRY(Image::decode_bitmap(contents));
        auto image = TRY(Image::create_from_bitmap(move(bitmap)));

        m_image = image;
        return {};
    }

    auto& json = json_or_error.value().as_object();
    auto image = TRY(Image::create_from_pixel_paint_json(json));

    if (json.has_array("guides"sv))
        m_json_metadata = json.get_array("guides"sv).value();

    m_image = image;
    return {};
}

}