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