]> git.sesse.net Git - casparcg/blob - modules/image/util/image_loader.cpp
5f730fd10741891b31146ea819878a546690f997
[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/except.h>\r
25 #include <common/utf.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::wstring& filename)\r
37 {\r
38         if(!boost::filesystem::exists(filename))\r
39                 CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));\r
40 \r
41         FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeU(filename.c_str(), 0);            \r
42         if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) \r
43                 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));\r
44                 \r
45         auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadU(fif, filename.c_str(), 0), FreeImage_Unload);\r
46                   \r
47         if(FreeImage_GetBPP(bitmap.get()) != 32)\r
48         {\r
49                 bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);\r
50                 if(!bitmap)\r
51                         CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));                    \r
52         }\r
53         \r
54         return bitmap;\r
55 }\r
56 }}