2015-02-05 00:02:59 -06:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for ImageWriter class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>, Fabrice Bellard
|
|
|
|
|
*
|
2019-06-09 08:31:04 -04:00
|
|
|
* @ref License
|
|
|
|
|
*/
|
|
|
|
|
|
2021-10-16 01:26:26 -04:00
|
|
|
// Copyright (c) 2008-2019 OpenShot Studios, LLC, Fabrice Bellard
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2015-02-05 00:02:59 -06:00
|
|
|
|
Add backwards-compatible Imagemagick 7 support (#252)
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
Thanks @ferdnyc
2019-06-21 01:07:49 -04:00
|
|
|
//Require ImageMagick support
|
|
|
|
|
#ifdef USE_IMAGEMAGICK
|
|
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
#include "MagickUtilities.h"
|
|
|
|
|
#include "QtUtilities.h"
|
|
|
|
|
|
|
|
|
|
#include "ImageWriter.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
#include "Frame.h"
|
2021-11-10 23:33:27 -05:00
|
|
|
#include "ReaderBase.h"
|
2021-10-27 14:34:05 -04:00
|
|
|
#include "ZmqLogger.h"
|
2015-02-05 00:02:59 -06:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2019-08-04 23:02:36 -04:00
|
|
|
ImageWriter::ImageWriter(std::string path) :
|
2020-04-22 02:02:55 -04:00
|
|
|
path(path), cache_size(8), write_video_count(0), image_quality(75), number_of_loops(1),
|
2015-02-05 00:02:59 -06:00
|
|
|
combine_frames(true), is_open(false)
|
|
|
|
|
{
|
|
|
|
|
info.has_audio = false;
|
|
|
|
|
info.has_video = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Set video export options
|
2021-11-02 09:12:23 -04:00
|
|
|
void ImageWriter::SetVideoOptions(
|
|
|
|
|
std::string format, Fraction fps, int width, int height,
|
|
|
|
|
int quality, int loops, bool combine)
|
2015-02-05 00:02:59 -06:00
|
|
|
{
|
2021-11-02 09:12:23 -04:00
|
|
|
// Set frames per second (if provided)
|
|
|
|
|
info.fps = fps;
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Set image magic properties
|
|
|
|
|
image_quality = quality;
|
|
|
|
|
number_of_loops = loops;
|
|
|
|
|
combine_frames = combine;
|
|
|
|
|
info.vcodec = format;
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Set the timebase (inverse of fps)
|
|
|
|
|
info.video_timebase = fps.Reciprocal();
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
info.width = std::max(1, width);
|
|
|
|
|
info.height = std::max(1, height);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
info.video_bit_rate = quality;
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Calculate the DAR (display aspect ratio)
|
|
|
|
|
Fraction size(
|
|
|
|
|
info.width * info.pixel_ratio.num,
|
|
|
|
|
info.height * info.pixel_ratio.den);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Reduce size fraction
|
|
|
|
|
size.Reduce();
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Set the ratio based on the reduced fraction
|
|
|
|
|
info.display_ratio = size;
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"ImageWriter::SetVideoOptions (" + format + ")",
|
|
|
|
|
"width", width,
|
|
|
|
|
"height", height,
|
|
|
|
|
"size.num", size.num,
|
|
|
|
|
"size.den", size.den,
|
|
|
|
|
"fps.num", fps.num,
|
|
|
|
|
"fps.den", fps.den);
|
2015-02-05 00:02:59 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open the writer
|
2017-10-26 18:44:35 -05:00
|
|
|
void ImageWriter::Open()
|
2015-02-05 00:02:59 -06:00
|
|
|
{
|
|
|
|
|
is_open = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Add a frame to the queue waiting to be encoded.
|
2017-10-26 18:44:35 -05:00
|
|
|
void ImageWriter::WriteFrame(std::shared_ptr<Frame> frame)
|
2015-02-05 00:02:59 -06:00
|
|
|
{
|
|
|
|
|
// Check for open reader (or throw exception)
|
2021-11-02 09:12:23 -04:00
|
|
|
if (!is_open) {
|
|
|
|
|
throw WriterClosed(
|
|
|
|
|
"The ImageWriter is closed. "
|
|
|
|
|
"Call Open() before calling this method.", path);
|
|
|
|
|
}
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Copy and resize image
|
2021-11-02 09:12:23 -04:00
|
|
|
auto qimage = frame->GetImage();
|
|
|
|
|
auto frame_image = openshot::QImage2Magick(qimage);
|
2015-06-01 00:20:14 -07:00
|
|
|
frame_image->magick( info.vcodec );
|
|
|
|
|
frame_image->backgroundColor(Magick::Color("none"));
|
Add backwards-compatible Imagemagick 7 support (#252)
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
Thanks @ferdnyc
2019-06-21 01:07:49 -04:00
|
|
|
MAGICK_IMAGE_ALPHA(frame_image, true);
|
2015-06-01 00:20:14 -07:00
|
|
|
frame_image->quality(image_quality);
|
|
|
|
|
frame_image->animationDelay(info.video_timebase.ToFloat() * 100);
|
|
|
|
|
frame_image->animationIterations(number_of_loops);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Calculate correct DAR (display aspect ratio)
|
|
|
|
|
int new_height = info.height * frame->GetPixelRatio().Reciprocal().ToDouble();
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
// Resize image
|
2021-11-02 09:12:23 -04:00
|
|
|
Magick::Geometry new_size(info.width, new_height);
|
2015-06-01 00:20:14 -07:00
|
|
|
new_size.aspect(true);
|
|
|
|
|
frame_image->resize(new_size);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
2015-06-01 00:20:14 -07:00
|
|
|
|
|
|
|
|
// Put resized frame in vector (waiting to be written)
|
|
|
|
|
frames.push_back(*frame_image.get());
|
2015-02-05 00:02:59 -06:00
|
|
|
|
|
|
|
|
// Keep track of the last frame added
|
|
|
|
|
last_frame = frame;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Write a block of frames from a reader
|
2017-10-26 18:44:35 -05:00
|
|
|
void ImageWriter::WriteFrame(ReaderBase* reader, int64_t start, int64_t length)
|
2015-02-05 00:02:59 -06:00
|
|
|
{
|
2021-11-02 09:12:23 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod(
|
|
|
|
|
"ImageWriter::WriteFrame (from Reader)",
|
|
|
|
|
"start", start,
|
|
|
|
|
"length", length);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
|
|
|
|
// Loop through each frame (and encoded it)
|
2017-09-28 16:03:01 -05:00
|
|
|
for (int64_t number = start; number <= length; number++)
|
2015-02-05 00:02:59 -06:00
|
|
|
{
|
|
|
|
|
// Get the frame
|
2017-08-20 17:37:39 -05:00
|
|
|
std::shared_ptr<Frame> f = reader->GetFrame(number);
|
2015-02-05 00:02:59 -06:00
|
|
|
|
|
|
|
|
// Encode frame
|
|
|
|
|
WriteFrame(f);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close the writer and encode/output final image to the disk.
|
|
|
|
|
void ImageWriter::Close()
|
|
|
|
|
{
|
2021-11-02 09:12:23 -04:00
|
|
|
// Write frame images to file
|
2015-02-05 00:02:59 -06:00
|
|
|
Magick::writeImages(frames.begin(), frames.end(), path, combine_frames);
|
|
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Clear frames vector & counters, close writer
|
2015-02-05 00:02:59 -06:00
|
|
|
frames.clear();
|
|
|
|
|
write_video_count = 0;
|
|
|
|
|
is_open = false;
|
|
|
|
|
|
2019-07-03 14:14:02 -04:00
|
|
|
ZmqLogger::Instance()->AppendDebugMethod("ImageWriter::Close");
|
2015-02-05 00:02:59 -06:00
|
|
|
}
|
|
|
|
|
|
Add backwards-compatible Imagemagick 7 support (#252)
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
* Add ImageMagick 7 compatibility
A new header, `imclude/MagickUtilities.h`, is created to hold the
compatibility `#define`s.
The image-conversion code in `src/Frame.cpp` received the only
major changes — instead of doing the export by hand (and having
to account for changes in the underlying API), it uses the
`MagickCore::ExportImagePixels()` function which does basically
the same work, but accounts for all of the API changes for us.
The API of that function is _unchanged_ from IM6 to IM7.
TODO: `MagickCore::ExportImagePixels()` will return an `exception`
struct if it encounters any problems. Currently the code ignores
that, which it should not.
Thanks @ferdnyc
2019-06-21 01:07:49 -04:00
|
|
|
#endif //USE_IMAGEMAGICK
|