]> git.sesse.net Git - casparcg/blob - modules/image/consumer/image_consumer.cpp
[general] #598 Removed all usages of asmlib, because it is worse performing than...
[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 #include <common/os/general_protection_fault.h>
31
32 #include <core/consumer/frame_consumer.h>
33 #include <core/video_format.h>
34 #include <core/frame/frame.h>
35 #include <core/help/help_sink.h>
36 #include <core/help/help_repository.h>
37
38 #include <boost/date_time/posix_time/posix_time.hpp>
39 #include <boost/thread.hpp>
40 #include <boost/algorithm/string.hpp>
41
42 #include <tbb/concurrent_queue.h>
43
44 #include <FreeImage.h>
45
46 #include <vector>
47 #include <algorithm>
48
49 #include "../util/image_view.h"
50
51 namespace caspar { namespace image {
52
53 void write_cropped_png(
54                 const core::const_frame& frame,
55                 const core::video_format_desc& format_desc,
56                 const boost::filesystem::path& output_file,
57                 int width,
58                 int height)
59 {
60         auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(width, height, 32), FreeImage_Unload);
61         image_view<bgra_pixel> destination_view(FreeImage_GetBits(bitmap.get()), width, height);
62         image_view<bgra_pixel> complete_frame(const_cast<uint8_t*>(frame.image_data().begin()), format_desc.width, format_desc.height);
63         auto thumbnail_view = complete_frame.subview(0, 0, width, height);
64
65         std::copy(thumbnail_view.begin(), thumbnail_view.end(), destination_view.begin());
66         FreeImage_FlipVertical(bitmap.get());
67 #ifdef WIN32
68         FreeImage_SaveU(FIF_PNG, bitmap.get(), output_file.wstring().c_str(), 0);
69 #else
70         FreeImage_Save(FIF_PNG, bitmap.get(), u8(output_file.wstring()).c_str(), 0);
71 #endif
72 }
73
74 struct image_consumer : public core::frame_consumer
75 {
76         core::monitor::subject  monitor_subject_;
77         std::wstring                    filename_;
78 public:
79
80         // frame_consumer
81
82         image_consumer(const std::wstring& filename)
83                 : filename_(filename)
84         {
85         }
86
87         void initialize(const core::video_format_desc&, const core::audio_channel_layout&, int) override
88         {
89         }
90
91         int64_t presentation_frame_age_millis() const override
92         {
93                 return 0;
94         }
95
96         std::future<bool> send(core::const_frame frame) override
97         {
98                 auto filename = filename_;
99
100                 boost::thread async([frame, filename]
101                 {
102                         ensure_gpf_handler_installed_for_thread("image-consumer");
103
104                         try
105                         {
106                                 auto filename2 = filename;
107
108                                 if (filename2.empty())
109                                         filename2 = env::media_folder() +  boost::posix_time::to_iso_wstring(boost::posix_time::second_clock::local_time()) + L".png";
110                                 else
111                                         filename2 = env::media_folder() + filename2 + L".png";
112
113                                 auto bitmap = std::shared_ptr<FIBITMAP>(FreeImage_Allocate(static_cast<int>(frame.width()), static_cast<int>(frame.height()), 32), FreeImage_Unload);
114                                 std::memcpy(FreeImage_GetBits(bitmap.get()), frame.image_data().begin(), frame.image_data().size());
115                                 FreeImage_FlipVertical(bitmap.get());
116 #ifdef WIN32
117                                 FreeImage_SaveU(FIF_PNG, bitmap.get(), filename2.c_str(), 0);
118 #else
119                                 FreeImage_Save(FIF_PNG, bitmap.get(), u8(filename2).c_str(), 0);
120 #endif
121                         }
122                         catch(...)
123                         {
124                                 CASPAR_LOG_CURRENT_EXCEPTION();
125                         }
126                 });
127                 async.detach();
128
129                 return make_ready_future(false);
130         }
131
132         std::wstring print() const override
133         {
134                 return L"image[]";
135         }
136
137         std::wstring name() const override
138         {
139                 return L"image";
140         }
141
142         boost::property_tree::wptree info() const override
143         {
144                 boost::property_tree::wptree info;
145                 info.add(L"type", L"image");
146                 return info;
147         }
148
149         int buffer_depth() const override
150         {
151                 return -1;
152         }
153
154         int index() const override
155         {
156                 return 100;
157         }
158
159         core::monitor::subject& monitor_output()
160         {
161                 return monitor_subject_;
162         }
163 };
164
165 void describe_consumer(core::help_sink& sink, const core::help_repository& repo)
166 {
167         sink.short_description(L"Writes a single PNG snapshot of a video channel.");
168         sink.syntax(L"IMAGE {[filename:string]|yyyyMMddTHHmmss}");
169         sink.para()
170                 ->text(L"Writes a single PNG snapshot of a video channel. ")->code(L".png")->text(L" will be appended to ")
171                 ->code(L"filename")->text(L". The PNG image will be stored under the ")->code(L"media")->text(L" folder.");
172         sink.para()->text(L"Examples:");
173         sink.example(L">> ADD 1 IMAGE screenshot", L"creating media/screenshot.png");
174         sink.example(L">> ADD 1 IMAGE", L"creating media/20130228T210946.png if the current time is 2013-02-28 21:09:46.");
175 }
176
177 spl::shared_ptr<core::frame_consumer> create_consumer(
178                 const std::vector<std::wstring>& params, core::interaction_sink*, std::vector<spl::shared_ptr<core::video_channel>> channels)
179 {
180         if (params.size() < 1 || !boost::iequals(params.at(0), L"IMAGE"))
181                 return core::frame_consumer::empty();
182
183         std::wstring filename;
184
185         if (params.size() > 1)
186                 filename = params.at(1);
187
188         return spl::make_shared<image_consumer>(filename);
189 }
190
191 }}