📄 qimage.cpp
字号:
"a c #cec6bd", .... \endcode The extra \c const makes the entire definition read-only, which is slightly more efficient (e.g. when the code is in a shared library) and ROMable when the application is to be stored in ROM.*/QImage::QImage(const char * const xpm[]) : QPaintDevice(){ d = 0; if (!qt_read_xpm_image_or_array(0, xpm, *this)) { // We use a qFatal rather than disabling the whole function, // as this constructor may be ambiguous. qFatal("QImage::QImage(), XPM is not supported"); }}#endif // QT_NO_IMAGEFORMAT_XPM/*! \fn QImage::QImage(const QByteArray &data) Use the static fromData() function instead. \oldcode QByteArray data; ... QImage image(data); \newcode QByteArray data; ... QImage image = QImage::fromData(data); \endcode*//*! Constructs a shallow copy of the given \a image. For more information about shallow copies, see the \l {Implicit Data Sharing} documentation. \sa copy()*/QImage::QImage(const QImage &image) : QPaintDevice(){ d = image.d; if (d) d->ref.ref();}#ifdef QT3_SUPPORT/*! \fn QImage::QImage(int width, int height, int depth, int numColors, Endian bitOrder) Constructs an image with the given \a width, \a height, \a depth, \a numColors colors and \a bitOrder. Use the constructor that accepts a width, a height and a format (i.e. specifying the depth and bit order), in combination with the setNumColors() function, instead. \oldcode QImage image(width, height, depth, numColors); \newcode QImage image(width, height, format); // For 8 bit images the default number of colors is 256. If // another number of colors is required it can be specified // using the setNumColors() function. image.setNumColors(numColors); \endcode*/QImage::QImage(int w, int h, int depth, int numColors, Endian bitOrder) : QPaintDevice(){ d = QImageData::create(QSize(w, h), formatFor(depth, bitOrder), numColors);}/*! Constructs an image with the given \a size, \a depth, \a numColors and \a bitOrder. Use the constructor that accepts a size and a format (i.e. specifying the depth and bit order), in combination with the setNumColors() function, instead. \oldcode QSize mySize(width, height); QImage image(mySize, depth, numColors); \newcode QSize mySize(width, height); QImage image(mySize, format); // For 8 bit images the default number of colors is 256. If // another number of colors is required it can be specified // using the setNumColors() function. image.setNumColors(numColors); \endcode*/QImage::QImage(const QSize& size, int depth, int numColors, Endian bitOrder) : QPaintDevice(){ d = QImageData::create(size, formatFor(depth, bitOrder), numColors);}/*! \fn QImage::QImage(uchar* data, int width, int height, int depth, const QRgb* colortable, int numColors, Endian bitOrder) Constructs an image with the given \a width, \a height, depth, \a colortable, \a numColors and \a bitOrder, that uses an existing memory buffer, \a data. Use the constructor that accepts a uchar pointer, a width, a height and a format (i.e. specifying the depth and bit order), in combination with the setColorTable() function, instead. \oldcode uchar *myData; QRgb *myColorTable; QImage image(myData, width, height, depth, myColorTable, numColors, IgnoreEndian); \newcode uchar *myData; QVector<QRgb> myColorTable; QImage image(myData, width, height, format); image.setColorTable(myColorTable); \endcode*/QImage::QImage(uchar* data, int w, int h, int depth, const QRgb* colortable, int numColors, Endian bitOrder) : QPaintDevice(){ d = 0; Format f = formatFor(depth, bitOrder); if (f == Format_Invalid) return; if (w <= 0 || h <= 0 || numColors < 0 || !data) return; // invalid parameter(s) d = new QImageData; d->ref.ref(); d->own_data = false; d->data = data; d->width = w; d->height = h; d->depth = depth; d->format = f; if (depth == 32) numColors = 0; d->bytes_per_line = ((w*depth+31)/32)*4; // bytes per scanline d->nbytes = d->bytes_per_line * h; if (colortable) { d->colortable.resize(numColors); for (int i = 0; i < numColors; ++i) d->colortable[i] = colortable[i]; } else if (numColors) { setNumColors(numColors); }}#endif#ifdef Q_WS_QWS/*! \fn QImage::QImage(uchar* data, int width, int height, int depth, int bytesPerLine, const QRgb* colortable, int numColors, Endian bitOrder) Constructs an image with the given \a width, \a height, \a depth, \a bytesPerLine, \a colortable, \a numColors and \a bitOrder, that uses an existing memory buffer, \a data. The image does not delete the buffer at destruction. \warning This constructor is only available in Qtopia Core. The data has to be 32-bit aligned, so it's no longer possible to specify a custom \a bytesPerLine.*/QImage::QImage(uchar* data, int w, int h, int depth, int bpl, const QRgb* colortable, int numColors, Endian bitOrder) : QPaintDevice(){ d = 0; Format f = formatFor(depth, bitOrder); if (f == Format_Invalid) return; if (!data || w <= 0 || h <= 0 || depth <= 0 || numColors < 0) return; // invalid parameter(s) d = new QImageData; d->ref.ref(); d->own_data = false; d->data = data; d->width = w; d->height = h; d->depth = depth; d->format = f; if (depth == 32) numColors = 0; d->bytes_per_line = bpl; d->nbytes = d->bytes_per_line * h; if (colortable) { d->colortable.reserve(numColors); for (int i = 0; i < numColors; ++i) d->colortable[i] = colortable[i]; } else if (numColors) { setNumColors(numColors); }}#endif // Q_WS_QWS/*! Destroys the image and cleans up.*/QImage::~QImage(){ if (d && !d->ref.deref()) { if (qt_image_cleanup_hook) qt_image_cleanup_hook(d->ser_no); delete d; }}/*! Assigns a shallow copy of the given \a image to this image and returns a reference to this image. For more information about shallow copies, see the \l {Implicit Data Sharing} documentation. \sa copy(), QImage()*/QImage &QImage::operator=(const QImage &image){ QImageData *x = image.d; if (x) x->ref.ref(); x = qAtomicSetPtr(&d, x); if (x && !x->ref.deref()) delete x; return *this;}/*! \internal*/int QImage::devType() const{ return QInternal::Image;}/*! Returns the image as a QVariant.*/QImage::operator QVariant() const{ return QVariant(QVariant::Image, this);}/*! \internal If multiple images share common data, this image makes a copy of the data and detaches itself from the sharing mechanism, making sure that this image is the only one referring to the data. Nothing is done if there is just a single reference. \sa copy(), isDetached(), {Implicit Data Sharing}*/void QImage::detach(){ if (d) { ++d->detach_no; if (d->ref != 1) *this = copy(); }}/*! \fn QImage QImage::copy(int x, int y, int width, int height) const \overload The returned image is copied from the position (\a x, \a y) in this image, and will always have the given \a width and \a height. In areas beyond this image, pixels are set to 0.*//*! \fn QImage QImage::copy(const QRect& rectangle) const Returns a sub-area of the image as a new image. The returned image is copied from the position (\a {rectangle}.x(), \a{rectangle}.y()) in this image, and will always have the size of the given \a rectangle. In areas beyond this image, pixels are set to 0. For 32-bit RGB images, this means black; for 32-bit ARGB images, this means transparent black; for 8-bit images, this means the color with index 0 in the color table which can be anything; for 1-bit images, this means Qt::color0. If the given \a rectangle is a null rectangle the entire image is copied. \sa QImage()*/QImage QImage::copy(const QRect& r) const{ if (!d) return QImage(); if (r.isNull()) { QImage image(d->width, d->height, d->format); if (image.isNull()) return image;#ifdef Q_WS_QWS // Qtopia Core can create images with non-default bpl // make sure we don't crash. if (image.d->nbytes != d->nbytes) { int bpl = image.bytesPerLine(); for (int i = 0; i < height(); i++) memcpy(image.scanLine(i), scanLine(i), bpl); } else#endif memcpy(image.bits(), bits(), d->nbytes); image.d->colortable = d->colortable; image.d->dpmx = d->dpmx; image.d->dpmy = d->dpmy; image.d->offset = d->offset; image.d->has_alpha_clut = d->has_alpha_clut;#ifndef QT_NO_IMAGE_TEXT image.d->text_lang = d->text_lang;#endif return image; } int x = r.x(); int y = r.y(); int w = r.width(); int h = r.height(); int dx = 0; int dy = 0; if (w <= 0 || h <= 0) return QImage(); QImage image(w, h, d->format); if (image.isNull()) return image; if (x < 0 || y < 0 || x + w > d->width || y + h > d->height) { // bitBlt will not cover entire image - clear it. image.fill(0); if (x < 0) { dx = -x; x = 0; } if (y < 0) { dy = -y; y = 0; } } image.d->colortable = d->colortable; int pixels_to_copy = w - dx; if (x > d->width) pixels_to_copy = 0; else if (pixels_to_copy > d->width - x) pixels_to_copy = d->width - x; int lines_to_copy = h - dy; if (y > d->height) lines_to_copy = 0; else if (lines_to_copy > d->height - y) lines_to_copy = d->height - y; bool byteAligned = true; if (d->format == Format_Mono || d->format == Format_MonoLSB) byteAligned = !(dx & 7) && !(x & 7) && !(pixels_to_copy & 7); if (byteAligned) { const uchar *src = d->data + ((x * d->depth) >> 3) + y * d->bytes_per_line; uchar *dest = image.d->data + ((dx * d->depth) >> 3) + dy * image.d->bytes_per_line; const int bytes_to_copy = (pixels_to_copy * d->depth) >> 3; for (int i = 0; i < lines_to_copy; ++i) { memcpy(dest, src, bytes_to_copy); src += d->bytes_per_line; dest += image.d->bytes_per_line; } } else if (d->format == Format_Mono) { const uchar *src = d->data + y * d->bytes_per_line; uchar *dest = image.d->data + dy * image.d->bytes_per_line; for (int i = 0; i < lines_to_copy; ++i) { for (int j = 0; j < pixels_to_copy; ++j) { if (src[(x + j) >> 3] & (0x80 >> ((x + j) & 7))) dest[(dx + j) >> 3] |= (0x80 >> ((dx + j) & 7)); else dest[(dx + j) >> 3] &= ~(0x80 >> ((dx + j) & 7)); } src += d->bytes_per_line; dest += image.d->bytes_per_line; } } else { // Format_MonoLSB Q_ASSERT(d->format == Format_MonoLSB); const uchar *src = d->data + y * d->bytes_per_line; uchar *dest = image.d->data + dy * image.d->bytes_per_line; for (int i = 0; i < lines_to_copy; ++i) { for (int j = 0; j < pixels_to_copy; ++j) { if (src[(x + j) >> 3] & (0x1 << ((x + j) & 7))) dest[(dx + j) >> 3] |= (0x1 << ((dx + j) & 7)); else dest[(dx + j) >> 3] &= ~(0x1 << ((dx + j) & 7)); } src += d->bytes_per_line; dest += image.d->bytes_per_line;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -