Save a list of QPixmaps to .ico file - c++

I'm interested in creating a single .ico file (with transparency) from a list of QPixmap images (with sizes 16x16, 32x32, 48x48...). I haven't seen any related method in Qt's documentation: QPixmap, QImage, QIcon (which is for storing images for UI states, not related to the file format)...
Does Qt has such functionality? How can I save such file? May be mixing with Windows API?
PS: A low-level solution would be to directly write the .ico file, but I'm more interested in not reinventing the wheel if possible.

It seems that there is no built in support in Qt for writing ICO files, so here I'm publishing a code snippet to generate one from a list of pixmaps. Hope it may be useful for somebody else.
template<typename T>
void write(QFile& f, const T t)
{
f.write((const char*)&t, sizeof(t));
}
bool savePixmapsToICO(const QList<QPixmap>& pixmaps, const QString& path)
{
static_assert(sizeof(short) == 2, "short int is not 2 bytes");
static_assert(sizeof(int) == 4, "int is not 4 bytes");
QFile f(path);
if (!f.open(QFile::OpenModeFlag::WriteOnly)) return false;
// Header
write<short>(f, 0);
write<short>(f, 1);
write<short>(f, pixmaps.count());
// Compute size of individual images
QList<int> images_size;
for (int ii = 0; ii < pixmaps.count(); ++ii) {
QTemporaryFile temp;
temp.setAutoRemove(true);
if (!temp.open()) return false;
const auto& pixmap = pixmaps[ii];
pixmap.save(&temp, "PNG");
temp.close();
images_size.push_back(QFileInfo(temp).size());
}
// Images directory
constexpr unsigned int entry_size = sizeof(char) + sizeof(char) + sizeof(char) + sizeof(char) + sizeof(short) + sizeof(short) + sizeof(unsigned int) + sizeof(unsigned int);
static_assert(entry_size == 16, "wrong entry size");
unsigned int offset = 3 * sizeof(short) + pixmaps.count() * entry_size;
for (int ii = 0; ii < pixmaps.count(); ++ii) {
const auto& pixmap = pixmaps[ii];
if (pixmap.width() > 256 || pixmap.height() > 256) continue;
write<char>(f, pixmap.width() == 256 ? 0 : pixmap.width());
write<char>(f, pixmap.height() == 256 ? 0 : pixmap.height());
write<char>(f, 0); // palette size
write<char>(f, 0); // reserved
write<short>(f, 1); // color planes
write<short>(f, pixmap.depth()); // bits-per-pixel
write<unsigned int>(f, images_size[ii]); // size of image in bytes
write<unsigned int>(f, offset); // offset
offset += images_size[ii];
}
for (int ii = 0; ii < pixmaps.count(); ++ii) {
const auto& pixmap = pixmaps[ii];
if (pixmap.width() > 256 || pixmap.height() > 256) continue;
pixmap.save(&f, "PNG");
}
return true;
}
Code also available in GitHub.

Related

Draw waveform from raw data using QAudioProbe

For some strange reason QAudioRecorder::audioInputs() returns twice as much devices that I actually have
They're seem to be duplicated but not really - looks like they giving different samples, because when I'm trying to play recorded audio from first two devices it sounds twice as fast, when second two devices sounds normally.
Heres my code:
#include "MicrophoneWidget.h"
#include <QLayout>
#include <sndfile.h>
MicrophoneWidget::MicrophoneWidget(QWidget *parent) : QWidget(parent)
{
QAudioEncoderSettings settings;
settings.setCodec("audio/PCM");
settings.setQuality(QMultimedia::HighQuality);
settings.setChannelCount(1);
recorder = new QAudioRecorder(this);
recorder->setEncodingSettings(settings);
button = new QPushButton();
button->setCheckable(true);
devicesBox = new QComboBox();
connect(devicesBox, SIGNAL(currentIndexChanged(int)), this, SLOT(onDeviceChanged(int)));
for(const QString& device : recorder->audioInputs()) devicesBox->addItem(device, QVariant(device));
label = new QLabel();
connect(button, SIGNAL(toggled(bool)), this, SLOT(onButtonToggled(bool)));
QVBoxLayout* layout = new QVBoxLayout();
layout->addWidget(devicesBox);
layout->addWidget(button);
layout->addWidget(label);
setLayout(layout);
probe = new QAudioProbe();
probe->setSource(recorder);
connect(probe, SIGNAL(audioBufferProbed(QAudioBuffer)), this, SLOT(onAudioBufferProbed(QAudioBuffer)));
}
void MicrophoneWidget::resizeEvent(QResizeEvent*)
{
pixmap = QPixmap(label->size());
}
void MicrophoneWidget::onAudioBufferProbed(QAudioBuffer buffer)
{
qDebug() << buffer.byteCount() / buffer.sampleCount();
const qint32 *data = buffer.constData<qint32>();
pixmap.fill(Qt::transparent);
painter.begin(&pixmap);
int count = buffer.sampleCount() / 2;
float xScale = (float)label->width() / count;
float center = (float)label->height() / 2;
for(int i = 0; i < count; i++) samples.push_back(data[i]);
for(int i = 1; i < count; i++)
{
painter.drawLine(
(i - 1) * xScale,
center + ((float)(data[i-1]) / INT_MAX * center),
i * xScale,
center + ((float)(data[i]) / INT_MAX * center)
);
}
painter.end();
label->setPixmap(pixmap);
}
void MicrophoneWidget::onButtonToggled(bool toggled)
{
if(toggled)
{
samples.clear();
recorder->record();
}
else
{
recorder->stop();
SF_INFO sndFileInfo;
sndFileInfo.channels = 1;
sndFileInfo.samplerate = 44100;
sndFileInfo.format = SF_FORMAT_WAV | SF_FORMAT_PCM_32;
QString filePath = "customWAV-" + QString::number(QDateTime::currentMSecsSinceEpoch()) + ".wav";
SNDFILE* sndFile = sf_open(filePath.toStdString().c_str(), SFM_WRITE, &sndFileInfo);
if(sndFile != nullptr)
{
sf_count_t count = sf_write_int(sndFile, samples.data(), samples.size());
qDebug() << "Written " << count << " items; " << (samples.size() / sndFileInfo.samplerate) << " seconds";
}
sf_close(sndFile);
}
}
void MicrophoneWidget::onDeviceChanged(int index)
{
recorder->stop();
recorder->setAudioInput(devicesBox->itemData(index).toString());
if(button->isChecked())recorder->record();
}
So, how should I parse the raw data to draw correct waveform?
Firs of all check that the buffer hast exactly the sample type that you expect, to do it, check the QAudioFormat sampleType function. There are 3 alternatives:
QAudioFormat::SignedInt,
QAudioFormat::UnSignedInt,
QAudioFormat::Float
This should help you to decide the correct cast for the given samples. In my case, as the different Qt examples, I use:
const qint16 *data = buffer.data<qint16>();
And them you can normalise it easily using this function:
qreal getPeakValue(const QAudioFormat& format)
{
// Note: Only the most common sample formats are supported
if (!format.isValid())
return qreal(0);
if (format.codec() != "audio/pcm")
return qreal(0);
switch (format.sampleType()) {
case QAudioFormat::Unknown:
break;
case QAudioFormat::Float:
if (format.sampleSize() != 32) // other sample formats are not supported
return qreal(0);
return qreal(1.00003);
case QAudioFormat::SignedInt:
if (format.sampleSize() == 32)
#ifdef Q_OS_WIN
return qreal(INT_MAX);
#endif
#ifdef Q_OS_UNIX
return qreal(SHRT_MAX);
#endif
if (format.sampleSize() == 16)
return qreal(SHRT_MAX);
if (format.sampleSize() == 8)
return qreal(CHAR_MAX);
break;
case QAudioFormat::UnSignedInt:
if (format.sampleSize() == 32)
return qreal(UINT_MAX);
if (format.sampleSize() == 16)
return qreal(USHRT_MAX);
if (format.sampleSize() == 8)
return qreal(UCHAR_MAX);
break;
}
return qreal(0);
}
Now you should iterate over the vector and divide by the peak that the functions returns, this will give a range of samples from [-1, 1], so save it in a QVector array to plot it.
To plot it, you have different alternatives, Qt introduce his own QtCharts module but you can still use QCustomPlot or Qwt. An example with QCustomPlot:
QCPGraph myPlot = ui->chart->addGraph();
myPlot->setData(xAxys.data(), recorded.data()); // init an X vector from 0 to the size of Y or whatever you want
ui->chart->yAxis->setRange(QCPRange(-1,1)); // set the range
ui->chart->replot();
You can find a complete example in the Qt examples or check my little GitHub project, LogoSpeech Studio, you will find complete example of how to plot the wave form, spectrogram, pitch and different properties of a signal.

Creating ARGB QImage from uint32 or uchar array

When I try to create a ARGB32 QImage from a reinterpret_cast<uchar*>(quint32*) using the QImage constructor the Image looses its color and alpha channel and the resulting QImage is grayscale!
The grayscale image is displayed as expected, if I was trying to display it in grayscale. So I know the scaling and indexing of ushort data to the quint32 array went well, but what is going wrong?
A Qt forum post suggested to do it the way I am doing it (as far as I can see), but maybe behavior has changed since that version of Qt? (I am Using Qt 5.9)
I realise that the documentation says:
data must be 32-bit aligned, and each scanline of data in the image
must also be 32-bit aligned.
But I would expect quint32 to be 32-bit aligned even after reinterpret_cast<uchar*>()?
Now the details:
I am converting the results of a calculation (an array with unsigned short values) to a semi-transparent blue-to-green-to-red image like this:
inline uchar val_to_blue(const double val) {
if (val > 0.5)
return 0;
else if (val < 0.25)
return 255;
else // x={.5,...,.25}:a=255/(.25-.5)=-4*255 & b=-255*0.5/(0.25-0.5)=4/2*255=2*255
return (uchar)(val * -4.0 * 255.0) + 2 * 255;
}
inline uchar val_to_green(const double val) {
if (val > 0.25 && val < 0.75)
return 255;
else if (val < 0.25)// x={0,...,.25}:a=255/(.25-0)=4*255 & b=-255*0/(0.25-0)=0
return (uchar)(val * 4.0 * 255.0);
else // if (val > .75) // x={.75,...,1}:a=255/(.75-.5)=4*255 & b=-255*0.5/(0.75-0.5)=-4/2*255=-2*255
return (uchar)(val * -4.0 * 255.0) - 2 * 255;
}
inline uchar val_to_red(const double val) {
if (val < 0.5)
return 0;
if (val > 0.75)
return 255;
else // x={0.5,...,0.75}:a=255/(0.75-0.5)=4*255 & b=-255*0.5/(0.75-0.5)=-4/2*255=-2*255
return (uchar)(val * 4.0 * 255.0) - 2 * 255;
}
inline QRgb val_to_rgba_scale(const double val) {
return qRgba( // ax+b={0,...,255} for x={i,...,j}, a=255/(j-i), b= -255i/(j-i)
val_to_blue(val),
val_to_green(val),
val_to_red(val),
(uchar)(val * 81)
);
}
Where val is a double between 0 and 1 scaled from the ushort data.
Each QRgb value is stored at the corresponding index of a quint32 array, like this:
if (m_pData[i*m_iWidth + j] >= uppVal)
tmpData[tmpIdx] = 0x45ff0000;
else if (m_pData[i*m_iWidth + j] <= lowVal)
tmpData[tmpIdx] = 0x00000000;
else
tmpData[tmpIdx] = val_to_rgba_scale((m_pData[i*m_iWidth + j] - lowVal) / (double)winWidth);
Where (m_pData[i*m_iWidth + j] - lowVal) / (double)winWidthis the ushort-to-double scaling method.
This is done in a for loop.
Finally I attempt to construct the image with:
QImage tmpQImage = QImage(reinterpret_cast<unsigned char*>(tmpData), m_iWidth, m_iHeight, QImage::Format_ARGB32);
But this doesn't work as I expect, because tmpQImage.allGray() returns true when called immediately after!
What am I doing wrong, and what should I do instead to create a ARGB image and keep both the colors and alpha channel?
I tried to reproduce your problem but I couldn't.
Either the actual issue of the OP is not part of the presented code, or I accidentally missed a detail when I tried to form an MCVE from the OP.
However, I want to present what I got so far as this may be helpful to fix the OP.
My source testQImageGrayToRGB.cc:
#include <vector>
#include <QtWidgets>
typedef unsigned char uchar;
namespace AGA {
uchar val_to_blue(const double val) {
if (val > 0.5)
return 0;
else if (val < 0.25)
return 255;
else // x={.5,...,.25}:a=255/(.25-.5)=-4*255 & b=-255*0.5/(0.25-0.5)=4/2*255=2*255
return (uchar)(val * -4.0 * 255.0) + 2 * 255;
}
uchar val_to_green(const double val) {
if (val > 0.25 && val < 0.75)
return 255;
else if (val < 0.25)// x={0,...,.25}:a=255/(.25-0)=4*255 & b=-255*0/(0.25-0)=0
return (uchar)(val * 4.0 * 255.0);
else // if (val > .75) // x={.75,...,1}:a=255/(.75-.5)=4*255 & b=-255*0.5/(0.75-0.5)=-4/2*255=-2*255
return (uchar)(val * -4.0 * 255.0) - 2 * 255;
}
uchar val_to_red(const double val) {
if (val < 0.5)
return 0;
if (val > 0.75)
return 255;
else // x={0.5,...,0.75}:a=255/(0.75-0.5)=4*255 & b=-255*0.5/(0.75-0.5)=-4/2*255=-2*255
return (uchar)(val * 4.0 * 255.0) - 2 * 255;
}
} // namespace AGA
namespace DS {
uchar val_to_blue(const double val)
{
return val < 0.25 ? 255
: val < 0.5 ? (0.5 - val) * 4 * 255
: 0;
}
uchar val_to_green(const double val)
{
return val < 0.25 ? val * 4 * 255
: val < 0.75 ? 255
: (1.0 - val) * 4 * 255;
}
uchar val_to_red(const double val)
{
return val < 0.5 ? 0
: val < 0.75 ? (val - 0.5) * 4 * 255
: 255;
}
} // namespace DS
std::vector<quint32> buildImageData(
const int w, const int h,
uchar (*pFuncValToR)(double),
uchar (*pFuncValToG)(double),
uchar (*pFuncValToB)(double))
{
// make temp. buffer to build up raw image data
std::vector<quint32> data(w * h);
// fill raw image - make values 0 ... 1 in n steps
const int n = w - 1;
for (int x = 0; x < w; ++x) {
const double v = (double)x / n;
QRgb qRgb = qRgba(pFuncValToR(v), pFuncValToG(v), pFuncValToB(v), 255);
for (int y = 0; y < h; ++y) data[y * w + x] = qRgb;
}
// done
return data;
}
int main(int argc, char **argv)
{
qDebug() << "Qt Version: " << QT_VERSION_STR;
QApplication app(argc, argv);
// build contents
enum { w = 256, h = 32 };
std::vector<quint32> dataAGA = buildImageData(w, h,
&AGA::val_to_red, &AGA::val_to_green, &AGA::val_to_blue);
QImage qImgAGA((const uchar*)dataAGA.data(), w, h, QImage::Format_ARGB32);
std::vector<quint32> dataDS = buildImageData(w, h,
&DS::val_to_red, &DS::val_to_green, &DS::val_to_blue);
QImage qImgDS((const uchar*)dataDS.data(), w, h, QImage::Format_ARGB32);
// build some GUI
QWidget win;
QVBoxLayout qVBox;
QLabel qLblAGA(
QString::fromUtf8("QImage (Functions of Andreas Gravgaard Andersen):"));
qVBox.addWidget(&qLblAGA);
QLabel qLblImgAGA;
qLblImgAGA.setPixmap(QPixmap::fromImage(qImgAGA));
qVBox.addWidget(&qLblImgAGA);
QLabel qLblDS(
QString::fromUtf8("QImage (Functions of Scheff):"));
qVBox.addWidget(&qLblDS);
QLabel qLblImgDS;
qLblImgDS.setPixmap(QPixmap::fromImage(qImgDS));
qVBox.addWidget(&qLblImgDS);
win.setLayout(&qVBox);
win.show();
// exec. application
return app.exec();
}
I compiled and tested it with VS2013, Qt5.6 on Windows 10 (64 bit):
Notes:
The val_to_ functions made me a little bit suspicious: an expression casted to (uchar), then a constant term added (which definitely does not fit into (uchar), the result returned as uchar...
Hmm...
Therefore, I remade them – with a little bit clean-up.
Actually, the visual comparison shows the differences are nearly invisible (with the only exception of the red line in the yellow region).
I had no problems to make a QImage out of the raw quint32 array (including the cast-to-uchar*-hack).
Update:
May be, it is not obvious: The sample code is carefully designed to grant that life-time of buffer data (std::vector<quint32> dataAGA and std::vector<quint32> dataDS) is longer than the life-time of Qt images (QImage qImgAGA and QImage qImgDS). This has been done according to the Qt doc. for QImage::QImage():
The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. You can provide a function pointer cleanupFunction along with an extra pointer cleanupInfo that will be called when the last copy is destroyed.
Image data may consume a significant amount of memory. Thus, the QImage implementation tries to prevent unnecessary copies (to safe memory space and time). Instead, the "user" (i.e. application developer) is responsible to ensure proper storage of image data.

how to create a graphics object using an Atom text editor

I am having a hard time determining a way to include graphics.h file in my compiler. All information I have came across is for IDE such as CodeBlocks. I would like to be able include graphics file for use without facing any problems. My questions are:
Can you use a text editor like Atom to create a graphics object?
If so what steps should be taken in order to accomplish that?
There are lot of graphics formats available with varying capabilities.
First distinction I would make is:
raster graphics vs. vector graphics
Raster graphics (storing the image pixel by pixel) are more often binary encoded as the amount of data is usally directly proportional to size of image. However some of them are textual encoded or can be textual as well as binary encoded.
Examples are:
Portable anymap
X Pixmap
Although these file formats are a little bit exotic, it is not difficult to find software which supports them. E.g. GIMP supports both out of the box (and even on Windows). Btw. they are that simple that it is not too complicated to write loader and writer by yourself.
A simple reader and writer for PPM (the color version of Portable anymap) can be found in my answer to SO: Convolution for Edge Detection in C.
Vector graphics (store graphics primitives which build the image) are more often textual encoded. As vector graphics can be "loss-less" scaled to any image size by simply applying a scaling factor to all coordinates, file size and destination image size are not directly related. Thus, vector graphics are the preferrable format for drawings especially if they are needed in multiple target resolutions.
For this, I would exclusively recommend:
Scalable Vector Graphics
which is (hopefully) the upcoming standard for scalable graphics in Web contents. Qt does provide (limited) support for SVG and thus, it is my preferred option for resolution independent icons.
A different (but maybe related) option is to embed graphics in source code. This can be done with rather any format if your image loader library provides image loading from memory (as well as from file). (All I know does this.)
Thus, the problem can be reduced to: How to embed a large chunk of (ASCII or binary) data as constant in C/C++ source code? which is IMHO trivial to solve.
I did this in my answer for SO: Paint a rect on qglwidget at specifit times.
Update:
As I noticed that the linked sample for PPM (as well as another for PBM) read actually the binary format, I implemented a sample application which demonstrates usage of ASCII PPM.
I believe that XPM is better suitable for the specific requirement to be editable in a text editor. Thus, I considered this in my sample also.
As the question doesn't mention what specific internal image format is desired nor in what API it shall be usable, I choosed Qt which
is something I'm familiar with
provides a QImage which is used as destination for image import
needs only a few lines of code for visual output of result.
Source code test-QShowPPM-XPM.cc:
// standard C++ header:
#include <cassert>
#include <iostream>
#include <string>
#include <sstream>
// Qt header:
#include <QtWidgets>
// sample image in ASCII PPM format
// (taken from https://en.wikipedia.org/wiki/Netpbm_format)
const char ppmData[] =
"P3\n"
"3 2\n"
"255\n"
"255 0 0 0 255 0 0 0 255\n"
"255 255 0 255 255 255 0 0 0\n";
// sample image in XPM3 format
/* XPM */
const char *xpmData[] = {
// w, h, nC, cPP
"16 16 5 1",
// colors
" c #ffffff",
"# c #000000",
"g c #ffff00",
"r c #ff0000",
"b c #0000ff",
// pixels
" ## ",
" ###gg### ",
" #gggggggg# ",
" #gggggggggg# ",
" #ggbbggggbbgg# ",
" #ggbbggggbbgg# ",
" #gggggggggggg# ",
"#gggggggggggggg#",
"#ggrrggggggrrgg#",
" #ggrrrrrrrrgg# ",
" #ggggrrrrgggg# ",
" #gggggggggggg# ",
" #gggggggggg# ",
" #gggggggg# ",
" ###gg### ",
" ## "
};
// Simplified PPM ASCII Reader (no support of comments)
inline int clamp(int value, int min, int max)
{
return value < min ? min : value > max ? max : value;
}
inline int scale(int value, int maxOld, int maxNew)
{
return value * maxNew / maxOld;
}
QImage readPPM(std::istream &in)
{
std::string header;
std::getline(in, header);
if (header != "P3") throw "ERROR! Not a PPM ASCII file.";
int w = 0, h = 0, max = 255; // width, height, bits per component
if (!(in >> w >> h >> max)) throw "ERROR! Premature end of file.";
if (max <= 0 || max > 255) throw "ERROR! Invalid format.";
QImage qImg(w, h, QImage::Format_RGB32);
for (int y = 0; y < h; ++y) {
for (int x = 0; x < w; ++x) {
int r, g, b;
if (!(in >> r >> g >> b)) throw "ERROR! Premature end of file.";
qImg.setPixel(x, y,
scale(clamp(r, 0, 255), max, 255) << 16
| scale(clamp(g, 0, 255), max, 255) << 8
| scale(clamp(b, 0, 255), max, 255));
}
}
return qImg;
}
// Simplified XPM Reader (implements sub-set of XPM3)
char getChar(const char *&p)
{
if (!*p) throw "ERROR! Premature end of file.";
return *p++;
}
std::string getString(const char *&p)
{
std::string str;
while (*p && !isspace(*p)) str += *p++;
return str;
}
void skipWS(const char *&p)
{
while (*p && isspace(*p)) ++p;
}
QImage readXPM(const char **xpmData)
{
int w = 0, h = 0; // width, height
int nC = 0, cPP = 1; // number of colors, chars per pixel
{ std::istringstream in(*xpmData);
if (!(in >> w >> h >> nC >> cPP)) throw "ERROR! Premature end of file.";
++xpmData;
}
std::map<std::string, std::string> colTbl;
for (int i = nC; i--; ++xpmData) {
const char *p = *xpmData;
std::string chr;
for (int j = cPP; j--;) chr += getChar(p);
skipWS(p);
if (getChar(p) != 'c') throw "ERROR! Format not supported.";
skipWS(p);
colTbl[chr] = getString(p);
}
QImage qImg(w, h, QImage::Format_RGB32);
for (int y = 0; y < h; ++y, ++xpmData) {
const char *p = *xpmData;
for (int x = 0; x < w; ++x) {
std::string pixel;
for (int j = cPP; j--;) pixel += getChar(p);
qImg.setPixelColor(x, y, QColor(colTbl[pixel].c_str()));
}
}
return qImg;
}
// a customized QLabel to handle scaling
class LabelImage: public QLabel {
private:
QPixmap _qPixmap, _qPixmapScaled;
public:
LabelImage();
LabelImage(const QPixmap &qPixmap): LabelImage()
{
setPixmap(qPixmap);
}
LabelImage(const QImage &qImg): LabelImage(QPixmap::fromImage(qImg))
{ }
void setPixmap(const QPixmap &qPixmap) { setPixmap(qPixmap, size()); }
protected:
virtual void resizeEvent(QResizeEvent *pQEvent);
private:
void setPixmap(const QPixmap &qPixmap, const QSize &size);
};
// main function
int main(int argc, char **argv)
{
qDebug() << QT_VERSION_STR;
// main application
#undef qApp // undef macro qApp out of the way
QApplication qApp(argc, argv);
// setup GUI
QMainWindow qWin;
QGroupBox qBox;
QGridLayout qGrid;
LabelImage qLblImgPPM(readPPM(std::istringstream(ppmData)));
qGrid.addWidget(&qLblImgPPM, 0, 0, Qt::AlignCenter);
LabelImage qLblImgXPM(readXPM(xpmData));
qGrid.addWidget(&qLblImgXPM, 1, 0, Qt::AlignCenter);
qBox.setLayout(&qGrid);
qWin.setCentralWidget(&qBox);
qWin.show();
// run application
return qApp.exec();
}
// implementation of LabelImage
LabelImage::LabelImage(): QLabel()
{
setFrameStyle(Raised | Box);
setAlignment(Qt::AlignCenter);
//setMinimumSize(QSize(1, 1)); // seems to be not necessary
setSizePolicy(QSizePolicy(QSizePolicy::Ignored, QSizePolicy::Ignored));
}
void LabelImage::resizeEvent(QResizeEvent *pQEvent)
{
QLabel::resizeEvent(pQEvent);
setPixmap(_qPixmap, pQEvent->size());
}
void LabelImage::setPixmap(const QPixmap &qPixmap, const QSize &size)
{
_qPixmap = qPixmap;
_qPixmapScaled = _qPixmap.scaled(size, Qt::KeepAspectRatio);
QLabel::setPixmap(_qPixmapScaled);
}
This has been compiled in VS2013 and tested in Windows 10 (64 bit):

Freetype renders crap in bitmap

I'm trying to create monochrome glyph atlas but encountered a problem. Freetype renders 'crap' in glyph's bitmap. I blame freetype because some of the glyphs are still rendered correctly.
The resulting texture atlas:
Why could it be and how can i fix it?
However i still could be wrong and here is bitmap processing code:
static std::vector<unsigned char> generateBitmap(FT_Face &face, unsigned int glyph, size_t *width, size_t *height) {
FT_Load_Glyph(face, FT_Get_Char_Index(face, glyph), FT_LOAD_RENDER | FT_LOAD_MONOCHROME );
FT_Bitmap bitmap;
FT_Bitmap_New(&bitmap);
FT_Bitmap_Convert(ftLib, &face->glyph->bitmap, &bitmap, 1);
*width = bitmap.width;
*height = bitmap.rows;
std::vector<unsigned char> result(bitmap.width * bitmap.rows);//
for (size_t y = 0; y < bitmap.rows; ++y)
{
for (size_t x = 0; x < bitmap.width; ++x)
{
result[(bitmap.width * y) + x] = bitmap.buffer[(bitmap.width * y) + x];
}
}
FT_Bitmap_Done(ftLib, &bitmap);
return result;
}
And code for putting it on main buffer:
static void putOnBuffer(std::vector<unsigned char> &buffer, std::vector<unsigned char> &bitmap, size_t height, size_t width) {
int r = 0;
while (r < height) {
int w = 0;
while (w < width) {
//assume buffer is enough large
size_t mainBufPos = ((currentBufferPositionY + r) * imageWidth) + (currentBufferPositionX + w);
size_t bitmapBufPos = (r * width) + w;
buffer[mainBufPos] = clamp(int(bitmap[bitmapBufPos] * 0x100), 0xff);
w++;
}
r++;
}
}
From documentation:
Convert a bitmap object with depth 1bpp, 2bpp, 4bpp, 8bpp or 32bpp to a bitmap object with depth 8bpp, making the number of used bytes [per] line (a.k.a. the ‘pitch’) a multiple of ‘alignment’.
In your code, you pass 1 as the value of the alignment parameter in the call to FT_Bitmap_Convert. In monochrome, one byte will be eight pixels, so the horizontal render loop needs to enforce a multiple of eight for the width.
Reference: https://www.freetype.org/freetype2/docs/reference/ft2-bitmap_handling.html

Get HBITMAPs For *ALL* Sizes and Depths of a File Type Icon (C++)

Compiler: MinGW/GCC
I'm trying to get the HICON of a file type based on what icon windows has registered for that file type, and then grab all of the HICON's images.
The problem is, I can't seem to get anything other than the 32x32 or 16x16 icon. Also, I've looked at GetIconInfoEx() but that function doesn't allow me to choose the icon size that I'm wanting, it just sort of arbitrarily pukes up whatever Windows feels like handing me at the time.
I want to at least have all of the 16x16, 32x32, and 48x48 icons, but I would really enjoy being able to extract every size that's in the HICON that I pass in.
Here's the code I'm currently working with (copy and pasted most of this from the web and stitched it together):
HBITMAP GetFileTypeIcon(const char* ext, int type, int depth)
{
HICON hIcon;
SHFILEINFO sfi= {0};
UINT flag = SHGFI_ICON|SHGFI_USEFILEATTRIBUTES;
int wh = 16;
switch(type)
{
default:
case FILE_ICON_SIZE_16:
{
wh = 16; flag|=SHGFI_SMALLICON;
}
break;
case FILE_ICON_SIZE_32:
{
wh = 32; flag|=SHGFI_LARGEICON;
}
break;
case FILE_ICON_SIZE_48:
{
wh = 48; flag|=SHGFI_SYSICONINDEX;
}
break;
case FILE_ICON_SIZE_256:
{
wh = 256; flag|=SHGFI_SYSICONINDEX;
}
break;
}
HRESULT hr = SHGetFileInfo(ext,FILE_ATTRIBUTE_NORMAL,&sfi,sizeof(sfi),flag);
if(SUCCEEDED(hr))
{
if((type == FILE_ICON_SIZE_48) || (type == FILE_ICON_SIZE_256))
{
// THIS PART DOESN'T COMPILE: undeclared function/indentifiers
// HIMAGELIST* imageList;
// hr = SHGetImageList(((type == FILE_ICON_SIZE_256)?SHIL_JUMBO:SHIL_EXTRALARGE), IID_IImageList, (void**)&imageList);
// if(SUCCEEDED(hr))
// {
// //Get the icon we need from the list. Note that the HIMAGELIST we retrieved
// //earlier needs to be casted to the IImageList interface before use.
// hr = ((IImageList*)imageList)->GetIcon(sfi.iIcon, ILD_TRANSPARENT, &hIcon);
// }
}
else
{
hIcon=sfi.hIcon;
}
}
// Convert to an HBITMAP (to get it out of the icon...)
HDC hDC = GetDC(NULL);
HDC hMemDC = CreateCompatibleDC(hDC);
HBITMAP hMemBmp = CreateCompatibleBitmap(hDC, wh, wh);
HGDIOBJ hOrgBMP = SelectObject(hMemDC, hMemBmp);
DrawIconEx(hMemDC, 0, 0, hIcon, wh, wh, 0, NULL, DI_NORMAL);
SelectObject(hMemDC, hOrgBMP);
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
DestroyIcon(hIcon);
return hMemBmp;
}
I don't even know what to do about color depths. I'll hazard a guess: make a DC that has a certain color depth (rather than just a compatible DC) and pass that into DrawIconEx()?
Edit: I answered my own question after much research/work.
See my answer below for a way to find and parse the raw icon data.
I basically had to do everything myself (with the help of the web, Stack Overflow, and several MSDN articles) so I think I'll just post my own solution here.
I ended up parsing the registry to find the locations of the icons of each previously registered file extension, since the API functions that should have easily gotten me the information I wanted have some... problems.
After that I spent several days manually observing the data formats at hand by observing output of an icon program, and with this information in hand I was able to construct an image loader.
I used Allegro game library to make dealing with BITMAP images easier - Win32/GDI is a bit too much to deal with and would have made the code exorbitantly messy.
Finding the Icon Location and Index:
(1) Look for extension under HKEY_CLASSES_ROOT, eg HKCR\.foo\(default) = "foofile"
(2) Default data of this is the next key to look at, eg HKCR\foofile\
(3) Default data here is the description eg HKCR\foofile\(default) = "Foo Document"
(4) The icon location may be in one of two places that I know of:
Either in HKCR\foofile\DefaultIcon\(default) or there may be an entry something like HKCR\foofile\CurVer\(default) = "foofile.1" which tells you to look at the key HKCR\foofile.1\DefaultIcon\(default) for the icon location.
Parsing the Icon Location String:
The string is simply a path followed by a comma, white space, possibly a negative sign, and a number indicating the "index" of the icon.
Here's the big gotcha: Let the icon index be N. If N is negative (might want to check for negative zeros!), it is a resource ID within the file specified. If N is positive, it means to find the N'th icon within the file, but the icon is not necessarily at resource ID number N.
Parsing Icon Structures Manually:
This is the bulk of the code and time spent, but it works beautifully. First off, here's the data formats for the various sections of color and mask data.
Data Block Formats:
32bit ... Color Data:
====================================================================================
Little Endian 4 byte ARGB values.
The rows are stored in reverse order (bottom to top).
24bit ... Color Data:
====================================================================================
Little Endian 3 byte RGB values.
Tightly Packed (NO PADDING).
INSERT PADDING BYTES TO GO UP TO NEXT DWORD AT END OF ROW. SET THEM TO 0x00.
The rows are stored in reverse order (bottom to top).
16bit ... Color Data:
====================================================================================
Little Endian 2 byte RGB values. 5 bits each with MSB = 0.
Tightly Packed (NO PADDING).
INSERT PADDING BYTES TO GO UP TO NEXT DWORD AT END OF ROW. SET THEM TO 0x00.
The rows are stored in reverse order (bottom to top).
8bit ... Palette & Color Data:
====================================================================================
The Palette is Little Endian 4 byte RGB0 values. No alpha.
There *might* be up to 256 palette entries.
If number of colors is reported as zero, assume 256 color entires.
The Pixels are 1 byte index values.
INSERT PADDING BYTES TO GO UP TO NEXT DWORD AT END OF ROW. SET THEM TO 0x00.
The rows are stored in reverse order (bottom to top).
4bit ... Palette & Color Data:
====================================================================================
The Palette is Little Endian 4 byte RGB0 values. No alpha.
There *might* be up to 16 palette entries.
If number of colors is reported as zero, assume 16 color entires.
The Pixels are nybble-length index values.
INSERT PADDING BYTES TO GO UP TO NEXT DWORD AT END OF ROW. SET THEM TO 0x00.
The rows are stored in reverse order (bottom to top).
Mask Data:
====================================================================================
Is a string of bytes with mask bits starting at MSB and going towards LSB.
There are ((imagewidth+31)>>5) DWORDS per row in *BIG ENDIAN* order.
Like the color data, there is a set of DWORDS for each row.
The rows are stored in reverse order (bottom to top).
Set unused padding bits/pixels at end of each row to 1.
0 indicates opaque and 1 indicates transparent.
1bit ... XOR Mask, AND Mask, & Color Data:
====================================================================================
The Palette is Little Endian 4 byte RGB0 values. No alpha.
There should be exactly 2 palette entries: usually 0x00000000 and 0x00FFFFFF.
The two masks follow the Mask Data format decribed above.
The following results from combining two mask bits:
XOR AND RESULT:
0 0 Color #0 (Black)
0 1 Transparent
1 0 Color #1 (White)
1 1 Invert Destination Bitmap
Of course I wouldn't have left it at this. There's code to be had!
The following code will load up and convert all of the icon images for a given icon location to a vector of 32bpp BITMAPs. If loading a given image fails, it will simply just not be added to the vector (or, in the case of a corrupt icon, it will most likely generate a corrupted image, so be careful).
The code does not support the "invert" color in monochrome images, and will just generate a different color that still has zero alpha.
WARNING: Some psuedo-code is included to shorten things to just the essentials.
Icon Loader Code (Supports: EXE, DLL, 32bit ICL, ICO):
// Code written by Simion32.
// Please feel free to use it anywhere.
// Credit would be nice but isn't required.
#include "include.h" //std::vectors and whatever else you need
#include <allegro.h>
#include <winalleg.h> //Allegro and Win32
#include "Shellapi.h"
// In the following block, the (required!!) pragmas
// force correct data alignment. Needed in at least GCC.
#pragma pack( push, 1 )
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
{
BITMAPINFOHEADER icHeader; // DIB header
RGBQUAD icColors[1]; // Color table
BYTE icXOR[1]; // DIB bits for XOR mask
BYTE icAND[1]; // DIB bits for AND mask
} ICONIMAGE, *LPICONIMAGE;
#pragma pack( pop)
#pragma pack( push, 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
WORD wPlanes; // Color Planes
WORD wBitCount; // Bits per pixel
DWORD dwBytesInRes; // total size of the RT_ICON resource referenced by the nID member.
WORD nID; // resourceID of RT_ICON (LockResource to obtain a pointer to its ICONIMAGE)
} 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 )
uint32_t Convert16BitToARGB(uint16_t value)
{
return (0xFF000000|((value >> 7) & 0x0000F8)|((value << 6) & 0x00F800)|((value << 19) & 0xF80000));
}
uint32_t GetMaskBit(uint8_t* data, int x, int y, int w, int h)
{
uint32_t mask_data_rowsize = (((w+31)>>5) * 4);
return ((~(data[(mask_data_rowsize * ((h-1)-y)) + (x >> 3)] >> (0x07 - (x & 0x07))) & 1) * 0xFFFFFFFF);
}
uint32_t GetColorMonochrome(uint8_t* xordata, uint8_t* anddata, int x, int y, int w, int h, uint32_t* pal)
{
uint32_t mask_data_rowsize = (((w+31)>>5) * 4);
uint32_t xor_bit = (((xordata[(mask_data_rowsize * ((h-1)-y)) + (x >> 3)] >> (0x07 - (x & 0x07))) << 1) & 2);
uint32_t and_bit = (((anddata[(mask_data_rowsize * ((h-1)-y)) + (x >> 3)] >> (0x07 - (x & 0x07))) ) & 1);
uint32_t value = (xor_bit | and_bit);
return pal[value];
}
BITMAP* CreateBmp32bppFromIconResData(void* data, int size, int depth, int w, int h, int colors)
{
char* pngheader = "\211PNG\r\n\032\n";
char* cpd = (char*)data;
bool is_png = ((cpd[0]==pngheader[0])
&& (cpd[1]==pngheader[1])
&& (cpd[2]==pngheader[2])
&& (cpd[3]==pngheader[3])
&& (cpd[4]==pngheader[4])
&& (cpd[5]==pngheader[5])
&& (cpd[6]==pngheader[6])
&& (cpd[7]==pngheader[7]));
if(is_png)
{
//###########################################################
//# PSEUDO-CODE: Somehow convert the PNG file into a bitmap.
BITMAP* result = ConvertPngFileToBmp32bpp(data, size);
return result;
}
else
{
uint32_t ignore_size = ((BITMAPINFOHEADER*)(data))->biSize;
BITMAP* bmp = create_bitmap_ex(32,w,h);
uint32_t pixel_count = (w * h);
uint32_t color_data_size = ((((((w * depth)+7) >> 3) +3) & ~3) * h);
switch(depth)
{
default: return bmp; break;
case 32:
{
uint32_t* src = (uint32_t*)(((uint8_t*)data) + ignore_size);
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
_putpixel32(bmp,xx,yy,src[0]);
src++;
}
//There should never be any padding to jump over here.
}
return bmp;
}
break;
case 24:
{
uint32_t* src = (uint32_t*)(((uint8_t*)data) + ignore_size);
uint8_t* bitmask = (uint8_t*)(((uint8_t*)data) + ignore_size + color_data_size);
int padding_checker = 0;
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
_putpixel32(bmp,xx,yy,((src[0] & 0x00FFFFFF) | 0xFF000000) & GetMaskBit(bitmask, xx, yy, w, h));
src++;
src = (uint32_t*)(((uint8_t*)src)-1); //go back a byte due to packing
padding_checker += 3;
padding_checker &= 3;
}
//This loop jumps over any padding bytes.
while(padding_checker)
{
src = (uint32_t*)(((uint8_t*)src)+1);
padding_checker++;
padding_checker &= 3;
}
}
return bmp;
}
break;
case 16:
{
//Note: there might be a color table present! ignore it.
uint16_t* src = (uint16_t*)(((uint8_t*)data) + ignore_size + (colors << 2));
uint8_t* bitmask = (uint8_t*)(((uint8_t*)data) + ignore_size + (colors << 2) + color_data_size);
int padding_checker = 0;
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
_putpixel32(bmp,xx,yy,Convert16BitToARGB(src[0]) & GetMaskBit(bitmask, xx, yy, w, h));
src++;
padding_checker += 2;
padding_checker &= 3;
}
//This loop jumps over any padding bytes.
while(padding_checker)
{
src = (uint16_t*)(((uint8_t*)src)+1);
padding_checker++;
padding_checker &= 3;
}
}
return bmp;
}
break;
case 8:
{
if(colors > 256) colors = 256; //Color Count must be restricted to 256 entries at the most.
if(colors <= 0) colors = 256; //Color Count might be reported as zero. This means 256.
uint8_t* src = (((uint8_t*)data) + ignore_size + (colors << 2));
uint32_t* pal = ((uint32_t*)(((uint8_t*)data) + ignore_size));
uint8_t* bitmask = (uint8_t*)(((uint8_t*)data) + ignore_size + (colors << 2) + color_data_size);
int padding_checker = 0;
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
uint8_t color = src[0];
if(color < colors){
_putpixel32(bmp,xx,yy,(pal[color] | 0xFF000000) & GetMaskBit(bitmask, xx, yy, w, h));
}else{
_putpixel32(bmp,xx,yy,0x00FF00FF);
}
src++;
padding_checker++;
padding_checker &= 3;
}
//This loop jumps over any padding bytes.
while(padding_checker)
{
src++;
padding_checker++;
padding_checker &= 3;
}
}
return bmp;
}
break;
case 4:
{
if(colors > 16) colors = 16; //Color Count must be restricted to 16 entries at the most.
if(colors <= 0) colors = 16; //Color Count might be reported as zero. This means 16.
uint8_t* src = (((uint8_t*)data) + ignore_size + (colors << 2));
uint32_t* pal = ((uint32_t*)(((uint8_t*)data) + ignore_size));
uint8_t* bitmask = (uint8_t*)(((uint8_t*)data) + ignore_size + (colors << 2) + color_data_size);
int padding_checker = 0;
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
uint8_t color = src[0];
if(xx & 1) color = ( color & 0x0F);
else color = ((color >> 4) & 0x0F);
if(color < colors){
_putpixel32(bmp,xx,yy,(pal[color] | 0xFF000000) & GetMaskBit(bitmask, xx, yy, w, h));
}else{
_putpixel32(bmp,xx,yy,0x00FF00FF);
}
if(xx & 1)
{
src++;
padding_checker++;
padding_checker &= 3;
}
}
//if the pointer hasn't incremented to the next byte yet, do so.
if(w & 1) //odd width
{
src++;
padding_checker++;
padding_checker &= 3;
}
//This loop jumps over any padding bytes.
while(padding_checker)
{
src++;
padding_checker++;
padding_checker &= 3;
}
}
return bmp;
}
break;
case 1:
{
if(colors > 2) colors = 2; //Color Count must be restricted to 2 entries at the most.
if(colors <= 0) colors = 2; //Color Count might be reported as zero. This means 2.
uint32_t* pal = (uint32_t*)(((uint8_t*)data) + ignore_size);
uint8_t* bitmaskXOR = (uint8_t*)(((uint8_t*)data) + ignore_size + (colors << 2));
uint8_t* bitmaskAND = (uint8_t*)(((uint8_t*)data) + ignore_size + (colors << 2) + color_data_size);
uint32_t ret_colors[4] = {pal[0]|0xFF000000, 0x00FF00FF, pal[1]|0xFF000000, 0x0000FF00};
for(int yy = h-1; yy >= 0; --yy){
for(int xx = 0; xx < w; ++xx){
_putpixel32(bmp,xx,yy,GetColorMonochrome(bitmaskXOR, bitmaskAND, xx, yy, w, h, ret_colors));
}
}
return bmp;
}
break;
}
return bmp;
}
}
vector< BITMAP* > ResourceToBitmapVector(HMODULE hm, HRSRC hr, bool is_group_icon)
{
vector< BITMAP* > results;
if(is_group_icon)
{
HGLOBAL hg = LoadResource(hm,hr);
GRPICONDIR* gd = (GRPICONDIR*)LockResource(hg);
if(gd->idType == 1)
{
for(int i = 0; i < gd->idCount; ++i)
{
//WARNING: The GRPICONDIRENTRY's data might be wrong!
GRPICONDIRENTRY* ie = (GRPICONDIRENTRY*)&(gd->idEntries[i]);
HRSRC ihr = FindResource(hm,MAKEINTRESOURCE(ie->nID),RT_ICON);
if(ihr != NULL)
{
HGLOBAL ihg = LoadResource(hm,ihr);
void* data = (void*)LockResource(ihg);
DWORD size = SizeofResource(hm,ihr);
uint32_t b = ((BITMAPINFOHEADER*)(data))->biBitCount;
uint32_t w = ((BITMAPINFOHEADER*)(data))->biWidth;
uint32_t h = (((BITMAPINFOHEADER*)(data))->biHeight >> 1); //icons have doubled height value.
uint32_t c = ((BITMAPINFOHEADER*)(data))->biClrUsed;
results.push_back(CreateBmp32bppFromIconResData(data, size, b, w, h, c));
}
}
}
}
else
{
HGLOBAL ihg = LoadResource(hm,hr);
void* data = (void*)LockResource(ihg);
DWORD size = SizeofResource(hm,hr);
uint32_t b = ((BITMAPINFOHEADER*)(data))->biBitCount;
uint32_t w = ((BITMAPINFOHEADER*)(data))->biWidth;
uint32_t h = (((BITMAPINFOHEADER*)(data))->biHeight >> 1); //icons have doubled height value.
uint32_t c = ((BITMAPINFOHEADER*)(data))->biClrUsed;
results.push_back(CreateBmp32bppFromIconResData(data, size, b, w, h, c));
}
return results;
}
vector< BITMAP* > IconFileToBitmapVector(void* icon_data, uint32_t icon_size)
{
vector< BITMAP* > results;
ICONDIR* gd = (ICONDIR*)icon_data;
if(gd->idType == 1)
{
for(int i = 0; i < gd->idCount; ++i)
{
//WARNING: The ICONDIRENTRY's data might be wrong!
DWORD offset = gd->idEntries[i].dwImageOffset;
DWORD size = gd->idEntries[i].dwBytesInRes;
void* data = (void*)(((uint8_t*)icon_data) + ((uint32_t)offset));
uint32_t b = ((BITMAPINFOHEADER*)(data))->biBitCount;
uint32_t w = ((BITMAPINFOHEADER*)(data))->biWidth;
uint32_t h = (((BITMAPINFOHEADER*)(data))->biHeight >> 1); //icons have doubled height value.
uint32_t c = ((BITMAPINFOHEADER*)(data))->biClrUsed;
results.push_back(CreateBmp32bppFromIconResData(data, size, b, w, h, c));
}
}
return results;
}
vector< BITMAP* > UnearthIconResource(string& file, bool self_refrence, bool res_index, int index)
{
#define LOAD_IGNORE_CODE_AUTHZ_LEVEL 0x00000010
//prevents a negative indexing error
// (the boolean res_index handles whether it's icon index VS resource ID)
index = abs(index);
vector< BITMAP* > results; //array of results to return (pointers to 32bpp images)
//extract and 'demangle' the file extension by convertng to lowercase.
string ext = get_file_extension(file.c_str());
for(int i = 0; i < ext.size(); ++i) ext[i] = tolower(ext[i]);
bool is_icl = false;
if((ext == "exe") || (ext == "dll") || (ext == "scr") || (is_icl = (ext == "icl")))
{
// Portable Executable Resource (works for both DLL and EXE)
// Also works for any 32bit Icon Library (Microangelo Studio?)
HMODULE hm = LoadLibraryEx(file.c_str(), NULL,
(DONT_RESOLVE_DLL_REFERENCES | LOAD_IGNORE_CODE_AUTHZ_LEVEL | LOAD_LIBRARY_AS_DATAFILE));
if(hm != NULL)
{
HRSRC hr;
if(!self_refrence)
{
if(res_index)
{
//The icon we want is at the resource ID (==index)
bool is_single_icon = false;
hr = FindResource(hm,MAKEINTRESOURCE(index),RT_GROUP_ICON);
if(hr == NULL)
{
hr = FindResource(hm,MAKEINTRESOURCE(index),RT_ICON);
is_single_icon = (hr != NULL);
}
if(hr != NULL)
{
results = ResourceToBitmapVector(hm, hr, !is_single_icon);
}
}
else
{
//The icon we want is the (index)'th icon in the file
//We must preform a manual search for the resource ID!
//WARNING: Using EnumResourceNames() *DOES NOT WORK PROPERLY* for this.
for(int nicon = 0, i = 0; i < 0x8000; ++i)
{
bool is_single_icon = false;
hr = FindResource(hm,MAKEINTRESOURCE(i),RT_GROUP_ICON);
if(hr != NULL)
{
if(nicon == index)
{
results = ResourceToBitmapVector(hm, hr, true);
break;
}
nicon++;
}
}
}
}
else
{
//The icon we want is the "first" icon in the file.
//Happens when location is a %1.
//We must preform a manual search for the resource ID!
//WARNING: Using EnumResourceNames() *DOES NOT WORK PROPERLY* for this.
for(int i = 0; i < 0x8000; ++i)
{
bool is_single_icon = false;
hr = FindResource(hm,MAKEINTRESOURCE(i),RT_GROUP_ICON);
if(hr != NULL)
{
results = ResourceToBitmapVector(hm, hr, true);
break;
}
}
}
FreeLibrary(hm);
}
else /*if(is_icl)
{//OH NOES. We have to load a *16bit* .icl file!
//not supported yet. sorry. left as another excecise to the programmer.
}*/
}
else if(ext == "ico")
{
//Single Icon File
//###################################################
//# PSEUDO-CODE: Do the file loading yourself ;)
void* data_pointer = NULL;
uint32_t data_size = 0;
if(data_pointer = MyLoadFile(file.c_str(), &data_size))
{
if(data_size)
{
results = IconFileToBitmapVector((void*)data_pointer, data_size);
}
}
MyCloseFile(data_pointer);
}
return results;
}
I think that almost covers it all...
One last thing I should mention: Be sure to ignore the size and bit depth information coming from the icon directory entries. They can often be wrong. I've seen a few 256-color images reported as 24bit, causing data corruption inside the image loader.
Wow, talk about reinventing the wheel!
With all due respect, this code is so bloated for nothing. I (and probably thousands of others) achieved the exact same result with 1/10 of this code. Also, this solution contains many inaccuracies.
Here's a quick run-down:
Why parse the registry manually? You state the API has some problems; like what? I've used reg parsing API extensively and never had a problem! The Indexing vs ResID logic is correct though.
Why do all the icon to bitmap conversions manually? This can be achieved with 3 to 5 lines of code using the right Icon API calls. Here's a complete reference.
Why limit the conversion to 32bpp? Again, using the right APIs will generate a device dependent hIcon handle with the max color bit-depth supported by that device. Check out the CreateIconFromResourceEx() API function. All you need to do is combine it with the Find/Load/Lock Resource APIs that you're already using. Using this technique will load icons of any size and color depth (from monochrome up to alpha-channel 32bpp icons).
Finally, regarding the search for icon resources by group (RT_GROUP_ICON), or by single icons (RT_ICON), and matching for a given index instead of resource, it could be done much more efficiently using EnumResourceNames(). It might be that you've failed to account for string resource identifiers when parsing the Enum return, because it seems you've omitted such case in your manual search and match procedure. This might be the source of your problems with EnumResourceNames(). It works perfectly fine for me and for others in countless online samples. At the very least, the "manual" search should match up to 0xFFFF rather than 0x8000. Res IDs are recommended in the 0x0001 to 0x8000 range, but legal in the 0x0000 to 0xFFFF range.
If it does have not to be platform independent:
a bit time ago i wrote a little class that reads a file and extract all icons.
It retreives a std::vector with HICONs.
With GetIconInfo you can retreive the HBITMAP for pixeldata an pixelmask.
The function is a little bit heuristic. It scans the binary Data for a typical icon begin and tries to load them.
The function also works on dlls, exe or icl (16bit dlls that just contain icon resources)
#ifndef __ICON_LIST_H__
#define __ICON_LIST_H__
#include <windows.h>
#include <vector>
class IconFile: public std::vector<HICON>{
public:
IconFile(){};
IconFile(std::string i_filename){
addIconsFromFile(i_filename);
};
int addIconsFromFile(std::string i_fileName){
int iCount=0;
HANDLE file = CreateFile( i_fileName.c_str(), GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, 0, NULL);
if(file!=INVALID_HANDLE_VALUE){
int size = GetFileSize(file,NULL);
DWORD actRead;
BYTE* buffer = new BYTE[size];
ReadFile(file, buffer, size, &actRead, NULL);
CloseHandle(file);
int ind = -1;
for(int p = 0; p< size-4; ++p){
if(buffer[p]==40 && buffer[p+1]==0 && buffer[p+2]==0 && buffer[p+3]==0){
HICON icon = CreateIconFromResourceEx(&buffer[p], size-p, true, 0x00030000,0,0,0);
if(icon){
++iCount;
this->push_back(icon);
}
}
}
delete[] buffer;
}
return iCount;
};
};
#endif //__ICON_LIST_H__