/src/gui/image/qimage_sse2.cpp

https://bitbucket.org/ultra_iter/qt-vtl · C++ · 109 lines · 51 code · 14 blank · 44 comment · 15 complexity · 1a24b4860feab589c2ae124170dceb38 MD5 · raw file

  1. /****************************************************************************
  2. **
  3. ** Copyright (C) 2012 Nokia Corporation and/or its subsidiary(-ies).
  4. ** All rights reserved.
  5. ** Contact: Nokia Corporation (qt-info@nokia.com)
  6. **
  7. ** This file is part of the QtGui module of the Qt Toolkit.
  8. **
  9. ** $QT_BEGIN_LICENSE:LGPL$
  10. ** GNU Lesser General Public License Usage
  11. ** This file may be used under the terms of the GNU Lesser General Public
  12. ** License version 2.1 as published by the Free Software Foundation and
  13. ** appearing in the file LICENSE.LGPL included in the packaging of this
  14. ** file. Please review the following information to ensure the GNU Lesser
  15. ** General Public License version 2.1 requirements will be met:
  16. ** http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
  17. **
  18. ** In addition, as a special exception, Nokia gives you certain additional
  19. ** rights. These rights are described in the Nokia Qt LGPL Exception
  20. ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
  21. **
  22. ** GNU General Public License Usage
  23. ** Alternatively, this file may be used under the terms of the GNU General
  24. ** Public License version 3.0 as published by the Free Software Foundation
  25. ** and appearing in the file LICENSE.GPL included in the packaging of this
  26. ** file. Please review the following information to ensure the GNU General
  27. ** Public License version 3.0 requirements will be met:
  28. ** http://www.gnu.org/copyleft/gpl.html.
  29. **
  30. ** Other Usage
  31. ** Alternatively, this file may be used in accordance with the terms and
  32. ** conditions contained in a signed written agreement between you and Nokia.
  33. **
  34. **
  35. **
  36. **
  37. **
  38. ** $QT_END_LICENSE$
  39. **
  40. ****************************************************************************/
  41. #include "qimage.h"
  42. #include <private/qimage_p.h>
  43. #include <private/qsimd_p.h>
  44. #include <private/qdrawhelper_p.h>
  45. #include <private/qdrawingprimitive_sse2_p.h>
  46. #ifdef QT_HAVE_SSE2
  47. QT_BEGIN_NAMESPACE
  48. bool convert_ARGB_to_ARGB_PM_inplace_sse2(QImageData *data, Qt::ImageConversionFlags)
  49. {
  50. Q_ASSERT(data->format == QImage::Format_ARGB32);
  51. // extra pixels on each line
  52. const int spare = data->width & 3;
  53. // width in pixels of the pad at the end of each line
  54. const int pad = (data->bytes_per_line >> 2) - data->width;
  55. const int iter = data->width >> 2;
  56. int height = data->height;
  57. const __m128i alphaMask = _mm_set1_epi32(0xff000000);
  58. const __m128i nullVector = _mm_setzero_si128();
  59. const __m128i half = _mm_set1_epi16(0x80);
  60. const __m128i colorMask = _mm_set1_epi32(0x00ff00ff);
  61. __m128i *d = reinterpret_cast<__m128i*>(data->data);
  62. while (height--) {
  63. const __m128i *end = d + iter;
  64. for (; d != end; ++d) {
  65. const __m128i srcVector = _mm_loadu_si128(d);
  66. const __m128i srcVectorAlpha = _mm_and_si128(srcVector, alphaMask);
  67. if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, alphaMask)) == 0xffff) {
  68. // opaque, data is unchanged
  69. } else if (_mm_movemask_epi8(_mm_cmpeq_epi32(srcVectorAlpha, nullVector)) == 0xffff) {
  70. // fully transparent
  71. _mm_storeu_si128(d, nullVector);
  72. } else {
  73. __m128i alphaChannel = _mm_srli_epi32(srcVector, 24);
  74. alphaChannel = _mm_or_si128(alphaChannel, _mm_slli_epi32(alphaChannel, 16));
  75. __m128i result;
  76. BYTE_MUL_SSE2(result, srcVector, alphaChannel, colorMask, half);
  77. result = _mm_or_si128(_mm_andnot_si128(alphaMask, result), srcVectorAlpha);
  78. _mm_storeu_si128(d, result);
  79. }
  80. }
  81. QRgb *p = reinterpret_cast<QRgb*>(d);
  82. QRgb *pe = p+spare;
  83. for (; p != pe; ++p) {
  84. if (*p < 0x00ffffff)
  85. *p = 0;
  86. else if (*p < 0xff000000)
  87. *p = PREMUL(*p);
  88. }
  89. d = reinterpret_cast<__m128i*>(p+pad);
  90. }
  91. data->format = QImage::Format_ARGB32_Premultiplied;
  92. return true;
  93. }
  94. QT_END_NAMESPACE
  95. #endif // QT_HAVE_SSE2