A big refactor of the Open() method on Readers, and the constructor of Readers. All resource consuming code has been moved into the Open() methods, so the Clips and Timeline can control them better.

This commit is contained in:
Jonathan Thomas
2012-10-09 01:45:34 -05:00
parent 87d12254ef
commit 1d89fd140a
11 changed files with 154 additions and 79 deletions

View File

@@ -2,77 +2,91 @@
using namespace openshot;
ImageReader::ImageReader(string path) throw(InvalidFile) : path(path)
ImageReader::ImageReader(string path) : path(path), is_open(false)
{
// Init FileInfo struct (clear all values)
InitFileInfo();
// Open the file (if possible)
Open();
// Get 1st frame
GetFrame(1);
}
// Open image file
void ImageReader::Open()
void ImageReader::Open() throw(InvalidFile)
{
// Attempt to open file
Magick::Image* source = NULL;
try
// Open reader if not already open
if (!is_open)
{
// load image
source = new Magick::Image(path);
// Attempt to open file
Magick::Image* source = NULL;
try
{
// load image
source = new Magick::Image(path);
}
catch (Magick::Exception e) {
// raise exception
throw InvalidFile("File could not be opened.", path);
}
// Create or get frame object
image_frame = new Frame(1, source->size().width(), source->size().height(), "#000000", 0, 2);
// Add Image data to frame
image_frame->AddImage(source);
// Update image properties
info.has_audio = false;
info.has_video = true;
info.file_size = source->fileSize();
info.vcodec = source->format();
info.width = source->size().width();
info.height = source->size().height();
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.duration = 60 * 60 * 24; // 24 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());
// 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;
// Get 1st frame
GetFrame(1);
}
catch (Magick::Exception e) {
// raise exception
throw InvalidFile("File could not be opened.", path);
}
// Create or get frame object
image_frame = new Frame(1, source->size().width(), source->size().height(), "#000000", 0, 2);
// Add Image data to frame
image_frame->AddImage(source);
// Update image properties
info.has_audio = false;
info.has_video = true;
info.file_size = source->fileSize();
info.vcodec = source->format();
info.width = source->size().width();
info.height = source->size().height();
info.pixel_ratio.num = 1;
info.pixel_ratio.den = 1;
info.duration = 60 * 60 * 24; // 24 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());
// 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;
}
// Close image file
void ImageReader::Close()
{
// Deallocate frame
delete image_frame;
// Close all objects, if reader is 'open'
if (is_open)
{
// Deallocate frame
delete image_frame;
// Mark as "closed"
is_open = false;
}
}
// Get an openshot::Frame object for a specific frame number of this reader.
Frame* ImageReader::GetFrame(int requested_frame) throw(InvalidFile)
Frame* ImageReader::GetFrame(int requested_frame) throw(ReaderClosed)
{
// Check for open reader (or throw exception)
if (!is_open)
throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", path);
if (image_frame)
// Always return same frame (regardless of which frame number was requested)
return image_frame;