Supporters update.
[dcpomatic.git] / src / lib / image_proxy.h
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 #ifndef DCPOMATIC_IMAGE_PROXY_H
23 #define DCPOMATIC_IMAGE_PROXY_H
24
25
26 /** @file  src/lib/image_proxy.h
27  *  @brief ImageProxy and subclasses.
28  */
29
30
31 #include "image.h"
32 extern "C" {
33 #include <libavutil/pixfmt.h>
34 }
35 #include <dcp/types.h>
36 #include <boost/optional.hpp>
37 #include <boost/utility.hpp>
38
39
40 class Image;
41 class Socket;
42
43 namespace xmlpp {
44         class Node;
45 }
46
47 namespace cxml {
48         class Node;
49 }
50
51
52 /** @class ImageProxy
53  *  @brief A class which holds an Image, and can produce it on request.
54  *
55  *  This is so that decoding of source images can be postponed until
56  *  the encoder thread, where multi-threading is happening, instead
57  *  of happening in a single-threaded decoder.
58  *
59  *  For example, large TIFFs are slow to decode, so this class will keep
60  *  the TIFF data compressed until the decompressed image is needed.
61  *  At this point, the class decodes the TIFF to an Image.
62  */
63 class ImageProxy
64 {
65 public:
66         ImageProxy () {}
67         virtual ~ImageProxy () {}
68
69         ImageProxy (ImageProxy const&) = delete;
70         ImageProxy& operator= (ImageProxy const&) = delete;
71
72         struct Result {
73                 Result (std::shared_ptr<const Image> image_, int log2_scaling_)
74                         : image (image_)
75                         , log2_scaling (log2_scaling_)
76                         , error (false)
77                 {}
78
79                 Result (std::shared_ptr<Image> image_, int log2_scaling_, bool error_)
80                         : image (image_)
81                         , log2_scaling (log2_scaling_)
82                         , error (error_)
83                 {}
84
85                 std::shared_ptr<const Image> image;
86                 /** log2 of any scaling down that has already been applied to the image;
87                  *  e.g. if the image is already half the size of the original, this value
88                  *  will be 1.
89                  */
90                 int log2_scaling;
91                 /** true if there was an error during image decoding, otherwise false */
92                 bool error;
93         };
94
95         /** @param log Log to write to, or 0.
96          *  @param size Size that the returned image will be scaled to, in case this
97          *  can be used as an optimisation.
98          */
99         virtual Result image (
100                 Image::Alignment alignment,
101                 boost::optional<dcp::Size> size = boost::optional<dcp::Size> ()
102                 ) const = 0;
103
104         virtual void add_metadata (xmlpp::Node *) const = 0;
105         virtual void write_to_socket (std::shared_ptr<Socket>) const = 0;
106         /** @return true if our image is definitely the same as another, false if it is probably not */
107         virtual bool same (std::shared_ptr<const ImageProxy>) const = 0;
108         /** Do any useful work that would speed up a subsequent call to ::image().
109          *  This method may be called in a different thread to image().
110          *  @return log2 of any scaling down that will be applied to the image.
111          */
112         virtual int prepare (Image::Alignment, boost::optional<dcp::Size> = boost::optional<dcp::Size>()) const { return 0; }
113         virtual size_t memory_used () const = 0;
114 };
115
116
117 std::shared_ptr<ImageProxy> image_proxy_factory (std::shared_ptr<cxml::Node> xml, std::shared_ptr<Socket> socket);
118
119
120 #endif