DGtal 1.3.0
Loading...
Searching...
No Matches
STBReader.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 STBReader.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 STBReader.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_IMPLEMENTATION
32#endif //NO_ADD_STBIMAGE_IMPLEMENT
33#include <stb/stb_image.h>
34
35///////////////////////////////////////////////////////////////////////////////
36// Interface - public :
37
38
39template <typename TImageContainer, typename TFunctor>
40inline
41TImageContainer
42DGtal::STBReader<TImageContainer, TFunctor>::import(const std::string & filename,
43 const Functor & aFunctor)
44{
45 int x,y,n;
46 unsigned char *data = stbi_load(filename.c_str(), &x, &y, &n, 0);
47
48 typename TImageContainer::Point firstPoint(0,0);
49 typename TImageContainer::Point lastPoint(x-1,y-1);
50
51 typename TImageContainer::Domain domain(firstPoint,lastPoint);
52 TImageContainer image(domain);
53
54#ifdef VERBOSE
55 trace.info()<<"File size= "<<x<<"x"<<y<<" nbChannels= "<< n<<std::endl;
56#endif
57
58 Color col;
59 for(auto j=0; j < y; ++j)
60 for(auto i=0; i < x; ++i)
61 {
62 const auto id = j*x+i;
63 if (n==1)
64 col.setRGBi(data[id],data[id],data[id],data[id]);
65 else
66 if (n==2)
67 col.setRGBi(data[2*id],data[2*id],data[2*id],data[2*id+1]);
68 else
69 if (n==3)
70 col.setRGBi(data[3*id],data[3*id+1],data[3*id+2],255);
71 else
72 col.setRGBi(data[4*id],data[4*id+1],data[4*id+2],data[4*id+3]);
73
74 image.setValue(typename TImageContainer::Point(i,j), aFunctor(col) );
75 }
76
77 stbi_image_free(data);
78 return image;
79}
80
81// //
82///////////////////////////////////////////////////////////////////////////////
83
84