Skip to content

Instantly share code, notes, and snippets.

@kanryu
Created March 26, 2017 16:13
Show Gist options
  • Select an option

  • Save kanryu/a92fe87fb1f60b6d248a4c9fdcd1609e to your computer and use it in GitHub Desktop.

Select an option

Save kanryu/a92fe87fb1f60b6d248a4c9fdcd1609e to your computer and use it in GitHub Desktop.
Inside of QImage::QImage() Qt-5.7/5.7/Src/qtbase/src/gui/image/qimage.cpp
/*! \fn QImageData * QImageData::create(const QSize &size, QImage::Format format)
\internal
Creates a new image data.
Returns 0 if invalid parameters are give or anything else failed.
*/
QImageData * QImageData::create(const QSize &size, QImage::Format format)
{
if (!size.isValid() || format == QImage::Format_Invalid)
return 0; // invalid parameter(s)
uint width = size.width();
uint height = size.height();
uint depth = qt_depthForFormat(format);
const int bytes_per_line = ((width * depth + 31) >> 5) << 2; // bytes per scanline (must be multiple of 4)
// sanity check for potential overflows
if (INT_MAX/depth < width
|| bytes_per_line <= 0
|| height <= 0
|| INT_MAX/uint(bytes_per_line) < height
|| INT_MAX/sizeof(uchar *) < uint(height))
return 0;
QScopedPointer<QImageData> d(new QImageData);
switch (format) {
case QImage::Format_Mono:
case QImage::Format_MonoLSB:
d->colortable.resize(2);
d->colortable[0] = QColor(Qt::black).rgba();
d->colortable[1] = QColor(Qt::white).rgba();
break;
default:
break;
}
d->width = width;
d->height = height;
d->depth = depth;
d->format = format;
d->has_alpha_clut = false;
d->is_cached = false;
d->bytes_per_line = bytes_per_line;
d->nbytes = d->bytes_per_line*height;
d->data = (uchar *)malloc(d->nbytes);
if (!d->data) {
return 0;
}
d->ref.ref();
return d.take();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment