summaryrefslogtreecommitdiff
path: root/Userland/Libraries/LibGfx/ImageDecoder.cpp
blob: 8d9720767c19049e564beb8fc5d8115a20bbf63d (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
/*
 * Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
 *
 * SPDX-License-Identifier: BSD-2-Clause
 */

#include <LibGfx/BMPLoader.h>
#include <LibGfx/DDSLoader.h>
#include <LibGfx/GIFLoader.h>
#include <LibGfx/ICOLoader.h>
#include <LibGfx/ImageDecoder.h>
#include <LibGfx/JPGLoader.h>
#include <LibGfx/PBMLoader.h>
#include <LibGfx/PGMLoader.h>
#include <LibGfx/PNGLoader.h>
#include <LibGfx/PPMLoader.h>
#include <LibGfx/QOILoader.h>

namespace Gfx {

RefPtr<ImageDecoder> ImageDecoder::try_create(ReadonlyBytes bytes)
{
    auto* data = bytes.data();
    auto size = bytes.size();

    auto plugin = [](auto* data, auto size) -> OwnPtr<ImageDecoderPlugin> {
        OwnPtr<ImageDecoderPlugin> plugin;

        plugin = make<PNGImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<GIFImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<BMPImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<PBMImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<PGMImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<PPMImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<ICOImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<JPGImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<DDSImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        plugin = make<QOIImageDecoderPlugin>(data, size);
        if (plugin->sniff())
            return plugin;

        return {};
    }(data, size);

    if (!plugin)
        return {};
    return adopt_ref_if_nonnull(new (nothrow) ImageDecoder(plugin.release_nonnull()));
}

ImageDecoder::ImageDecoder(NonnullOwnPtr<ImageDecoderPlugin> plugin)
    : m_plugin(move(plugin))
{
}

}