Visual Computing Library  devel
Loading...
Searching...
No Matches
save.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_SAVE_H
24#define VCL_IO_IMAGE_SAVE_H
25
26#include "bmp/save.h"
27
28#ifdef VCLIB_WITH_QT
29#include "qt/save.h"
30#endif
31
32#ifdef VCLIB_WITH_STB
33#include "stb/save.h"
34#endif
35
36#include <vclib/base.h>
37#include <vclib/mesh.h>
38#include <vclib/space/core.h>
39
40#include <set>
41
42namespace vcl {
43
52inline std::set<FileFormat> saveImageFormats()
53{
54 std::set<FileFormat> ff;
55
56#ifdef VCLIB_WITH_QT
57 auto fqt = qt::saveImageFormats();
58 ff.insert(fqt.begin(), fqt.end());
59#endif
60
61#ifdef VCLIB_WITH_STB
62 auto fstb = stb::saveImageFormats();
63 ff.insert(fstb.begin(), fstb.end());
64#endif
65 return ff;
66}
67
68inline void saveImageData(
69 const std::string& filename,
70 int w,
71 int h,
72 const unsigned char* data,
73 uint quality = 90)
74{
75 FileFormat ff = FileInfo::fileFormat(filename);
76
77#ifdef VCLIB_WITH_QT
78 if (qt::saveImageFormats().contains(ff)) {
79 return qt::saveImageData(filename, w, h, data, quality);
80 }
81#endif
82
83#ifdef VCLIB_WITH_TINYGLTF
84 if (stb::saveImageFormats().contains(ff)) {
85 return stb::saveImageData(filename, w, h, data, quality);
86 }
87#endif
88 if (ff == FileFormat("bmp")) {
89 // save rgb image data into bmp file
90 return saveImageToBmp(filename, w, h, data);
91 }
92 throw UnknownFileFormatException(ff.extensions().front());
93}
94
95inline void saveImage(const Image& image, const std::string& filename)
96{
97 saveImageData(filename, image.width(), image.height(), image.data());
98}
99
114template<MeshConcept MeshType, LoggerConcept LogType = NullLogger>
115void saveTextureImages(
116 const MeshType& mesh,
117 const std::string& basePath,
118 const BitSet8& textureTypesToSave = BitSet8::ALL(),
119 LogType& log = nullLogger) requires HasMaterials<MeshType>
120{
121 // to avoid saving the same image multiple times
122 std::set<std::string> savedImages;
123
124 for (const Material& mat : mesh.materials()) {
125 using enum Material::TextureType;
126 const uint N_TEXTURE_TYPES = toUnderlying(COUNT);
127 for (uint i = 0; i < N_TEXTURE_TYPES; ++i) {
128 // supported textures to save
129 if (textureTypesToSave[i]) {
130 const TextureDescriptor& t = mat.textureDescriptor(i);
131 if (!savedImages.contains(t.path())) {
132 const Image& img = mesh.textureImage(t.path());
133 if (img.isNull() || t.isNull()) {
134 log.log(
135 "Cannot save empty texture " + t.path(),
136 LogType::WARNING_LOG);
137 }
138 else {
139 saveImage(img, basePath + t.path());
140 savedImages.insert(t.path());
141 }
142 }
143 }
144 }
145 }
146}
147
148} // namespace vcl
149
150#endif // VCL_IO_IMAGE_SAVE_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
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