QtX11ImageConversion.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. /*
  2. * Copyright (C) 2011 Nokia Corporation and/or its subsidiary(-ies)
  3. *
  4. * This library is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU Library General Public
  6. * License as published by the Free Software Foundation; either
  7. * version 2 of the License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful,
  10. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * Library General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU Library General Public License
  15. * along with this program; see the file COPYING.LIB. If not, write to
  16. * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
  17. * Boston, MA 02110-1301, USA.
  18. *
  19. */
  20. #include "config.h"
  21. #include "QtX11ImageConversion.h"
  22. namespace WebCore {
  23. QImage qimageFromXImage(XImage* xi)
  24. {
  25. QImage::Format format = QImage::Format_ARGB32_Premultiplied;
  26. if (xi->depth == 24)
  27. format = QImage::Format_RGB32;
  28. else if (xi->depth == 16)
  29. format = QImage::Format_RGB16;
  30. QImage image = QImage(reinterpret_cast<uchar*>(xi->data), xi->width, xi->height, xi->bytes_per_line, format).copy();
  31. // we may have to swap the byte order
  32. if ((QSysInfo::ByteOrder == QSysInfo::LittleEndian && xi->byte_order == MSBFirst)
  33. || (QSysInfo::ByteOrder == QSysInfo::BigEndian && xi->byte_order == LSBFirst)) {
  34. for (int i = 0; i < image.height(); i++) {
  35. if (xi->depth == 16) {
  36. ushort* p = reinterpret_cast<ushort*>(image.scanLine(i));
  37. ushort* end = p + image.width();
  38. while (p < end) {
  39. *p = ((*p << 8) & 0xff00) | ((*p >> 8) & 0x00ff);
  40. p++;
  41. }
  42. } else {
  43. uint* p = reinterpret_cast<uint*>(image.scanLine(i));
  44. uint* end = p + image.width();
  45. while (p < end) {
  46. *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
  47. | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
  48. p++;
  49. }
  50. }
  51. }
  52. }
  53. // fix-up alpha channel
  54. if (format == QImage::Format_RGB32) {
  55. QRgb* p = reinterpret_cast<QRgb*>(image.bits());
  56. for (int y = 0; y < xi->height; ++y) {
  57. for (int x = 0; x < xi->width; ++x)
  58. p[x] |= 0xff000000;
  59. p += xi->bytes_per_line / 4;
  60. }
  61. }
  62. return image;
  63. }
  64. } // namespace WebKit