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_IMPLEMENTATION
32#endif //NO_ADD_STBIMAGE_IMPLEMENT
33#include <stb/stb_image.h>
35///////////////////////////////////////////////////////////////////////////////
36// Interface - public :
39template <typename TImageContainer, typename TFunctor>
42DGtal::STBReader<TImageContainer, TFunctor>::import(const std::string & filename,
43 const Functor & aFunctor)
46 unsigned char *data = stbi_load(filename.c_str(), &x, &y, &n, 0);
48 typename TImageContainer::Point firstPoint(0,0);
49 typename TImageContainer::Point lastPoint(x-1,y-1);
51 typename TImageContainer::Domain domain(firstPoint,lastPoint);
52 TImageContainer image(domain);
55 trace.info()<<"File size= "<<x<<"x"<<y<<" nbChannels= "<< n<<std::endl;
59 for(auto j=0; j < y; ++j)
60 for(auto i=0; i < x; ++i)
62 const auto id = j*x+i;
64 col.setRGBi(data[id],data[id],data[id],data[id]);
67 col.setRGBi(data[2*id],data[2*id],data[2*id],data[2*id+1]);
70 col.setRGBi(data[3*id],data[3*id+1],data[3*id+2],255);
72 col.setRGBi(data[4*id],data[4*id+1],data[4*id+2],data[4*id+3]);
74 image.setValue(typename TImageContainer::Point(i,j), aFunctor(col) );
77 stbi_image_free(data);
82///////////////////////////////////////////////////////////////////////////////