Visual Computing Library  devel
Loading...
Searching...
No Matches
load.h
1/*****************************************************************************
2 * VCLib *
3 * Visual Computing Library *
4 * *
5 * Copyright(C) 2021-2025 *
6 * Visual Computing Lab *
7 * ISTI - Italian National Research Council *
8 * *
9 * All rights reserved. *
10 * *
11 * This program is free software; you can redistribute it and/or modify *
12 * it under the terms of the Mozilla Public License Version 2.0 as published *
13 * by the Mozilla Foundation; either version 2 of the License, or *
14 * (at your option) any later version. *
15 * *
16 * This program is distributed in the hope that it will be useful, *
17 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
18 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
19 * Mozilla Public License Version 2.0 *
20 * (https://www.mozilla.org/en-US/MPL/2.0/) for more details. *
21 ****************************************************************************/
22
23#ifndef VCL_IO_IMAGE_LOAD_H
24#define VCL_IO_IMAGE_LOAD_H
25
26#ifdef VCLIB_WITH_QT
27#include "qt/load.h"
28#endif
29
30#ifdef VCLIB_WITH_STB
31#include "stb/load.h"
32#endif
33
34#include <vclib/io/file_format.h>
35#include <vclib/io/file_info.h>
36
37#include <vclib/space/core.h>
38
39#include <memory>
40#include <set>
41#include <string>
42
43namespace vcl {
44
53inline std::set<FileFormat> loadImageFormats()
54{
55 std::set<FileFormat> ff;
56
57#ifdef VCLIB_WITH_QT
58 auto fqt = qt::loadImageFormats();
59 ff.insert(fqt.begin(), fqt.end());
60#endif
61
62#ifdef VCLIB_WITH_STB
63 auto fstb = stb::loadImageFormats();
64 ff.insert(fstb.begin(), fstb.end());
65#endif
66 return ff;
67}
68
69inline std::shared_ptr<unsigned char> loadImageData(
70 const std::string& filename,
71 int& w,
72 int& h)
73{
74 FileFormat ff = FileInfo::fileFormat(filename);
75
76#ifdef VCLIB_WITH_QT
77 if (qt::loadImageFormats().contains(ff)) {
78 return qt::loadImageData(filename, w, h);
79 }
80#endif
81
82#ifdef VCLIB_WITH_STB
83 if (stb::loadImageFormats().contains(ff)) {
84 return stb::loadImageData(filename, w, h);
85 }
86#endif
87 throw UnknownFileFormatException(ff.extensions().front());
88}
89
90inline Image loadImage(const std::string& filename)
91{
92 int w, h;
93 auto data = loadImageData(filename, w, h);
94 if (!data) {
95 return Image();
96 }
97 return Image(data.get(), w, h);
98}
99
100} // namespace vcl
101
102#endif // VCL_IO_IMAGE_LOAD_H
static FileFormat fileFormat(const std::string &filename)
Get the file format of a file from its filename.
Definition file_info.h:280