Loading...
Searching...
No Matches
ImageReader.cpp
Go to the documentation of this file.
// Copyright (c) 2008-2019 OpenShot Studios, LLC
//
// SPDX-License-Identifier: LGPL-3.0-or-later
// Require ImageMagick support
#ifdef USE_IMAGEMAGICK
#include "MagickUtilities.h"
#include "QtUtilities.h"
#include "ImageReader.h"
#include "Exceptions.h"
#include "Frame.h"
using namespace openshot;
ImageReader::ImageReader(const std::string& path, bool inspect_reader) : path(path), is_open(false)
{
// Open and Close the reader, to populate its attributes (such as height, width, etc...)
if (inspect_reader) {
Open();
Close();
}
}
// Open image file
void ImageReader::Open()
{
// Open reader if not already open
if (!is_open)
{
// Attempt to open file
try
{
// load image
image = std::make_shared<Magick::Image>(path);
// Give image a transparent background color
image->backgroundColor(Magick::Color("none"));
MAGICK_IMAGE_ALPHA(image, true);
}
catch (const Magick::Exception& e) {
// raise exception
throw InvalidFile("File could not be opened.", path);
}
// Update image properties
info.has_audio = false;
info.has_video = true;
info.has_single_image = true;
info.file_size = image->fileSize();
info.vcodec = image->format();
info.width = image->size().width();
info.height = image->size().height();
info.pixel_ratio = openshot::Fraction(1, 1);
info.duration = 60 * 60 * 1; // 1 hour duration
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 dar(
info.width * info.pixel_ratio.num,
info.height * info.pixel_ratio.den);
// Reduce DAR fraction & set ratio
dar.Reduce();
info.display_ratio = dar;
// Mark as "open"
is_open = true;
}
}
void ImageReader::Close()
{
if (is_open)
{
is_open = false;
// Delete the image
image.reset();
}
}
// Get an openshot::Frame object for a specific frame number of this reader.
std::shared_ptr<Frame> ImageReader::GetFrame(int64_t requested_frame)
{
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(),
"#000000", 0, 2);
// Add Image data to frame
auto qimage = openshot::Magick2QImage(image);
image_frame->AddImage(qimage);
return image_frame;
}
// Generate JSON string of this object
std::string ImageReader::Json() const {
// Return formatted string
return JsonValue().toStyledString();
}
// Generate Json::Value for this object
Json::Value ImageReader::JsonValue() const {
// get parent properties
Json::Value root = ReaderBase::JsonValue();
root["type"] = "ImageReader";
root["path"] = path;
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)
{
throw InvalidJSON(
"JSON is invalid (missing keys or invalid data types)");
}
}
// Load Json::Value into this object
void ImageReader::SetJsonValue(const Json::Value root) {
// Set parent data
ReaderBase::SetJsonValue(root);
// Set data from Json (if key is found)
if (!root["path"].isNull())
path = root["path"].asString();
if (is_open) {
Close();
Open();
}
}
#endif //USE_IMAGEMAGICK
Exceptions.h
Header file for all Exception classes.
path
path
Definition FFmpegWriter.cpp:1515
Frame.h
Header file for Frame class.
ImageReader.h
Header file for ImageReader class.
MagickUtilities.h
Header file for MagickUtilities (IM6/IM7 compatibility overlay)
MAGICK_IMAGE_ALPHA
#define MAGICK_IMAGE_ALPHA(im, a)
Definition MagickUtilities.h:44
QtUtilities.h
Header file for QtUtilities (compatibiity overlay)
openshot::Fraction
This class represents a fraction.
Definition Fraction.h:30
openshot::Fraction::num
int num
Numerator for the fraction.
Definition Fraction.h:32
openshot::Fraction::ToDouble
double ToDouble() const
Return this fraction as a double (i.e. 1/2 = 0.5)
Definition Fraction.cpp:40
openshot::Fraction::Reduce
void Reduce()
Reduce this fraction (i.e. 640/480 = 4/3)
Definition Fraction.cpp:65
openshot::Fraction::Reciprocal
Fraction Reciprocal() const
Return the reciprocal as a Fraction.
Definition Fraction.cpp:78
openshot::Fraction::den
int den
Denominator for the fraction.
Definition Fraction.h:33
openshot::ImageReader::Open
void Open() override
Open File - which is called by the constructor automatically.
Definition ImageReader.cpp:35
openshot::ImageReader::GetFrame
std::shared_ptr< Frame > GetFrame(int64_t requested_frame) override
Definition ImageReader.cpp:94
openshot::ImageReader::ImageReader
ImageReader(const std::string &path, bool inspect_reader=true)
Constructor for ImageReader.
Definition ImageReader.cpp:25
openshot::ImageReader::SetJson
void SetJson(const std::string value) override
Load JSON string into this object.
Definition ImageReader.cpp:133
openshot::ImageReader::SetJsonValue
void SetJsonValue(const Json::Value root) override
Load Json::Value into this object.
Definition ImageReader.cpp:150
openshot::ImageReader::JsonValue
Json::Value JsonValue() const override
Generate Json::Value for this object.
Definition ImageReader.cpp:122
openshot::ImageReader::Json
std::string Json() const override
Generate JSON string of this object.
Definition ImageReader.cpp:115
openshot::ImageReader::Close
void Close() override
Close File.
Definition ImageReader.cpp:83
openshot::InvalidFile
Exception for files that can not be found or opened.
Definition Exceptions.h:188
openshot::InvalidJSON
Exception for invalid JSON.
Definition Exceptions.h:218
openshot::ReaderBase::info
openshot::ReaderInfo info
Information about the current media file.
Definition ReaderBase.h:88
openshot::ReaderBase::SetJsonValue
virtual void SetJsonValue(const Json::Value root)=0
Load Json::Value into this object.
Definition ReaderBase.cpp:162
openshot::ReaderBase::JsonValue
virtual Json::Value JsonValue() const =0
Generate Json::Value for this object.
Definition ReaderBase.cpp:107
openshot::ReaderClosed
Exception when a reader is closed, and a frame is requested.
Definition Exceptions.h:364
openshot
This namespace is the default namespace for all code in the openshot library.
Definition Compressor.h:29
openshot::Magick2QImage
std::shared_ptr< QImage > Magick2QImage(std::shared_ptr< Magick::Image >)
Convert Magick::Image to QImage.
Definition MagickUtilities.cpp:46
openshot::stringToJson
const Json::Value stringToJson(const std::string value)
Definition Json.cpp:16
openshot::ReaderInfo::has_single_image
bool has_single_image
Determines if this file only contains a single image.
Definition ReaderBase.h:42
openshot::ReaderInfo::duration
float duration
Length of time (in seconds)
Definition ReaderBase.h:43
openshot::ReaderInfo::width
int width
The width of the video (in pixesl)
Definition ReaderBase.h:46
openshot::ReaderInfo::fps
openshot::Fraction fps
Frames per second, as a fraction (i.e. 24/1 = 24 fps)
Definition ReaderBase.h:48
openshot::ReaderInfo::display_ratio
openshot::Fraction display_ratio
The ratio of width to height of the video stream (i.e. 640x480 has a ratio of 4/3)
Definition ReaderBase.h:51
openshot::ReaderInfo::height
int height
The height of the video (in pixels)
Definition ReaderBase.h:45
openshot::ReaderInfo::video_length
int64_t video_length
The number of frames in the video stream.
Definition ReaderBase.h:53
openshot::ReaderInfo::vcodec
std::string vcodec
The name of the video codec used to encode / decode the video stream.
Definition ReaderBase.h:52
openshot::ReaderInfo::pixel_ratio
openshot::Fraction pixel_ratio
The pixel ratio of the video stream as a fraction (i.e. some pixels are not square)
Definition ReaderBase.h:50
openshot::ReaderInfo::has_video
bool has_video
Determines if this file has a video stream.
Definition ReaderBase.h:40
openshot::ReaderInfo::has_audio
bool has_audio
Determines if this file has an audio stream.
Definition ReaderBase.h:41
openshot::ReaderInfo::video_timebase
openshot::Fraction video_timebase
The video timebase determines how long each frame stays on the screen.
Definition ReaderBase.h:55
openshot::ReaderInfo::file_size
int64_t file_size
Size of file (in bytes)
Definition ReaderBase.h:44