Qt
Internal/Contributor docs for the Qt SDK. Note: These are NOT official API docs; those are found at https://doc.qt.io/
Loading...
Searching...
No Matches
qimage_ssse3.cpp
Go to the documentation of this file.
1// Copyright (C) 2016 The Qt Company Ltd.
2// SPDX-License-Identifier: LicenseRef-Qt-Commercial OR LGPL-3.0-only OR GPL-2.0-only OR GPL-3.0-only
3// Qt-Security score:significant reason:default
4
5#include <qimage.h>
6#include <private/qimage_p.h>
7#include <private/qsimd_p.h>
8
9#ifdef QT_COMPILER_SUPPORTS_SSSE3
10
11QT_BEGIN_NAMESPACE
12
13// Convert a scanline of RGB888 (src) to RGB32 (dst)
14// src must be at least len * 3 bytes
15// dst must be at least len * 4 bytes
16Q_GUI_EXPORT void QT_FASTCALL qt_convert_rgb888_to_rgb32_ssse3(quint32 *dst, const uchar *src, int len)
17{
18 int i = 0;
19
20 // Prologue, align dst to 16 bytes.
21 ALIGNMENT_PROLOGUE_16BYTES(dst, i, len) {
22 dst[i] = qRgb(src[0], src[1], src[2]);
23 src += 3;
24 }
25
26 // Mask the 4 first colors of the RGB888 vector
27 const __m128i shuffleMask = _mm_set_epi8(char(0xff), 9, 10, 11, char(0xff), 6, 7, 8, char(0xff), 3, 4, 5, char(0xff), 0, 1, 2);
28
29 // Mask the 4 last colors of a RGB888 vector with an offset of 1 (so the last 3 bytes are RGB)
30 const __m128i shuffleMaskEnd = _mm_set_epi8(char(0xff), 13, 14, 15, char(0xff), 10, 11, 12, char(0xff), 7, 8, 9, char(0xff), 4, 5, 6);
31
32 // Mask to have alpha = 0xff
33 const __m128i alphaMask = _mm_set1_epi32(0xff000000);
34
35 const __m128i *inVectorPtr = (const __m128i *)src;
36 __m128i *dstVectorPtr = (__m128i *)(dst + i);
37
38 for (; i < (len - 15); i += 16) { // one iteration in the loop converts 16 pixels
39 /*
40 RGB888 has 5 pixels per vector, + 1 byte from the next pixel. The idea here is
41 to load vectors of RGB888 and use palignr to select a vector out of two vectors.
42
43 After 3 loads of RGB888 and 3 stores of RGB32, we have 4 pixels left in the last
44 vector of RGB888, we can mask it directly to get a last store or RGB32. After that,
45 the first next byte is a R, and we can loop for the next 16 pixels.
46
47 The conversion itself is done with a byte permutation (pshufb).
48 */
49 __m128i firstSrcVector = _mm_lddqu_si128(inVectorPtr);
50 __m128i outputVector = _mm_shuffle_epi8(firstSrcVector, shuffleMask);
51 _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
52 ++inVectorPtr;
53 ++dstVectorPtr;
54
55 // There are 4 unused bytes left in srcVector, we need to load the next 16 bytes
56 // and load the next input with palignr
57 __m128i secondSrcVector = _mm_lddqu_si128(inVectorPtr);
58 __m128i srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 12);
59 outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
60 _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
61 ++inVectorPtr;
62 ++dstVectorPtr;
63 firstSrcVector = secondSrcVector;
64
65 // We now have 8 unused bytes left in firstSrcVector
66 secondSrcVector = _mm_lddqu_si128(inVectorPtr);
67 srcVector = _mm_alignr_epi8(secondSrcVector, firstSrcVector, 8);
68 outputVector = _mm_shuffle_epi8(srcVector, shuffleMask);
69 _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
70 ++inVectorPtr;
71 ++dstVectorPtr;
72
73 // There are now 12 unused bytes in firstSrcVector.
74 // We can mask them directly, almost there.
75 outputVector = _mm_shuffle_epi8(secondSrcVector, shuffleMaskEnd);
76 _mm_store_si128(dstVectorPtr, _mm_or_si128(outputVector, alphaMask));
77 ++dstVectorPtr;
78 }
79 src = (const uchar *)inVectorPtr;
80
81 SIMD_EPILOGUE(i, len, 15) {
82 dst[i] = qRgb(src[0], src[1], src[2]);
83 src += 3;
84 }
85}
86
87void convert_RGB888_to_RGB32_ssse3(QImageData *dest, const QImageData *src, Qt::ImageConversionFlags)
88{
89 Q_ASSERT(src->format == QImage::Format_RGB888 || src->format == QImage::Format_BGR888);
90 if (src->format == QImage::Format_BGR888)
91 Q_ASSERT(dest->format == QImage::Format_RGBX8888 || dest->format == QImage::Format_RGBA8888 || dest->format == QImage::Format_RGBA8888_Premultiplied);
92 else
93 Q_ASSERT(dest->format == QImage::Format_RGB32 || dest->format == QImage::Format_ARGB32 || dest->format == QImage::Format_ARGB32_Premultiplied);
94 Q_ASSERT(src->width == dest->width);
95 Q_ASSERT(src->height == dest->height);
96
97 const uchar *src_data = (uchar *) src->data;
98 quint32 *dest_data = (quint32 *) dest->data;
99
100 for (int i = 0; i < src->height; ++i) {
101 qt_convert_rgb888_to_rgb32_ssse3(dest_data, src_data, src->width);
102 src_data += src->bytes_per_line;
103 dest_data = (quint32 *)((uchar*)dest_data + dest->bytes_per_line);
104 }
105}
106
107QT_END_NAMESPACE
108
109#endif // QT_COMPILER_SUPPORTS_SSSE3