📄 qdrawhelper.cpp
字号:
/******************************************************************************** Copyright (C) 1992-2007 Trolltech ASA. All rights reserved.**** This file is part of the QtGui module of the Qt Toolkit.**** This file may be used under the terms of the GNU General Public** License version 2.0 as published by the Free Software Foundation** and appearing in the file LICENSE.GPL included in the packaging of** this file. Please review the following information to ensure GNU** General Public Licensing requirements will be met:** http://trolltech.com/products/qt/licenses/licensing/opensource/**** If you are unsure which license is appropriate for your use, please** review the following information:** http://trolltech.com/products/qt/licenses/licensing/licensingoverview** or contact the sales department at sales@trolltech.com.**** In addition, as a special exception, Trolltech gives you certain** additional rights. These rights are described in the Trolltech GPL** Exception version 1.0, which can be found at** http://www.trolltech.com/products/qt/gplexception/ and in the file** GPL_EXCEPTION.txt in this package.**** In addition, as a special exception, Trolltech, as the sole copyright** holder for Qt Designer, grants users of the Qt/Eclipse Integration** plug-in the right for the Qt/Eclipse Integration to link to** functionality provided by Qt Designer and its related libraries.**** Trolltech reserves all rights not expressly granted herein.**** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.******************************************************************************/#include <private/qdrawhelper_p.h>#include <private/qpaintengine_raster_p.h>#include <private/qpainter_p.h>#include <private/qmath_p.h>#include <private/qdrawhelper_x86_p.h>#include <math.h>#define MASK(src, a) src = BYTE_MUL(src, a)#if defined(Q_OS_IRIX) && defined(Q_CC_GNU) && __GNUC__ == 3 && __GNUC__ < 4 && QT_POINTER_SIZE == 8#define Q_IRIX_GCC3_3_WORKAROUND//// work around http://gcc.gnu.org/bugzilla/show_bug.cgi?id=14484//static uint gccBug(uint value) __attribute__((noinline));static uint gccBug(uint value){ return value;}#endif/* constants and structures*/static const int fixed_scale = 1 << 16;static const int half_point = 1 << 15;static const int buffer_size = 2048;struct LinearGradientValues{ qreal dx; qreal dy; qreal l; qreal off;};struct RadialGradientValues{ qreal dx; qreal dy; qreal a;};struct Operator;typedef uint *QT_FASTCALL (*DestFetchProc)(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length);typedef void QT_FASTCALL (*DestStoreProc)(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length);typedef const uint *QT_FASTCALL (*SourceFetchProc)(uint *buffer, const Operator *o, const QSpanData *data, int y, int x, int length);struct Operator{ QPainter::CompositionMode mode; DestFetchProc dest_fetch; DestStoreProc dest_store; SourceFetchProc src_fetch; CompositionFunctionSolid funcSolid; CompositionFunction func; union { LinearGradientValues linear; RadialGradientValues radial;// TextureValues texture; };};/* Destination fetch. This is simple as we don't have to do bounds checks or transformations*/static uint * QT_FASTCALL destFetchMono(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length){ uchar *data = (uchar *)rasterBuffer->scanLine(y); uint *start = buffer; const uint *end = buffer + length; while (buffer < end) { *buffer = data[x>>3] & (0x80 >> (x & 7)) ? 0xff000000 : 0xffffffff; ++buffer; ++x; } return start;}static uint * QT_FASTCALL destFetchMonoLsb(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length){ uchar *data = (uchar *)rasterBuffer->scanLine(y); uint *start = buffer; const uint *end = buffer + length; while (buffer < end) { *buffer = data[x>>3] & (0x1 << (x & 7)) ? 0xff000000 : 0xffffffff; ++buffer; ++x; } return start;}static uint * QT_FASTCALL destFetchRGB32(uint *, QRasterBuffer *rasterBuffer, int x, int y, int){ uint *data = (uint *)rasterBuffer->scanLine(y) + x; // This should work without us having to fix the alpha channel manually.// for (int i = 0; i < length; ++i)// data[i] |= 0xff000000; return data;}static uint * QT_FASTCALL destFetchARGB32(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length){ const uint *data = (const uint *)rasterBuffer->scanLine(y) + x; for (int i = 0; i < length; ++i) buffer[i] = PREMUL(data[i]); return buffer;}static uint * QT_FASTCALL destFetchARGB32P(uint *, QRasterBuffer *rasterBuffer, int x, int y, int){ return (uint *)rasterBuffer->scanLine(y) + x;}static uint * QT_FASTCALL destFetchRGB16(uint *buffer, QRasterBuffer *rasterBuffer, int x, int y, int length){ const ushort *data = (const ushort *)rasterBuffer->scanLine(y) + x; for (int i = 0; i < length; ++i) buffer[i] = qConvertRgb16To32(data[i]); return buffer;}static const DestFetchProc destFetchProc[QImage::NImageFormats] ={ 0, // Format_Invalid destFetchMono, // Format_Mono, destFetchMonoLsb, // Format_MonoLSB 0, // Format_Indexed8 destFetchRGB32, // Format_RGB32 destFetchARGB32, // Format_ARGB32, destFetchARGB32P, // Format_ARGB32_Premultiplied destFetchRGB16 // Format_RGB16};/* Returns the color in the mono destination color table that is the "nearest" to /color/.*/static inline QRgb findNearestColor(QRgb color, QRasterBuffer *rbuf){ QRgb color_0 = PREMUL(rbuf->destColor0); QRgb color_1 = PREMUL(rbuf->destColor1); color = PREMUL(color); int r = qRed(color); int g = qGreen(color); int b = qBlue(color); int rx, gx, bx; int dist_0, dist_1; rx = r - qRed(color_0); gx = g - qGreen(color_0); bx = b - qBlue(color_0); dist_0 = rx*rx + gx*gx + bx*bx; rx = r - qRed(color_1); gx = g - qGreen(color_1); bx = b - qBlue(color_1); dist_1 = rx*rx + gx*gx + bx*bx; if (dist_0 < dist_1) return color_0; return color_1;}/* Destination store.*/static void QT_FASTCALL destStoreMono(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length){ uchar *data = (uchar *)rasterBuffer->scanLine(y); if (rasterBuffer->monoDestinationWithClut) { for (int i = 0; i < length; ++i) { if (buffer[i] == rasterBuffer->destColor0) { data[x >> 3] &= ~(0x80 >> (x & 7)); } else if (buffer[i] == rasterBuffer->destColor1) { data[x >> 3] |= 0x80 >> (x & 7); } else if (findNearestColor(buffer[i], rasterBuffer) == rasterBuffer->destColor0) { data[x >> 3] &= ~(0x80 >> (x & 7)); } else { data[x >> 3] |= 0x80 >> (x & 7); } ++x; } } else { for (int i = 0; i < length; ++i) { if (qGray(buffer[i]) < int(qt_bayer_matrix[y & 15][x & 15])) data[x >> 3] |= 0x80 >> (x & 7); else data[x >> 3] &= ~(0x80 >> (x & 7)); ++x; } }}static void QT_FASTCALL destStoreMonoLsb(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length){ uchar *data = (uchar *)rasterBuffer->scanLine(y); if (rasterBuffer->monoDestinationWithClut) { for (int i = 0; i < length; ++i) { if (buffer[i] == rasterBuffer->destColor0) { data[x >> 3] &= ~(1 << (x & 7)); } else if (buffer[i] == rasterBuffer->destColor1) { data[x >> 3] |= 1 << (x & 7); } else if (findNearestColor(buffer[i], rasterBuffer) == rasterBuffer->destColor0) { data[x >> 3] &= ~(1 << (x & 7)); } else { data[x >> 3] |= 1 << (x & 7); } ++x; } } else { for (int i = 0; i < length; ++i) { if (qGray(buffer[i]) < int(qt_bayer_matrix[y & 15][x & 15])) data[x >> 3] |= 1 << (x & 7); else data[x >> 3] &= ~(1 << (x & 7)); ++x; } }}static void QT_FASTCALL destStoreARGB32(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length){ uint *data = (uint *)rasterBuffer->scanLine(y) + x; for (int i = 0; i < length; ++i) data[i] = INV_PREMUL(buffer[i]);}static void QT_FASTCALL destStoreRGB16(QRasterBuffer *rasterBuffer, int x, int y, const uint *buffer, int length){ quint16 *data = (quint16*)rasterBuffer->scanLine(y) + x; qt_memconvert<quint16, quint32>(data, buffer, length);}static const DestStoreProc destStoreProc[QImage::NImageFormats] ={ 0, // Format_Invalid destStoreMono, // Format_Mono, destStoreMonoLsb, // Format_MonoLSB 0, // Format_Indexed8 0, // Format_RGB32 destStoreARGB32, // Format_ARGB32, 0, // Format_ARGB32_Premultiplied destStoreRGB16 // Format_RGB16};/* Source fetches This is a bit more complicated, as we need several fetch routines for every surface type We need 5 fetch methods per surface type: untransformed transformed transformed tiled transformed bilinear transformed bilinear tiled We don't need bounds checks for untransformed, but we need them for the other ones. The generic implementation does pixel by pixel fetches*/static uint QT_FASTCALL fetchPixel_Mono(const uchar *scanLine, int x, const QVector<QRgb> *rgb){ bool pixel = scanLine[x>>3] & (0x80 >> (x & 7)); if (rgb) return rgb->at(pixel ? 1 : 0); return pixel ? 0xff000000 : 0xffffffff;}static uint QT_FASTCALL fetchPixel_MonoLSB(const uchar *scanLine, int x, const QVector<QRgb> *rgb){ bool pixel = scanLine[x>>3] & (0x1 << (x & 7)); if (rgb) return rgb->at(pixel ? 1 : 0); return pixel ? 0xff000000 : 0xffffffff;}static uint QT_FASTCALL fetchPixel_Indexed8(const uchar *scanLine, int x, const QVector<QRgb> *rgb){ return PREMUL(rgb->at(scanLine[x]));}static uint QT_FASTCALL fetchPixel_RGB32(const uchar *scanLine, int x, const QVector<QRgb> *){ return ((const uint *)scanLine)[x] | 0xff000000;}static uint QT_FASTCALL fetchPixel_ARGB32(const uchar *scanLine, int x, const QVector<QRgb> *){ return PREMUL(((const uint *)scanLine)[x]);}static uint QT_FASTCALL fetchPixel_ARGB32_Premultiplied(const uchar *scanLine, int x, const QVector<QRgb> *){ return ((const uint *)scanLine)[x];}static uint QT_FASTCALL fetchPixel_RGB16(const uchar *scanLine, int x, const QVector<QRgb> *){ return qConvertRgb16To32(((const ushort *)scanLine)[x]);}typedef uint QT_FASTCALL (*FetchPixelProc)(const uchar *scanLine, int x, const QVector<QRgb> *);static const FetchPixelProc fetchPixelProc[QImage::NImageFormats] ={ 0, fetchPixel_Mono, fetchPixel_MonoLSB, fetchPixel_Indexed8, fetchPixel_RGB32, fetchPixel_ARGB32, fetchPixel_ARGB32_Premultiplied, fetchPixel_RGB16};enum TextureBlendType { BlendUntransformed, BlendTiled, BlendTransformed, BlendTransformedTiled, BlendTransformedBilinear, BlendTransformedBilinearTiled, NBlendTypes};static const uint * QT_FASTCALL fetch_generic(uint *buffer, const Operator *, const QSpanData *data, int y, int x, int length){ FetchPixelProc fetch = fetchPixelProc[data->texture.format]; const uchar *scanLine = data->texture.scanLine(y); for (int i = 0; i < length; ++i) buffer[i] = fetch(scanLine, x + i, data->texture.colorTable); return buffer;}static const uint * QT_FASTCALL fetchTransformed_generic(uint *buffer, const Operator *, const QSpanData *data, int y, int x, int length){ FetchPixelProc fetch = fetchPixelProc[data->texture.format]; int image_width = data->texture.width; int image_height = data->texture.height; // The increment pr x in the scanline int fdx = (int)(data->m11 * fixed_scale); int fdy = (int)(data->m12 * fixed_scale); bool affine = !data->m13 && !data->m23; int fx = int((data->m21 * (y + 0.5) + data->m11 * (x + 0.5) + data->dx) * fixed_scale); int fy = int((data->m22 * (y + 0.5) + data->m12 * (x + 0.5) + data->dy) * fixed_scale); const uint *end = buffer + length; uint *b = buffer; if (affine) { while (b < end) { int px = fx >> 16; int py = fy >> 16; bool out = (px < 0) || (px >= image_width) || (py < 0) || (py >= image_height); const uchar *scanLine = data->texture.scanLine(py); *b = out ? uint(0) : fetch(scanLine, px, data->texture.colorTable); fx += fdx; fy += fdy; ++b; } } else { int fdw = (int)(data->m13 * fixed_scale); int fw = int((data->m13 * (x + 0.5) + data->m23 * (y + 0.5) + 1.) * fixed_scale); if (!fw) fw = 1; while (b < end) { int px = fx/fw; int py = fy/fw; bool out = (px < 0) || (px >= image_width) || (py < 0) || (py >= image_height);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -