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 // Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
10 //
11 // SPDX-License-Identifier: LGPL-3.0-or-later
12 
13 #ifndef OPENSHOT_IMAGE_WRITER_H
14 #define OPENSHOT_IMAGE_WRITER_H
15 
16 #ifdef USE_IMAGEMAGICK
17 
18 #include "ReaderBase.h"
19 #include "WriterBase.h"
20 
21 #include <string>
22 #include <vector>
23 #include "CacheMemory.h"
24 #include "Exceptions.h"
25 #include "Fraction.h"
26 
27 #include "OpenMPUtilities.h"
28 #include "MagickUtilities.h"
29 
30 namespace openshot
31 {
32 
33  /**
34  * @brief This class uses the ImageMagick library to write image files (including animated GIFs)
35  *
36  * All image formats supported by ImageMagick are supported by this class.
37  *
38  * @code
39  * // Create a reader for a video
40  * FFmpegReader r("MyAwesomeVideo.webm");
41  * r.Open(); // Open the reader
42  *
43  * // Create a writer (which will create an animated GIF file)
44  * ImageWriter w("/home/jonathan/NewAnimation.gif");
45  *
46  * // Set the image output settings (format, fps, width, height, quality, loops, combine)
47  * w.SetVideoOptions("GIF", r.info.fps, r.info.width, r.info.height, 70, 1, true);
48  *
49  * // Open the writer
50  * w.Open();
51  *
52  * // Write the 1st 30 frames from the reader
53  * w.WriteFrame(&r, 1, 30);
54  *
55  * // Close the reader & writer
56  * w.Close();
57  * r.Close();
58  * @endcode
59  */
60  class ImageWriter : public WriterBase
61  {
62  private:
63  std::string path;
64  int cache_size;
65  bool is_open;
66  int64_t write_video_count;
67  std::vector<Magick::Image> frames;
68  int image_quality;
69  int number_of_loops;
70  bool combine_frames;
71 
72  std::shared_ptr<Frame> last_frame;
73 
74  public:
75 
76  /// @brief Constructor for ImageWriter. Throws one of the following exceptions.
77  /// @param path The path of the file you want to create
78  ImageWriter(std::string path);
79 
80  /// @brief Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,
81  /// which writes all frames of a multi-frame image at one time.
82  void Close();
83 
84  /// @brief Get the cache size
85  int GetCacheSize() { return cache_size; };
86 
87  /// Determine if writer is open or closed
88  bool IsOpen() { return is_open; };
89 
90  /// Open writer
91  void Open();
92 
93  /// @brief Set the cache size (number of frames to queue before writing)
94  /// @param new_size Number of frames to queue before writing
95  void SetCacheSize(int new_size) { cache_size = new_size; };
96 
97  /// @brief Set the video export options
98  /// @param format The image format (such as GIF)
99  /// @param fps Frames per second of the image (used on certain multi-frame image formats, such as GIF)
100  /// @param width Width in pixels of image
101  /// @param height Height in pixels of image
102  /// @param quality Quality of image (0 to 100, 70 is default)
103  /// @param loops Number of times to repeat the image (used on certain multi-frame image formats, such as GIF)
104  /// @param combine Combine frames into a single image (if possible), or save each frame as its own image
105  void SetVideoOptions(std::string format, Fraction fps, int width, int height,
106  int quality, int loops, bool combine);
107 
108  /// @brief Add a frame to the stack waiting to be encoded.
109  /// @param frame The openshot::Frame object to write to this image
110  void WriteFrame(std::shared_ptr<Frame> frame);
111 
112  /// @brief Write a block of frames from a reader
113  /// @param reader A openshot::ReaderBase object which will provide frames to be written
114  /// @param start The starting frame number of the reader
115  /// @param length The number of frames to write
116  void WriteFrame(ReaderBase* reader, int64_t start, int64_t length);
117 
118  };
119 
120 }
121 
122 #endif //USE_IMAGEMAGICK
123 #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:75
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:77
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:85
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:69
This class represents a fraction.
Definition: Fraction.h:30
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:21
This class uses the ImageMagick library to write image files (including animated GIFs) ...
Definition: ImageWriter.h:60
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:31
void SetCacheSize(int new_size)
Set the cache size (number of frames to queue before writing)
Definition: ImageWriter.h:95
This namespace is the default namespace for all code in the openshot library.
Definition: Compressor.h:28
bool IsOpen()
Determine if writer is open or closed.
Definition: ImageWriter.h:88
void Open()
Open writer.
Definition: ImageWriter.cpp:69