Some const correctness.
[dcpomatic.git] / src / lib / image.h
1 /*
2     Copyright (C) 2012 Carl Hetherington <cth@carlh.net>
3
4     This program is free software; you can redistribute it and/or modify
5     it under the terms of the GNU General Public License as published by
6     the Free Software Foundation; either version 2 of the License, or
7     (at your option) any later version.
8
9     This program is distributed in the hope that it will be useful,
10     but WITHOUT ANY WARRANTY; without even the implied warranty of
11     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12     GNU General Public License for more details.
13
14     You should have received a copy of the GNU General Public License
15     along with this program; if not, write to the Free Software
16     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17
18 */
19
20 /** @file src/image.h
21  *  @brief A set of classes to describe video images.
22  */
23
24 #ifndef DVDOMATIC_IMAGE_H
25 #define DVDOMATIC_IMAGE_H
26
27 #include <string>
28 #include <boost/shared_ptr.hpp>
29 #include <boost/function.hpp>
30 extern "C" {
31 #include <libavcodec/avcodec.h>
32 #include <libavfilter/avfilter.h>
33 }
34 #include "util.h"
35 #include "ffmpeg_compatibility.h"
36
37 class Scaler;
38 class RGBFrameImage;
39 class SimpleImage;
40
41 /** @class Image
42  *  @brief Parent class for wrappers of some image, in some format, that
43  *  can present a set of components and a size in pixels.
44  *
45  *  This class also has some conversion / processing methods.
46  *
47  *  The main point of this class (and its subclasses) is to abstract
48  *  details of FFmpeg's memory management and varying data formats.
49  */
50 class Image
51 {
52 public:
53         Image (AVPixelFormat p)
54                 : _pixel_format (p)
55         {}
56         
57         virtual ~Image () {}
58
59         /** @return Array of pointers to arrays of the component data */
60         virtual uint8_t ** data () const = 0;
61
62         /** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
63         virtual int * line_size () const = 0;
64
65         /** @return Array of strides for each line (including any alignment padding bytes) */
66         virtual int * stride () const = 0;
67
68         /** @return Size of the image, in pixels */
69         virtual libdcp::Size size () const = 0;
70
71         virtual bool aligned () const = 0;
72
73         virtual boost::shared_ptr<Image> clone () const = 0;
74
75         int components () const;
76         int lines (int) const;
77
78         boost::shared_ptr<Image> scale_and_convert_to_rgb (libdcp::Size out_size, int padding, Scaler const * scaler, bool aligned) const;
79         boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, bool aligned) const;
80         boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
81         void alpha_blend (boost::shared_ptr<const Image> image, Position pos);
82         boost::shared_ptr<Image> crop (Crop c, bool aligned) const;
83         
84         void make_black ();
85
86         void read_from_socket (boost::shared_ptr<Socket>);
87         void write_to_socket (boost::shared_ptr<Socket>) const;
88         
89         AVPixelFormat pixel_format () const {
90                 return _pixel_format;
91         }
92
93 protected:
94         virtual void swap (Image &);
95         float bytes_per_pixel (int) const;
96
97 private:
98         void yuv_16_black (uint16_t);
99         static uint16_t swap_16 (uint16_t);
100         
101         AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
102 };
103
104 /** @class FilterBufferImage
105  *  @brief An Image that is held in an AVFilterBufferRef.
106  */
107 class FilterBufferImage : public Image
108 {
109 public:
110         FilterBufferImage (AVPixelFormat, AVFilterBufferRef *);
111         ~FilterBufferImage ();
112
113         uint8_t ** data () const;
114         int * line_size () const;
115         int * stride () const;
116         libdcp::Size size () const;
117         bool aligned () const;
118
119 private:
120         /* Not allowed */
121         FilterBufferImage (FilterBufferImage const &);
122         FilterBufferImage& operator= (FilterBufferImage const &);
123         boost::shared_ptr<Image> clone () const {
124                 assert (false);
125         }
126         
127         AVFilterBufferRef* _buffer;
128         int* _line_size;
129 };
130
131 /** @class SimpleImage
132  *  @brief An Image for which memory is allocated using a `simple' av_malloc().
133  */
134 class SimpleImage : public Image
135 {
136 public:
137         SimpleImage (AVPixelFormat, libdcp::Size, bool);
138         SimpleImage (SimpleImage const &);
139         SimpleImage& operator= (SimpleImage const &);
140         ~SimpleImage ();
141
142         uint8_t ** data () const;
143         int * line_size () const;
144         int * stride () const;
145         libdcp::Size size () const;
146         bool aligned () const;
147         boost::shared_ptr<Image> clone () const;
148
149 protected:
150         void allocate ();
151         void swap (SimpleImage &);
152         
153 private:
154         libdcp::Size _size; ///< size in pixels
155         uint8_t** _data; ///< array of pointers to components
156         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
157         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
158         bool _aligned;
159 };
160
161 class RGBPlusAlphaImage : public SimpleImage
162 {
163 public:
164         RGBPlusAlphaImage (boost::shared_ptr<const Image>);
165         ~RGBPlusAlphaImage ();
166
167         uint8_t* alpha () const {
168                 return _alpha;
169         }
170         
171 private:
172         uint8_t* _alpha;
173 };
174
175 #endif