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.
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.
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/>.
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
24 * Implementation of inline methods defined in STBReader.h
26 * This file is part of the DGtal library.
29 #ifndef NO_ADD_STBIMAGE_IMPLEMENT //To avoid duplicated linking errors (like LNK2005 in MSVC)
31 #define STB_IMAGE_STATIC //issue #1714
32 #define STB_IMAGE_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"
41 #include <stb/stb_image.h>
42 #pragma GCC diagnostic pop
43 #pragma clang diagnostic pop
45 ///////////////////////////////////////////////////////////////////////////////
46 // Interface - public :
49 template <typename TImageContainer, typename TFunctor>
52 DGtal::STBReader<TImageContainer, TFunctor>::import(const std::string & filename,
53 const Functor & aFunctor)
56 unsigned char *data = stbi_load(filename.c_str(), &x, &y, &n, 0);
58 typename TImageContainer::Point firstPoint(0,0);
59 typename TImageContainer::Point lastPoint(x-1,y-1);
61 typename TImageContainer::Domain domain(firstPoint,lastPoint);
62 TImageContainer image(domain);
65 trace.info()<<"File size= "<<x<<"x"<<y<<" nbChannels= "<< n<<std::endl;
69 for(auto j=0; j < y; ++j)
70 for(auto i=0; i < x; ++i)
72 const auto id = j*x+i;
74 col.setRGBi(data[id],data[id],data[id],data[id]);
77 col.setRGBi(data[2*id],data[2*id],data[2*id],data[2*id+1]);
80 col.setRGBi(data[3*id],data[3*id+1],data[3*id+2],255);
82 col.setRGBi(data[4*id],data[4*id+1],data[4*id+2],data[4*id+3]);
84 image.setValue(typename TImageContainer::Point(i,j), aFunctor(col) );
87 stbi_image_free(data);
92 ///////////////////////////////////////////////////////////////////////////////