source: trunk/src/gui/image/qpixmapdata.cpp@ 180

Last change on this file since 180 was 2, checked in by Dmitry A. Kuminov, 16 years ago

Initially imported qt-all-opensource-src-4.5.1 from Trolltech.

File size: 5.7 KB
Line 
1/****************************************************************************
2**
3** Copyright (C) 2009 Nokia Corporation and/or its subsidiary(-ies).
4** Contact: Qt Software Information ([email protected])
5**
6** This file is part of the QtGui module of the Qt Toolkit.
7**
8** $QT_BEGIN_LICENSE:LGPL$
9** Commercial Usage
10** Licensees holding valid Qt Commercial licenses may use this file in
11** accordance with the Qt Commercial License Agreement provided with the
12** Software or, alternatively, in accordance with the terms contained in
13** a written agreement between you and Nokia.
14**
15** GNU Lesser General Public License Usage
16** Alternatively, this file may be used under the terms of the GNU Lesser
17** General Public License version 2.1 as published by the Free Software
18** Foundation and appearing in the file LICENSE.LGPL included in the
19** packaging of this file. Please review the following information to
20** ensure the GNU Lesser General Public License version 2.1 requirements
21** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
22**
23** In addition, as a special exception, Nokia gives you certain
24** additional rights. These rights are described in the Nokia Qt LGPL
25** Exception version 1.0, included in the file LGPL_EXCEPTION.txt in this
26** package.
27**
28** GNU General Public License Usage
29** Alternatively, this file may be used under the terms of the GNU
30** General Public License version 3.0 as published by the Free Software
31** Foundation and appearing in the file LICENSE.GPL included in the
32** packaging of this file. Please review the following information to
33** ensure the GNU General Public License version 3.0 requirements will be
34** met: http://www.gnu.org/copyleft/gpl.html.
35**
36** If you are unsure which license is appropriate for your use, please
37** contact the sales department at [email protected].
38** $QT_END_LICENSE$
39**
40****************************************************************************/
41
42#include "qpixmapdata_p.h"
43#include <QtGui/qbitmap.h>
44#include <QtGui/qimagereader.h>
45
46QT_BEGIN_NAMESPACE
47
48const uchar qt_pixmap_bit_mask[] = { 0x01, 0x02, 0x04, 0x08,
49 0x10, 0x20, 0x40, 0x80 };
50
51QPixmapData::QPixmapData(PixelType pixelType, int objectId)
52 : ref(0), detach_no(0), type(pixelType), id(objectId), ser_no(0), is_cached(false)
53{
54
55}
56
57QPixmapData::~QPixmapData()
58{
59}
60
61void QPixmapData::fromFile(const QString &fileName, const char *format,
62 Qt::ImageConversionFlags flags)
63{
64 const QImage image = QImageReader(fileName, format).read();
65 if (image.isNull())
66 return;
67
68 fromImage(image, flags);
69}
70
71void QPixmapData::copy(const QPixmapData *data, const QRect &rect)
72{
73 fromImage(data->toImage().copy(rect), Qt::AutoColor);
74}
75
76void QPixmapData::setMask(const QBitmap &mask)
77{
78 if (mask.size().isEmpty()) {
79 if (depth() != 1)
80 fromImage(toImage().convertToFormat(QImage::Format_RGB32),
81 Qt::AutoColor);
82 } else {
83 QImage image = toImage();
84 const int w = image.width();
85 const int h = image.height();
86
87 switch (image.depth()) {
88 case 1: {
89 const QImage imageMask = mask.toImage().convertToFormat(image.format());
90 for (int y = 0; y < h; ++y) {
91 const uchar *mscan = imageMask.scanLine(y);
92 uchar *tscan = image.scanLine(y);
93 int bytesPerLine = image.bytesPerLine();
94 for (int i = 0; i < bytesPerLine; ++i)
95 tscan[i] &= mscan[i];
96 }
97 break;
98 }
99 default: {
100 const QImage imageMask = mask.toImage().convertToFormat(QImage::Format_MonoLSB);
101 image = image.convertToFormat(QImage::Format_ARGB32_Premultiplied);
102 for (int y = 0; y < h; ++y) {
103 const uchar *mscan = imageMask.scanLine(y);
104 QRgb *tscan = (QRgb *)image.scanLine(y);
105 for (int x = 0; x < w; ++x) {
106 if (!(mscan[x>>3] & qt_pixmap_bit_mask[x&7]))
107 tscan[x] = 0;
108 }
109 }
110 break;
111 }
112 }
113 fromImage(image, Qt::AutoColor);
114 }
115}
116
117QBitmap QPixmapData::mask() const
118{
119 if (!hasAlphaChannel())
120 return QBitmap();
121
122 const QImage img = toImage();
123 const QImage image = (img.depth() < 32 ? img.convertToFormat(QImage::Format_ARGB32_Premultiplied) : img);
124 const int w = image.width();
125 const int h = image.height();
126
127 QImage mask(w, h, QImage::Format_MonoLSB);
128 if (mask.isNull()) // allocation failed
129 return QBitmap();
130
131 mask.setNumColors(2);
132 mask.setColor(0, QColor(Qt::color0).rgba());
133 mask.setColor(1, QColor(Qt::color1).rgba());
134
135 const int bpl = mask.bytesPerLine();
136
137 for (int y = 0; y < h; ++y) {
138 const QRgb *src = reinterpret_cast<const QRgb*>(image.scanLine(y));
139 uchar *dest = mask.scanLine(y);
140 memset(dest, 0, bpl);
141 for (int x = 0; x < w; ++x) {
142 if (qAlpha(*src) > 0)
143 dest[x >> 3] |= qt_pixmap_bit_mask[x & 7];
144 ++src;
145 }
146 }
147
148 return QBitmap::fromImage(mask);
149}
150
151QPixmap QPixmapData::transformed(const QTransform &matrix,
152 Qt::TransformationMode mode) const
153{
154 return QPixmap::fromImage(toImage().transformed(matrix, mode));
155}
156
157void QPixmapData::setAlphaChannel(const QPixmap &alphaChannel)
158{
159 QImage image = toImage();
160 image.setAlphaChannel(alphaChannel.toImage());
161 fromImage(image, Qt::AutoColor);
162}
163
164QPixmap QPixmapData::alphaChannel() const
165{
166 return QPixmap::fromImage(toImage().alphaChannel());
167}
168
169void QPixmapData::setSerialNumber(int serNo)
170{
171 ser_no = serNo;
172}
173