summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibImageDecoderClient/Client.cpp
blob: abb151c279e2238321bda60b0b87f5e22ffa9656 (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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (c) 2020-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibCore/AnonymousBuffer.h>
#include <LibImageDecoderClient/Client.h>

namespace ImageDecoderClient {

Client::Client(NonnullOwnPtr<Core::Stream::LocalSocket> socket)
    : IPC::ConnectionToServer<ImageDecoderClientEndpoint, ImageDecoderServerEndpoint>(*this, move(socket))
{
}

void Client::die()
{
    if (on_death)
        on_death();
}

Optional<DecodedImage> Client::decode_image_with_known_path(DeprecatedString const& path, ReadonlyBytes encoded_data)
{
    if (encoded_data.is_empty())
        return {};

    auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
    if (encoded_buffer_or_error.is_error()) {
        dbgln("Could not allocate encoded buffer");
        return {};
    }
    auto encoded_buffer = encoded_buffer_or_error.release_value();

    memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
    auto response_or_error = try_decode_image_with_known_path(path, move(encoded_buffer));

    if (response_or_error.is_error()) {
        dbgln("ImageDecoder died heroically");
        return {};
    }

    auto& response = response_or_error.value();

    if (response.bitmaps().is_empty())
        return {};

    DecodedImage image;
    image.is_animated = response.is_animated();
    image.loop_count = response.loop_count();
    image.frames.resize(response.bitmaps().size());
    for (size_t i = 0; i < image.frames.size(); ++i) {
        auto& frame = image.frames[i];
        frame.bitmap = response.bitmaps()[i].bitmap();
        frame.duration = response.durations()[i];
    }
    return image;
}

Optional<DecodedImage> Client::decode_image(ReadonlyBytes encoded_data)
{
    if (encoded_data.is_empty())
        return {};

    auto encoded_buffer_or_error = Core::AnonymousBuffer::create_with_size(encoded_data.size());
    if (encoded_buffer_or_error.is_error()) {
        dbgln("Could not allocate encoded buffer");
        return {};
    }
    auto encoded_buffer = encoded_buffer_or_error.release_value();

    memcpy(encoded_buffer.data<void>(), encoded_data.data(), encoded_data.size());
    auto response_or_error = try_decode_image(move(encoded_buffer));

    if (response_or_error.is_error()) {
        dbgln("ImageDecoder died heroically");
        return {};
    }

    auto& response = response_or_error.value();

    if (response.bitmaps().is_empty())
        return {};

    DecodedImage image;
    image.is_animated = response.is_animated();
    image.loop_count = response.loop_count();
    image.frames.resize(response.bitmaps().size());
    for (size_t i = 0; i < image.frames.size(); ++i) {
        auto& frame = image.frames[i];
        frame.bitmap = response.bitmaps()[i].bitmap();
        frame.duration = response.durations()[i];
    }
    return image;
}

}