]> git.sesse.net Git - casparcg/blobdiff - modules/image/consumer/image_consumer.cpp
fixed problem with wrong alpha in thumbnails.
[casparcg] / modules / image / consumer / image_consumer.cpp
index b4b7db10cf38ed3e2ec5da4f0d903ee5a584179d..21fdafb0db7601a0a793d4a81d70f06e6dc50fc8 100644 (file)
@@ -1,30 +1,33 @@
 /*\r
-* copyright (c) 2010 Sveriges Television AB <info@casparcg.com>\r
+* Copyright 2013 Sveriges Television AB http://casparcg.com/\r
 *\r
-*  This file is part of CasparCG.\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
+* 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
+* 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
+\r
 #include "image_consumer.h"\r
 \r
 #include <common/exception/exceptions.h>\r
 #include <common/env.h>\r
 #include <common/log/log.h>\r
 #include <common/utility/string.h>\r
+#include <common/concurrency/future_util.h>\r
 \r
+#include <core/parameters/parameters.h>\r
 #include <core/consumer/frame_consumer.h>\r
 #include <core/video_format.h>\r
 #include <core/mixer/read_frame.h>\r
 #include <boost/date_time/posix_time/posix_time.hpp>\r
 #include <boost/thread.hpp>\r
 \r
-#include <tbb/concurrent_queue.h>\r
-\r
 #include <FreeImage.h>\r
-\r
 #include <vector>\r
+#include <algorithm>\r
+\r
+#include "../util/image_view.h"\r
 \r
 namespace caspar { namespace image {\r
-       \r
+\r
+void write_cropped_png(\r
+               const safe_ptr<core::read_frame>& frame,\r
+               const core::video_format_desc& format_desc,\r
+               const boost::filesystem::wpath& output_file,\r
+               int width,\r
+               int height)\r
+{\r
+       auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(width, height, 32), FreeImage_Unload);\r
+       image_view<bgra_pixel> destination_view(FreeImage_GetBits(bitmap.get()), width, height);\r
+       image_view<bgra_pixel> complete_frame(const_cast<uint8_t*>(frame->image_data().begin()), format_desc.width, format_desc.height);\r
+       auto thumbnail_view = complete_frame.subview(0, 0, width, height);\r
+\r
+       std::copy(thumbnail_view.begin(), thumbnail_view.end(), destination_view.begin());\r
+       FreeImage_FlipVertical(bitmap.get());\r
+       FreeImage_SaveU(FIF_PNG, bitmap.get(), output_file.string().c_str(), 0);\r
+}\r
+\r
 struct image_consumer : public core::frame_consumer\r
 {\r
-       core::video_format_desc                                 format_desc_;\r
+       core::video_format_desc format_desc_;\r
+       std::wstring                    filename_;\r
 public:\r
 \r
        // frame_consumer\r
 \r
+       image_consumer(const std::wstring& filename)\r
+               : filename_(filename)\r
+       {\r
+       }\r
+\r
        virtual void initialize(const core::video_format_desc& format_desc, int) override\r
        {\r
                format_desc_ = format_desc;\r
        }\r
+\r
+       virtual int64_t presentation_frame_age_millis() const override\r
+       {\r
+               return 0;\r
+       }\r
        \r
-       virtual bool send(const safe_ptr<core::read_frame>& frame) override\r
+       virtual boost::unique_future<bool> send(const safe_ptr<core::read_frame>& frame) override\r
        {                               \r
                auto format_desc = format_desc_;\r
-               boost::thread async([format_desc, frame]\r
+               auto filename = filename_;\r
+\r
+               boost::thread async([format_desc, frame, filename]\r
                {\r
                        try\r
                        {\r
-                               auto filename = narrow(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";\r
+                               auto filename2 = filename;\r
+\r
+                               if (filename2.empty())\r
+                                       filename2 = env::media_folder() + widen(boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time())) + L".png";\r
+                               else\r
+                                       filename2 = env::media_folder() + filename2 + L".png";\r
 \r
                                auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(format_desc.width, format_desc.height, 32), FreeImage_Unload);\r
                                memcpy(FreeImage_GetBits(bitmap.get()), frame->image_data().begin(), frame->image_size());\r
                                FreeImage_FlipVertical(bitmap.get());\r
-                               FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);\r
+                               FreeImage_SaveU(FIF_PNG, bitmap.get(), filename2.c_str(), 0);\r
                        }\r
                        catch(...)\r
                        {\r
@@ -73,7 +111,7 @@ public:
                });\r
                async.detach();\r
 \r
-               return false;\r
+               return wrap_as_future(false);\r
        }\r
 \r
        virtual std::wstring print() const override\r
@@ -81,6 +119,13 @@ public:
                return L"image[]";\r
        }\r
 \r
+       virtual boost::property_tree::wptree info() const override\r
+       {\r
+               boost::property_tree::wptree info;\r
+               info.add(L"type", L"image-consumer");\r
+               return info;\r
+       }\r
+\r
        virtual size_t buffer_depth() const override\r
        {\r
                return 0;\r
@@ -92,12 +137,17 @@ public:
        }\r
 };\r
 \r
-safe_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)\r
+safe_ptr<core::frame_consumer> create_consumer(const core::parameters& params)\r
 {\r
-       if(params.size() < 1 || params[0] != L"IMAGE")\r
+       if(params.size() < 1 || params.at(0) != L"IMAGE")\r
                return core::frame_consumer::empty();\r
 \r
-       return make_safe<image_consumer>();\r
+       std::wstring filename;\r
+\r
+       if (params.size() > 1)\r
+               filename = params.at(1);\r
+\r
+       return make_safe<image_consumer>(filename);\r
 }\r
 \r
 }}\r