2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for ImageReader class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
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
|
|
|
|
|
//
|
|
|
|
|
// SPDX-License-Identifier: LGPL-3.0-or-later
|
2013-09-12 17:52:10 -05: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"
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "ImageReader.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.h"
|
2021-09-18 04:16:30 -04:00
|
|
|
#include "Frame.h"
|
2012-08-29 15:29:15 -05:00
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2020-10-17 05:56:02 -04:00
|
|
|
ImageReader::ImageReader(const std::string& path, bool inspect_reader) : path(path), is_open(false)
|
2016-09-16 17:43:26 -05:00
|
|
|
{
|
2019-03-14 09:26:56 -07:00
|
|
|
// Open and Close the reader, to populate its attributes (such as height, width, etc...)
|
2016-09-16 17:43:26 -05:00
|
|
|
if (inspect_reader) {
|
|
|
|
|
Open();
|
|
|
|
|
Close();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2012-08-29 15:29:15 -05:00
|
|
|
// Open image file
|
2017-10-26 18:44:35 -05:00
|
|
|
void ImageReader::Open()
|
2012-08-29 15:29:15 -05:00
|
|
|
{
|
2012-10-09 01:45:34 -05:00
|
|
|
// Open reader if not already open
|
|
|
|
|
if (!is_open)
|
2012-08-29 15:29:15 -05:00
|
|
|
{
|
2012-10-09 01:45:34 -05:00
|
|
|
// Attempt to open file
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// load image
|
2020-08-20 16:50:12 -04:00
|
|
|
image = std::make_shared<Magick::Image>(path);
|
2012-11-08 04:35:21 -06:00
|
|
|
|
|
|
|
|
// Give image a transparent background color
|
|
|
|
|
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(image, true);
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const Magick::Exception& e) {
|
2012-10-09 01:45:34 -05:00
|
|
|
// raise exception
|
|
|
|
|
throw InvalidFile("File could not be opened.", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update image properties
|
|
|
|
|
info.has_audio = false;
|
|
|
|
|
info.has_video = true;
|
2015-09-28 22:05:50 -05:00
|
|
|
info.has_single_image = true;
|
2012-11-08 04:35:21 -06:00
|
|
|
info.file_size = image->fileSize();
|
|
|
|
|
info.vcodec = image->format();
|
|
|
|
|
info.width = image->size().width();
|
|
|
|
|
info.height = image->size().height();
|
2021-11-02 09:12:23 -04:00
|
|
|
info.pixel_ratio = openshot::Fraction(1, 1);
|
2019-10-25 13:42:57 -04:00
|
|
|
info.duration = 60 * 60 * 1; // 1 hour duration
|
2021-11-02 09:12:23 -04:00
|
|
|
info.fps = openshot::Fraction(30, 1);
|
|
|
|
|
info.video_timebase = info.fps.Reciprocal();
|
|
|
|
|
info.video_length = std::round(info.duration * info.fps.ToDouble());
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// Calculate the DAR (display aspect ratio)
|
2021-11-02 09:12:23 -04:00
|
|
|
Fraction dar(
|
|
|
|
|
info.width * info.pixel_ratio.num,
|
|
|
|
|
info.height * info.pixel_ratio.den);
|
2012-10-09 01:45:34 -05:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Reduce DAR fraction & set ratio
|
|
|
|
|
dar.Reduce();
|
|
|
|
|
info.display_ratio = dar;
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// Mark as "open"
|
|
|
|
|
is_open = true;
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ImageReader::Close()
|
|
|
|
|
{
|
2012-10-09 01:45:34 -05:00
|
|
|
if (is_open)
|
|
|
|
|
{
|
|
|
|
|
is_open = false;
|
2014-07-25 23:32:12 -05:00
|
|
|
// Delete the image
|
|
|
|
|
image.reset();
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an openshot::Frame object for a specific frame number of this reader.
|
2017-10-26 18:44:35 -05:00
|
|
|
std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
|
2012-08-29 15:29:15 -05:00
|
|
|
{
|
2021-11-02 09:12:23 -04:00
|
|
|
if (!is_open) {
|
|
|
|
|
throw ReaderClosed(
|
|
|
|
|
"The ImageReader is closed. "
|
|
|
|
|
"Call Open() before calling this method.", path);
|
|
|
|
|
}
|
2012-11-08 04:35:21 -06:00
|
|
|
|
2013-10-18 12:38:09 -05:00
|
|
|
// Create or get frame object
|
2020-08-20 16:50:12 -04:00
|
|
|
auto image_frame = std::make_shared<Frame>(
|
2021-11-02 09:12:23 -04:00
|
|
|
requested_frame,
|
|
|
|
|
image->size().width(), image->size().height(),
|
2020-08-20 16:50:12 -04:00
|
|
|
"#000000", 0, 2);
|
2012-11-08 04:35:21 -06:00
|
|
|
|
2013-10-18 12:38:09 -05:00
|
|
|
// Add Image data to frame
|
2021-11-02 09:12:23 -04:00
|
|
|
auto qimage = openshot::Magick2QImage(image);
|
|
|
|
|
image_frame->AddImage(qimage);
|
2013-10-18 12:38:09 -05:00
|
|
|
return image_frame;
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
// Generate JSON string of this object
|
2019-12-27 08:51:51 -05:00
|
|
|
std::string ImageReader::Json() const {
|
2013-12-07 21:09:55 -06:00
|
|
|
|
|
|
|
|
// Return formatted string
|
|
|
|
|
return JsonValue().toStyledString();
|
|
|
|
|
}
|
2012-08-29 15:29:15 -05:00
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Generate Json::Value for this object
|
|
|
|
|
Json::Value ImageReader::JsonValue() const {
|
2013-12-07 16:52:09 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// get parent properties
|
|
|
|
|
Json::Value root = ReaderBase::JsonValue();
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
root["type"] = "ImageReader";
|
2013-12-07 16:52:09 -06:00
|
|
|
root["path"] = path;
|
|
|
|
|
return root;
|
|
|
|
|
}
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
// Load JSON string into this object
|
2019-12-27 08:51:51 -05:00
|
|
|
void ImageReader::SetJson(const std::string value) {
|
2013-12-07 21:09:55 -06:00
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
|
|
|
|
// Set all values that match
|
|
|
|
|
SetJsonValue(root);
|
|
|
|
|
}
|
|
|
|
|
catch (const std::exception& e)
|
|
|
|
|
{
|
|
|
|
|
throw InvalidJSON(
|
|
|
|
|
"JSON is invalid (missing keys or invalid data types)");
|
|
|
|
|
}
|
2013-12-07 21:09:55 -06:00
|
|
|
}
|
|
|
|
|
|
2019-12-27 08:51:51 -05:00
|
|
|
// Load Json::Value into this object
|
|
|
|
|
void ImageReader::SetJsonValue(const Json::Value root) {
|
2013-12-07 16:52:09 -06:00
|
|
|
|
|
|
|
|
// Set parent data
|
2013-12-07 21:09:55 -06:00
|
|
|
ReaderBase::SetJsonValue(root);
|
2013-12-07 16:52:09 -06:00
|
|
|
|
|
|
|
|
// Set data from Json (if key is found)
|
2014-01-08 01:43:58 -06:00
|
|
|
if (!root["path"].isNull())
|
2013-12-07 16:52:09 -06:00
|
|
|
path = root["path"].asString();
|
|
|
|
|
|
2021-11-02 09:12:23 -04:00
|
|
|
if (is_open) {
|
2013-12-07 21:09:55 -06:00
|
|
|
Close();
|
|
|
|
|
Open();
|
|
|
|
|
}
|
2013-12-07 16:52:09 -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
|