7dd633f617c8a7166c4e2baa6d4119e62fe1cc20
[dcpomatic.git] / src / lib / image.h
1 /*
2     Copyright (C) 2012-2016 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 /** @file src/image.h
22  *  @brief A set of classes to describe video images.
23  */
24
25 #ifndef DCPOMATIC_IMAGE_H
26 #define DCPOMATIC_IMAGE_H
27
28 #include "position.h"
29 #include "position_image.h"
30 #include "types.h"
31 extern "C" {
32 #include <libavutil/pixfmt.h>
33 }
34 #include <dcp/array_data.h>
35 #include <dcp/colour_conversion.h>
36 #include <boost/shared_ptr.hpp>
37 #include <boost/enable_shared_from_this.hpp>
38
39 struct AVFrame;
40 class Socket;
41
42 class Image : public boost::enable_shared_from_this<Image>
43 {
44 public:
45         Image (AVPixelFormat p, dcp::Size s, bool aligned);
46         explicit Image (AVFrame *);
47         explicit Image (Image const &);
48         Image (boost::shared_ptr<const Image>, bool);
49         Image& operator= (Image const &);
50         ~Image ();
51
52         uint8_t * const * data () const;
53         /** @return array of sizes of the data in each line, in bytes (not including any alignment padding) */
54         int const * line_size () const;
55         /** @return array of sizes of the data in each line, in bytes (including any alignment padding) */
56         int const * stride () const;
57         dcp::Size size () const;
58         bool aligned () const;
59
60         int planes () const;
61         int vertical_factor (int) const;
62         int horizontal_factor (int) const;
63         dcp::Size sample_size (int) const;
64         float bytes_per_pixel (int) const;
65
66         boost::shared_ptr<Image> convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool aligned, bool fast) const;
67         boost::shared_ptr<Image> scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, bool aligned, bool fast) const;
68         boost::shared_ptr<Image> crop_scale_window (
69                 Crop crop,
70                 dcp::Size inter_size,
71                 dcp::Size out_size,
72                 dcp::YUVToRGB yuv_to_rgb,
73                 VideoRange video_range,
74                 AVPixelFormat out_format,
75                 VideoRange out_video_range,
76                 bool aligned,
77                 bool fast
78                 ) const;
79
80         void make_black ();
81         void make_transparent ();
82         void alpha_blend (boost::shared_ptr<const Image> image, Position<int> pos);
83         void copy (boost::shared_ptr<const Image> image, Position<int> pos);
84         void fade (float);
85         void video_range_to_full_range ();
86
87         void read_from_socket (boost::shared_ptr<Socket>);
88         void write_to_socket (boost::shared_ptr<Socket>) const;
89
90         AVPixelFormat pixel_format () const {
91                 return _pixel_format;
92         }
93
94         size_t memory_used () const;
95
96         dcp::ArrayData as_png () const;
97
98         void png_error (char const * message);
99
100         static boost::shared_ptr<const Image> ensure_aligned (boost::shared_ptr<const Image> image);
101
102 private:
103         friend struct pixel_formats_test;
104
105         void allocate ();
106         void swap (Image &);
107         void make_part_black (int x, int w);
108         void yuv_16_black (uint16_t, bool);
109         static uint16_t swap_16 (uint16_t);
110
111         dcp::Size _size;
112         AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
113         uint8_t** _data; ///< array of pointers to components
114         int* _line_size; ///< array of sizes of the data in each line, in bytes (without any alignment padding bytes)
115         int* _stride; ///< array of strides for each line, in bytes (including any alignment padding bytes)
116         bool _aligned;
117 };
118
119 extern PositionImage merge (std::list<PositionImage> images);
120 extern bool operator== (Image const & a, Image const & b);
121
122 #endif