2 Copyright (c) 2007 Paolo Capriotti <p.capriotti@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
10 #include "renderthread.h"
15 #include <KSvgRenderer>
17 RenderThread::RenderThread()
26 RenderThread::~RenderThread()
30 QMutexLocker
lock(&m_mutex
);
32 m_condition
.wakeOne();
38 void RenderThread::setSize(const QSize
& size
)
40 QMutexLocker
lock(&m_mutex
);
44 void RenderThread::setRatio(float ratio
)
46 QMutexLocker
lock(&m_mutex
);
50 int RenderThread::render(const QString
&file
,
52 Background::ResizeMethod method
,
53 Qt::TransformationMode mode
)
57 QMutexLocker
lock(&m_mutex
);
63 token
= ++m_current_token
;
70 m_condition
.wakeOne();
76 void RenderThread::run()
82 Background::ResizeMethod method
;
83 Qt::TransformationMode mode
;
88 QMutexLocker
lock(&m_mutex
);
90 while (!m_restart
&& !m_abort
) {
91 m_condition
.wait(&m_mutex
);
100 // load all parameters in nonshared variables
101 token
= m_current_token
;
110 QImage
result(size
, QImage::Format_ARGB32_Premultiplied
);
111 result
.fill(color
.rgba());
113 if (file
.isEmpty() || !QFile::exists(file
)) {
114 emit
done(token
, result
);
120 bool scalable
= file
.endsWith("svg") || file
.endsWith("svgz");
127 // scalable: image can be of any size
130 // otherwise, use the natural size of the loaded image
132 imgSize
= img
.size();
133 kDebug() << "loaded with" << imgSize
<< ratio
;
136 // if any of them is zero we may run into a div-by-zero below.
137 if (imgSize
.width() < 1) {
141 if (imgSize
.height() < 1) {
142 imgSize
.setHeight(1);
145 // set render parameters according to resize mode
148 case Background::Scale
:
152 case Background::Center
:
153 scaledSize
= imgSize
;
154 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
155 (size
.height() - scaledSize
.height()) / 2);
157 //If the picture is bigger than the screen, shrink it
158 if (size
.width() < imgSize
.width() && imgSize
.width() > imgSize
.height()) {
159 int width
= size
.width();
160 int height
= width
* scaledSize
.height() / imgSize
.width();
161 scaledSize
= QSize(width
, height
);
162 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
163 (size
.height() - scaledSize
.height()) / 2);
164 } else if (size
.height() < imgSize
.height()) {
165 int height
= size
.height();
166 int width
= height
* imgSize
.width() / imgSize
.height();
167 scaledSize
= QSize(width
, height
);
168 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
169 (size
.height() - scaledSize
.height()) / 2);
173 case Background::Maxpect
: {
175 float xratio
= (float) size
.width() / imgSize
.width();
176 float yratio
= (float) size
.height() / imgSize
.height();
177 if (xratio
> yratio
) {
178 int height
= size
.height();
179 int width
= height
* imgSize
.width() / imgSize
.height();
180 scaledSize
= QSize(width
, height
);
182 int width
= size
.width();
183 int height
= width
* imgSize
.height() / imgSize
.width();
184 scaledSize
= QSize(width
, height
);
186 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
187 (size
.height() - scaledSize
.height()) / 2);
190 case Background::ScaleCrop
: {
192 float xratio
= (float) size
.width() / imgSize
.width();
193 float yratio
= (float) size
.height() / imgSize
.height();
194 if (xratio
> yratio
) {
195 int width
= size
.width();
196 int height
= width
* imgSize
.height() / imgSize
.width();
197 scaledSize
= QSize(width
, height
);
199 int height
= size
.height();
200 int width
= height
* imgSize
.width() / imgSize
.height();
201 scaledSize
= QSize(width
, height
);
203 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
204 (size
.height() - scaledSize
.height()) / 2);
207 case Background::Tiled
:
208 scaledSize
= imgSize
;
211 case Background::CenterTiled
:
212 scaledSize
= imgSize
;
214 -scaledSize
.width() +
215 ((size
.width() - scaledSize
.width()) / 2) % scaledSize
.width(),
216 -scaledSize
.height() +
217 ((size
.height() - scaledSize
.height()) / 2) % scaledSize
.height());
223 kDebug() << token
<< scalable
<< scaledSize
<< imgSize
;
225 // tiling is ignored for scalable wallpapers
226 KSvgRenderer
svg(file
);
232 if (scaledSize
!= imgSize
) {
233 img
= img
.scaled(scaledSize
, Qt::IgnoreAspectRatio
, mode
);
241 for (int x
= pos
.x(); x
< size
.width(); x
+= scaledSize
.width()) {
242 for (int y
= pos
.y(); y
< size
.height(); y
+= scaledSize
.height()) {
243 p
.drawImage(QPoint(x
, y
), img
);
250 p
.drawImage(pos
, img
);
255 emit
done(token
, result
);