File indexing completed on 2025-01-05 04:25:24

0001 /* PhotoFlow - animated image viewer for mobile devices
0002  *
0003  * Copyright (C) 2008 Ariya Hidayat (ariya.hidayat@gmail.com)
0004  * Copyright (C) 2007 Ariya Hidayat (ariya.hidayat@gmail.com)
0005  *
0006  * This program is free software; you can redistribute it and/or
0007  * modify it under the terms of the GNU General Public License
0008  * as published by the Free Software Foundation; either
0009  * version 2 of the License, or (at your option) any later version.
0010  *
0011  * This library is distributed in the hope that it will be useful,
0012  * but WITHOUT ANY WARRANTY; without even the implied warranty of
0013  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
0014  * General Public License for more details.
0015  *
0016  * You should have received a copy of the GNU General Public License
0017  * along with this library; see the file COPYING.  If not, write to
0018  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
0019  * Boston, MA 02110-1301, USA
0020  *
0021  */
0022 
0023 #include "ImageLoader.h"
0024 
0025 #include "core/meta/Meta.h"
0026 
0027 #include <qimage.h>
0028 #include <QPixmap>
0029 
0030 #include <KStandardDirs>
0031 // load and resize image
0032 
0033 ImageLoader::ImageLoader(): QThread(),
0034         restart( false ), working( false ), idx( -1 )
0035 {
0036 }
0037 
0038 ImageLoader::~ImageLoader()
0039 {
0040     mutex.lock();
0041     condition.wakeOne();
0042     mutex.unlock();
0043     wait();
0044 }
0045 
0046 bool ImageLoader::busy() const
0047 {
0048     return isRunning() ? working : false;
0049 }
0050 
0051 void ImageLoader::generate( int index, Meta::AlbumPtr iAlbum, QSize size )
0052 {
0053     mutex.lock();
0054     this->idx = index;
0055     this->m_album = iAlbum;
0056     this->size = size;
0057     mutex.unlock();
0058 
0059     if ( !isRunning() )
0060         start();
0061     else
0062     {
0063         // already running, wake up whenever ready
0064         restart = true;
0065         condition.wakeOne();
0066     }
0067 }
0068 
0069 void ImageLoader::run()
0070 {
0071     for ( ;; )
0072     {
0073         // copy necessary data
0074         mutex.lock();
0075         this->working = true;
0076         Meta::AlbumPtr album_ptr = this->m_album;
0077         QSize size = this->size;
0078         mutex.unlock();
0079 
0080         QImage image = PlainImageLoader::loadAndResize( album_ptr, size );
0081 
0082         // let everyone knows it is ready
0083         mutex.lock();
0084         this->working = false;
0085         this->img = image;
0086         mutex.unlock();
0087 
0088         // put to sleep
0089         mutex.lock();
0090         if ( !this->restart )
0091             condition.wait( &mutex );
0092         restart = false;
0093         mutex.unlock();
0094     }
0095 }
0096 
0097 PlainImageLoader::PlainImageLoader(): idx( -1 )
0098 {
0099 }
0100 
0101 void PlainImageLoader::generate( int index, Meta::AlbumPtr iAlbum, QSize size )
0102 {
0103     img = loadAndResize( iAlbum, size );
0104     idx = index;
0105 }
0106 QPixmap PlainImageLoader::GetPixmap(Meta::AlbumPtr iAlbum)
0107 {
0108     QPixmap pixmap;
0109     if ( iAlbum->hasImage() )
0110     {
0111         pixmap = QPixmap::fromImage(iAlbum->image());
0112     }
0113     else
0114     {
0115         pixmap = QPixmap( KStandardDirs::locate( "data", "amarok/images/blingdefaultcover.png" ) );
0116     }
0117     return pixmap;
0118 }
0119 QImage PlainImageLoader::loadAndResize( Meta::AlbumPtr iAlbum, QSize size )
0120 {
0121     //qDebug() <<  <<"ImageLoader::loadAndresize()";
0122     QImage image;
0123     QPixmap pixmap = GetPixmap(iAlbum);
0124     image = pixmap.toImage();
0125     image = image.scaled( size, Qt::KeepAspectRatio, Qt::SmoothTransformation );
0126     return image;
0127 }