]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
Merged most recent OSC changes
[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 #include <common/future.h>
30
31 #include <core/consumer/frame_consumer.h>
32 #include <core/video_format.h>
33 #include <core/frame/frame.h>
34
35 #include <boost/date_time/posix_time/posix_time.hpp>
36 #include <boost/thread.hpp>
37
38 #include <tbb/concurrent_queue.h>
39
40 #include <asmlib.h>
41
42 #include <FreeImage.h>
43
44 #include <vector>
45 #include <algorithm>
46
47 #include "../util/image_view.h"
48
49 namespace caspar { namespace image {
50
51         void write_cropped_png(
52                 const core::const_frame& frame,
53                 const core::video_format_desc& format_desc,
54                 const boost::filesystem::wpath& output_file,
55                 int width,
56                 int height)
57         {
58                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(width, height, 32), FreeImage_Unload);
59                 image_view<bgra_pixel> destination_view(FreeImage_GetBits(bitmap.get()), width, height);
60                 image_view<bgra_pixel> complete_frame(const_cast<uint8_t*>(frame.image_data().begin()), format_desc.width, format_desc.height);
61                 auto thumbnail_view = complete_frame.subview(0, 0, width, height);
62
63                 std::copy(thumbnail_view.begin(), thumbnail_view.end(), destination_view.begin());
64                 FreeImage_FlipVertical(bitmap.get());
65                 FreeImage_SaveU(FIF_PNG, bitmap.get(), output_file.wstring().c_str(), 0);
66         }
67
68 struct image_consumer : public core::frame_consumer
69 {
70         core::monitor::subject  monitor_subject_;
71         std::wstring                    filename_;
72 public:
73
74         // frame_consumer
75
76         image_consumer(const std::wstring& filename)
77                 : filename_(filename)
78         {
79         }
80
81         void initialize(const core::video_format_desc&, int) override
82         {
83         }
84         
85         boost::unique_future<bool> send(core::const_frame frame) override
86         {
87                 auto filename = filename_;
88
89                 boost::thread async([frame, filename]
90                 {
91                         try
92                         {
93                                 auto filename2 = filename;
94                                 
95                                 if (filename2.empty())
96                                         filename2 = env::media_folder() +  boost::posix_time::to_iso_wstring(boost::posix_time::second_clock::local_time()) + L".png";
97                                 else
98                                         filename2 = env::media_folder() + filename2 + L".png";
99
100                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(static_cast<int>(frame.width()), static_cast<int>(frame.height()), 32), FreeImage_Unload);
101                                 A_memcpy(FreeImage_GetBits(bitmap.get()), frame.image_data().begin(), frame.image_data().size());
102                                 FreeImage_FlipVertical(bitmap.get());
103                                 FreeImage_SaveU(FIF_PNG, bitmap.get(), filename2.c_str(), 0);
104                         }
105                         catch(...)
106                         {
107                                 CASPAR_LOG_CURRENT_EXCEPTION();
108                         }
109                 });
110                 async.detach();
111
112                 return wrap_as_future(false);
113         }
114
115         std::wstring print() const override
116         {
117                 return L"image[]";
118         }
119         
120         std::wstring name() const override
121         {
122                 return L"image";
123         }
124
125         boost::property_tree::wptree info() const override
126         {
127                 boost::property_tree::wptree info;
128                 info.add(L"type", L"image");
129                 return info;
130         }
131
132         int buffer_depth() const override
133         {
134                 return 0;
135         }
136
137         int index() const override
138         {
139                 return 100;
140         }
141
142         core::monitor::subject& monitor_output()
143         {
144                 return monitor_subject_;
145         }
146 };
147
148 spl::shared_ptr<core::frame_consumer> create_consumer(const std::vector<std::wstring>& params)
149 {
150         if (params.size() < 1 || params[0] != L"IMAGE")
151                 return core::frame_consumer::empty();
152
153         std::wstring filename;
154
155         if (params.size() > 1)
156                 filename = params[1];
157
158         return spl::make_shared<image_consumer>(filename);
159 }
160
161 }}