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
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
/* LICENSE
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2019-06-11 06:48:32 -04:00
|
|
|
* Copyright (c) 2008-2019 OpenShot Studios, LLC
|
2014-03-29 18:49:22 -05:00
|
|
|
* <http://www.openshotstudios.com/>. This file is part of
|
|
|
|
|
* OpenShot Library (libopenshot), an open-source project dedicated to
|
|
|
|
|
* delivering high quality video editing and animation solutions to the
|
|
|
|
|
* world. For more information visit <http://www.openshot.org/>.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is free software: you can redistribute it
|
2014-07-11 16:52:14 -05:00
|
|
|
* and/or modify it under the terms of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* as published by the Free Software Foundation, either version 3 of the
|
|
|
|
|
* License, or (at your option) any later version.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-03-29 18:49:22 -05:00
|
|
|
* OpenShot Library (libopenshot) is distributed in the hope that it will be
|
|
|
|
|
* useful, but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2014-07-11 16:52:14 -05:00
|
|
|
* GNU Lesser General Public License for more details.
|
2013-09-12 17:52:10 -05:00
|
|
|
*
|
2014-07-11 16:52:14 -05:00
|
|
|
* You should have received a copy of the GNU Lesser General Public License
|
2014-03-29 18:49:22 -05:00
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
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
|
|
|
|
|
|
2020-10-18 07:43:37 -04:00
|
|
|
#include "ImageReader.h"
|
2021-01-26 10:52:04 -05:00
|
|
|
#include "Exceptions.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();
|
2012-10-09 01:45:34 -05:00
|
|
|
info.pixel_ratio.num = 1;
|
|
|
|
|
info.pixel_ratio.den = 1;
|
2019-10-25 13:42:57 -04:00
|
|
|
info.duration = 60 * 60 * 1; // 1 hour duration
|
2012-10-09 01:45:34 -05:00
|
|
|
info.fps.num = 30;
|
|
|
|
|
info.fps.den = 1;
|
|
|
|
|
info.video_timebase.num = 1;
|
|
|
|
|
info.video_timebase.den = 30;
|
|
|
|
|
info.video_length = round(info.duration * info.fps.ToDouble());
|
|
|
|
|
|
|
|
|
|
// Calculate the DAR (display aspect ratio)
|
|
|
|
|
Fraction size(info.width * info.pixel_ratio.num, info.height * info.pixel_ratio.den);
|
|
|
|
|
|
|
|
|
|
// Reduce size fraction
|
|
|
|
|
size.Reduce();
|
|
|
|
|
|
|
|
|
|
// Set the ratio based on the reduced fraction
|
|
|
|
|
info.display_ratio.num = size.num;
|
|
|
|
|
info.display_ratio.den = size.den;
|
|
|
|
|
|
|
|
|
|
// Mark as "open"
|
|
|
|
|
is_open = true;
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Close image file
|
|
|
|
|
void ImageReader::Close()
|
|
|
|
|
{
|
2012-10-09 01:45:34 -05:00
|
|
|
// Close all objects, if reader is 'open'
|
|
|
|
|
if (is_open)
|
|
|
|
|
{
|
|
|
|
|
// Mark as "closed"
|
|
|
|
|
is_open = false;
|
2019-03-14 09:26:56 -07:00
|
|
|
|
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
|
|
|
{
|
2013-10-18 12:38:09 -05:00
|
|
|
// Check for open reader (or throw exception)
|
|
|
|
|
if (!is_open)
|
|
|
|
|
throw ReaderClosed("The FFmpegReader 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>(
|
|
|
|
|
requested_frame, image->size().width(), image->size().height(),
|
|
|
|
|
"#000000", 0, 2);
|
2012-11-08 04:35:21 -06:00
|
|
|
|
2013-10-18 12:38:09 -05:00
|
|
|
// Add Image data to frame
|
2015-06-01 00:20:14 -07:00
|
|
|
image_frame->AddMagickImage(image);
|
2013-10-18 12:38:09 -05:00
|
|
|
|
|
|
|
|
// return frame object
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Create root json object
|
|
|
|
|
Json::Value root = ReaderBase::JsonValue(); // get parent properties
|
2013-12-07 21:09:55 -06:00
|
|
|
root["type"] = "ImageReader";
|
2013-12-07 16:52:09 -06:00
|
|
|
root["path"] = path;
|
|
|
|
|
|
|
|
|
|
// return JsonValue
|
|
|
|
|
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
|
|
|
|
|
|
|
|
// Parse JSON string into JSON objects
|
|
|
|
|
try
|
|
|
|
|
{
|
2019-12-27 08:51:51 -05:00
|
|
|
const Json::Value root = openshot::stringToJson(value);
|
2013-12-07 21:09:55 -06:00
|
|
|
// Set all values that match
|
|
|
|
|
SetJsonValue(root);
|
|
|
|
|
}
|
2019-07-03 12:58:02 -04:00
|
|
|
catch (const std::exception& e)
|
2013-12-07 21:09:55 -06:00
|
|
|
{
|
|
|
|
|
// Error parsing JSON (or missing keys)
|
2019-08-27 15:47:39 -04:00
|
|
|
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();
|
|
|
|
|
|
2013-12-07 21:09:55 -06:00
|
|
|
// Re-Open path, and re-init everything (if needed)
|
|
|
|
|
if (is_open)
|
|
|
|
|
{
|
|
|
|
|
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
|