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