]> git.sesse.net Git - casparcg/blob - modules/image/producer/image_producer.cpp
Draft of interaction with producers and scene_producer
[casparcg] / modules / image / producer / image_producer.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_producer.h"
23
24 #include "../util/image_loader.h"
25
26 #include <core/video_format.h>
27
28 #include <core/producer/frame_producer.h>
29 #include <core/frame/frame.h>
30 #include <core/frame/draw_frame.h>
31 #include <core/frame/frame_factory.h>
32 #include <core/frame/pixel_format.h>
33 #include <core/monitor/monitor.h>
34
35 #include <common/env.h>
36 #include <common/log.h>
37 #include <common/array.h>
38
39 #include <boost/assign.hpp>
40 #include <boost/filesystem.hpp>
41 #include <boost/property_tree/ptree.hpp>
42
43 #include <algorithm>
44
45 using namespace boost::assign;
46
47 namespace caspar { namespace image {
48
49 struct image_producer : public core::frame_producer_base
50 {       
51         monitor::basic_subject  event_subject_;
52         const std::wstring              filename_;
53         core::draw_frame                frame_;
54         core::constraints               constraints_;
55         
56         explicit image_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const std::wstring& filename) 
57                 : filename_(filename)
58                 , frame_(core::draw_frame::empty())     
59         {
60                 auto bitmap = load_image(filename_);
61                 FreeImage_FlipVertical(bitmap.get());
62                 
63                 core::pixel_format_desc desc = core::pixel_format::bgra;
64                 auto width = FreeImage_GetWidth(bitmap.get());
65                 auto height = FreeImage_GetHeight(bitmap.get());
66                 desc.planes.push_back(core::pixel_format_desc::plane(width, height, 4));
67                 auto frame = frame_factory->create_frame(this, desc);
68
69                 std::copy_n(FreeImage_GetBits(bitmap.get()), frame.image_data(0).size(), frame.image_data(0).begin());
70                 frame_ = core::draw_frame(std::move(frame));
71                 constraints_.width.set(width);
72                 constraints_.height.set(height);
73
74                 CASPAR_LOG(info) << print() << L" Initialized";
75         }
76         
77         // frame_producer
78
79         core::draw_frame receive_impl() override
80         {
81                 event_subject_ << monitor::event("file/path") % filename_;
82
83                 return frame_;
84         }
85
86         core::constraints& pixel_constraints() override
87         {
88                 return constraints_;
89         }
90                         
91         std::wstring print() const override
92         {
93                 return L"image_producer[" + filename_ + L"]";
94         }
95
96         std::wstring name() const override
97         {
98                 return L"image";
99         }
100
101         boost::property_tree::wptree info() const override
102         {
103                 boost::property_tree::wptree info;
104                 info.add(L"type", L"image");
105                 info.add(L"filename", filename_);
106                 return info;
107         }
108
109         void subscribe(const monitor::observable::observer_ptr& o) override                                                                                                                     
110         {
111                 return event_subject_.subscribe(o);
112         }
113
114         void unsubscribe(const monitor::observable::observer_ptr& o) override           
115         {
116                 return event_subject_.unsubscribe(o);
117         }
118 };
119
120 spl::shared_ptr<core::frame_producer> create_producer(const spl::shared_ptr<core::frame_factory>& frame_factory, const core::video_format_desc& format_desc, const std::vector<std::wstring>& params)
121 {
122         static const std::vector<std::wstring> extensions = list_of(L".png")(L".tga")(L".bmp")(L".jpg")(L".jpeg")(L".gif")(L".tiff")(L".tif")(L".jp2")(L".jpx")(L".j2k")(L".j2c");
123         std::wstring filename = env::media_folder() + L"\\" + params[0];
124         
125         auto ext = std::find_if(extensions.begin(), extensions.end(), [&](const std::wstring& ex) -> bool
126                 {                       
127                         return boost::filesystem::is_regular_file(boost::filesystem::path(filename).replace_extension(ex));
128                 });
129
130         if(ext == extensions.end())
131                 return core::frame_producer::empty();
132
133         return spl::make_shared<image_producer>(frame_factory, filename + *ext);
134 }
135
136
137 }}