Some work on DCI naming. Clean up compacted / aligned image handling somewhat.
[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
36 class Scaler;
37 class RGBFrameImage;
38 class SimpleImage;
39
40 /** @class Image
41  *  @brief Parent class for wrappers of some image, in some format, that
42  *  can present a set of components and a size in pixels.
43  *
44  *  This class also has some conversion / processing methods.
45  *
46  *  The main point of this class (and its subclasses) is to abstract
47  *  details of FFmpeg's memory management and varying data formats.
48  */
49 class Image
50 {
51 public:
52         Image (PixelFormat p)
53                 : _pixel_format (p)
54         {}
55         
56         virtual ~Image () {}
57
58         /** @return Array of pointers to arrays of the component data */
59         virtual uint8_t ** data () const = 0;
60
61         /** @return Array of sizes of the data in each line, in bytes (without any alignment padding bytes) */
62         virtual int * line_size () const = 0;
63
64         /** @return Array of strides for each line (including any alignment padding bytes) */
65         virtual int * stride () const = 0;
66
67         /** @return Size of the image, in pixels */
68         virtual Size size () const = 0;
69
70         int components () const;
71         int lines (int) const;
72         boost::shared_ptr<Image> scale_and_convert_to_rgb (Size, int, Scaler const *) const;
73         boost::shared_ptr<Image> scale (Size, Scaler const *) const;
74         boost::shared_ptr<Image> post_process (std::string) const;
75         void alpha_blend (boost::shared_ptr<Image> image, Position pos);
76         
77         void make_black ();
78         
79         PixelFormat pixel_format () const {
80                 return _pixel_format;
81         }
82
83 private:
84         PixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
85 };
86
87 /** @class FilterBufferImage
88  *  @brief An Image that is held in an AVFilterBufferRef.
89  */
90 class FilterBufferImage : public Image
91 {
92 public:
93         FilterBufferImage (PixelFormat, AVFilterBufferRef *);
94         ~FilterBufferImage ();
95
96         uint8_t ** data () const;
97         int * line_size () const;
98         int * stride () const;
99         Size size () const;
100
101 private:
102         AVFilterBufferRef* _buffer;
103 };
104
105 /** @class SimpleImage
106  *  @brief An Image for which memory is allocated using a `simple' av_malloc().
107  */
108 class SimpleImage : public Image
109 {
110 public:
111         SimpleImage (PixelFormat, Size, boost::function<int (int)> rounder);
112         ~SimpleImage ();
113
114         uint8_t ** data () const;
115         int * line_size () const;
116         int * stride () const;
117         Size size () const;
118         
119 private:
120         
121         Size _size; ///< size in pixels
122         uint8_t** _data; ///< array of pointers to components
123         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
124         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
125 };
126
127 class AlignedImage : public SimpleImage
128 {
129 public:
130         AlignedImage (PixelFormat, Size);
131 };
132
133 class CompactImage : public SimpleImage
134 {
135 public:
136         CompactImage (PixelFormat, Size);
137         CompactImage (boost::shared_ptr<Image>);
138 };
139
140 /** @class RGBFrameImage
141  *  @brief An RGB image that is held within an AVFrame.
142  */
143 class RGBFrameImage : public Image
144 {
145 public:
146         RGBFrameImage (Size);
147         ~RGBFrameImage ();
148
149         uint8_t ** data () const;
150         int * line_size () const;
151         int * stride () const;
152         Size size () const;
153         AVFrame * frame () const {
154                 return _frame;
155         }
156
157 private:
158         Size _size;
159         AVFrame* _frame;
160         uint8_t* _data;
161 };
162
163 #endif