DGtal 1.3.0
Loading...
Searching...
No Matches
STBWriter.ih
1/**
2 * This program is free software: you can redistribute it and/or modify
3 * it under the terms of the GNU Lesser General Public License as
4 * published by the Free Software Foundation, either version 3 of the
5 * License, or (at your option) any later version.
6 *
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
11 *
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
14 *
15 **/
16
17/**
18 * @file STBWriter.ih
19 * @author David Coeurjolly (\c david.coeurjolly@liris.cnrs.fr )
20 * Laboratoire d'InfoRmatique en Image et Systemes d'information - LIRIS (CNRS, UMR 5205), CNRS, France
21 *
22 * @date 2022/06/14
23 *
24 * Implementation of inline methods defined in STBWriter.h
25 *
26 * This file is part of the DGtal library.
27 */
28
29#ifndef NO_ADD_STBIMAGE_IMPLEMENT //To avoid duplicated linking errors (like LNK2005 in MSVC)
30#pragma once
31#define STB_IMAGE_WRITE_IMPLEMENTATION
32#endif //NO_ADD_STBIMAGE_IMPLEMENT
33#include <stb/stb_image_write.h>
34#pragma GCC diagnostic warning "-Wdeprecated"
35///////////////////////////////////////////////////////////////////////////////
36// Interface - public :
37
38
39template <typename TImageContainer, typename TFunctor>
40inline
41bool
42DGtal::STBWriter<TImageContainer, TFunctor>::exportPNG(const std::string & filename,
43 const TImageContainer & anImage,
44 const Functor & aFunctor)
45{
46 const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
47 const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
48 const int comp=4;
49 unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
50
51 unsigned int cpt=0;
52 for(const auto &value: anImage)
53 {
54 Color v = aFunctor(value);
55 data[4*cpt] = v.red();
56 data[4*cpt+1] = v.green();
57 data[4*cpt+2] = v.blue();
58 data[4*cpt+3] = v.alpha();
59 ++cpt;
60 }
61 const int error = stbi_write_png(filename.c_str(), w, h, comp, data, w*comp);
62 delete(data);
63 return error;
64}
65
66template <typename TImageContainer, typename TFunctor>
67inline
68bool
69DGtal::STBWriter<TImageContainer, TFunctor>::exportBMP(const std::string & filename,
70 const TImageContainer & anImage,
71 const Functor & aFunctor)
72{
73 const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
74 const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
75 const int comp=4;
76 unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
77
78 unsigned int cpt=0;
79 for(const auto &value: anImage)
80 {
81 Color v = aFunctor(value);
82 data[4*cpt] = v.red();
83 data[4*cpt+1] = v.green();
84 data[4*cpt+2] = v.blue();
85 data[4*cpt+3] = v.alpha();
86 ++cpt;
87 }
88 const int error = stbi_write_bmp(filename.c_str(), w, h, comp, data);
89 delete(data);
90 return error;
91}
92
93
94template <typename TImageContainer, typename TFunctor>
95inline
96bool
97DGtal::STBWriter<TImageContainer, TFunctor>::exportTGA(const std::string & filename,
98 const TImageContainer & anImage,
99 const Functor & aFunctor)
100{
101 const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
102 const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
103 const int comp=4;
104 unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
105
106 unsigned int cpt=0;
107 for(const auto &value: anImage)
108 {
109 Color v = aFunctor(value);
110 data[4*cpt] = v.red();
111 data[4*cpt+1] = v.green();
112 data[4*cpt+2] = v.blue();
113 data[4*cpt+3] = v.alpha();
114 ++cpt;
115 }
116 const int error = stbi_write_bmp(filename.c_str(), w, h, comp, data);
117 delete(data);
118 return error;
119}
120template <typename TImageContainer, typename TFunctor>
121inline
122bool
123DGtal::STBWriter<TImageContainer, TFunctor>::exportJPG(const std::string & filename,
124 const TImageContainer & anImage,
125 const Functor & aFunctor,
126 int quality )
127{
128 const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
129 const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
130 const int comp=4;
131 unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
132
133 unsigned int cpt=0;
134 for(const auto &value: anImage)
135 {
136 Color v = aFunctor(value);
137 data[4*cpt] = v.red();
138 data[4*cpt+1] = v.green();
139 data[4*cpt+2] = v.blue();
140 data[4*cpt+3] = v.alpha();
141 ++cpt;
142 }
143 const int error = stbi_write_jpg(filename.c_str(), w, h, comp, data, quality);
144 delete(data);
145 return error;
146}
147// //
148///////////////////////////////////////////////////////////////////////////////
149
150