DGtal  1.4.beta
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 #pragma GCC diagnostic push
34 #pragma GCC diagnostic ignored "-Wdeprecated"
35 #pragma GCC diagnostic ignored "-Wmissing-field-initializers"
36 #pragma clang diagnostic push
37 #pragma clang diagnostic ignored "-Wdeprecated"
38 #pragma clang diagnostic ignored "-Wmissing-field-initializers"
39 #include <stb/stb_image_write.h>
40 #pragma GCC diagnostic pop
41 #pragma clang diagnostic pop
42 
43 ///////////////////////////////////////////////////////////////////////////////
44 // Interface - public :
45 
46 
47 template <typename TImageContainer, typename TFunctor>
48 inline
49 bool
50 DGtal::STBWriter<TImageContainer, TFunctor>::exportPNG(const std::string & filename,
51  const TImageContainer & anImage,
52  const Functor & aFunctor)
53 {
54  const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
55  const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
56  const int comp=4;
57  unsigned char *data = (unsigned char*)malloc( w * h * comp );
58 
59  unsigned int cpt=0;
60  for(const auto &value: anImage)
61  {
62  Color v = aFunctor(value);
63  data[4*cpt] = v.red();
64  data[4*cpt+1] = v.green();
65  data[4*cpt+2] = v.blue();
66  data[4*cpt+3] = v.alpha();
67  ++cpt;
68  }
69  const int error = stbi_write_png(filename.c_str(), w, h, comp, data, w*comp);
70  free(data);
71  return error;
72 }
73 
74 template <typename TImageContainer, typename TFunctor>
75 inline
76 bool
77 DGtal::STBWriter<TImageContainer, TFunctor>::exportBMP(const std::string & filename,
78  const TImageContainer & anImage,
79  const Functor & aFunctor)
80 {
81  const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
82  const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
83  const int comp=4;
84  unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
85 
86  unsigned int cpt=0;
87  for(const auto &value: anImage)
88  {
89  Color v = aFunctor(value);
90  data[4*cpt] = v.red();
91  data[4*cpt+1] = v.green();
92  data[4*cpt+2] = v.blue();
93  data[4*cpt+3] = v.alpha();
94  ++cpt;
95  }
96  const int error = stbi_write_bmp(filename.c_str(), w, h, comp, data);
97  free(data);
98  return error;
99 }
100 
101 
102 template <typename TImageContainer, typename TFunctor>
103 inline
104 bool
105 DGtal::STBWriter<TImageContainer, TFunctor>::exportTGA(const std::string & filename,
106  const TImageContainer & anImage,
107  const Functor & aFunctor)
108 {
109  const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
110  const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
111  const int comp=4;
112  unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
113 
114  unsigned int cpt=0;
115  for(const auto &value: anImage)
116  {
117  Color v = aFunctor(value);
118  data[4*cpt] = v.red();
119  data[4*cpt+1] = v.green();
120  data[4*cpt+2] = v.blue();
121  data[4*cpt+3] = v.alpha();
122  ++cpt;
123  }
124  const int error = stbi_write_bmp(filename.c_str(), w, h, comp, data);
125  free(data);
126  return error;
127 }
128 template <typename TImageContainer, typename TFunctor>
129 inline
130 bool
131 DGtal::STBWriter<TImageContainer, TFunctor>::exportJPG(const std::string & filename,
132  const TImageContainer & anImage,
133  const Functor & aFunctor,
134  int quality )
135 {
136  const int w = anImage.domain().upperBound()[0] - anImage.domain().lowerBound()[0] +1 ;
137  const int h = anImage.domain().upperBound()[1] - anImage.domain().lowerBound()[1] +1 ;
138  const int comp=4;
139  unsigned char *data = (unsigned char*)malloc( sizeof(unsigned char)*w * h * comp);
140 
141  unsigned int cpt=0;
142  for(const auto &value: anImage)
143  {
144  Color v = aFunctor(value);
145  data[4*cpt] = v.red();
146  data[4*cpt+1] = v.green();
147  data[4*cpt+2] = v.blue();
148  data[4*cpt+3] = v.alpha();
149  ++cpt;
150  }
151  const int error = stbi_write_jpg(filename.c_str(), w, h, comp, data, quality);
152  free(data);
153  return error;
154 }
155 // //
156 ///////////////////////////////////////////////////////////////////////////////
157 
158