~ubuntu-branches/ubuntu/raring/qtwebkit-source/raring-proposed

« back to all changes in this revision

Viewing changes to Source/WebCore/plugins/qt/QtX11ImageConversion.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-02-18 14:24:18 UTC
  • Revision ID: package-import@ubuntu.com-20130218142418-eon0jmjg3nj438uy
Tags: upstream-2.3
ImportĀ upstreamĀ versionĀ 2.3

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
 
 
21
#include "config.h"
 
22
#include "QtX11ImageConversion.h"
 
23
 
 
24
namespace WebCore {
 
25
 
 
26
QImage qimageFromXImage(XImage* xi)
 
27
{
 
28
    QImage::Format format = QImage::Format_ARGB32_Premultiplied;
 
29
    if (xi->depth == 24)
 
30
        format = QImage::Format_RGB32;
 
31
    else if (xi->depth == 16)
 
32
        format = QImage::Format_RGB16;
 
33
 
 
34
    QImage image = QImage(reinterpret_cast<uchar*>(xi->data), xi->width, xi->height, xi->bytes_per_line, format).copy();
 
35
 
 
36
    // we may have to swap the byte order
 
37
    if ((QSysInfo::ByteOrder == QSysInfo::LittleEndian && xi->byte_order == MSBFirst)
 
38
        || (QSysInfo::ByteOrder == QSysInfo::BigEndian && xi->byte_order == LSBFirst)) {
 
39
 
 
40
        for (int i = 0; i < image.height(); i++) {
 
41
            if (xi->depth == 16) {
 
42
                ushort* p = reinterpret_cast<ushort*>(image.scanLine(i));
 
43
                ushort* end = p + image.width();
 
44
                while (p < end) {
 
45
                    *p = ((*p << 8) & 0xff00) | ((*p >> 8) & 0x00ff);
 
46
                    p++;
 
47
                }
 
48
            } else {
 
49
                uint* p = reinterpret_cast<uint*>(image.scanLine(i));
 
50
                uint* end = p + image.width();
 
51
                while (p < end) {
 
52
                    *p = ((*p << 24) & 0xff000000) | ((*p << 8) & 0x00ff0000)
 
53
                         | ((*p >> 8) & 0x0000ff00) | ((*p >> 24) & 0x000000ff);
 
54
                    p++;
 
55
                }
 
56
            }
 
57
        }
 
58
    }
 
59
 
 
60
    // fix-up alpha channel
 
61
    if (format == QImage::Format_RGB32) {
 
62
        QRgb* p = reinterpret_cast<QRgb*>(image.bits());
 
63
        for (int y = 0; y < xi->height; ++y) {
 
64
            for (int x = 0; x < xi->width; ++x)
 
65
                p[x] |= 0xff000000;
 
66
            p += xi->bytes_per_line / 4;
 
67
        }
 
68
    }
 
69
 
 
70
    return image;
 
71
}
 
72
 
 
73
} // namespace WebKit