From ba947123bf7763bcc4ec84e328838495b87c7a03 Mon Sep 17 00:00:00 2001 From: Ivailo Monev Date: Sun, 9 Oct 2022 00:28:41 +0300 Subject: [PATCH] kioslave: merge ICO format related function overloads Signed-off-by: Ivailo Monev --- kioslave/thumbnail/CMakeLists.txt | 4 +- .../{icoutils_wrestool.cpp => icoutils.cpp} | 71 +++++++++++++---- kioslave/thumbnail/icoutils.h | 3 - kioslave/thumbnail/icoutils_common.cpp | 89 ---------------------- 4 files changed, 60 insertions(+), 107 deletions(-) rename kioslave/thumbnail/{icoutils_wrestool.cpp => icoutils.cpp} (61%) delete mode 100644 kioslave/thumbnail/icoutils_common.cpp diff --git a/kioslave/thumbnail/CMakeLists.txt b/kioslave/thumbnail/CMakeLists.txt index eabe5426..55f9ffd4 100644 --- a/kioslave/thumbnail/CMakeLists.txt +++ b/kioslave/thumbnail/CMakeLists.txt @@ -81,8 +81,8 @@ endif() ########### next target ############### -set(windowsexethumbnail_SRCS windowsexecreator.cpp icoutils_common.cpp icoutils_wrestool.cpp) -set(windowsimagethumbnail_SRCS windowsimagecreator.cpp icoutils_common.cpp icoutils_wrestool.cpp) +set(windowsexethumbnail_SRCS windowsexecreator.cpp icoutils.cpp) +set(windowsimagethumbnail_SRCS windowsimagecreator.cpp icoutils.cpp) kde4_add_plugin(windowsexethumbnail ${windowsexethumbnail_SRCS}) target_link_libraries( windowsexethumbnail ${KDE4_KIO_LIBS} ) diff --git a/kioslave/thumbnail/icoutils_wrestool.cpp b/kioslave/thumbnail/icoutils.cpp similarity index 61% rename from kioslave/thumbnail/icoutils_wrestool.cpp rename to kioslave/thumbnail/icoutils.cpp index 04e3d6a3..3711d734 100644 --- a/kioslave/thumbnail/icoutils_wrestool.cpp +++ b/kioslave/thumbnail/icoutils.cpp @@ -1,5 +1,5 @@ /* - icoutils_wrestool.cpp - Extract Microsoft Window icons and images using icoutils package + icoutils_common.cpp - Extract Microsoft Window icons and images using icoutils package Copyright (c) 2009-2010 by Pali Rohár @@ -16,21 +16,18 @@ #include "icoutils.h" #include -#include -#include #include -#include -#include +#include +#include +#include +#include #include -#include typedef QPair < QString, int > IconInExe; -bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, const QString &outputFileName, const qint32 iconNumber) +bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, QImage &image, int needWidth, int needHeight, const qint32 iconNumber) { - QProcess wrestool; - wrestool.start("wrestool", QStringList() << "-l" << inputFileName); wrestool.waitForFinished(); @@ -75,16 +72,64 @@ bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, const QString & if ( name.at(0) == '\'' ) name = name.mid(1, name.size()-2); - QFile(outputFileName).resize(0); - wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName << "-o" << outputFileName); + QTemporaryFile outputFile; + + if ( ! outputFile.open() ) + return false; + + QFile(outputFile.fileName()).resize(0); + + wrestool.start("wrestool", QStringList() << "-x" << "-t" << QString::number(type) << "-n" << name << inputFileName << "-o" << outputFile.fileName()); wrestool.waitForFinished(); - if ( wrestool.exitCode() == 0 && QFile(outputFileName).size() != 0 ) - return true; + if ( wrestool.exitCode() == 0 && QFile(outputFile.fileName()).size() != 0 ) + return IcoUtils::loadIcoImage(outputFile.fileName(), image, needWidth, needHeight); } return false; +} + +bool IcoUtils::loadIcoImage(const QString &inputFileName, QImage &image, int needWidth, int needHeight) +{ + QImageReader reader(inputFileName, "ico"); + if ( ! reader.canRead() ) + return false; + + QList icons; + do icons << reader.read(); + while ( reader.jumpToNextImage() ); + + if ( icons.empty() ) + return false; + + int min_w = 1024; + int min_h = 1024; + int index = icons.size() - 1; + + + // we loop in reverse order because QtIcoHandler converts all images to 32-bit depth, and resources are ordered from lower depth to higher depth + for ( int i_index = icons.size() - 1; i_index >= 0 ; --i_index ) + { + + const QImage &icon = icons.at(i_index); + int i_width = icon.width(); + int i_height = icon.height(); + int i_w = qAbs(i_width - needWidth); + int i_h = qAbs(i_height - needHeight); + + if ( i_w < min_w || ( i_w == min_w && i_h < min_h ) ) + { + + min_w = i_w; + min_h = i_h; + index = i_index; + + } + + } + image = icons.at(index); + return true; } diff --git a/kioslave/thumbnail/icoutils.h b/kioslave/thumbnail/icoutils.h index 1fdce8a2..b7cd3f76 100644 --- a/kioslave/thumbnail/icoutils.h +++ b/kioslave/thumbnail/icoutils.h @@ -19,16 +19,13 @@ #include #include -#include namespace IcoUtils { bool loadIcoImageFromExe(const QString &inputPath, QImage &image, int needWidth=512, int needHeight=512, const qint32 iconNumber=0); - bool loadIcoImageFromExe(const QString &inputFileName, const QString &outputFileName, const qint32 iconNumber); bool loadIcoImage(const QString &inputFileName, QImage &image, int needWidth=512, int needHeight=512); - bool loadIcoImage(QImageReader &reader, QImage &image, int needWidth, int needHeight); } diff --git a/kioslave/thumbnail/icoutils_common.cpp b/kioslave/thumbnail/icoutils_common.cpp deleted file mode 100644 index f92e0aee..00000000 --- a/kioslave/thumbnail/icoutils_common.cpp +++ /dev/null @@ -1,89 +0,0 @@ -/* - icoutils_common.cpp - Extract Microsoft Window icons and images using icoutils package - - Copyright (c) 2009-2010 by Pali Rohár - - ************************************************************************* - * * - * This library is free software; you can redistribute it and/or * - * modify it under the terms of the GNU General Public * - * License as published by the Free Software Foundation; either * - * version 2 of the License, or (at your option) any later version. * - * * - ************************************************************************* -*/ - -#include "icoutils.h" - -#include -#include -#include -#include -#include - -bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, QImage &image, int needWidth, int needHeight, const qint32 iconNumber) -{ - - QTemporaryFile outputFile; - - if ( ! outputFile.open() ) - return false; - - if ( ! IcoUtils::loadIcoImageFromExe(inputFileName, outputFile.fileName(), iconNumber) ) - return false; - - return IcoUtils::loadIcoImage(outputFile.fileName(), image, needWidth, needHeight); - -} - -bool IcoUtils::loadIcoImage(QImageReader &reader, QImage &image, int needWidth, int needHeight) -{ - - if ( ! reader.canRead() ) - return false; - - QList icons; - do icons << reader.read(); - while ( reader.jumpToNextImage() ); - - if ( icons.empty() ) - return false; - - int min_w = 1024; - int min_h = 1024; - int index = icons.size() - 1; - - - // we loop in reverse order because QtIcoHandler converts all images to 32-bit depth, and resources are ordered from lower depth to higher depth - for ( int i_index = icons.size() - 1; i_index >= 0 ; --i_index ) - { - - const QImage &icon = icons.at(i_index); - int i_width = icon.width(); - int i_height = icon.height(); - int i_w = qAbs(i_width - needWidth); - int i_h = qAbs(i_height - needHeight); - - if ( i_w < min_w || ( i_w == min_w && i_h < min_h ) ) - { - - min_w = i_w; - min_h = i_h; - index = i_index; - - } - - } - - image = icons.at(index); - return true; - -} - -bool IcoUtils::loadIcoImage(const QString &inputFileName, QImage &image, int needWidth, int needHeight) -{ - - QImageReader reader(inputFileName, "ico"); - return IcoUtils::loadIcoImage(reader, image, needWidth, needHeight); - -} -- 2.11.0