]> git.sesse.net Git - casparcg/blob - modules/image/util/image_loader.cpp
2.0.2: Updated file info headers.
[casparcg] / modules / image / util / image_loader.cpp
1 /*\r
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
3 *\r
4 * This file is part of CasparCG (www.casparcg.com).\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 * Author: Robert Nagy, ronag89@gmail.com\r
20 */\r
21 \r
22 #include "image_loader.h"\r
23 \r
24 #include <common/exception/Exceptions.h>\r
25 #include <common/utility/string.h>\r
26 \r
27 #if defined(_MSC_VER)\r
28 #pragma warning (disable : 4714) // marked as __forceinline not inlined\r
29 #endif\r
30 \r
31 #include <boost/exception/errinfo_file_name.hpp>\r
32 #include <boost/filesystem.hpp>\r
33 \r
34 namespace caspar { namespace image {\r
35 \r
36 std::shared_ptr<FIBITMAP> load_image(const std::string& filename)\r
37 {\r
38         if(!boost::filesystem::exists(filename))\r
39                 BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(filename));\r
40 \r
41         FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;\r
42         fif = FreeImage_GetFileType(filename.c_str(), 0);\r
43         if(fif == FIF_UNKNOWN) \r
44                 fif = FreeImage_GetFIFFromFilename(filename.c_str());\r
45                 \r
46         if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) \r
47                 BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));\r
48                 \r
49         auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Load(fif, filename.c_str(), 0), FreeImage_Unload);\r
50                   \r
51         if(FreeImage_GetBPP(bitmap.get()) != 32)\r
52         {\r
53                 bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);\r
54                 if(!bitmap)\r
55                         BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));                     \r
56         }\r
57         \r
58         return bitmap;\r
59 }\r
60 \r
61 std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)\r
62 {\r
63         return load_image(narrow(filename));\r
64 }\r
65 \r
66 }}