]> git.sesse.net Git - casparcg/blob - modules/image/util/image_loader.cpp
a4b51962f11739a8bde8b594a3de6942b64bcdf1
[casparcg] / modules / image / util / image_loader.cpp
1 /*\r
2 * copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 *  This file is part of CasparCG.\r
5 *\r
6 *    CasparCG is free software: you can redistribute it and/or modify\r
7 *    it under the terms of the GNU General Public License as published by\r
8 *    the Free Software Foundation, either version 3 of the License, or\r
9 *    (at your option) any later version.\r
10 *\r
11 *    CasparCG is distributed in the hope that it will be useful,\r
12 *    but WITHOUT ANY WARRANTY; without even the implied warranty of\r
13 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
14 *    GNU General Public License for more details.\r
15 \r
16 *    You should have received a copy of the GNU General Public License\r
17 *    along with CasparCG.  If not, see <http://www.gnu.org/licenses/>.\r
18 *\r
19 */\r
20 #include "image_loader.h"\r
21 \r
22 #include <common/exception/Exceptions.h>\r
23 #include <common/utility/string.h>\r
24 \r
25 #if defined(_MSC_VER)\r
26 #pragma warning (disable : 4714) // marked as __forceinline not inlined\r
27 #endif\r
28 \r
29 #include <boost/exception/errinfo_file_name.hpp>\r
30 #include <boost/filesystem.hpp>\r
31 \r
32 namespace caspar { \r
33 \r
34 std::shared_ptr<FIBITMAP> load_image(const std::string& filename)\r
35 {\r
36         if(!boost::filesystem::exists(filename))\r
37                 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(filename));\r
38 \r
39         FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;\r
40         fif = FreeImage_GetFileType(filename.c_str(), 0);\r
41         if(fif == FIF_UNKNOWN) \r
42                 fif = FreeImage_GetFIFFromFilename(filename.c_str());\r
43                 \r
44         if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) \r
45                 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));\r
46                 \r
47         auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Load(fif, filename.c_str(), 0), FreeImage_Unload);\r
48                   \r
49         if(FreeImage_GetBPP(bitmap.get()) != 32)\r
50         {\r
51                 bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);\r
52                 if(!bitmap)\r
53                         BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));                     \r
54         }\r
55         \r
56         return bitmap;\r
57 }\r
58 \r
59 std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)\r
60 {\r
61         return load_image(narrow(filename));\r
62 }\r
63 \r
64 }