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/mesh.h>
38#include <vclib/space/core.h>
39
40#include <memory>
41#include <set>
42#include <string>
43
44namespace vcl {
45
54inline std::set<FileFormat> loadImageFormats()
55{
56 std::set<FileFormat> ff;
57
58#ifdef VCLIB_WITH_QT
59 auto fqt = qt::loadImageFormats();
60 ff.insert(fqt.begin(), fqt.end());
61#endif
62
63#ifdef VCLIB_WITH_STB
64 auto fstb = stb::loadImageFormats();
65 ff.insert(fstb.begin(), fstb.end());
66#endif
67 return ff;
68}
69
70inline std::shared_ptr<unsigned char> loadImageData(
71 const std::string& filename,
72 int& w,
73 int& h)
74{
75 FileFormat ff = FileInfo::fileFormat(filename);
76
77#ifdef VCLIB_WITH_QT
78 if (qt::loadImageFormats().contains(ff)) {
79 return qt::loadImageData(filename, w, h);
80 }
81#endif
82
83#ifdef VCLIB_WITH_STB
84 if (stb::loadImageFormats().contains(ff)) {
85 return stb::loadImageData(filename, w, h);
86 }
87#endif
88 throw UnknownFileFormatException(ff.extensions().front());
89}
90
91inline Image loadImage(const std::string& filename)
92{
93 int w, h;
94 auto data = loadImageData(filename, w, h);
95 if (!data) {
96 return Image();
97 }
98 return Image(data.get(), w, h);
99}
100
116template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
117void loadTextureImages(
118 MeshType& mesh,
119 std::string basePath = "",
120 const BitSet8& textureTypesToLoad = BitSet8::ALL(),
121 LogType& log = nullLogger) requires HasMaterials<MeshType>
122{
123 if (basePath.empty()) {
124 basePath = mesh.meshBasePath();
125 }
126
127 for (const Material& mat : mesh.materials()) {
128 using enum Material::TextureType;
129 const uint N_TEXTURE_TYPES = toUnderlying(COUNT);
130 for (uint i = 0; i < N_TEXTURE_TYPES; ++i) {
131 auto texType = static_cast<Material::TextureType>(i);
132 // supported textures to load
133 if (textureTypesToLoad[i]) {
134 const TextureDescriptor& tex = mat.textureDescriptor(i);
135 if (!tex.isNull()) {
136 // if not already loaded
137 if (mesh.textureImage(tex.path()).isNull()) {
138 Image img = loadImage(basePath + tex.path());
139 if (img.isNull()) {
140 log.log(
141 "Cannot load texture " + tex.path(),
142 LogType::WARNING_LOG);
143 }
144 else {
145 img.colorSpace() =
147 mesh.pushTextureImage(tex.path(), std::move(img));
148 }
149 }
150 }
151 }
152 }
153 }
154}
155
156} // namespace vcl
157
158#endif // VCL_IO_IMAGE_LOAD_H
static constexpr BitSet< T > ALL()
Returns a BitSet with all the bits set to true.
Definition bit_set.h:366
static FileFormat fileFormat(const std::string &filename)
Get the file format of a file from its filename.
Definition file_info.h:280
TextureType
Defines the types of textures used in the PBR material model.
Definition material.h:62
static Image::ColorSpace textureTypeToColorSpace(TextureType type)
Determines the appropriate color space for a given texture type.
Definition material.h:375
NullLogger nullLogger
The nullLogger object is an object of type NullLogger that is used as default argument in the functio...
Definition null_logger.h:123
BitSet< char > BitSet8
BitSet8 is a BitSet of 8 bits.
Definition bit_set.h:401