kioslave: merge ICO format related function overloads

Signed-off-by: Ivailo Monev <xakepa10@gmail.com>
This commit is contained in:
Ivailo Monev 2022-10-09 00:28:41 +03:00
parent e4e6a281b3
commit ba947123bf
4 changed files with 61 additions and 108 deletions

View file

@ -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} )

View file

@ -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 <pali.rohar@gmail.com>
@ -16,21 +16,18 @@
#include "icoutils.h"
#include <QList>
#include <QPair>
#include <QRegExp>
#include <QString>
#include <QStringList>
#include <QFile>
#include <QTemporaryFile>
#include <QImage>
#include <QImageReader>
#include <QPair>
#include <QProcess>
#include <QSet>
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 <QImage> 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;
}

View file

@ -19,16 +19,13 @@
#include <QString>
#include <QImage>
#include <QImageReader>
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);
}

View file

@ -1,89 +0,0 @@
/*
icoutils_common.cpp - Extract Microsoft Window icons and images using icoutils package
Copyright (c) 2009-2010 by Pali Rohár <pali.rohar@gmail.com>
*************************************************************************
* *
* 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 <QList>
#include <QString>
#include <QTemporaryFile>
#include <QImage>
#include <QImageReader>
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 <QImage> 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);
}