2013-09-12 17:52:10 -05:00
|
|
|
/**
|
|
|
|
|
* @file
|
|
|
|
|
* @brief Source file for ImageReader class
|
|
|
|
|
* @author Jonathan Thomas <jonathan@openshot.org>
|
|
|
|
|
*
|
|
|
|
|
* @section LICENSE
|
|
|
|
|
*
|
|
|
|
|
* Copyright (c) 2008-2013 OpenShot Studios, LLC
|
|
|
|
|
* (http://www.openshotstudios.com). This file is part of
|
|
|
|
|
* OpenShot Library (http://www.openshot.org), an open-source project
|
|
|
|
|
* dedicated to delivering high quality video editing and animation solutions
|
|
|
|
|
* to the world.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library is free software: you can redistribute it and/or modify
|
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
|
* (at your option) any later version.
|
|
|
|
|
*
|
|
|
|
|
* OpenShot Library 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
|
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
|
*
|
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
|
* along with OpenShot Library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
*/
|
|
|
|
|
|
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
|
|
|
|
|
try
|
|
|
|
|
{
|
|
|
|
|
// load image
|
2012-11-08 04:35:21 -06:00
|
|
|
image = tr1::shared_ptr<Magick::Image>(new Magick::Image(path));
|
|
|
|
|
|
|
|
|
|
// Give image a transparent background color
|
|
|
|
|
image->backgroundColor(Magick::Color("none"));
|
2013-10-14 18:18:34 -05:00
|
|
|
image->matte(true);
|
2012-10-09 01:45:34 -05:00
|
|
|
}
|
|
|
|
|
catch (Magick::Exception e) {
|
|
|
|
|
// raise exception
|
|
|
|
|
throw InvalidFile("File could not be opened.", path);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Update image properties
|
|
|
|
|
info.has_audio = false;
|
|
|
|
|
info.has_video = 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;
|
|
|
|
|
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
|
|
|
{
|
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
|
|
|
|
|
tr1::shared_ptr<Frame> image_frame(new Frame(requested_frame, image->size().width(), image->size().height(), "#000000", 0, 2));
|
|
|
|
|
image_frame->SetSampleRate(44100);
|
2012-11-08 04:35:21 -06:00
|
|
|
|
2013-10-18 12:38:09 -05:00
|
|
|
// Add Image data to frame
|
|
|
|
|
tr1::shared_ptr<Magick::Image> copy_image(new Magick::Image(*image.get()));
|
|
|
|
|
copy_image->modifyImage(); // actually copy the image data to this object
|
|
|
|
|
image_frame->AddImage(copy_image);
|
|
|
|
|
|
|
|
|
|
// return frame object
|
|
|
|
|
return image_frame;
|
2012-08-29 15:29:15 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|