]> git.sesse.net Git - casparcg/blobdiff - modules/image/util/image_loader.cpp
Merge pull request #462 from pkeuter/patch-1
[casparcg] / modules / image / util / image_loader.cpp
index 61fb8843d5675199eec61153da8433330043506e..1768d2891761ae1043e32258adf24fb1517941e8 100644 (file)
-/*\r
-* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>\r
-*\r
-* This file is part of CasparCG (www.casparcg.com).\r
-*\r
-* CasparCG is free software: you can redistribute it and/or modify\r
-* it under the terms of the GNU General Public License as published by\r
-* the Free Software Foundation, either version 3 of the License, or\r
-* (at your option) any later version.\r
-*\r
-* CasparCG is distributed in the hope that it will be useful,\r
-* but WITHOUT ANY WARRANTY; without even the implied warranty of\r
-* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the\r
-* GNU General Public License for more details.\r
-*\r
-* You should have received a copy of the GNU General Public License\r
-* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.\r
-*\r
-* Author: Robert Nagy, ronag89@gmail.com\r
-*/\r
-\r
-#include "image_loader.h"\r
-\r
-#include <common/exception/Exceptions.h>\r
-#include <common/utility/string.h>\r
-\r
-#if defined(_MSC_VER)\r
-#pragma warning (disable : 4714) // marked as __forceinline not inlined\r
-#endif\r
-\r
-#include <boost/exception/errinfo_file_name.hpp>\r
-#include <boost/filesystem.hpp>\r
-\r
-namespace caspar { namespace image {\r
-\r
-std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)\r
-{\r
-       if(!boost::filesystem::exists(filename))\r
-               BOOST_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));\r
-\r
-       FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeU(filename.c_str(), 0);            \r
-       if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) \r
-               BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));\r
-               \r
-       auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadU(fif, filename.c_str(), 0), FreeImage_Unload);\r
-                 \r
-       if(FreeImage_GetBPP(bitmap.get()) != 32)\r
-       {\r
-               bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);\r
-               if(!bitmap)\r
-                       BOOST_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));                     \r
-       }\r
-       \r
-       return bitmap;\r
-}\r
-}}
\ No newline at end of file
+/*
+* Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
+*
+* This file is part of CasparCG (www.casparcg.com).
+*
+* CasparCG is free software: you can redistribute it and/or modify
+* it under the terms of the GNU General Public License as published by
+* the Free Software Foundation, either version 3 of the License, or
+* (at your option) any later version.
+*
+* CasparCG is distributed in the hope that it will be useful,
+* but WITHOUT ANY WARRANTY; without even the implied warranty of
+* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+* GNU General Public License for more details.
+*
+* You should have received a copy of the GNU General Public License
+* along with CasparCG. If not, see <http://www.gnu.org/licenses/>.
+*
+* Author: Robert Nagy, ronag89@gmail.com
+*/
+
+#include "image_loader.h"
+
+#include <common/except.h>
+#include <common/utf.h>
+
+#if defined(_MSC_VER)
+#pragma warning (disable : 4714) // marked as __forceinline not inlined
+#endif
+
+#include <boost/exception/errinfo_file_name.hpp>
+#include <boost/filesystem.hpp>
+
+#include "image_algorithms.h"
+#include "image_view.h"
+
+namespace caspar { namespace image {
+
+std::shared_ptr<FIBITMAP> load_image(const std::wstring& filename)
+{
+       if(!boost::filesystem::exists(filename))
+               CASPAR_THROW_EXCEPTION(file_not_found() << boost::errinfo_file_name(u8(filename)));
+
+#ifdef WIN32
+       FREE_IMAGE_FORMAT fif = FreeImage_GetFileTypeU(filename.c_str(), 0);
+#else
+       FREE_IMAGE_FORMAT fif = FreeImage_GetFileType(u8(filename).c_str(), 0);
+#endif
+
+       if (fif == FIF_UNKNOWN)
+#ifdef WIN32
+               fif = FreeImage_GetFIFFromFilenameU(filename.c_str());
+#else
+               fif = FreeImage_GetFIFFromFilename(u8(filename).c_str());
+#endif
+
+       if(fif == FIF_UNKNOWN || !FreeImage_FIFSupportsReading(fif)) 
+               CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
+               
+#ifdef WIN32
+       auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadU(fif, filename.c_str(), 0), FreeImage_Unload);
+#else
+       auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Load(fif, u8(filename).c_str(), 0), FreeImage_Unload);
+#endif
+                 
+       if(FreeImage_GetBPP(bitmap.get()) != 32)
+       {
+               bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);
+               if(!bitmap)
+                       CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));                    
+       }
+
+       //PNG-images need to be premultiplied with their alpha
+       if(fif == FIF_PNG)
+       {
+               image_view<bgra_pixel> original_view(FreeImage_GetBits(bitmap.get()), FreeImage_GetWidth(bitmap.get()), FreeImage_GetHeight(bitmap.get()));
+               premultiply(original_view);
+       }
+       
+       return bitmap;
+}
+
+std::shared_ptr<FIBITMAP> load_png_from_memory(const void* memory_location, size_t size)
+{
+       FREE_IMAGE_FORMAT fif = FIF_PNG;
+
+       auto memory = std::unique_ptr<FIMEMORY, decltype(&FreeImage_CloseMemory)>(
+                       FreeImage_OpenMemory(static_cast<BYTE*>(const_cast<void*>(memory_location)), static_cast<DWORD>(size)),
+                       FreeImage_CloseMemory);
+       auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_LoadFromMemory(fif, memory.get(), 0), FreeImage_Unload);
+
+       if (FreeImage_GetBPP(bitmap.get()) != 32)
+       {
+               bitmap = std::shared_ptr<FIBITMAP>(FreeImage_ConvertTo32Bits(bitmap.get()), FreeImage_Unload);
+
+               if (!bitmap)
+                       CASPAR_THROW_EXCEPTION(invalid_argument() << msg_info("Unsupported image format."));
+       }
+
+       return bitmap;
+}
+
+}}