| 1 | /* |
| 2 | |
| 3 | This file is part of the KDE project, module kdecore. |
| 4 | SPDX-FileCopyrightText: 2000 Geert Jansen <jansen@kde.org> |
| 5 | SPDX-FileCopyrightText: 2007 Daniel M. Duley <daniel.duley@verizon.net> |
| 6 | |
| 7 | with minor additions and based on ideas from |
| 8 | SPDX-FileContributor: Torsten Rahn <torsten@kde.org> |
| 9 | |
| 10 | SPDX-License-Identifier: LGPL-2.0-only |
| 11 | */ |
| 12 | |
| 13 | #include "kiconeffect.h" |
| 14 | #include "debug.h" |
| 15 | #include "kiconloader.h" |
| 16 | |
| 17 | #include <KColorScheme> |
| 18 | |
| 19 | #include <QDebug> |
| 20 | #include <QPalette> |
| 21 | #include <QSysInfo> |
| 22 | |
| 23 | #include <qplatformdefs.h> |
| 24 | |
| 25 | #include <math.h> |
| 26 | |
| 27 | class KIconEffectPrivate |
| 28 | { |
| 29 | public: |
| 30 | // http://en.cppreference.com/w/cpp/language/zero_initialization |
| 31 | KIconEffectPrivate() |
| 32 | : effect{{}} |
| 33 | , value{{}} |
| 34 | , trans{{}} |
| 35 | , key{{}} |
| 36 | { |
| 37 | } |
| 38 | |
| 39 | public: |
| 40 | int effect[KIconLoader::LastGroup][KIconLoader::LastState]; |
| 41 | float value[KIconLoader::LastGroup][KIconLoader::LastState]; |
| 42 | bool trans[KIconLoader::LastGroup][KIconLoader::LastState]; |
| 43 | QString key[KIconLoader::LastGroup][KIconLoader::LastState]; |
| 44 | }; |
| 45 | |
| 46 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 47 | KIconEffect::KIconEffect() |
| 48 | : d(new KIconEffectPrivate) |
| 49 | { |
| 50 | init(); |
| 51 | } |
| 52 | #endif |
| 53 | |
| 54 | KIconEffect::~KIconEffect() = default; |
| 55 | |
| 56 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 57 | void KIconEffect::init() |
| 58 | { |
| 59 | int i; |
| 60 | // FIXME: this really should be using KIconLoader::metaObject() to guarantee synchronization |
| 61 | // performance wise it's also practically guaranteed to be faster |
| 62 | QStringList groups; |
| 63 | groups += QStringLiteral("Desktop" ); |
| 64 | groups += QStringLiteral("Toolbar" ); |
| 65 | groups += QStringLiteral("MainToolbar" ); |
| 66 | groups += QStringLiteral("Small" ); |
| 67 | groups += QStringLiteral("Panel" ); |
| 68 | groups += QStringLiteral("Dialog" ); |
| 69 | |
| 70 | QStringList states; |
| 71 | states += QStringLiteral("Default" ); |
| 72 | states += QStringLiteral("Active" ); |
| 73 | states += QStringLiteral("Disabled" ); |
| 74 | |
| 75 | QStringList::ConstIterator it; |
| 76 | |
| 77 | for (it = groups.constBegin(), i = 0; it != groups.constEnd(); ++it, ++i) { |
| 78 | d->effect[i][KIconLoader::DefaultState] = NoEffect; |
| 79 | d->effect[i][KIconLoader::ActiveState] = ((i == KIconLoader::Desktop) || (KIconLoader::Panel == 4)) ? ToGamma : NoEffect; |
| 80 | d->effect[i][KIconLoader::DisabledState] = ToGray; |
| 81 | |
| 82 | d->trans[i][KIconLoader::DefaultState] = false; |
| 83 | d->trans[i][KIconLoader::ActiveState] = false; |
| 84 | d->trans[i][KIconLoader::DisabledState] = true; |
| 85 | d->value[i][KIconLoader::DefaultState] = 1.0; |
| 86 | d->value[i][KIconLoader::ActiveState] = ((i == KIconLoader::Desktop) || (KIconLoader::Panel == 4)) ? 0.7 : 1.0; |
| 87 | d->value[i][KIconLoader::DisabledState] = 1.0; |
| 88 | } |
| 89 | } |
| 90 | #endif |
| 91 | |
| 92 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 93 | bool KIconEffect::hasEffect(int group, int state) const |
| 94 | { |
| 95 | if (group < 0 || group >= KIconLoader::LastGroup // |
| 96 | || state < 0 || state >= KIconLoader::LastState) { |
| 97 | return false; |
| 98 | } |
| 99 | |
| 100 | return d->effect[group][state] != NoEffect; |
| 101 | } |
| 102 | #endif |
| 103 | |
| 104 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 105 | QString KIconEffect::fingerprint(int group, int state) const |
| 106 | { |
| 107 | if (group < 0 || group >= KIconLoader::LastGroup // |
| 108 | || state < 0 || state >= KIconLoader::LastState) { |
| 109 | return QString(); |
| 110 | } |
| 111 | |
| 112 | QString cached = d->key[group][state]; |
| 113 | if (cached.isEmpty()) { |
| 114 | QString tmp; |
| 115 | cached = tmp.setNum(n: d->effect[group][state]); |
| 116 | cached += QLatin1Char(':'); |
| 117 | cached += tmp.setNum(n: d->value[group][state]); |
| 118 | cached += QLatin1Char(':'); |
| 119 | cached += d->trans[group][state] ? QLatin1String("trans" ) : QLatin1String("notrans" ); |
| 120 | |
| 121 | d->key[group][state] = cached; |
| 122 | } |
| 123 | |
| 124 | return cached; |
| 125 | } |
| 126 | #endif |
| 127 | |
| 128 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 129 | QImage KIconEffect::apply(const QImage &image, int group, int state) const |
| 130 | { |
| 131 | if (state >= KIconLoader::LastState) { |
| 132 | qCWarning(KICONTHEMES) << "Invalid icon state:" << state << ", should be one of KIconLoader::States" ; |
| 133 | return image; |
| 134 | } |
| 135 | if (group >= KIconLoader::LastGroup) { |
| 136 | qCWarning(KICONTHEMES) << "Invalid icon group:" << group << ", should be one of KIconLoader::Group" ; |
| 137 | return image; |
| 138 | } |
| 139 | return apply(src: image, effect: d->effect[group][state], value: d->value[group][state], rgb: QColor(), rgb2: QColor(), trans: d->trans[group][state]); |
| 140 | } |
| 141 | #endif |
| 142 | |
| 143 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 144 | QImage KIconEffect::apply(const QImage &image, int effect, float value, const QColor &col, bool trans) const |
| 145 | { |
| 146 | return apply(src: image, effect, value, rgb: col, rgb2: KColorScheme(QPalette::Active, KColorScheme::View).background().color(), trans); |
| 147 | } |
| 148 | #endif |
| 149 | |
| 150 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 151 | QImage KIconEffect::apply(const QImage &img, int effect, float value, const QColor &col, const QColor &col2, bool trans) const |
| 152 | { |
| 153 | QImage image = img; |
| 154 | if (effect >= LastEffect) { |
| 155 | qCWarning(KICONTHEMES) << "Invalid icon effect:" << effect << ", should be one of KIconLoader::Effects" ; |
| 156 | return image; |
| 157 | } |
| 158 | if (value > 1.0) { |
| 159 | value = 1.0; |
| 160 | } else if (value < 0.0) { |
| 161 | value = 0.0; |
| 162 | } |
| 163 | switch (effect) { |
| 164 | case ToGray: |
| 165 | toGray(image, value); |
| 166 | break; |
| 167 | case DeSaturate: |
| 168 | deSaturate(image, value); |
| 169 | break; |
| 170 | case Colorize: |
| 171 | colorize(image, col, value); |
| 172 | break; |
| 173 | case ToGamma: |
| 174 | toGamma(image, value); |
| 175 | break; |
| 176 | case ToMonochrome: |
| 177 | toMonochrome(image, black: col, white: col2, value); |
| 178 | break; |
| 179 | } |
| 180 | if (trans == true) { |
| 181 | semiTransparent(image); |
| 182 | } |
| 183 | return image; |
| 184 | } |
| 185 | #endif |
| 186 | |
| 187 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 188 | QPixmap KIconEffect::apply(const QPixmap &pixmap, int group, int state) const |
| 189 | { |
| 190 | if (state >= KIconLoader::LastState) { |
| 191 | qCWarning(KICONTHEMES) << "Invalid icon state:" << state << ", should be one of KIconLoader::States" ; |
| 192 | return pixmap; |
| 193 | } |
| 194 | if (group >= KIconLoader::LastGroup) { |
| 195 | qCWarning(KICONTHEMES) << "Invalid icon group:" << group << ", should be one of KIconLoader::Group" ; |
| 196 | return pixmap; |
| 197 | } |
| 198 | return apply(src: pixmap, effect: d->effect[group][state], value: d->value[group][state], rgb: QColor(), rgb2: QColor(), trans: d->trans[group][state]); |
| 199 | } |
| 200 | #endif |
| 201 | |
| 202 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 203 | QPixmap KIconEffect::apply(const QPixmap &pixmap, int effect, float value, const QColor &col, bool trans) const |
| 204 | { |
| 205 | return apply(src: pixmap, effect, value, rgb: col, rgb2: KColorScheme(QPalette::Active, KColorScheme::View).background().color(), trans); |
| 206 | } |
| 207 | #endif |
| 208 | |
| 209 | #if KICONTHEMES_BUILD_DEPRECATED_SINCE(6, 5) |
| 210 | QPixmap KIconEffect::apply(const QPixmap &pixmap, int effect, float value, const QColor &col, const QColor &col2, bool trans) const |
| 211 | { |
| 212 | QPixmap result; |
| 213 | |
| 214 | if (effect >= LastEffect) { |
| 215 | qCWarning(KICONTHEMES) << "Invalid icon effect:" << effect << ", should be one of KIconLoader::Effects" ; |
| 216 | return result; |
| 217 | } |
| 218 | |
| 219 | if ((trans == true) && (effect == NoEffect)) { |
| 220 | result = pixmap; |
| 221 | semiTransparent(pixmap&: result); |
| 222 | } else if (effect != NoEffect) { |
| 223 | QImage tmpImg = pixmap.toImage(); |
| 224 | tmpImg = apply(img: tmpImg, effect, value, col, col2, trans); |
| 225 | result = QPixmap::fromImage(image: std::move(tmpImg)); |
| 226 | } else { |
| 227 | result = pixmap; |
| 228 | } |
| 229 | |
| 230 | return result; |
| 231 | } |
| 232 | #endif |
| 233 | |
| 234 | struct KIEImgEdit { |
| 235 | QImage &img; |
| 236 | QList<QRgb> colors; |
| 237 | unsigned int *data; |
| 238 | unsigned int pixels; |
| 239 | |
| 240 | KIEImgEdit(QImage &_img) |
| 241 | : img(_img) |
| 242 | { |
| 243 | if (img.depth() > 8) { |
| 244 | // Code using data and pixels assumes that the pixels are stored |
| 245 | // in 32bit values and that the image is not premultiplied |
| 246 | if ((img.format() != QImage::Format_ARGB32) // |
| 247 | && (img.format() != QImage::Format_RGB32)) { |
| 248 | img.convertTo(f: QImage::Format_ARGB32); |
| 249 | } |
| 250 | data = (unsigned int *)img.bits(); |
| 251 | pixels = img.width() * img.height(); |
| 252 | } else { |
| 253 | pixels = img.colorCount(); |
| 254 | colors = img.colorTable(); |
| 255 | data = (unsigned int *)colors.data(); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | ~KIEImgEdit() |
| 260 | { |
| 261 | if (img.depth() <= 8) { |
| 262 | img.setColorTable(colors); |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | KIEImgEdit(const KIEImgEdit &) = delete; |
| 267 | KIEImgEdit &operator=(const KIEImgEdit &) = delete; |
| 268 | }; |
| 269 | |
| 270 | // Taken from KImageEffect. We don't want to link kdecore to kdeui! As long |
| 271 | // as this code is not too big, it doesn't seem much of a problem to me. |
| 272 | |
| 273 | void KIconEffect::toGray(QImage &img, float value) |
| 274 | { |
| 275 | if (value == 0.0) { |
| 276 | return; |
| 277 | } |
| 278 | |
| 279 | KIEImgEdit ii(img); |
| 280 | QRgb *data = ii.data; |
| 281 | QRgb *end = data + ii.pixels; |
| 282 | |
| 283 | unsigned char gray; |
| 284 | if (value == 1.0) { |
| 285 | while (data != end) { |
| 286 | gray = qGray(rgb: *data); |
| 287 | *data = qRgba(r: gray, g: gray, b: gray, a: qAlpha(rgb: *data)); |
| 288 | ++data; |
| 289 | } |
| 290 | } else { |
| 291 | unsigned char val = (unsigned char)(255.0 * value); |
| 292 | while (data != end) { |
| 293 | gray = qGray(rgb: *data); |
| 294 | *data = qRgba(r: (val * gray + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 295 | g: (val * gray + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 296 | b: (val * gray + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 297 | a: qAlpha(rgb: *data)); |
| 298 | ++data; |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | void KIconEffect::colorize(QImage &img, const QColor &col, float value) |
| 304 | { |
| 305 | if (value == 0.0) { |
| 306 | return; |
| 307 | } |
| 308 | |
| 309 | KIEImgEdit ii(img); |
| 310 | QRgb *data = ii.data; |
| 311 | QRgb *end = data + ii.pixels; |
| 312 | |
| 313 | float rcol = col.red(); |
| 314 | float gcol = col.green(); |
| 315 | float bcol = col.blue(); |
| 316 | unsigned char red; |
| 317 | unsigned char green; |
| 318 | unsigned char blue; |
| 319 | unsigned char gray; |
| 320 | unsigned char val = (unsigned char)(255.0 * value); |
| 321 | while (data != end) { |
| 322 | gray = qGray(rgb: *data); |
| 323 | if (gray < 128) { |
| 324 | red = static_cast<unsigned char>(rcol / 128 * gray); |
| 325 | green = static_cast<unsigned char>(gcol / 128 * gray); |
| 326 | blue = static_cast<unsigned char>(bcol / 128 * gray); |
| 327 | } else if (gray > 128) { |
| 328 | red = static_cast<unsigned char>((gray - 128) * (2 - rcol / 128) + rcol - 1); |
| 329 | green = static_cast<unsigned char>((gray - 128) * (2 - gcol / 128) + gcol - 1); |
| 330 | blue = static_cast<unsigned char>((gray - 128) * (2 - bcol / 128) + bcol - 1); |
| 331 | } else { |
| 332 | red = static_cast<unsigned char>(rcol); |
| 333 | green = static_cast<unsigned char>(gcol); |
| 334 | blue = static_cast<unsigned char>(bcol); |
| 335 | } |
| 336 | |
| 337 | *data = qRgba(r: (val * red + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 338 | g: (val * green + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 339 | b: (val * blue + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 340 | a: qAlpha(rgb: *data)); |
| 341 | ++data; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | void KIconEffect::toMonochrome(QImage &img, const QColor &black, const QColor &white, float value) |
| 346 | { |
| 347 | if (value == 0.0) { |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | KIEImgEdit ii(img); |
| 352 | QRgb *data = ii.data; |
| 353 | QRgb *end = data + ii.pixels; |
| 354 | |
| 355 | // Step 1: determine the average brightness |
| 356 | double values = 0.0; |
| 357 | double sum = 0.0; |
| 358 | bool grayscale = true; |
| 359 | while (data != end) { |
| 360 | sum += qGray(rgb: *data) * qAlpha(rgb: *data) + 255 * (255 - qAlpha(rgb: *data)); |
| 361 | values += 255; |
| 362 | if ((qRed(rgb: *data) != qGreen(rgb: *data)) || (qGreen(rgb: *data) != qBlue(rgb: *data))) { |
| 363 | grayscale = false; |
| 364 | } |
| 365 | ++data; |
| 366 | } |
| 367 | double medium = sum / values; |
| 368 | |
| 369 | // Step 2: Modify the image |
| 370 | unsigned char val = (unsigned char)(255.0 * value); |
| 371 | int rw = white.red(); |
| 372 | int gw = white.green(); |
| 373 | int bw = white.blue(); |
| 374 | int rb = black.red(); |
| 375 | int gb = black.green(); |
| 376 | int bb = black.blue(); |
| 377 | data = ii.data; |
| 378 | |
| 379 | if (grayscale) { |
| 380 | while (data != end) { |
| 381 | if (qRed(rgb: *data) <= medium) { |
| 382 | *data = qRgba(r: (val * rb + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 383 | g: (val * gb + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 384 | b: (val * bb + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 385 | a: qAlpha(rgb: *data)); |
| 386 | } else { |
| 387 | *data = qRgba(r: (val * rw + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 388 | g: (val * gw + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 389 | b: (val * bw + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 390 | a: qAlpha(rgb: *data)); |
| 391 | } |
| 392 | ++data; |
| 393 | } |
| 394 | } else { |
| 395 | while (data != end) { |
| 396 | if (qGray(rgb: *data) <= medium) { |
| 397 | *data = qRgba(r: (val * rb + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 398 | g: (val * gb + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 399 | b: (val * bb + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 400 | a: qAlpha(rgb: *data)); |
| 401 | } else { |
| 402 | *data = qRgba(r: (val * rw + (0xFF - val) * qRed(rgb: *data)) >> 8, |
| 403 | g: (val * gw + (0xFF - val) * qGreen(rgb: *data)) >> 8, |
| 404 | b: (val * bw + (0xFF - val) * qBlue(rgb: *data)) >> 8, |
| 405 | a: qAlpha(rgb: *data)); |
| 406 | } |
| 407 | ++data; |
| 408 | } |
| 409 | } |
| 410 | } |
| 411 | |
| 412 | void KIconEffect::deSaturate(QImage &img, float value) |
| 413 | { |
| 414 | if (value == 0.0) { |
| 415 | return; |
| 416 | } |
| 417 | |
| 418 | KIEImgEdit ii(img); |
| 419 | QRgb *data = ii.data; |
| 420 | QRgb *end = data + ii.pixels; |
| 421 | |
| 422 | QColor color; |
| 423 | int h; |
| 424 | int s; |
| 425 | int v; |
| 426 | while (data != end) { |
| 427 | color.setRgb(*data); |
| 428 | color.getHsv(h: &h, s: &s, v: &v); |
| 429 | color.setHsv(h, s: (int)(s * (1.0 - value) + 0.5), v); |
| 430 | *data = qRgba(r: color.red(), g: color.green(), b: color.blue(), a: qAlpha(rgb: *data)); |
| 431 | ++data; |
| 432 | } |
| 433 | } |
| 434 | |
| 435 | void KIconEffect::toGamma(QImage &img, float value) |
| 436 | { |
| 437 | KIEImgEdit ii(img); |
| 438 | QRgb *data = ii.data; |
| 439 | QRgb *end = data + ii.pixels; |
| 440 | |
| 441 | float gamma = 1 / (2 * value + 0.5); |
| 442 | while (data != end) { |
| 443 | *data = qRgba(r: static_cast<unsigned char>(pow(x: static_cast<float>(qRed(rgb: *data)) / 255, y: gamma) * 255), |
| 444 | g: static_cast<unsigned char>(pow(x: static_cast<float>(qGreen(rgb: *data)) / 255, y: gamma) * 255), |
| 445 | b: static_cast<unsigned char>(pow(x: static_cast<float>(qBlue(rgb: *data)) / 255, y: gamma) * 255), |
| 446 | a: qAlpha(rgb: *data)); |
| 447 | ++data; |
| 448 | } |
| 449 | } |
| 450 | |
| 451 | void KIconEffect::semiTransparent(QImage &img) |
| 452 | { |
| 453 | if (img.depth() == 32) { |
| 454 | if (img.format() == QImage::Format_ARGB32_Premultiplied) { |
| 455 | img.convertTo(f: QImage::Format_ARGB32); |
| 456 | } |
| 457 | int width = img.width(); |
| 458 | int height = img.height(); |
| 459 | |
| 460 | unsigned char *line; |
| 461 | for (int y = 0; y < height; ++y) { |
| 462 | if (QSysInfo::ByteOrder == QSysInfo::BigEndian) { |
| 463 | line = img.scanLine(y); |
| 464 | } else { |
| 465 | line = img.scanLine(y) + 3; |
| 466 | } |
| 467 | for (int x = 0; x < width; ++x) { |
| 468 | *line >>= 1; |
| 469 | line += 4; |
| 470 | } |
| 471 | } |
| 472 | } else if (img.depth() == 8) { |
| 473 | // not running on 8 bit, we can safely install a new colorTable |
| 474 | QList<QRgb> colorTable = img.colorTable(); |
| 475 | for (int i = 0; i < colorTable.size(); ++i) { |
| 476 | colorTable[i] = (colorTable[i] & 0x00ffffff) | ((colorTable[i] & 0xfe000000) >> 1); |
| 477 | } |
| 478 | img.setColorTable(colorTable); |
| 479 | } else { |
| 480 | // Insert transparent pixel into the clut. |
| 481 | int transColor = -1; |
| 482 | |
| 483 | // search for a color that is already transparent |
| 484 | for (int x = 0; x < img.colorCount(); ++x) { |
| 485 | // try to find already transparent pixel |
| 486 | if (qAlpha(rgb: img.color(i: x)) < 127) { |
| 487 | transColor = x; |
| 488 | break; |
| 489 | } |
| 490 | } |
| 491 | |
| 492 | // FIXME: image must have transparency |
| 493 | if (transColor < 0 || transColor >= img.colorCount()) { |
| 494 | return; |
| 495 | } |
| 496 | |
| 497 | img.setColor(i: transColor, c: 0); |
| 498 | unsigned char *line; |
| 499 | if (img.depth() == 8) { |
| 500 | for (int y = 0; y < img.height(); ++y) { |
| 501 | line = img.scanLine(y); |
| 502 | for (int x = (y % 2); x < img.width(); x += 2) { |
| 503 | line[x] = transColor; |
| 504 | } |
| 505 | } |
| 506 | } else { |
| 507 | const bool setOn = (transColor != 0); |
| 508 | if (img.format() == QImage::Format_MonoLSB) { |
| 509 | for (int y = 0; y < img.height(); ++y) { |
| 510 | line = img.scanLine(y); |
| 511 | for (int x = (y % 2); x < img.width(); x += 2) { |
| 512 | if (!setOn) { |
| 513 | *(line + (x >> 3)) &= ~(1 << (x & 7)); |
| 514 | } else { |
| 515 | *(line + (x >> 3)) |= (1 << (x & 7)); |
| 516 | } |
| 517 | } |
| 518 | } |
| 519 | } else { |
| 520 | for (int y = 0; y < img.height(); ++y) { |
| 521 | line = img.scanLine(y); |
| 522 | for (int x = (y % 2); x < img.width(); x += 2) { |
| 523 | if (!setOn) { |
| 524 | *(line + (x >> 3)) &= ~(1 << (7 - (x & 7))); |
| 525 | } else { |
| 526 | *(line + (x >> 3)) |= (1 << (7 - (x & 7))); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | } |
| 532 | } |
| 533 | } |
| 534 | |
| 535 | void KIconEffect::semiTransparent(QPixmap &pix) |
| 536 | { |
| 537 | QImage img = pix.toImage(); |
| 538 | semiTransparent(img); |
| 539 | pix = QPixmap::fromImage(image: img); |
| 540 | } |
| 541 | |
| 542 | QImage KIconEffect::doublePixels(const QImage &src) const |
| 543 | { |
| 544 | int w = src.width(); |
| 545 | int h = src.height(); |
| 546 | |
| 547 | QImage dst(w * 2, h * 2, src.format()); |
| 548 | |
| 549 | if (src.depth() == 1) { |
| 550 | qWarning() << "image depth 1 not supported" ; |
| 551 | return QImage(); |
| 552 | } |
| 553 | |
| 554 | int x; |
| 555 | int y; |
| 556 | if (src.depth() == 32) { |
| 557 | QRgb *l1; |
| 558 | QRgb *l2; |
| 559 | for (y = 0; y < h; ++y) { |
| 560 | l1 = (QRgb *)src.scanLine(y); |
| 561 | l2 = (QRgb *)dst.scanLine(y * 2); |
| 562 | for (x = 0; x < w; ++x) { |
| 563 | l2[x * 2] = l2[x * 2 + 1] = l1[x]; |
| 564 | } |
| 565 | memcpy(dest: dst.scanLine(y * 2 + 1), src: l2, n: dst.bytesPerLine()); |
| 566 | } |
| 567 | } else { |
| 568 | for (x = 0; x < src.colorCount(); ++x) { |
| 569 | dst.setColor(i: x, c: src.color(i: x)); |
| 570 | } |
| 571 | |
| 572 | const unsigned char *l1; |
| 573 | unsigned char *l2; |
| 574 | for (y = 0; y < h; ++y) { |
| 575 | l1 = src.scanLine(y); |
| 576 | l2 = dst.scanLine(y * 2); |
| 577 | for (x = 0; x < w; ++x) { |
| 578 | l2[x * 2] = l1[x]; |
| 579 | l2[x * 2 + 1] = l1[x]; |
| 580 | } |
| 581 | memcpy(dest: dst.scanLine(y * 2 + 1), src: l2, n: dst.bytesPerLine()); |
| 582 | } |
| 583 | } |
| 584 | return dst; |
| 585 | } |
| 586 | |
| 587 | void KIconEffect::overlay(QImage &src, QImage &overlay) |
| 588 | { |
| 589 | if (src.depth() != overlay.depth()) { |
| 590 | qWarning() << "Image depth src (" << src.depth() << ") != overlay " |
| 591 | << "(" << overlay.depth() << ")!" ; |
| 592 | return; |
| 593 | } |
| 594 | if (src.size() != overlay.size()) { |
| 595 | qWarning() << "Image size src != overlay" ; |
| 596 | return; |
| 597 | } |
| 598 | if (src.format() == QImage::Format_ARGB32_Premultiplied) { |
| 599 | src.convertTo(f: QImage::Format_ARGB32); |
| 600 | } |
| 601 | |
| 602 | if (overlay.format() == QImage::Format_RGB32) { |
| 603 | qWarning() << "Overlay doesn't have alpha buffer!" ; |
| 604 | return; |
| 605 | } else if (overlay.format() == QImage::Format_ARGB32_Premultiplied) { |
| 606 | overlay.convertTo(f: QImage::Format_ARGB32); |
| 607 | } |
| 608 | |
| 609 | int i; |
| 610 | int j; |
| 611 | |
| 612 | // We don't do 1 bpp |
| 613 | |
| 614 | if (src.depth() == 1) { |
| 615 | qWarning() << "1bpp not supported!" ; |
| 616 | return; |
| 617 | } |
| 618 | |
| 619 | // Overlay at 8 bpp doesn't use alpha blending |
| 620 | |
| 621 | if (src.depth() == 8) { |
| 622 | if (src.colorCount() + overlay.colorCount() > 255) { |
| 623 | qWarning() << "Too many colors in src + overlay!" ; |
| 624 | return; |
| 625 | } |
| 626 | |
| 627 | // Find transparent pixel in overlay |
| 628 | int trans; |
| 629 | for (trans = 0; trans < overlay.colorCount(); trans++) { |
| 630 | if (qAlpha(rgb: overlay.color(i: trans)) == 0) { |
| 631 | qWarning() << "transparent pixel found at " << trans; |
| 632 | break; |
| 633 | } |
| 634 | } |
| 635 | if (trans == overlay.colorCount()) { |
| 636 | qWarning() << "transparent pixel not found!" ; |
| 637 | return; |
| 638 | } |
| 639 | |
| 640 | // Merge color tables |
| 641 | int nc = src.colorCount(); |
| 642 | src.setColorCount(nc + overlay.colorCount()); |
| 643 | for (i = 0; i < overlay.colorCount(); ++i) { |
| 644 | src.setColor(i: nc + i, c: overlay.color(i)); |
| 645 | } |
| 646 | |
| 647 | // Overwrite nontransparent pixels. |
| 648 | unsigned char *oline; |
| 649 | unsigned char *sline; |
| 650 | for (i = 0; i < src.height(); ++i) { |
| 651 | oline = overlay.scanLine(i); |
| 652 | sline = src.scanLine(i); |
| 653 | for (j = 0; j < src.width(); ++j) { |
| 654 | if (oline[j] != trans) { |
| 655 | sline[j] = oline[j] + nc; |
| 656 | } |
| 657 | } |
| 658 | } |
| 659 | } |
| 660 | |
| 661 | // Overlay at 32 bpp does use alpha blending |
| 662 | |
| 663 | if (src.depth() == 32) { |
| 664 | QRgb *oline; |
| 665 | QRgb *sline; |
| 666 | int r1; |
| 667 | int g1; |
| 668 | int b1; |
| 669 | int a1; |
| 670 | int r2; |
| 671 | int g2; |
| 672 | int b2; |
| 673 | int a2; |
| 674 | |
| 675 | for (i = 0; i < src.height(); ++i) { |
| 676 | oline = (QRgb *)overlay.scanLine(i); |
| 677 | sline = (QRgb *)src.scanLine(i); |
| 678 | |
| 679 | for (j = 0; j < src.width(); ++j) { |
| 680 | r1 = qRed(rgb: oline[j]); |
| 681 | g1 = qGreen(rgb: oline[j]); |
| 682 | b1 = qBlue(rgb: oline[j]); |
| 683 | a1 = qAlpha(rgb: oline[j]); |
| 684 | |
| 685 | r2 = qRed(rgb: sline[j]); |
| 686 | g2 = qGreen(rgb: sline[j]); |
| 687 | b2 = qBlue(rgb: sline[j]); |
| 688 | a2 = qAlpha(rgb: sline[j]); |
| 689 | |
| 690 | r2 = (a1 * r1 + (0xff - a1) * r2) >> 8; |
| 691 | g2 = (a1 * g1 + (0xff - a1) * g2) >> 8; |
| 692 | b2 = (a1 * b1 + (0xff - a1) * b2) >> 8; |
| 693 | a2 = qMax(a: a1, b: a2); |
| 694 | |
| 695 | sline[j] = qRgba(r: r2, g: g2, b: b2, a: a2); |
| 696 | } |
| 697 | } |
| 698 | } |
| 699 | } |
| 700 | |
| 701 | void KIconEffect::toDisabled(QImage &image) |
| 702 | { |
| 703 | KIconEffect::toGray(img&: image, value: 1); |
| 704 | KIconEffect::semiTransparent(img&: image); |
| 705 | } |
| 706 | |
| 707 | void KIconEffect::toDisabled(QPixmap &pixmap) |
| 708 | { |
| 709 | QImage img = pixmap.toImage(); |
| 710 | toDisabled(image&: img); |
| 711 | pixmap = QPixmap::fromImage(image: img); |
| 712 | } |
| 713 | |
| 714 | void KIconEffect::toActive(QImage &image) |
| 715 | { |
| 716 | KIconEffect::toGamma(img&: image, value: 0.7); |
| 717 | } |
| 718 | |
| 719 | void KIconEffect::toActive(QPixmap &pixmap) |
| 720 | { |
| 721 | QImage img = pixmap.toImage(); |
| 722 | KIconEffect::toGamma(img, value: 0.7); |
| 723 | pixmap = QPixmap::fromImage(image: img); |
| 724 | } |
| 725 | |