2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
4 * This file is part of CasparCG (www.casparcg.com).
6 * CasparCG is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
11 * CasparCG is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
19 * Author: Robert Nagy, ronag89@gmail.com
22 #include "image_loader.h"
24 #include <common/except.h>
25 #include <common/utf.h>
28 #pragma warning (disable : 4714) // marked as __forceinline not inlined
31 #include <boost/exception/errinfo_file_name.hpp>
32 #include <boost/filesystem.hpp>
34 #include "image_algorithms.h"
35 #include "image_view.h"
37 namespace caspar { namespace image {
39 std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)
41 if(!boost::filesystem::exists(filename))
42 CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));
45 FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeU(filename.c_str(), 0);
47 FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(u8(filename).c_str(), 0);
50 if (fif == FIF_UNKNOWN)
52 fif = FreeImage_GetFIFFromFilenameU(filename.c_str());
54 fif = FreeImage_GetFIFFromFilename(u8(filename).c_str());
57 if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif))
58 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
61 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadU(fif, filename.c_str(), 0), FreeImage_Unload);
63 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Load(fif, u8(filename).c_str(), 0), FreeImage_Unload);
66 if(FreeImage_GetBPP(bitmap.get()) != 32)
68 bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);
70 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
73 //PNG-images need to be premultiplied with their alpha
76 image_view<bgra_pixel> original_view(FreeImage_GetBits(bitmap.get()), FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()));
77 premultiply(original_view);
83 std::shared_ptr<FIBITMAP> load_png_from_memory(const void* memory_location, size_t size)
85 FREE_IMAGE_FORMAT fif = FIF_PNG;
87 auto memory = std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)>(
88 FreeImage_OpenMemory(static_cast<BYTE*>(const_cast<void*>(memory_location)), static_cast<DWORD>(size)),
89 FreeImage_CloseMemory);
90 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadFromMemory(fif, memory.get(), 0), FreeImage_Unload);
92 if (FreeImage_GetBPP(bitmap.get()) != 32)
94 bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);
97 CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
103 const std::set<std::wstring>& supported_extensions()
105 static const std::set<std::wstring> extensions =