]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
set svn:eol-style native on .h and .cpp files
[casparcg] / modules / image / consumer / image_consumer.cpp
1 /*
2 * Copyright (c) 2011 Sveriges Television AB <info@casparcg.com>
3 *
4 * This file is part of CasparCG (www.casparcg.com).
5 *
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.
10 *
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.
15 *
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/>.
18 *
19 * Author: Robert Nagy, ronag89@gmail.com
20 */
21
22 #include "image_consumer.h"
23
24 #include <common/except.h>
25 #include <common/env.h>
26 #include <common/log.h>
27 #include <common/utf.h>
28 #include <common/array.h>
29
30 #include <core/consumer/frame_consumer.h>
31 #include <core/video_format.h>
32 #include <core/frame/frame.h>
33
34 #include <boost/date_time/posix_time/posix_time.hpp>
35 #include <boost/thread.hpp>
36
37 #include <tbb/concurrent_queue.h>
38
39 #include <asmlib.h>
40
41 #include <FreeImage.h>
42
43 #include <vector>
44
45 namespace caspar { namespace image {
46         
47 struct image_consumer : public core::frame_consumer
48 {
49 public:
50
51         // frame_consumer
52
53         void initialize(const core::video_format_desc&, int) override
54         {
55         }
56         
57         bool send(core::const_frame frame) override
58         {                               
59                 boost::thread async([frame]
60                 {
61                         try
62                         {
63                                 auto filename = u8(env::data_folder()) +  boost::posix_time::to_iso_string(boost::posix_time::second_clock::local_time()) + ".png";
64
65                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(static_cast<int>(frame.width()), static_cast<int>(frame.height()), 32), FreeImage_Unload);
66                                 A_memcpy(FreeImage_GetBits(bitmap.get()), frame.image_data().begin(), frame.image_data().size());
67                                 FreeImage_FlipVertical(bitmap.get());
68                                 FreeImage_Save(FIF_PNG, bitmap.get(), filename.c_str(), 0);
69                         }
70                         catch(...)
71                         {
72                                 CASPAR_LOG_CURRENT_EXCEPTION();
73                         }
74                 });
75                 async.detach();
76
77                 return false;
78         }
79
80         std::wstring print() const override
81         {
82                 return L"image[]";
83         }
84         
85         std::wstring name() const override
86         {
87                 return L"image";
88         }
89
90         boost::property_tree::wptree info() const override
91         {
92                 boost::property_tree::wptree info;
93                 info.add(L"type", L"image");
94                 return info;
95         }
96
97         int buffer_depth() const override
98         {
99                 return 0;
100         }
101
102         int index() const override
103         {
104                 return 100;
105         }
106
107         void subscribe(const monitor::observable::observer_ptr& o) override
108         {
109         }
110
111         void unsubscribe(const monitor::observable::observer_ptr& o) override
112         {
113         }       
114 };
115
116 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
117 {
118         if(params.size() < 1 || params[0] != L"IMAGE")
119                 return core::frame_consumer::empty();
120
121         return spl::make_shared<image_consumer>();
122 }
123
124 }}