diff options
author | Harald Fernengel <harald@trolltech.com> | 2009-08-13 13:31:02 (GMT) |
---|---|---|
committer | Harald Fernengel <harald@trolltech.com> | 2009-08-13 13:31:41 (GMT) |
commit | 6d2a1051970a417c4e60375cb43f06751da57728 (patch) | |
tree | a9a4b3a0d7bf5ad8be8074ca8c4d9b932946db37 /src/gui/image | |
parent | 7bce80068d1e8cc54ac129abb53545d6fad69bc1 (diff) | |
download | Qt-6d2a1051970a417c4e60375cb43f06751da57728.zip Qt-6d2a1051970a417c4e60375cb43f06751da57728.tar.gz Qt-6d2a1051970a417c4e60375cb43f06751da57728.tar.bz2 |
Use QScopedPointer wherever possible
Ensures that we don't leak when creating a new Pixmap while running out
of memory
Diffstat (limited to 'src/gui/image')
-rw-r--r-- | src/gui/image/qbitmap.cpp | 11 | ||||
-rw-r--r-- | src/gui/image/qimage.cpp | 54 | ||||
-rw-r--r-- | src/gui/image/qpixmap.cpp | 32 |
3 files changed, 44 insertions, 53 deletions
diff --git a/src/gui/image/qbitmap.cpp b/src/gui/image/qbitmap.cpp index a1497bc..5827dc1 100644 --- a/src/gui/image/qbitmap.cpp +++ b/src/gui/image/qbitmap.cpp @@ -265,15 +265,12 @@ QBitmap QBitmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) img.setColor(1, c0); } - QPixmapData *d; QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem(); - if (gs) - d = gs->createPixmapData(QPixmapData::BitmapType); - else - d = QGraphicsSystem::createDefaultPixmapData(QPixmapData::BitmapType); + QScopedPointer<QPixmapData> data(gs ? gs->createPixmapData(QPixmapData::BitmapType) + : QGraphicsSystem::createDefaultPixmapData(QPixmapData::BitmapType)); - d->fromImage(img, flags | Qt::MonoOnly); - return QPixmap(d); + data->fromImage(img, flags | Qt::MonoOnly); + return QPixmap(data.take()); } /*! diff --git a/src/gui/image/qimage.cpp b/src/gui/image/qimage.cpp index c19c450..5e635d6 100644 --- a/src/gui/image/qimage.cpp +++ b/src/gui/image/qimage.cpp @@ -2374,8 +2374,9 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src, switch (dithermode) { case Diffuse: { - int *line1 = new int[w]; - int *line2 = new int[w]; + QScopedArrayPointer<int> lineBuffer(new int[w * 2]); + int *line1 = lineBuffer.data(); + int *line2 = lineBuffer.data() + w; int bmwidth = (w+7)/8; int *b1, *b2; @@ -2455,8 +2456,6 @@ static void dither_to_Mono(QImageData *dst, const QImageData *src, b2++; } } - delete [] line1; - delete [] line2; } break; case Ordered: { @@ -2597,10 +2596,9 @@ static void convert_X_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageC static void convert_ARGB_PM_to_Mono(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags) { - QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32); - convert_ARGB_PM_to_ARGB(tmp, src, flags); - dither_to_Mono(dst, tmp, flags, false); - delete tmp; + QScopedPointer<QImageData> tmp(QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32)); + convert_ARGB_PM_to_ARGB(tmp.data(), src, flags); + dither_to_Mono(dst, tmp.data(), flags, false); } // @@ -2749,15 +2747,16 @@ static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt:: int* line1[3]; int* line2[3]; int* pv[3]; - line1[0] = new int[src->width]; - line2[0] = new int[src->width]; - line1[1] = new int[src->width]; - line2[1] = new int[src->width]; - line1[2] = new int[src->width]; - line2[2] = new int[src->width]; - pv[0] = new int[src->width]; - pv[1] = new int[src->width]; - pv[2] = new int[src->width]; + QScopedArrayPointer<int> lineBuffer(new int[src->width * 9]); + line1[0] = lineBuffer.data(); + line2[0] = lineBuffer.data() + src->width; + line1[1] = lineBuffer.data() + src->width * 2; + line2[1] = lineBuffer.data() + src->width * 3; + line1[2] = lineBuffer.data() + src->width * 4; + line2[2] = lineBuffer.data() + src->width * 5; + pv[0] = lineBuffer.data() + src->width * 6; + pv[1] = lineBuffer.data() + src->width * 7; + pv[2] = lineBuffer.data() + src->width * 8; int endian = (QSysInfo::ByteOrder == QSysInfo::BigEndian); for (int y = 0; y < src->height; y++) { @@ -2820,15 +2819,6 @@ static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt:: src_data += src->bytes_per_line; dest_data += dst->bytes_per_line; } - delete [] line1[0]; - delete [] line2[0]; - delete [] line1[1]; - delete [] line2[1]; - delete [] line1[2]; - delete [] line2[2]; - delete [] pv[0]; - delete [] pv[1]; - delete [] pv[2]; } else { // OrderedDither for (int y = 0; y < src->height; y++) { const QRgb *p = (const QRgb *)src_data; @@ -2861,8 +2851,8 @@ static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt:: const int trans = 216; Q_ASSERT(dst->colortable.size() > trans); dst->colortable[trans] = 0; - QImageData *mask = QImageData::create(QSize(src->width, src->height), QImage::Format_Mono); - dither_to_Mono(mask, src, flags, true); + QScopedPointer<QImageData> mask(QImageData::create(QSize(src->width, src->height), QImage::Format_Mono)); + dither_to_Mono(mask.data(), src, flags, true); uchar *dst_data = dst->data; const uchar *mask_data = mask->data; for (int y = 0; y < src->height; y++) { @@ -2874,7 +2864,6 @@ static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt:: dst_data += dst->bytes_per_line; } dst->has_alpha_clut = true; - delete mask; } #undef MAX_R @@ -2887,10 +2876,9 @@ static void convert_RGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt:: static void convert_ARGB_PM_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags) { - QImageData *tmp = QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32); - convert_ARGB_PM_to_ARGB(tmp, src, flags); - convert_RGB_to_Indexed8(dst, tmp, flags); - delete tmp; + QScopedPointer<QImageData> tmp(QImageData::create(QSize(src->width, src->height), QImage::Format_ARGB32)); + convert_ARGB_PM_to_ARGB(tmp.data(), src, flags); + convert_RGB_to_Indexed8(dst, tmp.data(), flags); } static void convert_ARGB_to_Indexed8(QImageData *dst, const QImageData *src, Qt::ImageConversionFlags flags) diff --git a/src/gui/image/qpixmap.cpp b/src/gui/image/qpixmap.cpp index e05068d..93077d7 100644 --- a/src/gui/image/qpixmap.cpp +++ b/src/gui/image/qpixmap.cpp @@ -852,11 +852,16 @@ bool QPixmap::load(const QString &fileName, const char *format, Qt::ImageConvers QImage image = QImageReader(fileName, format).read(); if (image.isNull()) return false; + QPixmap pm; - if (data->pixelType() == QPixmapData::BitmapType) - pm = QBitmap::fromImage(image, flags); - else - pm = fromImage(image, flags); + QT_TRY { + if (data->pixelType() == QPixmapData::BitmapType) + pm = QBitmap::fromImage(image, flags); + else + pm = fromImage(image, flags); + } QT_CATCH (const std::bad_alloc &) { + // swallow bad allocs and leave pm a null pixmap + } if (!pm.isNull()) { *this = pm; QPixmapCache::insert(key, *this); @@ -1988,15 +1993,16 @@ QPixmap QPixmap::fromImage(const QImage &image, Qt::ImageConversionFlags flags) if (image.isNull()) return QPixmap(); - QPixmapData *data; - QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem(); - if (gs) - data = gs->createPixmapData(QPixmapData::PixmapType); - else - data = QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType); - - data->fromImage(image, flags); - return QPixmap(data); + QT_TRY { + QGraphicsSystem* gs = QApplicationPrivate::graphicsSystem(); + QScopedPointer<QPixmapData> data(gs ? gs->createPixmapData(QPixmapData::PixmapType) + : QGraphicsSystem::createDefaultPixmapData(QPixmapData::PixmapType)); + data->fromImage(image, flags); + return QPixmap(data.take()); + } QT_CATCH(const std::bad_alloc &) { + // we're out of memory - return a null Pixmap + return QPixmap(); + } } /*! |