Try to clarify the difference between line size and stride.
[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 extern "C" {
30 #include <libavcodec/avcodec.h>
31 #include <libavfilter/avfilter.h>
32 }
33 #include "util.h"
34
35 class Scaler;
36 class RGBFrameImage;
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 (PixelFormat 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 Size size () const = 0;
68
69         int components () const;
70         int lines (int) const;
71         boost::shared_ptr<Image> scale_and_convert_to_rgb (Size, int, Scaler const *) const;
72         boost::shared_ptr<Image> scale (Size, Scaler const *) const;
73         boost::shared_ptr<Image> post_process (std::string) const;
74         void alpha_blend (boost::shared_ptr<Image> image, Position pos);
75         
76         void make_black ();
77         
78         PixelFormat pixel_format () const {
79                 return _pixel_format;
80         }
81
82 private:
83         PixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
84 };
85
86 /** @class FilterBufferImage
87  *  @brief An Image that is held in an AVFilterBufferRef.
88  */
89 class FilterBufferImage : public Image
90 {
91 public:
92         FilterBufferImage (PixelFormat, AVFilterBufferRef *);
93         ~FilterBufferImage ();
94
95         uint8_t ** data () const;
96         int * line_size () const;
97         int * stride () const;
98         Size size () const;
99
100 private:
101         AVFilterBufferRef* _buffer;
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 (PixelFormat, Size);
111         ~SimpleImage ();
112
113         uint8_t ** data () const;
114         int * line_size () const;
115         int * stride () const;
116         Size size () const;
117         
118 private:
119         Size _size; ///< size in pixels
120         uint8_t** _data; ///< array of pointers to components
121         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
122         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
123
124 };
125
126 /** @class RGBFrameImage
127  *  @brief An RGB image that is held within an AVFrame.
128  */
129 class RGBFrameImage : public Image
130 {
131 public:
132         RGBFrameImage (Size);
133         ~RGBFrameImage ();
134
135         uint8_t ** data () const;
136         int * line_size () const;
137         int * stride () const;
138         Size size () const;
139         AVFrame * frame () const {
140                 return _frame;
141         }
142
143 private:
144         Size _size;
145         AVFrame* _frame;
146         uint8_t* _data;
147 };
148
149 #endif