Move ImageMagick code out of Frame class

This commit is contained in:
FeRD (Frank Dana)
2021-11-02 09:12:23 -04:00
parent f7cebb7f22
commit 5da5adc3f8
12 changed files with 246 additions and 233 deletions

View File

@@ -13,6 +13,9 @@
// Require ImageMagick support
#ifdef USE_IMAGEMAGICK
#include "MagickUtilities.h"
#include "QtUtilities.h"
#include "ImageReader.h"
#include "Exceptions.h"
#include "Frame.h"
@@ -57,39 +60,31 @@ void ImageReader::Open()
info.vcodec = image->format();
info.width = image->size().width();
info.height = image->size().height();
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.pixel_ratio = openshot::Fraction(1, 1);
info.duration = 60 * 60 * 1; // 1 hour duration
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());
info.fps = openshot::Fraction(30, 1);
info.video_timebase = info.fps.Reciprocal();
info.video_length = std::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);
Fraction dar(
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;
// Reduce DAR fraction & set ratio
dar.Reduce();
info.display_ratio = dar;
// Mark as "open"
is_open = true;
}
}
// Close image file
void ImageReader::Close()
{
// Close all objects, if reader is 'open'
if (is_open)
{
// Mark as "closed"
is_open = false;
// Delete the image
image.reset();
}
@@ -98,19 +93,21 @@ void ImageReader::Close()
// Get an openshot::Frame object for a specific frame number of this reader.
std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
{
// Check for open reader (or throw exception)
if (!is_open)
throw ReaderClosed("The FFmpegReader is closed. Call Open() before calling this method.", path);
if (!is_open) {
throw ReaderClosed(
"The ImageReader is closed. "
"Call Open() before calling this method.", path);
}
// Create or get frame object
auto image_frame = std::make_shared<Frame>(
requested_frame, image->size().width(), image->size().height(),
requested_frame,
image->size().width(), image->size().height(),
"#000000", 0, 2);
// Add Image data to frame
image_frame->AddMagickImage(image);
// return frame object
auto qimage = openshot::Magick2QImage(image);
image_frame->AddImage(qimage);
return image_frame;
}
@@ -124,30 +121,29 @@ std::string ImageReader::Json() const {
// Generate Json::Value for this object
Json::Value ImageReader::JsonValue() const {
// Create root json object
Json::Value root = ReaderBase::JsonValue(); // get parent properties
// get parent properties
Json::Value root = ReaderBase::JsonValue();
root["type"] = "ImageReader";
root["path"] = path;
// return JsonValue
return root;
}
// Load JSON string into this object
void ImageReader::SetJson(const std::string value) {
// 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)
{
// Error parsing JSON (or missing keys)
throw InvalidJSON("JSON is invalid (missing keys or invalid data types)");
}
// 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)");
}
}
// Load Json::Value into this object
@@ -160,9 +156,7 @@ void ImageReader::SetJsonValue(const Json::Value root) {
if (!root["path"].isNull())
path = root["path"].asString();
// Re-Open path, and re-init everything (if needed)
if (is_open)
{
if (is_open) {
Close();
Open();
}