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/space/core.h>
37
38#include <set>
39
40namespace vcl {
41
50inline std::set<FileFormat> saveImageFormats()
51{
52 std::set<FileFormat> ff;
53
54#ifdef VCLIB_WITH_QT
55 auto fqt = qt::saveImageFormats();
56 ff.insert(fqt.begin(), fqt.end());
57#endif
58
59#ifdef VCLIB_WITH_STB
60 auto fstb = stb::saveImageFormats();
61 ff.insert(fstb.begin(), fstb.end());
62#endif
63 return ff;
64}
65
66inline void saveImageData(
67 const std::string& filename,
68 int w,
69 int h,
70 const unsigned char* data,
71 uint quality = 90)
72{
73 FileFormat ff = FileInfo::fileFormat(filename);
74
75#ifdef VCLIB_WITH_QT
76 if (qt::saveImageFormats().contains(ff)) {
77 return qt::saveImageData(filename, w, h, data, quality);
78 }
79#endif
80
81#ifdef VCLIB_WITH_TINYGLTF
82 if (stb::saveImageFormats().contains(ff)) {
83 return stb::saveImageData(filename, w, h, data, quality);
84 }
85#endif
86 if (ff == FileFormat("bmp")) {
87 // save rgb image data into bmp file
88 return saveImageToBmp(filename, w, h, data);
89 }
90 throw UnknownFileFormatException(ff.extensions().front());
91}
92
93inline void saveImage(const Image& image, const std::string& filename)
94{
95 saveImageData(filename, image.width(), image.height(), image.data());
96}
97
98} // namespace vcl
99
100#endif // VCL_IO_IMAGE_SAVE_H
static FileFormat fileFormat(const std::string &filename)
Get the file format of a file from its filename.
Definition file_info.h:280