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(const QSize
&size
, float ratio
)
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 int RenderThread::render(const QString
&file
,
46 Background::ResizeMethod method
,
47 Qt::TransformationMode mode
)
51 QMutexLocker
lock(&m_mutex
);
57 token
= ++m_current_token
;
64 m_condition
.wakeOne();
70 void RenderThread::run()
76 Background::ResizeMethod method
;
77 Qt::TransformationMode mode
;
82 QMutexLocker
lock(&m_mutex
);
83 while (!m_restart
&& !m_abort
) {
84 m_condition
.wait(&m_mutex
);
91 // load all parameters in nonshared variables
92 token
= m_current_token
;
101 QImage
result(size
, QImage::Format_ARGB32_Premultiplied
);
102 result
.fill(color
.rgb());
104 if (file
.isEmpty() || !QFile::exists(file
)) {
105 emit
done(token
, result
);
111 bool scalable
= file
.endsWith("svg") || file
.endsWith("svgz");
115 // load nonscalable image
123 // scalable: image can be of any size
127 // otherwise, use the natural size of the loaded image
128 imgSize
= img
.size();
132 // if any of them is zero we may run into a div-by-zero below.
133 if (imgSize
.width() == 0) {
136 if (imgSize
.height() == 0) {
137 imgSize
.setHeight(1);
140 // set render parameters according to resize mode
143 case Background::Scale
:
146 case Background::Center
:
147 scaledSize
= imgSize
;
148 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
149 (size
.height() - scaledSize
.height()) / 2);
151 case Background::ScaleCrop
: {
152 float xratio
= (float) size
.width() / imgSize
.width();
153 float yratio
= (float) size
.height() / imgSize
.height();
154 if (xratio
> yratio
) {
155 int width
= size
.width();
156 int height
= width
* imgSize
.height() / imgSize
.width();
157 scaledSize
= QSize(width
, height
);
160 int height
= size
.height();
161 int width
= height
* imgSize
.width() / imgSize
.height();
162 scaledSize
= QSize(width
, height
);
164 pos
= QPoint((size
.width() - scaledSize
.width()) / 2,
165 (size
.height() - scaledSize
.height()) / 2);
168 case Background::Tiled
:
169 scaledSize
= imgSize
;
172 case Background::CenterTiled
:
173 scaledSize
= imgSize
;
175 -scaledSize
.width() +
176 ((size
.width() - scaledSize
.width()) / 2) % scaledSize
.width(),
177 -scaledSize
.height() +
178 ((size
.height() - scaledSize
.height()) / 2) % scaledSize
.height());
185 // tiling is ignored for scalable wallpapers
186 KSvgRenderer
svg(file
);
193 QImage scaled
= img
.scaled(scaledSize
, Qt::IgnoreAspectRatio
, mode
);
198 for (int x
= pos
.x(); x
< size
.width(); x
+= scaledSize
.width()) {
199 for (int y
= pos
.y(); y
< size
.height(); y
+= scaledSize
.height()) {
200 p
.drawImage(QPoint(x
, y
), scaled
);
208 p
.drawImage(pos
, scaled
);
213 emit
done(token
, result
);