Fix the build for older macOS.
[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
37 struct AVFrame;
38 class Socket;
39
40 class Image : public std::enable_shared_from_this<Image>
41 {
42 public:
43         enum class Alignment {
44                 COMPACT,
45                 PADDED
46         };
47
48         Image (AVPixelFormat p, dcp::Size s, Alignment alignment);
49         explicit Image (AVFrame const *, Alignment alignment);
50         explicit Image (Image const &);
51         Image (std::shared_ptr<const Image>, Alignment alignment);
52         Image& operator= (Image const &);
53         ~Image ();
54
55         uint8_t * const * data () const;
56         /** @return array of sizes of the data in each line, in bytes (not including any alignment padding) */
57         int const * line_size () const;
58         /** @return array of sizes of the data in each line, in bytes (including any alignment padding) */
59         int const * stride () const;
60         dcp::Size size () const;
61         Alignment alignment () const;
62
63         int planes () const;
64         int vertical_factor (int) const;
65         int horizontal_factor (int) const;
66         dcp::Size sample_size (int) const;
67         float bytes_per_pixel (int) const;
68
69         std::shared_ptr<Image> convert_pixel_format (dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, Alignment alignment, bool fast) const;
70         std::shared_ptr<Image> scale (dcp::Size out_size, dcp::YUVToRGB yuv_to_rgb, AVPixelFormat out_format, Alignment alignment, bool fast) const;
71         std::shared_ptr<Image> crop_scale_window (
72                 Crop crop,
73                 dcp::Size inter_size,
74                 dcp::Size out_size,
75                 dcp::YUVToRGB yuv_to_rgb,
76                 VideoRange video_range,
77                 AVPixelFormat out_format,
78                 VideoRange out_video_range,
79                 Alignment alignment,
80                 bool fast
81                 ) const;
82
83         void make_black ();
84         void make_transparent ();
85         void alpha_blend (std::shared_ptr<const Image> image, Position<int> pos);
86         void copy (std::shared_ptr<const Image> image, Position<int> pos);
87         void fade (float);
88
89         void read_from_socket (std::shared_ptr<Socket>);
90         void write_to_socket (std::shared_ptr<Socket>) const;
91
92         AVPixelFormat pixel_format () const {
93                 return _pixel_format;
94         }
95
96         size_t memory_used () const;
97
98         dcp::ArrayData as_png () const;
99
100         void png_error (char const * message);
101
102         static std::shared_ptr<const Image> ensure_alignment (std::shared_ptr<const Image> image, Alignment alignment);
103
104 private:
105         friend struct pixel_formats_test;
106         friend struct make_part_black_test;
107
108         void allocate ();
109         void swap (Image &);
110         void make_part_black (int x, int w);
111         void yuv_16_black (uint16_t, bool);
112         static uint16_t swap_16 (uint16_t);
113         void video_range_to_full_range ();
114
115         dcp::Size _size;
116         AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
117         uint8_t** _data; ///< array of pointers to components
118         int* _line_size; ///< array of sizes of the data in each line, in bytes (without any alignment padding bytes)
119         int* _stride; ///< array of strides for each line, in bytes (including any alignment padding bytes)
120         Alignment _alignment;
121 };
122
123 extern PositionImage merge (std::list<PositionImage> images, Image::Alignment alignment);
124 extern bool operator== (Image const & a, Image const & b);
125
126 #endif