Merge branch '2.0' of git.carlh.net:git/dcpomatic into 2.0
[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 "position.h"
28 #include "position_image.h"
29 #include "types.h"
30 #include <dcp/image.h>
31 extern "C" {
32 #include <libavcodec/avcodec.h>
33 #include <libavfilter/avfilter.h>
34 }
35 #include <boost/shared_ptr.hpp>
36 #include <boost/function.hpp>
37 #include <string>
38
39 class Scaler;
40 class Socket;
41
42 class Image : public dcp::Image
43 {
44 public:
45         Image (AVPixelFormat, dcp::Size, bool);
46         Image (AVFrame *);
47         Image (Image const &);
48         Image (boost::shared_ptr<const Image>, bool);
49         Image& operator= (Image const &);
50         ~Image ();
51         
52         uint8_t * const * data () const;
53         int * line_size () const;
54         int const * stride () const;
55         dcp::Size size () const;
56         bool aligned () const;
57
58         int components () const;
59         int line_factor (int) const;
60         int lines (int) const;
61
62         boost::shared_ptr<Image> scale (dcp::Size, Scaler const *, AVPixelFormat, bool aligned) const;
63         boost::shared_ptr<Image> crop (Crop c, bool aligned) const;
64
65         boost::shared_ptr<Image> crop_scale_window (Crop c, dcp::Size, dcp::Size, Scaler const *, AVPixelFormat, bool aligned) const;
66         
67         void make_black ();
68         void make_transparent ();
69         void alpha_blend (boost::shared_ptr<const Image> image, Position<int> pos);
70         void copy (boost::shared_ptr<const Image> image, Position<int> pos);
71         void fade (float);
72
73         void read_from_socket (boost::shared_ptr<Socket>);
74         void write_to_socket (boost::shared_ptr<Socket>) const;
75         
76         AVPixelFormat pixel_format () const {
77                 return _pixel_format;
78         }
79
80         std::string digest () const;
81
82 private:
83         friend struct pixel_formats_test;
84         
85         void allocate ();
86         void swap (Image &);
87         float bytes_per_pixel (int) const;
88         void yuv_16_black (uint16_t, bool);
89         static uint16_t swap_16 (uint16_t);
90         
91         AVPixelFormat _pixel_format; ///< FFmpeg's way of describing the pixel format of this Image
92         uint8_t** _data; ///< array of pointers to components
93         int* _line_size; ///< array of sizes of the data in each line, in pixels (without any alignment padding bytes)
94         int* _stride; ///< array of strides for each line (including any alignment padding bytes)
95         bool _aligned;
96 };
97
98 extern PositionImage merge (std::list<PositionImage> images);
99 extern bool operator== (Image const & a, Image const & b);
100
101 #endif