2012-08-29 15:29:15 -05:00
|
|
|
#include "../include/ImageReader.h"
|
|
|
|
|
|
|
|
|
|
using namespace openshot;
|
|
|
|
|
|
2012-10-10 01:07:47 -05:00
|
|
|
ImageReader::ImageReader(string path) throw(InvalidFile) : path(path), is_open(false)
|
2012-08-29 15:29:15 -05:00
|
|
|
{
|
|
|
|
|
// Init FileInfo struct (clear all values)
|
|
|
|
|
InitFileInfo();
|
2012-10-09 10:41:07 -05:00
|
|
|
|
|
|
|
|
// Open and Close the reader, to populate it's attributes (such as height, width, etc...)
|
|
|
|
|
Open();
|
|
|
|
|
Close();
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Open image file
|
2012-10-09 01:45:34 -05:00
|
|
|
void ImageReader::Open() throw(InvalidFile)
|
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
|
|
|
|
|
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
|
2012-10-14 03:43:52 -05:00
|
|
|
image_frame = tr1::shared_ptr<Frame>(new Frame(1, source->size().width(), source->size().height(), "#000000", 0, 2));
|
2012-10-09 01:45:34 -05:00
|
|
|
|
|
|
|
|
// 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;
|
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;
|
|
|
|
|
}
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Get an openshot::Frame object for a specific frame number of this reader.
|
2012-10-14 03:43:52 -05:00
|
|
|
tr1::shared_ptr<Frame> ImageReader::GetFrame(int requested_frame) throw(ReaderClosed)
|
2012-08-29 15:29:15 -05:00
|
|
|
{
|
2012-10-09 01:45:34 -05:00
|
|
|
// Check for open reader (or throw exception)
|
|
|
|
|
if (!is_open)
|
|
|
|
|
throw ReaderClosed("The ImageReader is closed. Call Open() before calling this method.", path);
|
|
|
|
|
|
2012-08-29 15:29:15 -05:00
|
|
|
if (image_frame)
|
2012-10-16 16:45:31 -05:00
|
|
|
{
|
2012-08-29 15:29:15 -05:00
|
|
|
// Always return same frame (regardless of which frame number was requested)
|
2012-10-16 16:45:31 -05:00
|
|
|
image_frame->number = requested_frame;
|
2012-08-29 15:29:15 -05:00
|
|
|
return image_frame;
|
2012-10-16 16:45:31 -05:00
|
|
|
}
|
2012-08-29 15:29:15 -05:00
|
|
|
else
|
|
|
|
|
// no frame loaded
|
|
|
|
|
throw InvalidFile("No frame could be created from this type of file.", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|