Supporters update.
[dcpomatic.git] / src / lib / raw_image_proxy.cc
1 /*
2     Copyright (C) 2014-2021 Carl Hetherington <cth@carlh.net>
3
4     This file is part of DCP-o-matic.
5
6     DCP-o-matic 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 2 of the License, or
9     (at your option) any later version.
10
11     DCP-o-matic 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 DCP-o-matic.  If not, see <http://www.gnu.org/licenses/>.
18
19 */
20
21
22 #include "raw_image_proxy.h"
23 #include "image.h"
24 #include <dcp/raw_convert.h>
25 #include <dcp/util.h>
26 #include <dcp/warnings.h>
27 #include <libcxml/cxml.h>
28 extern "C" {
29 #include <libavutil/pixfmt.h>
30 }
31 LIBDCP_DISABLE_WARNINGS
32 #include <libxml++/libxml++.h>
33 LIBDCP_ENABLE_WARNINGS
34
35 #include "i18n.h"
36
37
38 using std::dynamic_pointer_cast;
39 using std::make_pair;
40 using std::make_shared;
41 using std::pair;
42 using std::shared_ptr;
43 using std::string;
44 using boost::optional;
45 using dcp::raw_convert;
46
47
48 RawImageProxy::RawImageProxy (shared_ptr<Image> image)
49         : _image (image)
50 {
51
52 }
53
54
55 RawImageProxy::RawImageProxy (shared_ptr<cxml::Node> xml, shared_ptr<Socket> socket)
56 {
57         dcp::Size size (
58                 xml->number_child<int>("Width"), xml->number_child<int>("Height")
59                 );
60
61         _image = make_shared<Image>(static_cast<AVPixelFormat>(xml->number_child<int>("PixelFormat")), size, Image::Alignment::PADDED);
62         _image->read_from_socket (socket);
63 }
64
65
66 ImageProxy::Result
67 RawImageProxy::image (Image::Alignment alignment, optional<dcp::Size>) const
68 {
69         /* This ensure_alignment could be wasteful */
70         return Result (Image::ensure_alignment(_image, alignment), 0);
71 }
72
73
74 void
75 RawImageProxy::add_metadata (xmlpp::Node* node) const
76 {
77         node->add_child("Type")->add_child_text(N_("Raw"));
78         node->add_child("Width")->add_child_text(raw_convert<string>(_image->size().width));
79         node->add_child("Height")->add_child_text(raw_convert<string>(_image->size().height));
80         node->add_child("PixelFormat")->add_child_text(raw_convert<string>(static_cast<int>(_image->pixel_format())));
81 }
82
83
84 void
85 RawImageProxy::write_to_socket (shared_ptr<Socket> socket) const
86 {
87         _image->write_to_socket (socket);
88 }
89
90
91 bool
92 RawImageProxy::same (shared_ptr<const ImageProxy> other) const
93 {
94         auto rp = dynamic_pointer_cast<const RawImageProxy> (other);
95         if (!rp) {
96                 return false;
97         }
98
99         return (*_image.get()) == (*rp->image(_image->alignment()).image.get());
100 }
101
102
103 size_t
104 RawImageProxy::memory_used () const
105 {
106         return _image->memory_used ();
107 }