Some work on cropping in the film viewer; also prevent player from always scaling...
[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 DCPOMATIC_IMAGE_H
25 #define DCPOMATIC_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
36 class Scaler;
37 class SimpleImage;
38
39 /** @class Image
40  *  @brief Parent class for wrappers of some image, in some format, that
41  *  can present a set of components and a size in pixels.
42  *
43  *  This class also has some conversion / processing methods.
44  *
45  *  The main point of this class (and its subclasses) is to abstract
46  *  details of FFmpeg's memory management and varying data formats.
47  */
48 class Image
49 {
50 public:
51         Image (AVPixelFormat p)
52                 : _pixel_format (p)
53         {}
54         
55         virtual ~Image () {}
56
57         /** @return Array of pointers to arrays of the component data */
58         virtual uint8_t ** data () const = 0;
59
60         /** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
61         virtual int * line_size () const = 0;
62
63         /** @return Array of strides for each line (including any alignment padding bytes) */
64         virtual int * stride () const = 0;
65
66         /** @return Size of the image, in pixels */
67         virtual libdcp::Size size () const = 0;
68
69         virtual bool aligned () const = 0;
70
71         int components () const;
72         int line_factor (int) const;
73         int lines (int) const;
74
75         boost::shared_ptr<Image> scale_and_convert_to_rgb (libdcp::Size, Scaler const *, bool) const;
76         boost::shared_ptr<Image> scale (libdcp::Size, Scaler const *, bool aligned) const;
77         boost::shared_ptr<Image> post_process (std::string, bool aligned) const;
78         void alpha_blend (boost::shared_ptr<const Image> image, Position pos);
79         void copy (boost::shared_ptr<const Image> image, Position pos);
80         boost::shared_ptr<Image> crop (Crop c, bool aligned) const;
81         
82         void make_black ();
83
84         void read_from_socket (boost::shared_ptr<Socket>);
85         void write_to_socket (boost::shared_ptr<Socket>) const;
86         
87         AVPixelFormat pixel_format () const {
88                 return _pixel_format;
89         }
90
91 protected:
92         virtual void swap (Image &);
93         float bytes_per_pixel (int) const;
94
95         friend class pixel_formats_test;
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 SimpleImage
105  *  @brief An Image for which memory is allocated using a `simple' av_malloc().
106  */
107 class SimpleImage : public Image
108 {
109 public:
110         SimpleImage (AVPixelFormat, libdcp::Size, bool);
111         SimpleImage (AVFrame *);
112         SimpleImage (SimpleImage const &);
113         SimpleImage (boost::shared_ptr<const Image>, bool);
114         SimpleImage& operator= (SimpleImage const &);
115         ~SimpleImage ();
116
117         uint8_t ** data () const;
118         int * line_size () const;
119         int * stride () const;
120         libdcp::Size size () const;
121         bool aligned () const;
122
123 protected:
124         void allocate ();
125         void swap (SimpleImage &);
126         
127 private:
128         libdcp::Size _size; ///< size in pixels
129         uint8_t** _data; ///< array of pointers to components
130         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
131         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
132         bool _aligned;
133 };
134
135 class RGBPlusAlphaImage : public SimpleImage
136 {
137 public:
138         RGBPlusAlphaImage (boost::shared_ptr<const Image>);
139         ~RGBPlusAlphaImage ();
140
141         uint8_t* alpha () const {
142                 return _alpha;
143         }
144         
145 private:
146         uint8_t* _alpha;
147 };
148
149 #endif