OpenShot Library | libopenshot  0.2.2
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  * @section LICENSE
7  *
8  * Copyright (c) 2008-2013 OpenShot Studios, LLC, Fabrice Bellard
9  * (http://www.openshotstudios.com). This file is part of
10  * OpenShot Library (http://www.openshot.org), an open-source project
11  * dedicated to delivering high quality video editing and animation solutions
12  * to the world.
13  *
14  * This file is originally based on the Libavformat API example, and then modified
15  * by the libopenshot project.
16  *
17  * OpenShot Library is free software: you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation, either version 3 of the License, or
20  * (at your option) any later version.
21  * * OpenShot Library (libopenshot) is free software: you can redistribute it
22  * and/or modify it under the terms of the GNU Lesser General Public License
23  * as published by the Free Software Foundation, either version 3 of the
24  * License, or (at your option) any later version.
25  *
26  * OpenShot Library (libopenshot) is distributed in the hope that it will be
27  * useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
28  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29  * GNU Lesser General Public License for more details.
30  *
31  * You should have received a copy of the GNU Lesser General Public License
32  * along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
33  */
34 
35 
36 #ifndef OPENSHOT_IMAGE_WRITER_H
37 #define OPENSHOT_IMAGE_WRITER_H
38 
39 #include "ReaderBase.h"
40 #include "WriterBase.h"
41 
42 #include <cmath>
43 #include <ctime>
44 #include <iostream>
45 #include <stdio.h>
46 #include <unistd.h>
47 #include "Magick++.h"
48 #include "CacheMemory.h"
49 #include "Exceptions.h"
50 #include "OpenMPUtilities.h"
51 
52 
53 using namespace std;
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  string path;
89  int cache_size;
90  bool is_writing;
91  bool is_open;
92  int64_t write_video_count;
93  vector<Magick::Image> frames;
94  int image_quality;
95  int number_of_loops;
96  bool combine_frames;
97 
98  std::shared_ptr<Frame> last_frame;
99 
100  public:
101 
102  /// @brief Constructor for ImageWriter. Throws one of the following exceptions.
103  /// @param path The path of the file you want to create
104  ImageWriter(string path);
105 
106  /// @brief Close the writer and encode/output final image to the disk. This is a requirement of ImageMagick,
107  /// which writes all frames of a multi-frame image at one time.
108  void Close();
109 
110  /// @brief Get the cache size
111  int GetCacheSize() { return cache_size; };
112 
113  /// Determine if writer is open or closed
114  bool IsOpen() { return is_open; };
115 
116  /// Open writer
117  void Open();
118 
119  /// @brief Set the cache size (number of frames to queue before writing)
120  /// @param new_size Number of frames to queue before writing
121  void SetCacheSize(int new_size) { cache_size = new_size; };
122 
123  /// @brief Set the video export options
124  /// @param format The image format (such as GIF)
125  /// @param fps Frames per second of the image (used on certain multi-frame image formats, such as GIF)
126  /// @param width Width in pixels of image
127  /// @param height Height in pixels of image
128  /// @param quality Quality of image (0 to 100, 70 is default)
129  /// @param loops Number of times to repeat the image (used on certain multi-frame image formats, such as GIF)
130  /// @param combine Combine frames into a single image (if possible), or save each frame as it's own image
131  void SetVideoOptions(string format, Fraction fps, int width, int height,
132  int quality, int loops, bool combine);
133 
134  /// @brief Add a frame to the stack waiting to be encoded.
135  /// @param frame The openshot::Frame object to write to this image
136  void WriteFrame(std::shared_ptr<Frame> frame);
137 
138  /// @brief Write a block of frames from a reader
139  /// @param reader A openshot::ReaderBase object which will provide frames to be written
140  /// @param start The starting frame number of the reader
141  /// @param length The number of frames to write
142  void WriteFrame(ReaderBase* reader, int64_t start, int64_t length);
143 
144  };
145 
146 }
147 
148 #endif
openshot::ImageWriter::GetCacheSize
int GetCacheSize()
Get the cache size.
Definition: ImageWriter.h:111
WriterBase.h
Header file for WriterBase class.
openshot
This namespace is the default namespace for all code in the openshot library.
Definition: AudioBufferSource.h:45
openshot::Fraction
This class represents a fraction.
Definition: Fraction.h:42
openshot::ImageWriter
This class uses the ImageMagick library to write image files (including animated GIFs)
Definition: ImageWriter.h:85
CacheMemory.h
Header file for CacheMemory class.
openshot::ImageWriter::IsOpen
bool IsOpen()
Determine if writer is open or closed.
Definition: ImageWriter.h:114
openshot::ImageWriter::SetCacheSize
void SetCacheSize(int new_size)
Set the cache size (number of frames to queue before writing)
Definition: ImageWriter.h:121
ReaderBase.h
Header file for ReaderBase class.
OpenMPUtilities.h
Header file for OpenMPUtilities (set some common macros)
openshot::ReaderBase
This abstract class is the base class, used by all readers in libopenshot.
Definition: ReaderBase.h:96
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:86