OpenShot Library | libopenshot  0.2.5
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 <cmath>
47 #include <ctime>
48 #include <iostream>
49 #include <stdio.h>
50 #include <unistd.h>
51 #include "CacheMemory.h"
52 #include "Exceptions.h"
53 #include "OpenMPUtilities.h"
54 #include "MagickUtilities.h"
55 
56 namespace openshot
57 {
58 
59  /**
60  * @brief This class uses the ImageMagick library to write image files (including animated GIFs)
61  *
62  * All image formats supported by ImageMagick are supported by this class.
63  *
64  * @code
65  * // Create a reader for a video
66  * FFmpegReader r("MyAwesomeVideo.webm");
67  * r.Open(); // Open the reader
68  *
69  * // Create a writer (which will create an animated GIF file)
70  * ImageWriter w("/home/jonathan/NewAnimation.gif");
71  *
72  * // Set the image output settings (format, fps, width, height, quality, loops, combine)
73  * w.SetVideoOptions("GIF", r.info.fps, r.info.width, r.info.height, 70, 1, true);
74  *
75  * // Open the writer
76  * w.Open();
77  *
78  * // Write the 1st 30 frames from the reader
79  * w.WriteFrame(&r, 1, 30);
80  *
81  * // Close the reader & writer
82  * w.Close();
83  * r.Close();
84  * @endcode
85  */
86  class ImageWriter : public WriterBase
87  {
88  private:
89  std::string path;
90  int cache_size;
91  bool is_writing;
92  bool is_open;
93  int64_t write_video_count;
94  std::vector<Magick::Image> frames;
95  int image_quality;
96  int number_of_loops;
97  bool combine_frames;
98 
99  std::shared_ptr<Frame> last_frame;
100 
101  public:
102 
103  /// @brief Constructor for ImageWriter. Throws one of the following exceptions.
104  /// @param path The path of the file you want to create
105  ImageWriter(std::string path);
106 
107  /// @brief Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,
108  /// which writes all frames of a multi-frame image at one time.
109  void Close();
110 
111  /// @brief Get the cache size
112  int GetCacheSize() { return cache_size; };
113 
114  /// Determine if writer is open or closed
115  bool IsOpen() { return is_open; };
116 
117  /// Open writer
118  void Open();
119 
120  /// @brief Set the cache size (number of frames to queue before writing)
121  /// @param new_size Number of frames to queue before writing
122  void SetCacheSize(int new_size) { cache_size = new_size; };
123 
124  /// @brief Set the video export options
125  /// @param format The image format (such as GIF)
126  /// @param fps Frames per second of the image (used on certain multi-frame image formats, such as GIF)
127  /// @param width Width in pixels of image
128  /// @param height Height in pixels of image
129  /// @param quality Quality of image (0 to 100, 70 is default)
130  /// @param loops Number of times to repeat the image (used on certain multi-frame image formats, such as GIF)
131  /// @param combine Combine frames into a single image (if possible), or save each frame as its own image
132  void SetVideoOptions(std::string format, Fraction fps, int width, int height,
133  int quality, int loops, bool combine);
134 
135  /// @brief Add a frame to the stack waiting to be encoded.
136  /// @param frame The openshot::Frame object to write to this image
137  void WriteFrame(std::shared_ptr<Frame> frame);
138 
139  /// @brief Write a block of frames from a reader
140  /// @param reader A openshot::ReaderBase object which will provide frames to be written
141  /// @param start The starting frame number of the reader
142  /// @param length The number of frames to write
143  void WriteFrame(ReaderBase* reader, int64_t start, int64_t length);
144 
145  };
146 
147 }
148 
149 #endif //USE_IMAGEMAGICK
150 #endif //OPENSHOT_IMAGE_WRITER_H
openshot::ImageWriter::GetCacheSize
int GetCacheSize()
Get the cache size.
Definition: ImageWriter.h:112
WriterBase.h
Header file for WriterBase class.
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:38
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:45
openshot::ImageWriter::Open
void Open()
Open writer.
Definition: ImageWriter.cpp:89
openshot::ImageWriter::WriteFrame
void WriteFrame(std::shared_ptr< Frame > frame)
Add a frame to the stack waiting to be encoded.
Definition: ImageWriter.cpp:95
openshot::ImageWriter
This class uses the ImageMagick library to write image files (including animated GIFs)
Definition: ImageWriter.h:86
CacheMemory.h
Header file for CacheMemory class.
openshot::ImageWriter::IsOpen
bool IsOpen()
Determine if writer is open or closed.
Definition: ImageWriter.h:115
path
path
Definition: FFmpegWriter.cpp:1410
openshot::ImageWriter::SetCacheSize
void SetCacheSize(int new_size)
Set the cache size (number of frames to queue before writing)
Definition: ImageWriter.h:122
openshot::ImageWriter::Close
void Close()
Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,...
Definition: ImageWriter.cpp:145
ReaderBase.h
Header file for ReaderBase class.
OpenMPUtilities.h
Header file for OpenMPUtilities (set some common macros)
openshot::ImageWriter::SetVideoOptions
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:51
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:97
openshot::ImageWriter::ImageWriter
ImageWriter(std::string path)
Constructor for ImageWriter. Throws one of the following exceptions.
Definition: ImageWriter.cpp:41
Exceptions.h
Header file for all Exception classes.
openshot::WriterBase
This abstract class is the base class, used by writers. Writers are types of classes that encode vide...
Definition: WriterBase.h:87