File: icoutils_common.cpp

package info (click to toggle)
kio-extras 4%3A18.08.3-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 7,364 kB
  • sloc: cpp: 25,948; ansic: 4,443; perl: 1,056; xml: 460; python: 28; sh: 20; makefile: 6
file content (116 lines) | stat: -rw-r--r-- 3,320 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
/*
    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>

#define abs(n) ( ( n < 0 ) ? -n : n )

bool IcoUtils::loadIcoImageFromExe(QIODevice * inputDevice, QImage &image, int needWidth, int needHeight, const qint32 iconNumber)
{

    QTemporaryFile inputFile;

    if ( ! inputFile.open() )
         return false;

    QByteArray data = inputDevice->readAll();

    if ( inputFile.write(data) == -1 )
        return false;

    return IcoUtils::loadIcoImageFromExe(inputFile.fileName(), image, needWidth, needHeight, iconNumber);

}

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 = abs(i_width - needWidth);
        int i_h = abs(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(QIODevice * inputDevice, QImage &image, int needWidth, int needHeight)
{

    QImageReader reader(inputDevice, "ico");
    return IcoUtils::loadIcoImage(reader, image, needWidth, needHeight);

}

bool IcoUtils::loadIcoImage(const QString &inputFileName, QImage &image, int needWidth, int needHeight)
{

    QImageReader reader(inputFileName, "ico");
    return IcoUtils::loadIcoImage(reader, image, needWidth, needHeight);

}