author | Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com> |
Tue, 02 Feb 2010 00:43:10 +0200 | |
changeset 3 | 41300fa6a67c |
parent 0 | 1918ee327afb |
child 4 | 3b1da2848fc7 |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
3 |
** Copyright (C) 2009 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 plugins of the Qt Toolkit. |
|
8 |
** |
|
9 |
** $QT_BEGIN_LICENSE:LGPL$ |
|
10 |
** No Commercial Usage |
|
11 |
** This file contains pre-release code and may not be distributed. |
|
12 |
** You may use this file in accordance with the terms and conditions |
|
13 |
** contained in the Technology Preview License Agreement accompanying |
|
14 |
** this package. |
|
15 |
** |
|
16 |
** GNU Lesser General Public License Usage |
|
17 |
** Alternatively, this file may be used under the terms of the GNU Lesser |
|
18 |
** General Public License version 2.1 as published by the Free Software |
|
19 |
** Foundation and appearing in the file LICENSE.LGPL included in the |
|
20 |
** packaging of this file. Please review the following information to |
|
21 |
** ensure the GNU Lesser General Public License version 2.1 requirements |
|
22 |
** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html. |
|
23 |
** |
|
24 |
** In addition, as a special exception, Nokia gives you certain additional |
|
25 |
** rights. These rights are described in the Nokia Qt LGPL Exception |
|
26 |
** version 1.1, included in the file LGPL_EXCEPTION.txt in this package. |
|
27 |
** |
|
28 |
** If you have questions regarding the use of this file, please contact |
|
29 |
** Nokia at qt-info@nokia.com. |
|
30 |
** |
|
31 |
** |
|
32 |
** |
|
33 |
** |
|
34 |
** |
|
35 |
** |
|
36 |
** |
|
37 |
** |
|
38 |
** $QT_END_LICENSE$ |
|
39 |
** |
|
40 |
****************************************************************************/ |
|
41 |
||
42 |
#include "qjpeghandler.h" |
|
43 |
||
44 |
#include <qimage.h> |
|
45 |
#include <qvariant.h> |
|
46 |
#include <qvector.h> |
|
47 |
||
48 |
#include <stdio.h> // jpeglib needs this to be pre-included |
|
49 |
#include <setjmp.h> |
|
50 |
||
51 |
#ifdef FAR |
|
52 |
#undef FAR |
|
53 |
#endif |
|
54 |
||
55 |
// hw: optimize smoothscaler for returning 24-bit images |
|
56 |
||
57 |
// including jpeglib.h seems to be a little messy |
|
58 |
extern "C" { |
|
59 |
// mingw includes rpcndr.h but does not define boolean |
|
60 |
#if defined(Q_OS_WIN) && defined(Q_CC_GNU) |
|
61 |
# if defined(__RPCNDR_H__) && !defined(boolean) |
|
62 |
typedef unsigned char boolean; |
|
63 |
# define HAVE_BOOLEAN |
|
64 |
# endif |
|
65 |
#endif |
|
66 |
||
67 |
#define XMD_H // shut JPEGlib up |
|
68 |
#if defined(Q_OS_UNIXWARE) |
|
69 |
# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this |
|
70 |
#endif |
|
71 |
#include <jpeglib.h> |
|
72 |
#ifdef const |
|
73 |
# undef const // remove crazy C hackery in jconfig.h |
|
74 |
#endif |
|
75 |
} |
|
76 |
||
77 |
QT_BEGIN_NAMESPACE |
|
78 |
||
79 |
//#define QT_NO_IMAGE_SMOOTHSCALE |
|
80 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
81 |
class QImageSmoothScalerPrivate; |
|
82 |
class QImageSmoothScaler |
|
83 |
{ |
|
84 |
public: |
|
85 |
QImageSmoothScaler(const int w, const int h, const QImage &src); |
|
86 |
QImageSmoothScaler(const int srcWidth, const int srcHeight, |
|
87 |
const char *parameters); |
|
88 |
||
89 |
virtual ~QImageSmoothScaler(void); |
|
90 |
||
91 |
QImage scale(); |
|
92 |
||
93 |
protected: |
|
94 |
int scaledWidth(void) const; |
|
95 |
||
96 |
private: |
|
97 |
QImageSmoothScalerPrivate *d; |
|
98 |
virtual QRgb *scanLine(const int line = 0, const QImage *src = 0); |
|
99 |
}; |
|
100 |
||
101 |
class QImageSmoothScalerPrivate |
|
102 |
{ |
|
103 |
public: |
|
104 |
int cols; |
|
105 |
int newcols; |
|
106 |
int rows; |
|
107 |
int newrows; |
|
108 |
bool hasAlpha; |
|
109 |
||
110 |
const QImage *src; |
|
111 |
||
112 |
void setup(const int srcWidth, const int srcHeight, const int dstWidth, |
|
113 |
const int dstHeight, bool hasAlphaChannel); |
|
114 |
}; |
|
115 |
||
116 |
QImageSmoothScaler::QImageSmoothScaler(const int w, const int h, |
|
117 |
const QImage &src) |
|
118 |
{ |
|
119 |
d = new QImageSmoothScalerPrivate; |
|
120 |
||
121 |
d->setup(src.width(), src.height(), w, h, src.hasAlphaChannel() ); |
|
122 |
this->d->src = &src; |
|
123 |
} |
|
124 |
||
125 |
QImageSmoothScaler::QImageSmoothScaler(const int srcWidth, const int srcHeight, |
|
126 |
const char *parameters) |
|
127 |
{ |
|
128 |
char sModeStr[1024]; |
|
129 |
int t1; |
|
130 |
int t2; |
|
131 |
int dstWidth; |
|
132 |
int dstHeight; |
|
133 |
||
134 |
sModeStr[0] = '\0'; |
|
135 |
||
136 |
d = new QImageSmoothScalerPrivate; |
|
137 |
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && defined(_MSC_VER) && _MSC_VER >= 1400 |
|
138 |
sscanf_s(parameters, "Scale( %i, %i, %1023s )", &dstWidth, &dstHeight, sModeStr, sizeof(sModeStr)); |
|
139 |
#else |
|
140 |
sscanf(parameters, "Scale( %i, %i, %s )", &dstWidth, &dstHeight, sModeStr); |
|
141 |
#endif |
|
142 |
QString sModeQStr = QString::fromLatin1(sModeStr); |
|
143 |
||
144 |
t1 = srcWidth * dstHeight; |
|
145 |
t2 = srcHeight * dstWidth; |
|
146 |
||
147 |
if (((sModeQStr == QLatin1String("ScaleMin")) && (t1 > t2)) || ((sModeQStr == QLatin1String("ScaleMax")) && (t2 < t2))) { |
|
148 |
dstHeight = t2 / srcWidth; |
|
149 |
} else if (sModeQStr != QLatin1String("ScaleFree")) { |
|
150 |
dstWidth = t1 / srcHeight; |
|
151 |
} |
|
152 |
||
153 |
d->setup(srcWidth, srcHeight, dstWidth, dstHeight, 0); |
|
154 |
} |
|
155 |
||
156 |
void QImageSmoothScalerPrivate::setup(const int srcWidth, const int srcHeight, |
|
157 |
const int dstWidth, const int dstHeight, |
|
158 |
bool hasAlphaChannel) |
|
159 |
{ |
|
160 |
cols = srcWidth; |
|
161 |
rows = srcHeight; |
|
162 |
newcols = dstWidth; |
|
163 |
newrows = dstHeight; |
|
164 |
hasAlpha = hasAlphaChannel; |
|
165 |
} |
|
166 |
||
167 |
int QImageSmoothScaler::scaledWidth() const |
|
168 |
{ |
|
169 |
return d->cols; |
|
170 |
} |
|
171 |
||
172 |
QImageSmoothScaler::~QImageSmoothScaler() |
|
173 |
{ |
|
174 |
delete d; |
|
175 |
} |
|
176 |
||
177 |
inline QRgb *QImageSmoothScaler::scanLine(const int line, const QImage *src) |
|
178 |
{ |
|
179 |
return (QRgb*)src->scanLine(line); |
|
180 |
} |
|
181 |
||
182 |
/* |
|
183 |
This function uses code based on pnmscale.c by Jef Poskanzer. |
|
184 |
||
185 |
pnmscale.c - read a portable anymap and scale it |
|
186 |
||
187 |
Copyright (C) 1989, 1991 by Jef Poskanzer. |
|
188 |
||
189 |
Permission to use, copy, modify, and distribute this software and its |
|
190 |
documentation for any purpose and without fee is hereby granted, provided |
|
191 |
that the above copyright notice appear in all copies and that both that |
|
192 |
copyright notice and this permission notice appear in supporting |
|
193 |
documentation. This software is provided "as is" without express or |
|
194 |
implied warranty. |
|
195 |
*/ |
|
196 |
||
197 |
QImage QImageSmoothScaler::scale() |
|
198 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
long SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
200 |
long HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
201 |
QRgb *xelrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
202 |
QRgb *tempxelrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
QRgb *xP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
QRgb *nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
int row, rowsread; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
206 |
int col, needtoreadrow; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
207 |
uchar maxval = 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
qreal xscale, yscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
long sxscale, syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
long fracrowtofill, fracrowleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
211 |
long *as; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
long *rs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
213 |
long *gs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
214 |
long *bs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
215 |
int rowswritten = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
216 |
QImage dst; |
0 | 217 |
|
218 |
if (d->cols > 4096) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
219 |
SCALE = 4096; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
HALFSCALE = 2048; |
0 | 221 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
222 |
int fac = 4096; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
223 |
while (d->cols * fac > 4096) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
fac /= 2; |
0 | 225 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
SCALE = fac * d->cols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
HALFSCALE = fac * d->cols / 2; |
0 | 228 |
} |
229 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
xscale = (qreal)d->newcols / (qreal)d->cols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
yscale = (qreal)d->newrows / (qreal)d->rows; |
0 | 232 |
sxscale = (long)(xscale * SCALE); |
233 |
syscale = (long)(yscale * SCALE); |
|
234 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
// shortcut Y scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
236 |
if (d->newrows != d->rows) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
tempxelrow = new QRgb[d->cols]; |
0 | 238 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
239 |
if (d->hasAlpha) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
240 |
as = new long[d->cols]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
241 |
for (col = 0; col < d->cols; ++col) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
242 |
as[col] = HALFSCALE; |
0 | 243 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
244 |
as = 0; |
0 | 245 |
} |
246 |
rs = new long[d->cols]; |
|
247 |
gs = new long[d->cols]; |
|
248 |
bs = new long[d->cols]; |
|
249 |
rowsread = 0; |
|
250 |
fracrowleft = syscale; |
|
251 |
needtoreadrow = 1; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
252 |
for (col = 0; col < d->cols; ++col) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
rs[col] = gs[col] = bs[col] = HALFSCALE; |
0 | 254 |
fracrowtofill = SCALE; |
255 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
dst = QImage(d->newcols, d->newrows, d->hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32); |
0 | 257 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
258 |
for (row = 0; row < d->newrows; ++row) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
259 |
// First scale Y from xelrow into tempxelrow. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
260 |
if (d->newrows == d->rows) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
261 |
// shortcut Y scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
262 |
tempxelrow = xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
263 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
264 |
while (fracrowleft < fracrowtofill) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
265 |
if (needtoreadrow && rowsread < d->rows) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
266 |
xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
267 |
for (col = 0, xP = xelrow; col < d->cols; ++col, ++xP) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
268 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
269 |
as[col] += fracrowleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
270 |
rs[col] += fracrowleft * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
271 |
gs[col] += fracrowleft * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
bs[col] += fracrowleft * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
rs[col] += fracrowleft * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
gs[col] += fracrowleft * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
bs[col] += fracrowleft * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
fracrowtofill -= fracrowleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
fracrowleft = syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
281 |
needtoreadrow = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
282 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
283 |
// Now fracrowleft is >= fracrowtofill, so we can produce a row. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
284 |
if (needtoreadrow && rowsread < d->rows) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
285 |
xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
286 |
needtoreadrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
287 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
for (col = 0, xP = xelrow, nxP = tempxelrow; col < d->cols; ++col, ++xP, ++nxP) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
289 |
register long a, r, g, b; |
0 | 290 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
292 |
r = rs[col] + fracrowtofill * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
g = gs[col] + fracrowtofill * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
b = bs[col] + fracrowtofill * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
a = as[col] + fracrowtofill * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
301 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
302 |
r = rs[col] + fracrowtofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
g = gs[col] + fracrowtofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
304 |
b = bs[col] + fracrowtofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
305 |
a = 0; // unwarn |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
306 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
307 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
308 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
310 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
311 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
312 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
313 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
314 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
315 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
316 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
317 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
318 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
319 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
320 |
*nxP = qRgba((int)r, (int)g, (int)b, (int)a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
321 |
as[col] = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
322 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
323 |
*nxP = qRgb((int)r, (int)g, (int)b); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
325 |
rs[col] = gs[col] = bs[col] = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
326 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
327 |
fracrowleft -= fracrowtofill; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
328 |
if (fracrowleft == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
329 |
fracrowleft = syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
330 |
needtoreadrow = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
331 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
332 |
fracrowtofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
333 |
} |
0 | 334 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
335 |
// Now scale X from tempxelrow into dst and write it out. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
336 |
if (d->newcols == d->cols) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
// shortcut X scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
memcpy(dst.scanLine(rowswritten++), tempxelrow, d->newcols * 4); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
339 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
register long a, r, g, b; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
register long fraccoltofill, fraccolleft = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
342 |
register int needcol; |
0 | 343 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
344 |
nxP = (QRgb *)dst.scanLine(rowswritten++); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
345 |
QRgb *nxPEnd = nxP + d->newcols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
346 |
fraccoltofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
348 |
needcol = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
for (col = 0, xP = tempxelrow; col < d->cols; ++col, ++xP) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
350 |
fraccolleft = sxscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
351 |
while (fraccolleft >= fraccoltofill) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
352 |
if (needcol) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
353 |
++nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
355 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
356 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
357 |
r += fraccoltofill * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
358 |
g += fraccoltofill * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
359 |
b += fraccoltofill * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
360 |
a += fraccoltofill * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
361 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
362 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
363 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
364 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
365 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
366 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
367 |
r += fraccoltofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
368 |
g += fraccoltofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
369 |
b += fraccoltofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
370 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
371 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
372 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
373 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
374 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
375 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
376 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
377 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
378 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
379 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
380 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
381 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
382 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
383 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
384 |
*nxP = qRgba((int)r, (int)g, (int)b, (int)a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
385 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
386 |
*nxP = qRgb((int)r, (int)g, (int)b); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
387 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
388 |
fraccolleft -= fraccoltofill; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
389 |
fraccoltofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
390 |
needcol = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
391 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
392 |
if (fraccolleft > 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
393 |
if (needcol) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
++nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
396 |
needcol = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
397 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
398 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
399 |
a += fraccolleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
400 |
r += fraccolleft * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
g += fraccolleft * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
402 |
b += fraccolleft * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
403 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
r += fraccolleft * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
g += fraccolleft * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
406 |
b += fraccolleft * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
407 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
408 |
fraccoltofill -= fraccolleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
409 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
411 |
if (fraccoltofill > 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
412 |
--xP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
413 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
414 |
a += fraccolleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
415 |
r += fraccoltofill * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
416 |
g += fraccoltofill * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
417 |
b += fraccoltofill * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
418 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
419 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
420 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
421 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
422 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
423 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
424 |
r += fraccoltofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
g += fraccoltofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
426 |
b += fraccoltofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
427 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
428 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
429 |
if (nxP < nxPEnd) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
430 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
431 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
432 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
433 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
434 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
435 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
436 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
437 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
438 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
439 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
440 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
441 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
442 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
443 |
*nxP = qRgba((int)r, (int)g, (int)b, (int)a); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
444 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
445 |
*nxP = qRgb((int)r, (int)g, (int)b); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
446 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
447 |
while (++nxP != nxPEnd) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
448 |
nxP[0] = nxP[-1]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
449 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
450 |
} |
0 | 451 |
} |
452 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
453 |
if (d->newrows != d->rows && tempxelrow)// Robust, tempxelrow might be 0 1 day |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
454 |
delete [] tempxelrow; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
455 |
if (as) // Avoid purify complaint |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
456 |
delete [] as; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
457 |
if (rs) // Robust, rs might be 0 one day |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
458 |
delete [] rs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
459 |
if (gs) // Robust, gs might be 0 one day |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
460 |
delete [] gs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
461 |
if (bs) // Robust, bs might be 0 one day |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
462 |
delete [] bs; |
0 | 463 |
|
464 |
return dst; |
|
465 |
} |
|
466 |
||
467 |
class jpegSmoothScaler : public QImageSmoothScaler |
|
468 |
{ |
|
469 |
public: |
|
470 |
jpegSmoothScaler(struct jpeg_decompress_struct *info, const char *params): |
|
471 |
QImageSmoothScaler(info->output_width, info->output_height, params) |
|
472 |
{ |
|
473 |
cinfo = info; |
|
474 |
cols24Bit = scaledWidth() * 3; |
|
475 |
||
476 |
cacheHeight = 1; |
|
477 |
imageCache = QImage( info->output_width, cacheHeight, QImage::Format_RGB32 ); |
|
478 |
} |
|
479 |
||
480 |
private: |
|
481 |
int cols24Bit; |
|
482 |
QImage imageCache; |
|
483 |
int cacheHeight; |
|
484 |
struct jpeg_decompress_struct *cinfo; |
|
485 |
||
486 |
QRgb *scanLine(const int line = 0, const QImage *src = 0) |
|
487 |
{ |
|
488 |
QRgb *out; |
|
489 |
uchar *in; |
|
490 |
||
491 |
Q_UNUSED(line); |
|
492 |
Q_UNUSED(src); |
|
493 |
||
494 |
uchar* data = imageCache.bits(); |
|
495 |
jpeg_read_scanlines(cinfo, &data, 1); |
|
496 |
out = (QRgb*)imageCache.scanLine(0); |
|
497 |
||
498 |
// |
|
499 |
// The smooth scale algorithm only works on 32-bit images; |
|
500 |
// convert from (8|24) bits to 32. |
|
501 |
// |
|
502 |
if (cinfo->output_components == 1) { |
|
503 |
in = (uchar*)out + scaledWidth(); |
|
504 |
for (uint i = scaledWidth(); i--; ) { |
|
505 |
in--; |
|
506 |
out[i] = qRgb(*in, *in, *in); |
|
507 |
} |
|
508 |
} else if (cinfo->out_color_space == JCS_CMYK) { |
|
509 |
int cols32Bit = scaledWidth() * 4; |
|
510 |
in = (uchar*)out + cols32Bit; |
|
511 |
for (uint i = scaledWidth(); i--; ) { |
|
512 |
in -= 4; |
|
513 |
int k = in[3]; |
|
514 |
out[i] = qRgb(k * in[0] / 255, k * in[1] / 255, k * in[2] / 255); |
|
515 |
//out[i] = qRgb(in[0], in[1], in[2]); |
|
516 |
} |
|
517 |
} else { |
|
518 |
in = (uchar*)out + cols24Bit; |
|
519 |
for (uint i = scaledWidth(); i--; ) { |
|
520 |
in -= 3; |
|
521 |
out[i] = qRgb(in[0], in[1], in[2]); |
|
522 |
} |
|
523 |
} |
|
524 |
||
525 |
return out; |
|
526 |
} |
|
527 |
||
528 |
}; |
|
529 |
#endif |
|
530 |
||
531 |
struct my_error_mgr : public jpeg_error_mgr { |
|
532 |
jmp_buf setjmp_buffer; |
|
533 |
}; |
|
534 |
||
535 |
#if defined(Q_C_CALLBACKS) |
|
536 |
extern "C" { |
|
537 |
#endif |
|
538 |
||
539 |
static void my_error_exit (j_common_ptr cinfo) |
|
540 |
{ |
|
541 |
my_error_mgr* myerr = (my_error_mgr*) cinfo->err; |
|
542 |
char buffer[JMSG_LENGTH_MAX]; |
|
543 |
(*cinfo->err->format_message)(cinfo, buffer); |
|
544 |
qWarning("%s", buffer); |
|
545 |
longjmp(myerr->setjmp_buffer, 1); |
|
546 |
} |
|
547 |
||
548 |
#if defined(Q_C_CALLBACKS) |
|
549 |
} |
|
550 |
#endif |
|
551 |
||
552 |
||
553 |
static const int max_buf = 4096; |
|
554 |
||
555 |
struct my_jpeg_source_mgr : public jpeg_source_mgr { |
|
556 |
// Nothing dynamic - cannot rely on destruction over longjump |
|
557 |
QIODevice *device; |
|
558 |
JOCTET buffer[max_buf]; |
|
559 |
||
560 |
public: |
|
561 |
my_jpeg_source_mgr(QIODevice *device); |
|
562 |
}; |
|
563 |
||
564 |
#if defined(Q_C_CALLBACKS) |
|
565 |
extern "C" { |
|
566 |
#endif |
|
567 |
||
568 |
static void qt_init_source(j_decompress_ptr) |
|
569 |
{ |
|
570 |
} |
|
571 |
||
572 |
static boolean qt_fill_input_buffer(j_decompress_ptr cinfo) |
|
573 |
{ |
|
574 |
int num_read; |
|
575 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
576 |
src->next_input_byte = src->buffer; |
|
577 |
num_read = src->device->read((char*)src->buffer, max_buf); |
|
578 |
if (num_read <= 0) { |
|
579 |
// Insert a fake EOI marker - as per jpeglib recommendation |
|
580 |
src->buffer[0] = (JOCTET) 0xFF; |
|
581 |
src->buffer[1] = (JOCTET) JPEG_EOI; |
|
582 |
src->bytes_in_buffer = 2; |
|
583 |
} else { |
|
584 |
src->bytes_in_buffer = num_read; |
|
585 |
} |
|
586 |
#if defined(Q_OS_UNIXWARE) |
|
587 |
return B_TRUE; |
|
588 |
#else |
|
589 |
return true; |
|
590 |
#endif |
|
591 |
} |
|
592 |
||
593 |
static void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
|
594 |
{ |
|
595 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
596 |
||
597 |
// `dumb' implementation from jpeglib |
|
598 |
||
599 |
/* Just a dumb implementation for now. Could use fseek() except |
|
600 |
* it doesn't work on pipes. Not clear that being smart is worth |
|
601 |
* any trouble anyway --- large skips are infrequent. |
|
602 |
*/ |
|
603 |
if (num_bytes > 0) { |
|
604 |
while (num_bytes > (long) src->bytes_in_buffer) { |
|
605 |
num_bytes -= (long) src->bytes_in_buffer; |
|
606 |
(void) qt_fill_input_buffer(cinfo); |
|
607 |
/* note we assume that qt_fill_input_buffer will never return false, |
|
608 |
* so suspension need not be handled. |
|
609 |
*/ |
|
610 |
} |
|
611 |
src->next_input_byte += (size_t) num_bytes; |
|
612 |
src->bytes_in_buffer -= (size_t) num_bytes; |
|
613 |
} |
|
614 |
} |
|
615 |
||
616 |
static void qt_term_source(j_decompress_ptr cinfo) |
|
617 |
{ |
|
618 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
619 |
if (!src->device->isSequential()) |
|
620 |
src->device->seek(src->device->pos() - src->bytes_in_buffer); |
|
621 |
} |
|
622 |
||
623 |
#if defined(Q_C_CALLBACKS) |
|
624 |
} |
|
625 |
#endif |
|
626 |
||
627 |
inline my_jpeg_source_mgr::my_jpeg_source_mgr(QIODevice *device) |
|
628 |
{ |
|
629 |
jpeg_source_mgr::init_source = qt_init_source; |
|
630 |
jpeg_source_mgr::fill_input_buffer = qt_fill_input_buffer; |
|
631 |
jpeg_source_mgr::skip_input_data = qt_skip_input_data; |
|
632 |
jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; |
|
633 |
jpeg_source_mgr::term_source = qt_term_source; |
|
634 |
this->device = device; |
|
635 |
bytes_in_buffer = 0; |
|
636 |
next_input_byte = buffer; |
|
637 |
} |
|
638 |
||
639 |
||
640 |
static void scaleSize(int &reqW, int &reqH, int imgW, int imgH, Qt::AspectRatioMode mode) |
|
641 |
{ |
|
642 |
if (mode == Qt::IgnoreAspectRatio) |
|
643 |
return; |
|
644 |
int t1 = imgW * reqH; |
|
645 |
int t2 = reqW * imgH; |
|
646 |
if ((mode == Qt::KeepAspectRatio && (t1 > t2)) || (mode == Qt::KeepAspectRatioByExpanding && (t1 < t2))) |
|
647 |
reqH = t2 / imgW; |
|
648 |
else |
|
649 |
reqW = t1 / imgH; |
|
650 |
} |
|
651 |
||
652 |
static bool read_jpeg_size(QIODevice *device, int &w, int &h) |
|
653 |
{ |
|
654 |
bool rt = false; |
|
655 |
struct jpeg_decompress_struct cinfo; |
|
656 |
||
657 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
658 |
struct my_error_mgr jerr; |
|
659 |
||
660 |
jpeg_create_decompress(&cinfo); |
|
661 |
||
662 |
cinfo.src = iod_src; |
|
663 |
||
664 |
cinfo.err = jpeg_std_error(&jerr); |
|
665 |
jerr.error_exit = my_error_exit; |
|
666 |
||
667 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
668 |
#if defined(Q_OS_UNIXWARE) |
|
669 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
670 |
#else |
|
671 |
(void) jpeg_read_header(&cinfo, true); |
|
672 |
#endif |
|
673 |
(void) jpeg_calc_output_dimensions(&cinfo); |
|
674 |
||
675 |
w = cinfo.output_width; |
|
676 |
h = cinfo.output_height; |
|
677 |
rt = true; |
|
678 |
} |
|
679 |
jpeg_destroy_decompress(&cinfo); |
|
680 |
delete iod_src; |
|
681 |
return rt; |
|
682 |
} |
|
683 |
||
684 |
#define HIGH_QUALITY_THRESHOLD 50 |
|
685 |
||
686 |
static bool read_jpeg_format(QIODevice *device, QImage::Format &format) |
|
687 |
{ |
|
688 |
bool result = false; |
|
689 |
struct jpeg_decompress_struct cinfo; |
|
690 |
||
691 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
692 |
struct my_error_mgr jerr; |
|
693 |
||
694 |
jpeg_create_decompress(&cinfo); |
|
695 |
||
696 |
cinfo.src = iod_src; |
|
697 |
||
698 |
cinfo.err = jpeg_std_error(&jerr); |
|
699 |
jerr.error_exit = my_error_exit; |
|
700 |
||
701 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
702 |
#if defined(Q_OS_UNIXWARE) |
|
703 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
704 |
#else |
|
705 |
(void) jpeg_read_header(&cinfo, true); |
|
706 |
#endif |
|
707 |
// This does not allocate memory for the whole image |
|
708 |
// or such, so we are safe. |
|
709 |
(void) jpeg_start_decompress(&cinfo); |
|
710 |
result = true; |
|
711 |
switch (cinfo.output_components) { |
|
712 |
case 1: |
|
713 |
format = QImage::Format_Indexed8; |
|
714 |
break; |
|
715 |
case 3: |
|
716 |
case 4: |
|
717 |
format = QImage::Format_RGB32; |
|
718 |
break; |
|
719 |
default: |
|
720 |
result = false; |
|
721 |
break; |
|
722 |
} |
|
723 |
cinfo.output_scanline = cinfo.output_height; |
|
724 |
(void) jpeg_finish_decompress(&cinfo); |
|
725 |
} |
|
726 |
jpeg_destroy_decompress(&cinfo); |
|
727 |
delete iod_src; |
|
728 |
return result; |
|
729 |
} |
|
730 |
||
731 |
static bool ensureValidImage(QImage *dest, struct jpeg_decompress_struct *info, |
|
732 |
bool dummy = false) |
|
733 |
{ |
|
734 |
QImage::Format format; |
|
735 |
switch (info->output_components) { |
|
736 |
case 1: |
|
737 |
format = QImage::Format_Indexed8; |
|
738 |
break; |
|
739 |
case 3: |
|
740 |
case 4: |
|
741 |
format = QImage::Format_RGB32; |
|
742 |
break; |
|
743 |
default: |
|
744 |
return false; // unsupported format |
|
745 |
} |
|
746 |
||
747 |
const QSize size(info->output_width, info->output_height); |
|
748 |
if (dest->size() != size || dest->format() != format) { |
|
749 |
static uchar dummyImage[1]; |
|
750 |
if (dummy) // Create QImage but don't read the pixels |
|
751 |
*dest = QImage(dummyImage, size.width(), size.height(), format); |
|
752 |
else |
|
753 |
*dest = QImage(size, format); |
|
754 |
||
755 |
if (format == QImage::Format_Indexed8) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
756 |
dest->setColorCount(256); |
0 | 757 |
for (int i = 0; i < 256; i++) |
758 |
dest->setColor(i, qRgb(i,i,i)); |
|
759 |
} |
|
760 |
} |
|
761 |
||
762 |
return !dest->isNull(); |
|
763 |
} |
|
764 |
||
765 |
static bool read_jpeg_image(QIODevice *device, QImage *outImage, |
|
766 |
const QByteArray ¶meters, QSize scaledSize, |
|
767 |
int inQuality ) |
|
768 |
{ |
|
769 |
#ifdef QT_NO_IMAGE_SMOOTHSCALE |
|
770 |
Q_UNUSED( scaledSize ); |
|
771 |
#endif |
|
772 |
||
773 |
struct jpeg_decompress_struct cinfo; |
|
774 |
||
775 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
776 |
struct my_error_mgr jerr; |
|
777 |
||
778 |
jpeg_create_decompress(&cinfo); |
|
779 |
||
780 |
cinfo.src = iod_src; |
|
781 |
||
782 |
cinfo.err = jpeg_std_error(&jerr); |
|
783 |
jerr.error_exit = my_error_exit; |
|
784 |
||
785 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
786 |
#if defined(Q_OS_UNIXWARE) |
|
787 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
788 |
#else |
|
789 |
(void) jpeg_read_header(&cinfo, true); |
|
790 |
#endif |
|
791 |
||
792 |
// -1 means default quality. |
|
793 |
int quality = inQuality; |
|
794 |
if (quality < 0) |
|
795 |
quality = 75; |
|
796 |
||
797 |
QString params = QString::fromLatin1(parameters); |
|
798 |
params.simplified(); |
|
799 |
int sWidth = 0, sHeight = 0; |
|
800 |
char sModeStr[1024] = ""; |
|
801 |
Qt::AspectRatioMode sMode; |
|
802 |
||
803 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
804 |
// If high quality not required, shrink image during decompression |
|
805 |
if (scaledSize.isValid() && !scaledSize.isEmpty() && quality < HIGH_QUALITY_THRESHOLD |
|
806 |
&& !params.contains(QLatin1String("GetHeaderInformation")) ) { |
|
807 |
cinfo.scale_denom = qMin(cinfo.image_width / scaledSize.width(), |
|
808 |
cinfo.image_width / scaledSize.height()); |
|
809 |
if (cinfo.scale_denom < 2) { |
|
810 |
cinfo.scale_denom = 1; |
|
811 |
} else if (cinfo.scale_denom < 4) { |
|
812 |
cinfo.scale_denom = 2; |
|
813 |
} else if (cinfo.scale_denom < 8) { |
|
814 |
cinfo.scale_denom = 4; |
|
815 |
} else { |
|
816 |
cinfo.scale_denom = 8; |
|
817 |
} |
|
818 |
cinfo.scale_num = 1; |
|
819 |
} |
|
820 |
#endif |
|
821 |
||
822 |
||
823 |
// If high quality not required, use fast decompression |
|
824 |
if( quality < HIGH_QUALITY_THRESHOLD ) { |
|
825 |
cinfo.dct_method = JDCT_IFAST; |
|
826 |
cinfo.do_fancy_upsampling = FALSE; |
|
827 |
} |
|
828 |
||
829 |
||
830 |
(void) jpeg_start_decompress(&cinfo); |
|
831 |
||
832 |
if (params.contains(QLatin1String("GetHeaderInformation"))) { |
|
833 |
if (!ensureValidImage(outImage, &cinfo, true)) |
|
834 |
longjmp(jerr.setjmp_buffer, 1); |
|
835 |
} else if (params.contains(QLatin1String("Scale"))) { |
|
836 |
#if defined(_MSC_VER) && _MSC_VER >= 1400 && !defined(Q_OS_WINCE) |
|
837 |
sscanf_s(params.toLatin1().data(), "Scale(%i, %i, %1023s)", |
|
838 |
&sWidth, &sHeight, sModeStr, sizeof(sModeStr)); |
|
839 |
#else |
|
840 |
sscanf(params.toLatin1().data(), "Scale(%i, %i, %1023s)", |
|
841 |
&sWidth, &sHeight, sModeStr); |
|
842 |
#endif |
|
843 |
||
844 |
QString sModeQStr(QString::fromLatin1(sModeStr)); |
|
845 |
if (sModeQStr == QLatin1String("IgnoreAspectRatio")) { |
|
846 |
sMode = Qt::IgnoreAspectRatio; |
|
847 |
} else if (sModeQStr == QLatin1String("KeepAspectRatio")) { |
|
848 |
sMode = Qt::KeepAspectRatio; |
|
849 |
} else if (sModeQStr == QLatin1String("KeepAspectRatioByExpanding")) { |
|
850 |
sMode = Qt::KeepAspectRatioByExpanding; |
|
851 |
} else { |
|
852 |
qDebug("read_jpeg_image: invalid aspect ratio mode \"%s\", see QImage::AspectRatioMode documentation", sModeStr); |
|
853 |
sMode = Qt::KeepAspectRatio; |
|
854 |
} |
|
855 |
||
856 |
// qDebug("Parameters ask to scale the image to %i x %i AspectRatioMode: %s", sWidth, sHeight, sModeStr); |
|
857 |
scaleSize(sWidth, sHeight, cinfo.output_width, cinfo.output_height, sMode); |
|
858 |
// qDebug("Scaling the jpeg to %i x %i", sWidth, sHeight, sModeStr); |
|
859 |
||
860 |
if (cinfo.output_components == 3 || cinfo.output_components == 4) { |
|
861 |
if (outImage->size() != QSize(sWidth, sHeight) || outImage->format() != QImage::Format_RGB32) |
|
862 |
*outImage = QImage(sWidth, sHeight, QImage::Format_RGB32); |
|
863 |
} else if (cinfo.output_components == 1) { |
|
864 |
if (outImage->size() != QSize(sWidth, sHeight) || outImage->format() != QImage::Format_Indexed8) |
|
865 |
*outImage = QImage(sWidth, sHeight, QImage::Format_Indexed8); |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
866 |
outImage->setColorCount(256); |
0 | 867 |
for (int i = 0; i < 256; ++i) |
868 |
outImage->setColor(i, qRgb(i,i,i)); |
|
869 |
} else { |
|
870 |
// Unsupported format |
|
871 |
} |
|
872 |
if (outImage->isNull()) |
|
873 |
longjmp(jerr.setjmp_buffer, 1); |
|
874 |
||
875 |
if (!outImage->isNull()) { |
|
876 |
QImage tmpImage(cinfo.output_width, 1, QImage::Format_RGB32); |
|
877 |
uchar* inData = tmpImage.bits(); |
|
878 |
uchar* outData = outImage->bits(); |
|
879 |
int out_bpl = outImage->bytesPerLine(); |
|
880 |
while (cinfo.output_scanline < cinfo.output_height) { |
|
881 |
int outputLine = sHeight * cinfo.output_scanline / cinfo.output_height; |
|
882 |
(void) jpeg_read_scanlines(&cinfo, &inData, 1); |
|
883 |
if (cinfo.output_components == 3) { |
|
884 |
uchar *in = inData; |
|
885 |
QRgb *out = (QRgb*)outData + outputLine * out_bpl; |
|
886 |
for (uint i=0; i<cinfo.output_width; i++) { |
|
887 |
// ### Only scaling down an image works, I don't think scaling up will work at the moment |
|
888 |
// ### An idea I have to make this a smooth scale is to progressively add the pixel values up |
|
889 |
// When scaling down, multiple values are being over drawn in to the output buffer. |
|
890 |
// Instead, a weighting based on the distance the line or pixel is from the output pixel determines |
|
891 |
// the weight of it when added to the output buffer. At present it is a non-smooth scale which is |
|
892 |
// inefficently implemented, it still uncompresses all the jpeg, an optimization for progressive |
|
893 |
// jpegs could be made if scaling by say 50% or some other special cases |
|
894 |
out[sWidth * i / cinfo.output_width] = qRgb(in[0], in[1], in[2]); |
|
895 |
in += 3; |
|
896 |
} |
|
897 |
} else { |
|
898 |
// ### Need to test the case where the jpeg is grayscale, need some black and white jpegs to test |
|
899 |
// this code. (also only scales down and probably won't scale to a larger size) |
|
900 |
uchar *in = inData; |
|
901 |
uchar *out = outData + outputLine*out_bpl; |
|
902 |
for (uint i=0; i<cinfo.output_width; i++) { |
|
903 |
out[sWidth * i / cinfo.output_width] = in[i]; |
|
904 |
} |
|
905 |
} |
|
906 |
} |
|
907 |
(void) jpeg_finish_decompress(&cinfo); |
|
908 |
} |
|
909 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
910 |
} else if (scaledSize.isValid() && scaledSize != QSize(cinfo.output_width, cinfo.output_height) |
|
911 |
&& quality >= HIGH_QUALITY_THRESHOLD) { |
|
912 |
||
913 |
jpegSmoothScaler scaler(&cinfo, QString().sprintf("Scale( %d, %d, ScaleFree )", |
|
914 |
scaledSize.width(), |
|
915 |
scaledSize.height()).toLatin1().data()); |
|
916 |
*outImage = scaler.scale(); |
|
917 |
#endif |
|
918 |
} else { |
|
919 |
if (!ensureValidImage(outImage, &cinfo)) |
|
920 |
longjmp(jerr.setjmp_buffer, 1); |
|
921 |
||
922 |
uchar* data = outImage->bits(); |
|
923 |
int bpl = outImage->bytesPerLine(); |
|
924 |
while (cinfo.output_scanline < cinfo.output_height) { |
|
925 |
uchar *d = data + cinfo.output_scanline * bpl; |
|
926 |
(void) jpeg_read_scanlines(&cinfo, |
|
927 |
&d, |
|
928 |
1); |
|
929 |
} |
|
930 |
(void) jpeg_finish_decompress(&cinfo); |
|
931 |
||
932 |
if (cinfo.output_components == 3) { |
|
933 |
// Expand 24->32 bpp. |
|
934 |
for (uint j=0; j<cinfo.output_height; j++) { |
|
935 |
uchar *in = outImage->scanLine(j) + cinfo.output_width * 3; |
|
936 |
QRgb *out = (QRgb*)outImage->scanLine(j); |
|
937 |
||
938 |
for (uint i=cinfo.output_width; i--;) { |
|
939 |
in-=3; |
|
940 |
out[i] = qRgb(in[0], in[1], in[2]); |
|
941 |
} |
|
942 |
} |
|
943 |
} else if (cinfo.out_color_space == JCS_CMYK) { |
|
944 |
for (uint j = 0; j < cinfo.output_height; ++j) { |
|
945 |
uchar *in = outImage->scanLine(j) + cinfo.output_width * 4; |
|
946 |
QRgb *out = (QRgb*)outImage->scanLine(j); |
|
947 |
||
948 |
for (uint i = cinfo.output_width; i--; ) { |
|
949 |
in-=4; |
|
950 |
int k = in[3]; |
|
951 |
out[i] = qRgb(k * in[0] / 255, k * in[1] / 255, k * in[2] / 255); |
|
952 |
} |
|
953 |
} |
|
954 |
} |
|
955 |
if (cinfo.density_unit == 1) { |
|
956 |
outImage->setDotsPerMeterX(int(100. * cinfo.X_density / 2.54)); |
|
957 |
outImage->setDotsPerMeterY(int(100. * cinfo.Y_density / 2.54)); |
|
958 |
} else if (cinfo.density_unit == 2) { |
|
959 |
outImage->setDotsPerMeterX(int(100. * cinfo.X_density)); |
|
960 |
outImage->setDotsPerMeterY(int(100. * cinfo.Y_density)); |
|
961 |
} |
|
962 |
||
963 |
if (scaledSize.isValid() && scaledSize != QSize(cinfo.output_width, cinfo.output_height)) |
|
964 |
*outImage = outImage->scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::FastTransformation); |
|
965 |
} |
|
966 |
} |
|
967 |
||
968 |
jpeg_destroy_decompress(&cinfo); |
|
969 |
delete iod_src; |
|
970 |
return !outImage->isNull(); |
|
971 |
} |
|
972 |
||
973 |
||
974 |
struct my_jpeg_destination_mgr : public jpeg_destination_mgr { |
|
975 |
// Nothing dynamic - cannot rely on destruction over longjump |
|
976 |
QIODevice *device; |
|
977 |
JOCTET buffer[max_buf]; |
|
978 |
||
979 |
public: |
|
980 |
my_jpeg_destination_mgr(QIODevice *); |
|
981 |
}; |
|
982 |
||
983 |
||
984 |
#if defined(Q_C_CALLBACKS) |
|
985 |
extern "C" { |
|
986 |
#endif |
|
987 |
||
988 |
static void qt_init_destination(j_compress_ptr) |
|
989 |
{ |
|
990 |
} |
|
991 |
||
992 |
static boolean qt_empty_output_buffer(j_compress_ptr cinfo) |
|
993 |
{ |
|
994 |
my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; |
|
995 |
||
996 |
int written = dest->device->write((char*)dest->buffer, max_buf); |
|
997 |
if (written == -1) |
|
998 |
(*cinfo->err->error_exit)((j_common_ptr)cinfo); |
|
999 |
||
1000 |
dest->next_output_byte = dest->buffer; |
|
1001 |
dest->free_in_buffer = max_buf; |
|
1002 |
||
1003 |
#if defined(Q_OS_UNIXWARE) |
|
1004 |
return B_TRUE; |
|
1005 |
#else |
|
1006 |
return true; |
|
1007 |
#endif |
|
1008 |
} |
|
1009 |
||
1010 |
static void qt_term_destination(j_compress_ptr cinfo) |
|
1011 |
{ |
|
1012 |
my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; |
|
1013 |
qint64 n = max_buf - dest->free_in_buffer; |
|
1014 |
||
1015 |
qint64 written = dest->device->write((char*)dest->buffer, n); |
|
1016 |
if (written == -1) |
|
1017 |
(*cinfo->err->error_exit)((j_common_ptr)cinfo); |
|
1018 |
} |
|
1019 |
||
1020 |
#if defined(Q_C_CALLBACKS) |
|
1021 |
} |
|
1022 |
#endif |
|
1023 |
||
1024 |
inline my_jpeg_destination_mgr::my_jpeg_destination_mgr(QIODevice *device) |
|
1025 |
{ |
|
1026 |
jpeg_destination_mgr::init_destination = qt_init_destination; |
|
1027 |
jpeg_destination_mgr::empty_output_buffer = qt_empty_output_buffer; |
|
1028 |
jpeg_destination_mgr::term_destination = qt_term_destination; |
|
1029 |
this->device = device; |
|
1030 |
next_output_byte = buffer; |
|
1031 |
free_in_buffer = max_buf; |
|
1032 |
} |
|
1033 |
||
1034 |
||
1035 |
static bool write_jpeg_image(const QImage &sourceImage, QIODevice *device, int sourceQuality) |
|
1036 |
{ |
|
1037 |
bool success = false; |
|
1038 |
const QImage image = sourceImage; |
|
1039 |
const QVector<QRgb> cmap = image.colorTable(); |
|
1040 |
||
1041 |
struct jpeg_compress_struct cinfo; |
|
1042 |
JSAMPROW row_pointer[1]; |
|
1043 |
row_pointer[0] = 0; |
|
1044 |
||
1045 |
struct my_jpeg_destination_mgr *iod_dest = new my_jpeg_destination_mgr(device); |
|
1046 |
struct my_error_mgr jerr; |
|
1047 |
||
1048 |
cinfo.err = jpeg_std_error(&jerr); |
|
1049 |
jerr.error_exit = my_error_exit; |
|
1050 |
||
1051 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
1052 |
// WARNING: |
|
1053 |
// this if loop is inside a setjmp/longjmp branch |
|
1054 |
// do not create C++ temporaries here because the destructor may never be called |
|
1055 |
// if you allocate memory, make sure that you can free it (row_pointer[0]) |
|
1056 |
jpeg_create_compress(&cinfo); |
|
1057 |
||
1058 |
cinfo.dest = iod_dest; |
|
1059 |
||
1060 |
cinfo.image_width = image.width(); |
|
1061 |
cinfo.image_height = image.height(); |
|
1062 |
||
1063 |
bool gray=false; |
|
1064 |
switch (image.format()) { |
|
1065 |
case QImage::Format_Mono: |
|
1066 |
case QImage::Format_MonoLSB: |
|
1067 |
case QImage::Format_Indexed8: |
|
1068 |
gray = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1069 |
for (int i = image.colorCount(); gray && i--;) { |
0 | 1070 |
gray = gray & (qRed(cmap[i]) == qGreen(cmap[i]) && |
1071 |
qRed(cmap[i]) == qBlue(cmap[i])); |
|
1072 |
} |
|
1073 |
cinfo.input_components = gray ? 1 : 3; |
|
1074 |
cinfo.in_color_space = gray ? JCS_GRAYSCALE : JCS_RGB; |
|
1075 |
break; |
|
1076 |
default: |
|
1077 |
cinfo.input_components = 3; |
|
1078 |
cinfo.in_color_space = JCS_RGB; |
|
1079 |
} |
|
1080 |
||
1081 |
jpeg_set_defaults(&cinfo); |
|
1082 |
||
1083 |
qreal diffInch = qAbs(image.dotsPerMeterX()*2.54/100. - qRound(image.dotsPerMeterX()*2.54/100.)) |
|
1084 |
+ qAbs(image.dotsPerMeterY()*2.54/100. - qRound(image.dotsPerMeterY()*2.54/100.)); |
|
1085 |
qreal diffCm = (qAbs(image.dotsPerMeterX()/100. - qRound(image.dotsPerMeterX()/100.)) |
|
1086 |
+ qAbs(image.dotsPerMeterY()/100. - qRound(image.dotsPerMeterY()/100.)))*2.54; |
|
1087 |
if (diffInch < diffCm) { |
|
1088 |
cinfo.density_unit = 1; // dots/inch |
|
1089 |
cinfo.X_density = qRound(image.dotsPerMeterX()*2.54/100.); |
|
1090 |
cinfo.Y_density = qRound(image.dotsPerMeterY()*2.54/100.); |
|
1091 |
} else { |
|
1092 |
cinfo.density_unit = 2; // dots/cm |
|
1093 |
cinfo.X_density = (image.dotsPerMeterX()+50) / 100; |
|
1094 |
cinfo.Y_density = (image.dotsPerMeterY()+50) / 100; |
|
1095 |
} |
|
1096 |
||
1097 |
||
1098 |
int quality = sourceQuality >= 0 ? qMin(sourceQuality,100) : 75; |
|
1099 |
#if defined(Q_OS_UNIXWARE) |
|
1100 |
jpeg_set_quality(&cinfo, quality, B_TRUE /* limit to baseline-JPEG values */); |
|
1101 |
jpeg_start_compress(&cinfo, B_TRUE); |
|
1102 |
#else |
|
1103 |
jpeg_set_quality(&cinfo, quality, true /* limit to baseline-JPEG values */); |
|
1104 |
jpeg_start_compress(&cinfo, true); |
|
1105 |
#endif |
|
1106 |
||
1107 |
row_pointer[0] = new uchar[cinfo.image_width*cinfo.input_components]; |
|
1108 |
int w = cinfo.image_width; |
|
1109 |
while (cinfo.next_scanline < cinfo.image_height) { |
|
1110 |
uchar *row = row_pointer[0]; |
|
1111 |
switch (image.format()) { |
|
1112 |
case QImage::Format_Mono: |
|
1113 |
case QImage::Format_MonoLSB: |
|
1114 |
if (gray) { |
|
1115 |
const uchar* data = image.scanLine(cinfo.next_scanline); |
|
1116 |
if (image.format() == QImage::Format_MonoLSB) { |
|
1117 |
for (int i=0; i<w; i++) { |
|
1118 |
bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); |
|
1119 |
row[i] = qRed(cmap[bit]); |
|
1120 |
} |
|
1121 |
} else { |
|
1122 |
for (int i=0; i<w; i++) { |
|
1123 |
bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); |
|
1124 |
row[i] = qRed(cmap[bit]); |
|
1125 |
} |
|
1126 |
} |
|
1127 |
} else { |
|
1128 |
const uchar* data = image.scanLine(cinfo.next_scanline); |
|
1129 |
if (image.format() == QImage::Format_MonoLSB) { |
|
1130 |
for (int i=0; i<w; i++) { |
|
1131 |
bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); |
|
1132 |
*row++ = qRed(cmap[bit]); |
|
1133 |
*row++ = qGreen(cmap[bit]); |
|
1134 |
*row++ = qBlue(cmap[bit]); |
|
1135 |
} |
|
1136 |
} else { |
|
1137 |
for (int i=0; i<w; i++) { |
|
1138 |
bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); |
|
1139 |
*row++ = qRed(cmap[bit]); |
|
1140 |
*row++ = qGreen(cmap[bit]); |
|
1141 |
*row++ = qBlue(cmap[bit]); |
|
1142 |
} |
|
1143 |
} |
|
1144 |
} |
|
1145 |
break; |
|
1146 |
case QImage::Format_Indexed8: |
|
1147 |
if (gray) { |
|
1148 |
const uchar* pix = image.scanLine(cinfo.next_scanline); |
|
1149 |
for (int i=0; i<w; i++) { |
|
1150 |
*row = qRed(cmap[*pix]); |
|
1151 |
++row; ++pix; |
|
1152 |
} |
|
1153 |
} else { |
|
1154 |
const uchar* pix = image.scanLine(cinfo.next_scanline); |
|
1155 |
for (int i=0; i<w; i++) { |
|
1156 |
*row++ = qRed(cmap[*pix]); |
|
1157 |
*row++ = qGreen(cmap[*pix]); |
|
1158 |
*row++ = qBlue(cmap[*pix]); |
|
1159 |
++pix; |
|
1160 |
} |
|
1161 |
} |
|
1162 |
break; |
|
1163 |
case QImage::Format_RGB888: |
|
1164 |
memcpy(row, image.scanLine(cinfo.next_scanline), w * 3); |
|
1165 |
break; |
|
1166 |
case QImage::Format_RGB32: |
|
1167 |
case QImage::Format_ARGB32: |
|
1168 |
case QImage::Format_ARGB32_Premultiplied: { |
|
1169 |
QRgb* rgb = (QRgb*)image.scanLine(cinfo.next_scanline); |
|
1170 |
for (int i=0; i<w; i++) { |
|
1171 |
*row++ = qRed(*rgb); |
|
1172 |
*row++ = qGreen(*rgb); |
|
1173 |
*row++ = qBlue(*rgb); |
|
1174 |
++rgb; |
|
1175 |
} |
|
1176 |
break; |
|
1177 |
} |
|
1178 |
default: |
|
1179 |
qWarning("QJpegHandler: unable to write image of format %i", |
|
1180 |
image.format()); |
|
1181 |
break; |
|
1182 |
} |
|
1183 |
jpeg_write_scanlines(&cinfo, row_pointer, 1); |
|
1184 |
} |
|
1185 |
||
1186 |
jpeg_finish_compress(&cinfo); |
|
1187 |
jpeg_destroy_compress(&cinfo); |
|
1188 |
success = true; |
|
1189 |
} else { |
|
1190 |
jpeg_destroy_compress(&cinfo); |
|
1191 |
success = false; |
|
1192 |
} |
|
1193 |
||
1194 |
delete iod_dest; |
|
1195 |
delete [] row_pointer[0]; |
|
1196 |
return success; |
|
1197 |
} |
|
1198 |
||
1199 |
QJpegHandler::QJpegHandler() |
|
1200 |
{ |
|
1201 |
quality = 75; |
|
1202 |
} |
|
1203 |
||
1204 |
bool QJpegHandler::canRead() const |
|
1205 |
{ |
|
1206 |
if (canRead(device())) { |
|
1207 |
setFormat("jpeg"); |
|
1208 |
return true; |
|
1209 |
} |
|
1210 |
return false; |
|
1211 |
} |
|
1212 |
||
1213 |
bool QJpegHandler::canRead(QIODevice *device) |
|
1214 |
{ |
|
1215 |
if (!device) { |
|
1216 |
qWarning("QJpegHandler::canRead() called with no device"); |
|
1217 |
return false; |
|
1218 |
} |
|
1219 |
||
1220 |
return device->peek(2) == "\xFF\xD8"; |
|
1221 |
} |
|
1222 |
||
1223 |
bool QJpegHandler::read(QImage *image) |
|
1224 |
{ |
|
1225 |
if (!canRead()) |
|
1226 |
return false; |
|
1227 |
return read_jpeg_image(device(), image, parameters, scaledSize, quality); |
|
1228 |
} |
|
1229 |
||
1230 |
bool QJpegHandler::write(const QImage &image) |
|
1231 |
{ |
|
1232 |
return write_jpeg_image(image, device(), quality); |
|
1233 |
} |
|
1234 |
||
1235 |
bool QJpegHandler::supportsOption(ImageOption option) const |
|
1236 |
{ |
|
1237 |
return option == Quality |
|
1238 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
1239 |
|| option == ScaledSize |
|
1240 |
#endif |
|
1241 |
|| option == Size |
|
1242 |
|| option == ImageFormat; |
|
1243 |
} |
|
1244 |
||
1245 |
QVariant QJpegHandler::option(ImageOption option) const |
|
1246 |
{ |
|
1247 |
if (option == Quality) { |
|
1248 |
return quality; |
|
1249 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
1250 |
} else if (option == ScaledSize) { |
|
1251 |
return scaledSize; |
|
1252 |
#endif |
|
1253 |
} else if (option == Size) { |
|
1254 |
if (canRead() && !device()->isSequential()) { |
|
1255 |
qint64 pos = device()->pos(); |
|
1256 |
int width = 0; |
|
1257 |
int height = 0; |
|
1258 |
read_jpeg_size(device(), width, height); |
|
1259 |
device()->seek(pos); |
|
1260 |
return QSize(width, height); |
|
1261 |
} |
|
1262 |
} else if (option == ImageFormat) { |
|
1263 |
if (canRead() && !device()->isSequential()) { |
|
1264 |
qint64 pos = device()->pos(); |
|
1265 |
QImage::Format format = QImage::Format_Invalid; |
|
1266 |
read_jpeg_format(device(), format); |
|
1267 |
device()->seek(pos); |
|
1268 |
return format; |
|
1269 |
} |
|
1270 |
return QImage::Format_Invalid; |
|
1271 |
} |
|
1272 |
return QVariant(); |
|
1273 |
} |
|
1274 |
||
1275 |
void QJpegHandler::setOption(ImageOption option, const QVariant &value) |
|
1276 |
{ |
|
1277 |
if (option == Quality) |
|
1278 |
quality = value.toInt(); |
|
1279 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
|
1280 |
else if ( option == ScaledSize ) |
|
1281 |
scaledSize = value.toSize(); |
|
1282 |
#endif |
|
1283 |
} |
|
1284 |
||
1285 |
QByteArray QJpegHandler::name() const |
|
1286 |
{ |
|
1287 |
return "jpeg"; |
|
1288 |
} |
|
1289 |
||
1290 |
QT_END_NAMESPACE |