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