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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
/*
icoutils_win.cpp - Extract Microsoft Window icons and images using icoutils package
Copyright (c) 2013 by Andrius da Costa Ribas <andriusmao@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 <windows.h>
#include <QBuffer>
#include <QDir>
#include <QFile>
#include <QList>
extern "C"
{
// icon structs, as per http://msdn.microsoft.com/en-us/library/ms997538.aspx
#pragma pack( push )
#pragma pack( 2 )
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved ( must be 0)
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // How many bytes in this resource?
DWORD dwImageOffset; // Where in the file is this image?
} ICONDIRENTRY, *LPICONDIRENTRY;
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource Type (1 for icons)
WORD idCount; // How many images?
ICONDIRENTRY idEntries[1]; // An entry for each image (idCount of 'em)
} ICONDIR, *LPICONDIR;
typedef struct
{
BYTE bWidth; // Width, in pixels, of the image
BYTE bHeight; // Height, in pixels, of the image
BYTE bColorCount; // Number of colors in image (0 if >=8bpp)
BYTE bReserved; // Reserved
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // how many bytes in this resource?
WORD nID; // the ID
} GRPICONDIRENTRY, *LPGRPICONDIRENTRY;
typedef struct
{
WORD idReserved; // Reserved (must be 0)
WORD idType; // Resource type (1 for icons)
WORD idCount; // How many images?
GRPICONDIRENTRY idEntries[1]; // The entries for each image
} GRPICONDIR, *LPGRPICONDIR;
#pragma pack( pop )
}
BOOL CALLBACK enumResNameCallback( HMODULE hModule, LPCTSTR lpszType, LPTSTR lpszName, LONG_PTR lParam )
{
QList<LPTSTR>* iconResources = (QList<LPTSTR>*) lParam;
LPTSTR copyString;
if ( IS_INTRESOURCE(lpszName) )
{
copyString = lpszName;
}
else
{
copyString = new WCHAR[lstrlen(lpszName) + 1];
lstrcpy(copyString, lpszName);
}
if ( lpszType == RT_GROUP_ICON )
{
(*iconResources) << copyString;
}
return TRUE;
}
bool IcoUtils::loadIcoImageFromExe(const QString &inputFileName, const QString &outputFileName, const qint32 iconNumber)
{
HMODULE hModule;
LPCTSTR fileName;
QList<LPTSTR> iconResources;
fileName = (TCHAR*) QDir::toNativeSeparators( inputFileName ).utf16();
hModule = LoadLibraryEx ( fileName, 0, LOAD_LIBRARY_AS_DATAFILE );
if ( !hModule )
{
return false;
}
EnumResourceNames ( hModule, RT_GROUP_ICON, enumResNameCallback, (LONG_PTR) &iconResources );
if ( iconNumber < iconResources.size() )
{
HRSRC resourceInfo = FindResourceW ( hModule, (LPCTSTR) iconResources.at(iconNumber), RT_GROUP_ICON );
if ( resourceInfo == 0 )
{
FreeLibrary( hModule );
return false;
}
// we can get rid of the iconResources list now
foreach ( LPTSTR iconRes, iconResources )
{
if ( !IS_INTRESOURCE(iconRes) )
{
delete [] iconRes;
}
}
HGLOBAL resourceData = LoadResource ( hModule, resourceInfo );
LPVOID resourcePointer = LockResource ( resourceData );
int resourceSize = SizeofResource( hModule, resourceInfo );
if ( resourceSize == 0 )
{
FreeLibrary( hModule );
return false;
}
LPGRPICONDIR grpIconDir = (LPGRPICONDIR) resourcePointer;
QBuffer outBuffer;
outBuffer.open(QIODevice::ReadWrite);
// helper
const int iconDirHeaderSize = sizeof(grpIconDir->idReserved) + sizeof(grpIconDir->idType) + sizeof(grpIconDir->idCount);
// copy the common part of GRPICONDIR and ICONDIR structures to the file
outBuffer.write( (char*) resourcePointer, iconDirHeaderSize );
DWORD imageOffset = iconDirHeaderSize + (grpIconDir->idCount) * sizeof(ICONDIRENTRY) ;
for ( int i = 0 ; i < (grpIconDir->idCount) ; ++i )
{
// copy the common part of GRPICONDIRENTRY and ICONDIRENTRY structures to the respective ICONDIRENTRY's position
LPGRPICONDIRENTRY grpIconDirEntry = &(grpIconDir->idEntries[i]);
outBuffer.seek( iconDirHeaderSize + i * sizeof(ICONDIRENTRY) );
outBuffer.write( (char*) grpIconDirEntry, sizeof(GRPICONDIRENTRY) - sizeof(grpIconDirEntry->nID) );
// now, instead of nID, write the image offset
outBuffer.write( (char*) &imageOffset, sizeof(DWORD) );
// find the icon resource
resourceInfo = FindResourceW ( hModule, MAKEINTRESOURCE(grpIconDirEntry->nID), RT_ICON );
if ( resourceInfo == 0 )
{
FreeLibrary( hModule );
return false;
}
resourceData = LoadResource ( hModule, resourceInfo );
resourcePointer = LockResource ( resourceData );
resourceSize = SizeofResource( hModule, resourceInfo );
if ( resourceSize == 0 )
{
FreeLibrary( hModule );
return false;
}
// seek to imageOffset
outBuffer.seek(imageOffset);
// write the icon data
outBuffer.write( (char*) resourcePointer, resourceSize );
// increment imageOffset
imageOffset += resourceSize;
}
QFile outFile(outputFileName);
outFile.open(QIODevice::WriteOnly);
outFile.write(outBuffer.data());
FreeLibrary( hModule );
return true;
}
FreeLibrary( hModule );
return false;
}
|