Skip to content

Commit

Permalink
API: Add GetImageFile, allow any artwork to be resized.
Browse files Browse the repository at this point in the history
This adds a special GetImageFile method, and makes the existing means of getting recording and video artwork use it.

You can now request a fanart, banner, coverart, or screenshot for a given recording or video and specify a width, height, or both (better to specify one to preserve aspect ratio).  A new image is created in the appropriate storage group, where it remains cached (not unlike screenshots for mythweb).
  • Loading branch information
Robert McNamara committed Jul 25, 2011
1 parent 2a14f6f commit 1f15ac4
Show file tree
Hide file tree
Showing 3 changed files with 136 additions and 22 deletions.
19 changes: 12 additions & 7 deletions mythtv/libs/libmythservicecontracts/services/contentServices.h
Expand Up @@ -48,8 +48,8 @@
class SERVICE_PUBLIC ContentServices : public Service //, public QScriptable ???
{
Q_OBJECT
Q_CLASSINFO( "version" , "1.1" );
Q_CLASSINFO( "DownloadFile_Method", "POST" )
Q_CLASSINFO( "version" , "1.2" );
Q_CLASSINFO( "DownloadFile_Method", "POST" )

public:

Expand All @@ -65,19 +65,24 @@ class SERVICE_PUBLIC ContentServices : public Service //, public QScriptable ??
virtual QFileInfo GetFile ( const QString &StorageGroup,
const QString &FileName ) = 0;

virtual QFileInfo GetImageFile ( const QString &StorageGroup,
const QString &FileName,
int Width, int Height ) = 0;

virtual QStringList GetFileList ( const QString &StorageGroup ) = 0;

virtual QFileInfo GetRecordingArtwork ( const QString &Type,
const QString &Inetref,
int Season ) = 0;
int Season, int Width,
int Height ) = 0;

virtual QFileInfo GetVideoCoverart ( int Id ) = 0;
virtual QFileInfo GetVideoCoverart ( int Id, int Width, int Height ) = 0;

virtual QFileInfo GetVideoFanart ( int Id ) = 0;
virtual QFileInfo GetVideoFanart ( int Id, int Width, int Height ) = 0;

virtual QFileInfo GetVideoBanner ( int Id ) = 0;
virtual QFileInfo GetVideoBanner ( int Id, int Width, int Height ) = 0;

virtual QFileInfo GetVideoScreenshot ( int Id ) = 0;
virtual QFileInfo GetVideoScreenshot ( int Id, int Width, int Height ) = 0;

virtual QFileInfo GetAlbumArt ( int Id, int Width, int Height ) = 0;

Expand Down
124 changes: 114 additions & 10 deletions mythtv/programs/mythbackend/services/content.cpp
Expand Up @@ -96,6 +96,108 @@ QFileInfo Content::GetFile( const QString &sStorageGroup,
//
/////////////////////////////////////////////////////////////////////////////

QFileInfo Content::GetImageFile( const QString &sStorageGroup,
const QString &sFileName,
int nWidth,
int nHeight)
{
QString sGroup = sStorageGroup;

if (sGroup.isEmpty())
{
LOG(VB_UPNP, LOG_WARNING,
"GetFile - StorageGroup missing... using 'Default'");
sGroup = "Default";
}

if (sFileName.isEmpty())
{
QString sMsg ( "GetFile - FileName missing." );

LOG(VB_UPNP, LOG_ERR, sMsg);

throw sMsg;
}

// ------------------------------------------------------------------
// Search for the filename
// ------------------------------------------------------------------

StorageGroup storage( sGroup );
QString sFullFileName = storage.FindFile( sFileName );

if (sFullFileName.isEmpty())
{
LOG(VB_UPNP, LOG_ERR,
QString("GetImageFile - Unable to find %1.").arg(sFileName));

return QFileInfo();
}

// ----------------------------------------------------------------------
// check to see if the file (still) exists
// ----------------------------------------------------------------------

if ((nWidth == 0) && (nHeight == 0))
{
if (QFile::exists( sFullFileName ))
{
return QFileInfo( sFullFileName );
}

LOG(VB_UPNP, LOG_ERR,
QString("GetImageFile - File Does not exist %1.").arg(sFullFileName));

return QFileInfo();
}

QString sNewFileName = QString( "%1.%2x%3.png" )
.arg( sFullFileName )
.arg( nWidth )
.arg( nHeight );

// ----------------------------------------------------------------------
// check to see if image is already created.
// ----------------------------------------------------------------------

if (QFile::exists( sNewFileName ))
return QFileInfo( sNewFileName );

// ----------------------------------------------------------------------
// Must generate Generate Image and save.
// ----------------------------------------------------------------------

float fAspect = 0.0;

QImage *pImage = new QImage( sFullFileName);

if (!pImage)
return QFileInfo();

if (fAspect <= 0)
fAspect = (float)(pImage->width()) / pImage->height();

if ( nWidth == 0 )
nWidth = (int)rint(nHeight * fAspect);

if ( nHeight == 0 )
nHeight = (int)rint(nWidth / fAspect);

QImage img = pImage->scaled( nWidth, nHeight, Qt::IgnoreAspectRatio,
Qt::SmoothTransformation);

QByteArray fname = sNewFileName.toAscii();
img.save( fname.constData(), "PNG" );

delete pImage;

return QFileInfo( sNewFileName );
}

/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////

QStringList Content::GetFileList( const QString &sStorageGroup )
{

Expand All @@ -118,7 +220,9 @@ QStringList Content::GetFileList( const QString &sStorageGroup )

QFileInfo Content::GetRecordingArtwork ( const QString &sType,
const QString &sInetref,
int nSeason )
int nSeason,
int nWidth,
int nHeight)
{
ArtworkMap map = GetArtwork(sInetref, nSeason);

Expand All @@ -144,14 +248,14 @@ QFileInfo Content::GetRecordingArtwork ( const QString &sType,
QUrl url(map.value(type).url);
QString sFileName = url.path();

return GetFile( sgroup, sFileName );
return GetImageFile( sgroup, sFileName, nWidth, nHeight);
}

/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////

QFileInfo Content::GetVideoCoverart( int nId )
QFileInfo Content::GetVideoCoverart( int nId, int nWidth, int nHeight )
{
LOG(VB_UPNP, LOG_INFO, QString("GetVideoCoverart ID = %1").arg(nId));

Expand Down Expand Up @@ -183,14 +287,14 @@ QFileInfo Content::GetVideoCoverart( int nId )
// Not there? Perhaps we need to look in a storage group?
// ----------------------------------------------------------------------

return GetFile( "Coverart", sFileName );
return GetImageFile( "Coverart", sFileName, nWidth, nHeight );
}

/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////

QFileInfo Content::GetVideoFanart( int nId )
QFileInfo Content::GetVideoFanart( int nId, int nWidth, int nHeight )
{
LOG(VB_UPNP, LOG_INFO, QString("GetVideoFanart ID = %1").arg(nId));

Expand Down Expand Up @@ -222,14 +326,14 @@ QFileInfo Content::GetVideoFanart( int nId )
// Not there? Perhaps we need to look in a storage group?
// ----------------------------------------------------------------------

return GetFile( "Fanart", sFileName );
return GetImageFile( "Fanart", sFileName, nWidth, nHeight );
}

/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////

QFileInfo Content::GetVideoBanner( int nId )
QFileInfo Content::GetVideoBanner( int nId, int nWidth, int nHeight )
{
LOG(VB_UPNP, LOG_INFO, QString("GetVideoBanner ID = %1").arg(nId));

Expand Down Expand Up @@ -261,14 +365,14 @@ QFileInfo Content::GetVideoBanner( int nId )
// Not there? Perhaps we need to look in a storage group?
// ----------------------------------------------------------------------

return GetFile( "Banner", sFileName );
return GetImageFile( "Banner", sFileName, nWidth, nHeight );
}

/////////////////////////////////////////////////////////////////////////////
//
/////////////////////////////////////////////////////////////////////////////

QFileInfo Content::GetVideoScreenshot( int nId )
QFileInfo Content::GetVideoScreenshot( int nId, int nWidth, int nHeight )
{
LOG(VB_UPNP, LOG_INFO, QString("GetVideoScreenshot ID = %1").arg(nId));

Expand Down Expand Up @@ -300,7 +404,7 @@ QFileInfo Content::GetVideoScreenshot( int nId )
// Not there? Perhaps we need to look in a storage group?
// ----------------------------------------------------------------------

return GetFile( "Screenshot", sFileName );
return GetImageFile( "Screenshot", sFileName, nWidth, nHeight );
}

/////////////////////////////////////////////////////////////////////////////
Expand Down
15 changes: 10 additions & 5 deletions mythtv/programs/mythbackend/services/content.h
Expand Up @@ -39,19 +39,24 @@ class Content : public ContentServices
QFileInfo GetFile ( const QString &StorageGroup,
const QString &FileName );

QFileInfo GetImageFile ( const QString &StorageGroup,
const QString &FileName,
int Width, int Height );

QStringList GetFileList ( const QString &StorageGroup );

QFileInfo GetRecordingArtwork ( const QString &Type,
const QString &Inetref,
int Season );
int Season, int Width,
int Height);

QFileInfo GetVideoCoverart ( int Id );
QFileInfo GetVideoCoverart ( int Id, int Width, int Height );

QFileInfo GetVideoFanart ( int Id );
QFileInfo GetVideoFanart ( int Id, int Width, int Height );

QFileInfo GetVideoBanner ( int Id );
QFileInfo GetVideoBanner ( int Id, int Width, int Height );

QFileInfo GetVideoScreenshot ( int Id );
QFileInfo GetVideoScreenshot ( int Id, int Width, int Height );

QFileInfo GetAlbumArt ( int Id, int Width, int Height );

Expand Down

0 comments on commit 1f15ac4

Please sign in to comment.