OpenShot Library | libopenshot  0.2.7
ImageWriter.h
Go to the documentation of this file.
1 /**
2  * @file
3  * @brief Header file for ImageWriter class
4  * @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
5  *
6  * @ref License
7  */
8 
9 /* LICENSE
10  *
11  * Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
12  * (http://www.openshotstudios.com). This file is part of
13  * OpenShot Library (http://www.openshot.org), an open-source project
14  * dedicated to delivering high quality video editing and animation solutions
15  * to the world.
16  *
17  * This file is originally based on the Libavformat API example, and then modified
18  * by the libopenshot project.
19  *
20  * OpenShot Library is free software: you can redistribute it and/or modify
21  * it under the terms of the GNU General Public License as published by
22  * the Free Software Foundation, either version 3 of the License, or
23  * (at your option) any later version.
24  * * OpenShot Library (libopenshot) is free software: you can redistribute it
25  * and/or modify it under the terms of the GNU Lesser General Public License
26  * as published by the Free Software Foundation, either version 3 of the
27  * License, or (at your option) any later version.
28  *
29  * OpenShot Library (libopenshot) is distributed in the hope that it will be
30  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
31  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32  * GNU Lesser General Public License for more details.
33  *
34  * You should have received a copy of the GNU Lesser General Public License
35  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
36  */
37 
38 #ifndef OPENSHOT_IMAGE_WRITER_H
39 #define OPENSHOT_IMAGE_WRITER_H
40 
41 #ifdef USE_IMAGEMAGICK
42 
43 #include "ReaderBase.h"
44 #include "WriterBase.h"
45 
46 #include <string>
47 #include <vector>
48 #include "CacheMemory.h"
49 #include "Exceptions.h"
50 #include "Fraction.h"
51 
52 #include "OpenMPUtilities.h"
53 #include "MagickUtilities.h"
54 
55 namespace openshot
56 {
57 
58  /**
59  * @brief This class uses the ImageMagick library to write image files (including animated GIFs)
60  *
61  * All image formats supported by ImageMagick are supported by this class.
62  *
63  * @code
64  * // Create a reader for a video
65  * FFmpegReader r("MyAwesomeVideo.webm");
66  * r.Open(); // Open the reader
67  *
68  * // Create a writer (which will create an animated GIF file)
69  * ImageWriter w("/home/jonathan/NewAnimation.gif");
70  *
71  * // Set the image output settings (format, fps, width, height, quality, loops, combine)
72  * w.SetVideoOptions("GIF", r.info.fps, r.info.width, r.info.height, 70, 1, true);
73  *
74  * // Open the writer
75  * w.Open();
76  *
77  * // Write the 1st 30 frames from the reader
78  * w.WriteFrame(&r, 1, 30);
79  *
80  * // Close the reader & writer
81  * w.Close();
82  * r.Close();
83  * @endcode
84  */
85  class ImageWriter : public WriterBase
86  {
87  private:
88  std::string path;
89  int cache_size;
90  bool is_open;
91  int64_t write_video_count;
92  std::vector<Magick::Image> frames;
93  int image_quality;
94  int number_of_loops;
95  bool combine_frames;
96 
97  std::shared_ptr<Frame> last_frame;
98 
99  public:
100 
101  /// @brief Constructor for ImageWriter. Throws one of the following exceptions.
102  /// @param path The path of the file you want to create
103  ImageWriter(std::string path);
104 
105  /// @brief Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,
106  /// which writes all frames of a multi-frame image at one time.
107  void Close();
108 
109  /// @brief Get the cache size
110  int GetCacheSize() { return cache_size; };
111 
112  /// Determine if writer is open or closed
113  bool IsOpen() { return is_open; };
114 
115  /// Open writer
116  void Open();
117 
118  /// @brief Set the cache size (number of frames to queue before writing)
119  /// @param new_size Number of frames to queue before writing
120  void SetCacheSize(int new_size) { cache_size = new_size; };
121 
122  /// @brief Set the video export options
123  /// @param format The image format (such as GIF)
124  /// @param fps Frames per second of the image (used on certain multi-frame image formats, such as GIF)
125  /// @param width Width in pixels of image
126  /// @param height Height in pixels of image
127  /// @param quality Quality of image (0 to 100, 70 is default)
128  /// @param loops Number of times to repeat the image (used on certain multi-frame image formats, such as GIF)
129  /// @param combine Combine frames into a single image (if possible), or save each frame as its own image
130  void SetVideoOptions(std::string format, Fraction fps, int width, int height,
131  int quality, int loops, bool combine);
132 
133  /// @brief Add a frame to the stack waiting to be encoded.
134  /// @param frame The openshot::Frame object to write to this image
135  void WriteFrame(std::shared_ptr<Frame> frame);
136 
137  /// @brief Write a block of frames from a reader
138  /// @param reader A openshot::ReaderBase object which will provide frames to be written
139  /// @param start The starting frame number of the reader
140  /// @param length The number of frames to write
141  void WriteFrame(ReaderBase* reader, int64_t start, int64_t length);
142 
143  };
144 
145 }
146 
147 #endif //USE_IMAGEMAGICK
148 #endif //OPENSHOT_IMAGE_WRITER_H
Header file for Fraction class.
Header file for ReaderBase class.
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:96
Header file for OpenMPUtilities (set some common macros)
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
Header file for CacheMemory class.
void Close()
Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick...
int GetCacheSize()
Get the cache size.
Definition: ImageWriter.h:110
Header file for all Exception classes.
Header file for WriterBase class.
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:87
This class represents a fraction.
Definition: Fraction.h:48
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:42
This class uses the ImageMagick library to write image files (including animated GIFs) ...
Definition: ImageWriter.h:85
void SetVideoOptions(std::string format, Fraction fps, int width, int height, int quality, int loops, bool combine)
Set the video export options.
Definition: ImageWriter.cpp:52
void SetCacheSize(int new_size)
Set the cache size (number of frames to queue before writing)
Definition: ImageWriter.h:120
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:46
bool IsOpen()
Determine if writer is open or closed.
Definition: ImageWriter.h:113
void Open()
Open writer.
Definition: ImageWriter.cpp:90