qimage_conversions.cpp: keep shared copies of gray and alpha color tables

Since QVector is implicitly shared, don't re-generate the same
two color tables all over again, but create them once and keep
them around in a Q_GLOBAL_STATIC.

Change-Id: I9a8d32021d8cc327264f2818a23beaae67fe3ee8
Reviewed-by: Allan Sandfeld Jensen <allan.jensen@qt.io>
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2017-02-04 12:41:17 +01:00
parent 1b46872cde
commit 9bf8cc1f18

View File

@ -46,6 +46,22 @@
QT_BEGIN_NAMESPACE QT_BEGIN_NAMESPACE
struct QDefaultColorTables
{
QDefaultColorTables()
: gray(256), alpha(256)
{
for (int i = 0; i < 256; ++i) {
gray[i] = qRgb(i, i, i);
alpha[i] = qRgba(0, 0, 0, i);
}
}
QVector<QRgb> gray, alpha;
};
Q_GLOBAL_STATIC(QDefaultColorTables, defaultColorTables);
// table to flip bits // table to flip bits
static const uchar bitflip[256] = { static const uchar bitflip[256] = {
/* /*
@ -1952,11 +1968,7 @@ static void convert_Alpha8_to_Indexed8(QImageData *dest, const QImageData *src,
memcpy(dest->data, src->data, src->bytes_per_line * src->height); memcpy(dest->data, src->data, src->bytes_per_line * src->height);
QVector<QRgb> colors(256); dest->colortable = defaultColorTables->alpha;
for (int i=0; i<256; ++i)
colors[i] = qRgba(0, 0, 0, i);
dest->colortable = colors;
} }
static void convert_Grayscale8_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags) static void convert_Grayscale8_to_Indexed8(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
@ -1966,22 +1978,15 @@ static void convert_Grayscale8_to_Indexed8(QImageData *dest, const QImageData *s
memcpy(dest->data, src->data, src->bytes_per_line * src->height); memcpy(dest->data, src->data, src->bytes_per_line * src->height);
QVector<QRgb> colors(256);
for (int i=0; i<256; ++i)
colors[i] = qRgb(i, i, i);
dest->colortable = colors; dest->colortable = defaultColorTables->gray;
} }
static bool convert_Alpha8_to_Indexed8_inplace(QImageData *data, Qt::ImageConversionFlags) static bool convert_Alpha8_to_Indexed8_inplace(QImageData *data, Qt::ImageConversionFlags)
{ {
Q_ASSERT(data->format == QImage::Format_Alpha8); Q_ASSERT(data->format == QImage::Format_Alpha8);
QVector<QRgb> colors(256); data->colortable = defaultColorTables->alpha;
for (int i=0; i<256; ++i)
colors[i] = qRgba(0, 0, 0, i);
data->colortable = colors;
data->format = QImage::Format_Indexed8; data->format = QImage::Format_Indexed8;
return true; return true;
@ -1991,11 +1996,7 @@ static bool convert_Grayscale8_to_Indexed8_inplace(QImageData *data, Qt::ImageCo
{ {
Q_ASSERT(data->format == QImage::Format_Grayscale8); Q_ASSERT(data->format == QImage::Format_Grayscale8);
QVector<QRgb> colors(256); data->colortable = defaultColorTables->gray;
for (int i=0; i<256; ++i)
colors[i] = qRgb(i, i, i);
data->colortable = colors;
data->format = QImage::Format_Indexed8; data->format = QImage::Format_Indexed8;
return true; return true;