author | Eckhart Koeppen <eckhart.koppen@nokia.com> |
Thu, 08 Apr 2010 14:19:33 +0300 | |
branch | RCL_3 |
changeset 8 | 3f74d0d4af4c |
parent 4 | 3b1da2848fc7 |
child 30 | 5dc02b23752f |
permissions | -rw-r--r-- |
0 | 1 |
/**************************************************************************** |
2 |
** |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
3 |
** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies). |
0 | 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, |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
87 |
const int dstWidth, const int dstHeight); |
0 | 88 |
|
89 |
virtual ~QImageSmoothScaler(void); |
|
90 |
||
91 |
QImage scale(); |
|
92 |
||
93 |
private: |
|
94 |
QImageSmoothScalerPrivate *d; |
|
95 |
virtual QRgb *scanLine(const int line = 0, const QImage *src = 0); |
|
96 |
}; |
|
97 |
||
98 |
class QImageSmoothScalerPrivate |
|
99 |
{ |
|
100 |
public: |
|
101 |
int cols; |
|
102 |
int newcols; |
|
103 |
int rows; |
|
104 |
int newrows; |
|
105 |
bool hasAlpha; |
|
106 |
||
107 |
const QImage *src; |
|
108 |
||
109 |
void setup(const int srcWidth, const int srcHeight, const int dstWidth, |
|
110 |
const int dstHeight, bool hasAlphaChannel); |
|
111 |
}; |
|
112 |
||
113 |
QImageSmoothScaler::QImageSmoothScaler(const int w, const int h, |
|
114 |
const QImage &src) |
|
115 |
{ |
|
116 |
d = new QImageSmoothScalerPrivate; |
|
117 |
||
118 |
d->setup(src.width(), src.height(), w, h, src.hasAlphaChannel() ); |
|
119 |
this->d->src = &src; |
|
120 |
} |
|
121 |
||
122 |
QImageSmoothScaler::QImageSmoothScaler(const int srcWidth, const int srcHeight, |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
123 |
const int dstWidth, const int dstHeight) |
0 | 124 |
{ |
125 |
d = new QImageSmoothScalerPrivate; |
|
126 |
d->setup(srcWidth, srcHeight, dstWidth, dstHeight, 0); |
|
127 |
} |
|
128 |
||
129 |
void QImageSmoothScalerPrivate::setup(const int srcWidth, const int srcHeight, |
|
130 |
const int dstWidth, const int dstHeight, |
|
131 |
bool hasAlphaChannel) |
|
132 |
{ |
|
133 |
cols = srcWidth; |
|
134 |
rows = srcHeight; |
|
135 |
newcols = dstWidth; |
|
136 |
newrows = dstHeight; |
|
137 |
hasAlpha = hasAlphaChannel; |
|
138 |
} |
|
139 |
||
140 |
QImageSmoothScaler::~QImageSmoothScaler() |
|
141 |
{ |
|
142 |
delete d; |
|
143 |
} |
|
144 |
||
145 |
inline QRgb *QImageSmoothScaler::scanLine(const int line, const QImage *src) |
|
146 |
{ |
|
147 |
return (QRgb*)src->scanLine(line); |
|
148 |
} |
|
149 |
||
150 |
/* |
|
151 |
This function uses code based on pnmscale.c by Jef Poskanzer. |
|
152 |
||
153 |
pnmscale.c - read a portable anymap and scale it |
|
154 |
||
155 |
Copyright (C) 1989, 1991 by Jef Poskanzer. |
|
156 |
||
157 |
Permission to use, copy, modify, and distribute this software and its |
|
158 |
documentation for any purpose and without fee is hereby granted, provided |
|
159 |
that the above copyright notice appear in all copies and that both that |
|
160 |
copyright notice and this permission notice appear in supporting |
|
161 |
documentation. This software is provided "as is" without express or |
|
162 |
implied warranty. |
|
163 |
*/ |
|
164 |
||
165 |
QImage QImageSmoothScaler::scale() |
|
166 |
{ |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
167 |
long SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
168 |
long HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
169 |
QRgb *xelrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
170 |
QRgb *tempxelrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
171 |
QRgb *xP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
172 |
QRgb *nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
173 |
int row, rowsread; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
174 |
int col, needtoreadrow; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
175 |
uchar maxval = 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
176 |
qreal xscale, yscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
177 |
long sxscale, syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
178 |
long fracrowtofill, fracrowleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
179 |
long *as; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
180 |
long *rs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
181 |
long *gs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
182 |
long *bs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
183 |
int rowswritten = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
184 |
QImage dst; |
0 | 185 |
|
186 |
if (d->cols > 4096) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
187 |
SCALE = 4096; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
188 |
HALFSCALE = 2048; |
0 | 189 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
190 |
int fac = 4096; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
191 |
while (d->cols * fac > 4096) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
192 |
fac /= 2; |
0 | 193 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
194 |
SCALE = fac * d->cols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
195 |
HALFSCALE = fac * d->cols / 2; |
0 | 196 |
} |
197 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
198 |
xscale = (qreal)d->newcols / (qreal)d->cols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
199 |
yscale = (qreal)d->newrows / (qreal)d->rows; |
0 | 200 |
sxscale = (long)(xscale * SCALE); |
201 |
syscale = (long)(yscale * SCALE); |
|
202 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
203 |
// shortcut Y scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
204 |
if (d->newrows != d->rows) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
205 |
tempxelrow = new QRgb[d->cols]; |
0 | 206 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
207 |
if (d->hasAlpha) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
208 |
as = new long[d->cols]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
209 |
for (col = 0; col < d->cols; ++col) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
210 |
as[col] = HALFSCALE; |
0 | 211 |
} else { |
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
212 |
as = 0; |
0 | 213 |
} |
214 |
rs = new long[d->cols]; |
|
215 |
gs = new long[d->cols]; |
|
216 |
bs = new long[d->cols]; |
|
217 |
rowsread = 0; |
|
218 |
fracrowleft = syscale; |
|
219 |
needtoreadrow = 1; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
220 |
for (col = 0; col < d->cols; ++col) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
221 |
rs[col] = gs[col] = bs[col] = HALFSCALE; |
0 | 222 |
fracrowtofill = SCALE; |
223 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
224 |
dst = QImage(d->newcols, d->newrows, d->hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32); |
0 | 225 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
226 |
for (row = 0; row < d->newrows; ++row) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
227 |
// First scale Y from xelrow into tempxelrow. |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
228 |
if (d->newrows == d->rows) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
229 |
// shortcut Y scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
230 |
tempxelrow = xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
231 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
232 |
while (fracrowleft < fracrowtofill) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
233 |
if (needtoreadrow && rowsread < d->rows) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
234 |
xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
235 |
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
|
236 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
237 |
as[col] += fracrowleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
238 |
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
|
239 |
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
|
240 |
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
|
241 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
242 |
rs[col] += fracrowleft * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
243 |
gs[col] += fracrowleft * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
244 |
bs[col] += fracrowleft * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
245 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
246 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
247 |
fracrowtofill -= fracrowleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
248 |
fracrowleft = syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
249 |
needtoreadrow = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
250 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
251 |
// 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
|
252 |
if (needtoreadrow && rowsread < d->rows) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
253 |
xelrow = scanLine(rowsread++, d->src); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
254 |
needtoreadrow = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
255 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
256 |
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
|
257 |
register long a, r, g, b; |
0 | 258 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
259 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
260 |
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
|
261 |
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
|
262 |
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
|
263 |
a = as[col] + fracrowtofill * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
264 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
265 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
266 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
267 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
268 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
269 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
270 |
r = rs[col] + fracrowtofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
271 |
g = gs[col] + fracrowtofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
272 |
b = bs[col] + fracrowtofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
273 |
a = 0; // unwarn |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
274 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
275 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
276 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
277 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
278 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
279 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
280 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
281 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
282 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
283 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
284 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
285 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
286 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
287 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
288 |
*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
|
289 |
as[col] = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
290 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
291 |
*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
|
292 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
293 |
rs[col] = gs[col] = bs[col] = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
294 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
295 |
fracrowleft -= fracrowtofill; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
296 |
if (fracrowleft == 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
297 |
fracrowleft = syscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
298 |
needtoreadrow = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
299 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
300 |
fracrowtofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
301 |
} |
0 | 302 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
303 |
// 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
|
304 |
if (d->newcols == d->cols) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
305 |
// shortcut X scaling if possible |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
306 |
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
|
307 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
308 |
register long a, r, g, b; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
309 |
register long fraccoltofill, fraccolleft = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
310 |
register int needcol; |
0 | 311 |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
312 |
nxP = (QRgb *)dst.scanLine(rowswritten++); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
313 |
QRgb *nxPEnd = nxP + d->newcols; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
314 |
fraccoltofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
315 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
316 |
needcol = 0; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
317 |
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
|
318 |
fraccolleft = sxscale; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
319 |
while (fraccolleft >= fraccoltofill) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
320 |
if (needcol) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
321 |
++nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
322 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
323 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
324 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
325 |
r += fraccoltofill * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
326 |
g += fraccoltofill * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
327 |
b += fraccoltofill * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
328 |
a += fraccoltofill * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
329 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
330 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
331 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
332 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
333 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
334 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
335 |
r += fraccoltofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
336 |
g += fraccoltofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
337 |
b += fraccoltofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
338 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
339 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
340 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
341 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
342 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
343 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
344 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
345 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
346 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
347 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
348 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
349 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
350 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
351 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
352 |
*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
|
353 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
354 |
*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
|
355 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
356 |
fraccolleft -= fraccoltofill; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
357 |
fraccoltofill = SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
358 |
needcol = 1; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
359 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
360 |
if (fraccolleft > 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
361 |
if (needcol) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
362 |
++nxP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
363 |
a = r = g = b = HALFSCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
364 |
needcol = 0; |
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 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
367 |
a += fraccolleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
368 |
r += fraccolleft * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
369 |
g += fraccolleft * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
370 |
b += fraccolleft * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
371 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
372 |
r += fraccolleft * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
373 |
g += fraccolleft * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
374 |
b += fraccolleft * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
375 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
376 |
fraccoltofill -= fraccolleft; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
377 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
378 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
379 |
if (fraccoltofill > 0) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
380 |
--xP; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
381 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
382 |
a += fraccolleft * qAlpha(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
383 |
r += fraccoltofill * qRed(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
384 |
g += fraccoltofill * qGreen(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
385 |
b += fraccoltofill * qBlue(*xP) * qAlpha(*xP) / 255; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
386 |
if (a) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
387 |
r = r * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
388 |
g = g * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
389 |
b = b * 255 / a * SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
390 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
391 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
392 |
r += fraccoltofill * qRed(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
393 |
g += fraccoltofill * qGreen(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
394 |
b += fraccoltofill * qBlue(*xP); |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
395 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
396 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
397 |
if (nxP < nxPEnd) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
398 |
r /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
399 |
if (r > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
400 |
r = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
401 |
g /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
402 |
if (g > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
403 |
g = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
404 |
b /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
405 |
if (b > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
406 |
b = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
407 |
if (as) { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
408 |
a /= SCALE; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
409 |
if (a > maxval) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
410 |
a = maxval; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
411 |
*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
|
412 |
} else { |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
413 |
*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
|
414 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
415 |
while (++nxP != nxPEnd) |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
416 |
nxP[0] = nxP[-1]; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
417 |
} |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
418 |
} |
0 | 419 |
} |
420 |
||
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
421 |
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
|
422 |
delete [] tempxelrow; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
423 |
if (as) // Avoid purify complaint |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
424 |
delete [] as; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
425 |
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
|
426 |
delete [] rs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
427 |
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
|
428 |
delete [] gs; |
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
429 |
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
|
430 |
delete [] bs; |
0 | 431 |
|
432 |
return dst; |
|
433 |
} |
|
434 |
||
435 |
class jpegSmoothScaler : public QImageSmoothScaler |
|
436 |
{ |
|
437 |
public: |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
438 |
jpegSmoothScaler(struct jpeg_decompress_struct *info, const QSize& dstSize, const QRect& clipRect) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
439 |
: QImageSmoothScaler(clipRect.width(), clipRect.height(), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
440 |
dstSize.width(), dstSize.height()) |
0 | 441 |
{ |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
442 |
cinfo = info; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
443 |
clip = clipRect; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
444 |
imageCache = QImage(info->output_width, 1, QImage::Format_RGB32); |
0 | 445 |
} |
446 |
||
447 |
private: |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
448 |
QRect clip; |
0 | 449 |
QImage imageCache; |
450 |
struct jpeg_decompress_struct *cinfo; |
|
451 |
||
452 |
QRgb *scanLine(const int line = 0, const QImage *src = 0) |
|
453 |
{ |
|
454 |
QRgb *out; |
|
455 |
uchar *in; |
|
456 |
||
457 |
Q_UNUSED(line); |
|
458 |
Q_UNUSED(src); |
|
459 |
||
460 |
uchar* data = imageCache.bits(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
461 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
462 |
// Read ahead if we haven't reached the first clipped scanline yet. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
463 |
while (int(cinfo->output_scanline) < clip.y() && |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
464 |
cinfo->output_scanline < cinfo->output_height) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
465 |
jpeg_read_scanlines(cinfo, &data, 1); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
466 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
467 |
// Read the next scanline. We assume that "line" |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
468 |
// will never be >= clip.height(). |
0 | 469 |
jpeg_read_scanlines(cinfo, &data, 1); |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
470 |
if (cinfo->output_scanline == cinfo->output_height) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
471 |
jpeg_finish_decompress(cinfo); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
472 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
473 |
out = ((QRgb*)data) + clip.x(); |
0 | 474 |
|
475 |
// |
|
476 |
// The smooth scale algorithm only works on 32-bit images; |
|
477 |
// convert from (8|24) bits to 32. |
|
478 |
// |
|
479 |
if (cinfo->output_components == 1) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
480 |
in = data + clip.right(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
481 |
for (int i = clip.width(); i--; ) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
482 |
out[i] = qRgb(*in, *in, *in); |
0 | 483 |
in--; |
484 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
485 |
} else if (cinfo->out_color_space == JCS_CMYK) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
486 |
in = data + clip.right() * 4; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
487 |
for (int i = clip.width(); i--; ) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
488 |
int k = in[3]; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
489 |
out[i] = qRgb(k * in[0] / 255, k * in[1] / 255, k * in[2] / 255); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
490 |
in -= 4; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
491 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
492 |
} else { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
493 |
in = data + clip.right() * 3; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
494 |
for (int i = clip.width(); i--; ) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
495 |
out[i] = qRgb(in[0], in[1], in[2]); |
0 | 496 |
in -= 3; |
497 |
} |
|
498 |
} |
|
499 |
||
500 |
return out; |
|
501 |
} |
|
502 |
||
503 |
}; |
|
504 |
#endif |
|
505 |
||
506 |
struct my_error_mgr : public jpeg_error_mgr { |
|
507 |
jmp_buf setjmp_buffer; |
|
508 |
}; |
|
509 |
||
510 |
#if defined(Q_C_CALLBACKS) |
|
511 |
extern "C" { |
|
512 |
#endif |
|
513 |
||
514 |
static void my_error_exit (j_common_ptr cinfo) |
|
515 |
{ |
|
516 |
my_error_mgr* myerr = (my_error_mgr*) cinfo->err; |
|
517 |
char buffer[JMSG_LENGTH_MAX]; |
|
518 |
(*cinfo->err->format_message)(cinfo, buffer); |
|
519 |
qWarning("%s", buffer); |
|
520 |
longjmp(myerr->setjmp_buffer, 1); |
|
521 |
} |
|
522 |
||
523 |
#if defined(Q_C_CALLBACKS) |
|
524 |
} |
|
525 |
#endif |
|
526 |
||
527 |
||
528 |
static const int max_buf = 4096; |
|
529 |
||
530 |
struct my_jpeg_source_mgr : public jpeg_source_mgr { |
|
531 |
// Nothing dynamic - cannot rely on destruction over longjump |
|
532 |
QIODevice *device; |
|
533 |
JOCTET buffer[max_buf]; |
|
534 |
||
535 |
public: |
|
536 |
my_jpeg_source_mgr(QIODevice *device); |
|
537 |
}; |
|
538 |
||
539 |
#if defined(Q_C_CALLBACKS) |
|
540 |
extern "C" { |
|
541 |
#endif |
|
542 |
||
543 |
static void qt_init_source(j_decompress_ptr) |
|
544 |
{ |
|
545 |
} |
|
546 |
||
547 |
static boolean qt_fill_input_buffer(j_decompress_ptr cinfo) |
|
548 |
{ |
|
549 |
int num_read; |
|
550 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
551 |
src->next_input_byte = src->buffer; |
|
552 |
num_read = src->device->read((char*)src->buffer, max_buf); |
|
553 |
if (num_read <= 0) { |
|
554 |
// Insert a fake EOI marker - as per jpeglib recommendation |
|
555 |
src->buffer[0] = (JOCTET) 0xFF; |
|
556 |
src->buffer[1] = (JOCTET) JPEG_EOI; |
|
557 |
src->bytes_in_buffer = 2; |
|
558 |
} else { |
|
559 |
src->bytes_in_buffer = num_read; |
|
560 |
} |
|
561 |
#if defined(Q_OS_UNIXWARE) |
|
562 |
return B_TRUE; |
|
563 |
#else |
|
564 |
return true; |
|
565 |
#endif |
|
566 |
} |
|
567 |
||
568 |
static void qt_skip_input_data(j_decompress_ptr cinfo, long num_bytes) |
|
569 |
{ |
|
570 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
571 |
||
572 |
// `dumb' implementation from jpeglib |
|
573 |
||
574 |
/* Just a dumb implementation for now. Could use fseek() except |
|
575 |
* it doesn't work on pipes. Not clear that being smart is worth |
|
576 |
* any trouble anyway --- large skips are infrequent. |
|
577 |
*/ |
|
578 |
if (num_bytes > 0) { |
|
579 |
while (num_bytes > (long) src->bytes_in_buffer) { |
|
580 |
num_bytes -= (long) src->bytes_in_buffer; |
|
581 |
(void) qt_fill_input_buffer(cinfo); |
|
582 |
/* note we assume that qt_fill_input_buffer will never return false, |
|
583 |
* so suspension need not be handled. |
|
584 |
*/ |
|
585 |
} |
|
586 |
src->next_input_byte += (size_t) num_bytes; |
|
587 |
src->bytes_in_buffer -= (size_t) num_bytes; |
|
588 |
} |
|
589 |
} |
|
590 |
||
591 |
static void qt_term_source(j_decompress_ptr cinfo) |
|
592 |
{ |
|
593 |
my_jpeg_source_mgr* src = (my_jpeg_source_mgr*)cinfo->src; |
|
594 |
if (!src->device->isSequential()) |
|
595 |
src->device->seek(src->device->pos() - src->bytes_in_buffer); |
|
596 |
} |
|
597 |
||
598 |
#if defined(Q_C_CALLBACKS) |
|
599 |
} |
|
600 |
#endif |
|
601 |
||
602 |
inline my_jpeg_source_mgr::my_jpeg_source_mgr(QIODevice *device) |
|
603 |
{ |
|
604 |
jpeg_source_mgr::init_source = qt_init_source; |
|
605 |
jpeg_source_mgr::fill_input_buffer = qt_fill_input_buffer; |
|
606 |
jpeg_source_mgr::skip_input_data = qt_skip_input_data; |
|
607 |
jpeg_source_mgr::resync_to_restart = jpeg_resync_to_restart; |
|
608 |
jpeg_source_mgr::term_source = qt_term_source; |
|
609 |
this->device = device; |
|
610 |
bytes_in_buffer = 0; |
|
611 |
next_input_byte = buffer; |
|
612 |
} |
|
613 |
||
614 |
||
615 |
static bool read_jpeg_size(QIODevice *device, int &w, int &h) |
|
616 |
{ |
|
617 |
bool rt = false; |
|
618 |
struct jpeg_decompress_struct cinfo; |
|
619 |
||
620 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
621 |
struct my_error_mgr jerr; |
|
622 |
||
623 |
jpeg_create_decompress(&cinfo); |
|
624 |
||
625 |
cinfo.src = iod_src; |
|
626 |
||
627 |
cinfo.err = jpeg_std_error(&jerr); |
|
628 |
jerr.error_exit = my_error_exit; |
|
629 |
||
630 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
631 |
#if defined(Q_OS_UNIXWARE) |
|
632 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
633 |
#else |
|
634 |
(void) jpeg_read_header(&cinfo, true); |
|
635 |
#endif |
|
636 |
(void) jpeg_calc_output_dimensions(&cinfo); |
|
637 |
||
638 |
w = cinfo.output_width; |
|
639 |
h = cinfo.output_height; |
|
640 |
rt = true; |
|
641 |
} |
|
642 |
jpeg_destroy_decompress(&cinfo); |
|
643 |
delete iod_src; |
|
644 |
return rt; |
|
645 |
} |
|
646 |
||
647 |
#define HIGH_QUALITY_THRESHOLD 50 |
|
648 |
||
649 |
static bool read_jpeg_format(QIODevice *device, QImage::Format &format) |
|
650 |
{ |
|
651 |
bool result = false; |
|
652 |
struct jpeg_decompress_struct cinfo; |
|
653 |
||
654 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
655 |
struct my_error_mgr jerr; |
|
656 |
||
657 |
jpeg_create_decompress(&cinfo); |
|
658 |
||
659 |
cinfo.src = iod_src; |
|
660 |
||
661 |
cinfo.err = jpeg_std_error(&jerr); |
|
662 |
jerr.error_exit = my_error_exit; |
|
663 |
||
664 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
665 |
#if defined(Q_OS_UNIXWARE) |
|
666 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
667 |
#else |
|
668 |
(void) jpeg_read_header(&cinfo, true); |
|
669 |
#endif |
|
670 |
// This does not allocate memory for the whole image |
|
671 |
// or such, so we are safe. |
|
672 |
(void) jpeg_start_decompress(&cinfo); |
|
673 |
result = true; |
|
674 |
switch (cinfo.output_components) { |
|
675 |
case 1: |
|
676 |
format = QImage::Format_Indexed8; |
|
677 |
break; |
|
678 |
case 3: |
|
679 |
case 4: |
|
680 |
format = QImage::Format_RGB32; |
|
681 |
break; |
|
682 |
default: |
|
683 |
result = false; |
|
684 |
break; |
|
685 |
} |
|
686 |
cinfo.output_scanline = cinfo.output_height; |
|
687 |
(void) jpeg_finish_decompress(&cinfo); |
|
688 |
} |
|
689 |
jpeg_destroy_decompress(&cinfo); |
|
690 |
delete iod_src; |
|
691 |
return result; |
|
692 |
} |
|
693 |
||
694 |
static bool ensureValidImage(QImage *dest, struct jpeg_decompress_struct *info, |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
695 |
const QSize& size) |
0 | 696 |
{ |
697 |
QImage::Format format; |
|
698 |
switch (info->output_components) { |
|
699 |
case 1: |
|
700 |
format = QImage::Format_Indexed8; |
|
701 |
break; |
|
702 |
case 3: |
|
703 |
case 4: |
|
704 |
format = QImage::Format_RGB32; |
|
705 |
break; |
|
706 |
default: |
|
707 |
return false; // unsupported format |
|
708 |
} |
|
709 |
||
710 |
if (dest->size() != size || dest->format() != format) { |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
711 |
*dest = QImage(size, format); |
0 | 712 |
|
713 |
if (format == QImage::Format_Indexed8) { |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
714 |
dest->setColorCount(256); |
0 | 715 |
for (int i = 0; i < 256; i++) |
716 |
dest->setColor(i, qRgb(i,i,i)); |
|
717 |
} |
|
718 |
} |
|
719 |
||
720 |
return !dest->isNull(); |
|
721 |
} |
|
722 |
||
723 |
static bool read_jpeg_image(QIODevice *device, QImage *outImage, |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
724 |
QSize scaledSize, QRect scaledClipRect, |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
725 |
QRect clipRect, int inQuality ) |
0 | 726 |
{ |
727 |
struct jpeg_decompress_struct cinfo; |
|
728 |
||
729 |
struct my_jpeg_source_mgr *iod_src = new my_jpeg_source_mgr(device); |
|
730 |
struct my_error_mgr jerr; |
|
731 |
||
732 |
jpeg_create_decompress(&cinfo); |
|
733 |
||
734 |
cinfo.src = iod_src; |
|
735 |
||
736 |
cinfo.err = jpeg_std_error(&jerr); |
|
737 |
jerr.error_exit = my_error_exit; |
|
738 |
||
739 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
740 |
#if defined(Q_OS_UNIXWARE) |
|
741 |
(void) jpeg_read_header(&cinfo, B_TRUE); |
|
742 |
#else |
|
743 |
(void) jpeg_read_header(&cinfo, true); |
|
744 |
#endif |
|
745 |
||
746 |
// -1 means default quality. |
|
747 |
int quality = inQuality; |
|
748 |
if (quality < 0) |
|
749 |
quality = 75; |
|
750 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
751 |
// If possible, merge the scaledClipRect into either scaledSize |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
752 |
// or clipRect to avoid doing a separate scaled clipping pass. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
753 |
// Best results are achieved by clipping before scaling, not after. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
754 |
if (!scaledClipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
755 |
if (scaledSize.isEmpty() && clipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
756 |
// No clipping or scaling before final clip. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
757 |
clipRect = scaledClipRect; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
758 |
scaledClipRect = QRect(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
759 |
} else if (scaledSize.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
760 |
// Clipping, but no scaling: combine the clip regions. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
761 |
scaledClipRect.translate(clipRect.topLeft()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
762 |
clipRect = scaledClipRect.intersected(clipRect); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
763 |
scaledClipRect = QRect(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
764 |
} else if (clipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
765 |
// No clipping, but scaling: if we can map back to an |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
766 |
// integer pixel boundary, then clip before scaling. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
767 |
if ((cinfo.image_width % scaledSize.width()) == 0 && |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
768 |
(cinfo.image_height % scaledSize.height()) == 0) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
769 |
int x = scaledClipRect.x() * cinfo.image_width / |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
770 |
scaledSize.width(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
771 |
int y = scaledClipRect.y() * cinfo.image_height / |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
772 |
scaledSize.height(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
773 |
int width = (scaledClipRect.right() + 1) * |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
774 |
cinfo.image_width / scaledSize.width() - x; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
775 |
int height = (scaledClipRect.bottom() + 1) * |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
776 |
cinfo.image_height / scaledSize.height() - y; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
777 |
clipRect = QRect(x, y, width, height); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
778 |
scaledSize = scaledClipRect.size(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
779 |
scaledClipRect = QRect(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
780 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
781 |
} else { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
782 |
// Clipping and scaling: too difficult to figure out, |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
783 |
// and not a likely use case, so do it the long way. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
784 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
785 |
} |
0 | 786 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
787 |
// Determine the scale factor to pass to libjpeg for quick downscaling. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
788 |
if (!scaledSize.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
789 |
if (clipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
790 |
cinfo.scale_denom = |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
791 |
qMin(cinfo.image_width / scaledSize.width(), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
792 |
cinfo.image_height / scaledSize.height()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
793 |
} else { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
794 |
cinfo.scale_denom = |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
795 |
qMin(clipRect.width() / scaledSize.width(), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
796 |
clipRect.height() / scaledSize.height()); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
797 |
} |
0 | 798 |
if (cinfo.scale_denom < 2) { |
799 |
cinfo.scale_denom = 1; |
|
800 |
} else if (cinfo.scale_denom < 4) { |
|
801 |
cinfo.scale_denom = 2; |
|
802 |
} else if (cinfo.scale_denom < 8) { |
|
803 |
cinfo.scale_denom = 4; |
|
804 |
} else { |
|
805 |
cinfo.scale_denom = 8; |
|
806 |
} |
|
807 |
cinfo.scale_num = 1; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
808 |
if (!clipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
809 |
// Correct the scale factor so that we clip accurately. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
810 |
// It is recommended that the clip rectangle be aligned |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
811 |
// on an 8-pixel boundary for best performance. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
812 |
while (cinfo.scale_denom > 1 && |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
813 |
((clipRect.x() % cinfo.scale_denom) != 0 || |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
814 |
(clipRect.y() % cinfo.scale_denom) != 0 || |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
815 |
(clipRect.width() % cinfo.scale_denom) != 0 || |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
816 |
(clipRect.height() % cinfo.scale_denom) != 0)) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
817 |
cinfo.scale_denom /= 2; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
818 |
} |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
819 |
} |
0 | 820 |
} |
821 |
||
822 |
// If high quality not required, use fast decompression |
|
823 |
if( quality < HIGH_QUALITY_THRESHOLD ) { |
|
824 |
cinfo.dct_method = JDCT_IFAST; |
|
825 |
cinfo.do_fancy_upsampling = FALSE; |
|
826 |
} |
|
827 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
828 |
(void) jpeg_calc_output_dimensions(&cinfo); |
0 | 829 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
830 |
// Determine the clip region to extract. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
831 |
QRect imageRect(0, 0, cinfo.output_width, cinfo.output_height); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
832 |
QRect clip; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
833 |
if (clipRect.isEmpty()) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
834 |
clip = imageRect; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
835 |
} else if (cinfo.scale_denom == 1) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
836 |
clip = clipRect.intersected(imageRect); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
837 |
} else { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
838 |
// The scale factor was corrected above to ensure that |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
839 |
// we don't miss pixels when we scale the clip rectangle. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
840 |
clip = QRect(clipRect.x() / int(cinfo.scale_denom), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
841 |
clipRect.y() / int(cinfo.scale_denom), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
842 |
clipRect.width() / int(cinfo.scale_denom), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
843 |
clipRect.height() / int(cinfo.scale_denom)); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
844 |
clip = clip.intersected(imageRect); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
845 |
} |
0 | 846 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
847 |
#ifndef QT_NO_IMAGE_SMOOTHSCALE |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
848 |
if (scaledSize.isValid() && scaledSize != clip.size() |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
849 |
&& quality >= HIGH_QUALITY_THRESHOLD) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
850 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
851 |
(void) jpeg_start_decompress(&cinfo); |
0 | 852 |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
853 |
jpegSmoothScaler scaler(&cinfo, scaledSize, clip); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
854 |
*outImage = scaler.scale(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
855 |
} else |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
856 |
#endif |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
857 |
{ |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
858 |
// Allocate memory for the clipped QImage. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
859 |
if (!ensureValidImage(outImage, &cinfo, clip.size())) |
0 | 860 |
longjmp(jerr.setjmp_buffer, 1); |
861 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
862 |
// Avoid memcpy() overhead if grayscale with no clipping. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
863 |
bool quickGray = (cinfo.output_components == 1 && |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
864 |
clip == imageRect); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
865 |
if (!quickGray) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
866 |
// Ask the jpeg library to allocate a temporary row. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
867 |
// The library will automatically delete it for us later. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
868 |
// The libjpeg docs say we should do this before calling |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
869 |
// jpeg_start_decompress(). We can't use "new" here |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
870 |
// because we are inside the setjmp() block and an error |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
871 |
// in the jpeg input stream would cause a memory leak. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
872 |
JSAMPARRAY rows = (cinfo.mem->alloc_sarray) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
873 |
((j_common_ptr)&cinfo, JPOOL_IMAGE, |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
874 |
cinfo.output_width * cinfo.output_components, 1); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
875 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
876 |
(void) jpeg_start_decompress(&cinfo); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
877 |
|
0 | 878 |
while (cinfo.output_scanline < cinfo.output_height) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
879 |
int y = int(cinfo.output_scanline) - clip.y(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
880 |
if (y >= clip.height()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
881 |
break; // We've read the entire clip region, so abort. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
882 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
883 |
(void) jpeg_read_scanlines(&cinfo, rows, 1); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
884 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
885 |
if (y < 0) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
886 |
continue; // Haven't reached the starting line yet. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
887 |
|
0 | 888 |
if (cinfo.output_components == 3) { |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
889 |
// Expand 24->32 bpp. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
890 |
uchar *in = rows[0] + clip.x() * 3; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
891 |
QRgb *out = (QRgb*)outImage->scanLine(y); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
892 |
for (int i = 0; i < clip.width(); ++i) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
893 |
*out++ = qRgb(in[0], in[1], in[2]); |
0 | 894 |
in += 3; |
895 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
896 |
} else if (cinfo.out_color_space == JCS_CMYK) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
897 |
// Convert CMYK->RGB. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
898 |
uchar *in = rows[0] + clip.x() * 4; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
899 |
QRgb *out = (QRgb*)outImage->scanLine(y); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
900 |
for (int i = 0; i < clip.width(); ++i) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
901 |
int k = in[3]; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
902 |
*out++ = qRgb(k * in[0] / 255, k * in[1] / 255, |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
903 |
k * in[2] / 255); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
904 |
in += 4; |
0 | 905 |
} |
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
906 |
} else if (cinfo.output_components == 1) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
907 |
// Grayscale. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
908 |
memcpy(outImage->scanLine(y), |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
909 |
rows[0] + clip.x(), clip.width()); |
0 | 910 |
} |
911 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
912 |
} else { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
913 |
// Load unclipped grayscale data directly into the QImage. |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
914 |
(void) jpeg_start_decompress(&cinfo); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
915 |
while (cinfo.output_scanline < cinfo.output_height) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
916 |
uchar *row = outImage->scanLine(cinfo.output_scanline); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
917 |
(void) jpeg_read_scanlines(&cinfo, &row, 1); |
0 | 918 |
} |
919 |
} |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
920 |
|
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
921 |
if (cinfo.output_scanline == cinfo.output_height) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
922 |
(void) jpeg_finish_decompress(&cinfo); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
923 |
|
0 | 924 |
if (cinfo.density_unit == 1) { |
925 |
outImage->setDotsPerMeterX(int(100. * cinfo.X_density / 2.54)); |
|
926 |
outImage->setDotsPerMeterY(int(100. * cinfo.Y_density / 2.54)); |
|
927 |
} else if (cinfo.density_unit == 2) { |
|
928 |
outImage->setDotsPerMeterX(int(100. * cinfo.X_density)); |
|
929 |
outImage->setDotsPerMeterY(int(100. * cinfo.Y_density)); |
|
930 |
} |
|
931 |
||
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
932 |
if (scaledSize.isValid() && scaledSize != clip.size()) |
0 | 933 |
*outImage = outImage->scaled(scaledSize, Qt::IgnoreAspectRatio, Qt::FastTransformation); |
934 |
} |
|
935 |
} |
|
936 |
||
937 |
jpeg_destroy_decompress(&cinfo); |
|
938 |
delete iod_src; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
939 |
if (!scaledClipRect.isEmpty()) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
940 |
*outImage = outImage->copy(scaledClipRect); |
0 | 941 |
return !outImage->isNull(); |
942 |
} |
|
943 |
||
944 |
||
945 |
struct my_jpeg_destination_mgr : public jpeg_destination_mgr { |
|
946 |
// Nothing dynamic - cannot rely on destruction over longjump |
|
947 |
QIODevice *device; |
|
948 |
JOCTET buffer[max_buf]; |
|
949 |
||
950 |
public: |
|
951 |
my_jpeg_destination_mgr(QIODevice *); |
|
952 |
}; |
|
953 |
||
954 |
||
955 |
#if defined(Q_C_CALLBACKS) |
|
956 |
extern "C" { |
|
957 |
#endif |
|
958 |
||
959 |
static void qt_init_destination(j_compress_ptr) |
|
960 |
{ |
|
961 |
} |
|
962 |
||
963 |
static boolean qt_empty_output_buffer(j_compress_ptr cinfo) |
|
964 |
{ |
|
965 |
my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; |
|
966 |
||
967 |
int written = dest->device->write((char*)dest->buffer, max_buf); |
|
968 |
if (written == -1) |
|
969 |
(*cinfo->err->error_exit)((j_common_ptr)cinfo); |
|
970 |
||
971 |
dest->next_output_byte = dest->buffer; |
|
972 |
dest->free_in_buffer = max_buf; |
|
973 |
||
974 |
#if defined(Q_OS_UNIXWARE) |
|
975 |
return B_TRUE; |
|
976 |
#else |
|
977 |
return true; |
|
978 |
#endif |
|
979 |
} |
|
980 |
||
981 |
static void qt_term_destination(j_compress_ptr cinfo) |
|
982 |
{ |
|
983 |
my_jpeg_destination_mgr* dest = (my_jpeg_destination_mgr*)cinfo->dest; |
|
984 |
qint64 n = max_buf - dest->free_in_buffer; |
|
985 |
||
986 |
qint64 written = dest->device->write((char*)dest->buffer, n); |
|
987 |
if (written == -1) |
|
988 |
(*cinfo->err->error_exit)((j_common_ptr)cinfo); |
|
989 |
} |
|
990 |
||
991 |
#if defined(Q_C_CALLBACKS) |
|
992 |
} |
|
993 |
#endif |
|
994 |
||
995 |
inline my_jpeg_destination_mgr::my_jpeg_destination_mgr(QIODevice *device) |
|
996 |
{ |
|
997 |
jpeg_destination_mgr::init_destination = qt_init_destination; |
|
998 |
jpeg_destination_mgr::empty_output_buffer = qt_empty_output_buffer; |
|
999 |
jpeg_destination_mgr::term_destination = qt_term_destination; |
|
1000 |
this->device = device; |
|
1001 |
next_output_byte = buffer; |
|
1002 |
free_in_buffer = max_buf; |
|
1003 |
} |
|
1004 |
||
1005 |
||
1006 |
static bool write_jpeg_image(const QImage &sourceImage, QIODevice *device, int sourceQuality) |
|
1007 |
{ |
|
1008 |
bool success = false; |
|
1009 |
const QImage image = sourceImage; |
|
1010 |
const QVector<QRgb> cmap = image.colorTable(); |
|
1011 |
||
1012 |
struct jpeg_compress_struct cinfo; |
|
1013 |
JSAMPROW row_pointer[1]; |
|
1014 |
row_pointer[0] = 0; |
|
1015 |
||
1016 |
struct my_jpeg_destination_mgr *iod_dest = new my_jpeg_destination_mgr(device); |
|
1017 |
struct my_error_mgr jerr; |
|
1018 |
||
1019 |
cinfo.err = jpeg_std_error(&jerr); |
|
1020 |
jerr.error_exit = my_error_exit; |
|
1021 |
||
1022 |
if (!setjmp(jerr.setjmp_buffer)) { |
|
1023 |
// WARNING: |
|
1024 |
// this if loop is inside a setjmp/longjmp branch |
|
1025 |
// do not create C++ temporaries here because the destructor may never be called |
|
1026 |
// if you allocate memory, make sure that you can free it (row_pointer[0]) |
|
1027 |
jpeg_create_compress(&cinfo); |
|
1028 |
||
1029 |
cinfo.dest = iod_dest; |
|
1030 |
||
1031 |
cinfo.image_width = image.width(); |
|
1032 |
cinfo.image_height = image.height(); |
|
1033 |
||
1034 |
bool gray=false; |
|
1035 |
switch (image.format()) { |
|
1036 |
case QImage::Format_Mono: |
|
1037 |
case QImage::Format_MonoLSB: |
|
1038 |
case QImage::Format_Indexed8: |
|
1039 |
gray = true; |
|
3
41300fa6a67c
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
0
diff
changeset
|
1040 |
for (int i = image.colorCount(); gray && i--;) { |
0 | 1041 |
gray = gray & (qRed(cmap[i]) == qGreen(cmap[i]) && |
1042 |
qRed(cmap[i]) == qBlue(cmap[i])); |
|
1043 |
} |
|
1044 |
cinfo.input_components = gray ? 1 : 3; |
|
1045 |
cinfo.in_color_space = gray ? JCS_GRAYSCALE : JCS_RGB; |
|
1046 |
break; |
|
1047 |
default: |
|
1048 |
cinfo.input_components = 3; |
|
1049 |
cinfo.in_color_space = JCS_RGB; |
|
1050 |
} |
|
1051 |
||
1052 |
jpeg_set_defaults(&cinfo); |
|
1053 |
||
1054 |
qreal diffInch = qAbs(image.dotsPerMeterX()*2.54/100. - qRound(image.dotsPerMeterX()*2.54/100.)) |
|
1055 |
+ qAbs(image.dotsPerMeterY()*2.54/100. - qRound(image.dotsPerMeterY()*2.54/100.)); |
|
1056 |
qreal diffCm = (qAbs(image.dotsPerMeterX()/100. - qRound(image.dotsPerMeterX()/100.)) |
|
1057 |
+ qAbs(image.dotsPerMeterY()/100. - qRound(image.dotsPerMeterY()/100.)))*2.54; |
|
1058 |
if (diffInch < diffCm) { |
|
1059 |
cinfo.density_unit = 1; // dots/inch |
|
1060 |
cinfo.X_density = qRound(image.dotsPerMeterX()*2.54/100.); |
|
1061 |
cinfo.Y_density = qRound(image.dotsPerMeterY()*2.54/100.); |
|
1062 |
} else { |
|
1063 |
cinfo.density_unit = 2; // dots/cm |
|
1064 |
cinfo.X_density = (image.dotsPerMeterX()+50) / 100; |
|
1065 |
cinfo.Y_density = (image.dotsPerMeterY()+50) / 100; |
|
1066 |
} |
|
1067 |
||
1068 |
||
1069 |
int quality = sourceQuality >= 0 ? qMin(sourceQuality,100) : 75; |
|
1070 |
#if defined(Q_OS_UNIXWARE) |
|
1071 |
jpeg_set_quality(&cinfo, quality, B_TRUE /* limit to baseline-JPEG values */); |
|
1072 |
jpeg_start_compress(&cinfo, B_TRUE); |
|
1073 |
#else |
|
1074 |
jpeg_set_quality(&cinfo, quality, true /* limit to baseline-JPEG values */); |
|
1075 |
jpeg_start_compress(&cinfo, true); |
|
1076 |
#endif |
|
1077 |
||
1078 |
row_pointer[0] = new uchar[cinfo.image_width*cinfo.input_components]; |
|
1079 |
int w = cinfo.image_width; |
|
1080 |
while (cinfo.next_scanline < cinfo.image_height) { |
|
1081 |
uchar *row = row_pointer[0]; |
|
1082 |
switch (image.format()) { |
|
1083 |
case QImage::Format_Mono: |
|
1084 |
case QImage::Format_MonoLSB: |
|
1085 |
if (gray) { |
|
1086 |
const uchar* data = image.scanLine(cinfo.next_scanline); |
|
1087 |
if (image.format() == QImage::Format_MonoLSB) { |
|
1088 |
for (int i=0; i<w; i++) { |
|
1089 |
bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); |
|
1090 |
row[i] = qRed(cmap[bit]); |
|
1091 |
} |
|
1092 |
} else { |
|
1093 |
for (int i=0; i<w; i++) { |
|
1094 |
bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); |
|
1095 |
row[i] = qRed(cmap[bit]); |
|
1096 |
} |
|
1097 |
} |
|
1098 |
} else { |
|
1099 |
const uchar* data = image.scanLine(cinfo.next_scanline); |
|
1100 |
if (image.format() == QImage::Format_MonoLSB) { |
|
1101 |
for (int i=0; i<w; i++) { |
|
1102 |
bool bit = !!(*(data + (i >> 3)) & (1 << (i & 7))); |
|
1103 |
*row++ = qRed(cmap[bit]); |
|
1104 |
*row++ = qGreen(cmap[bit]); |
|
1105 |
*row++ = qBlue(cmap[bit]); |
|
1106 |
} |
|
1107 |
} else { |
|
1108 |
for (int i=0; i<w; i++) { |
|
1109 |
bool bit = !!(*(data + (i >> 3)) & (1 << (7 -(i & 7)))); |
|
1110 |
*row++ = qRed(cmap[bit]); |
|
1111 |
*row++ = qGreen(cmap[bit]); |
|
1112 |
*row++ = qBlue(cmap[bit]); |
|
1113 |
} |
|
1114 |
} |
|
1115 |
} |
|
1116 |
break; |
|
1117 |
case QImage::Format_Indexed8: |
|
1118 |
if (gray) { |
|
1119 |
const uchar* pix = image.scanLine(cinfo.next_scanline); |
|
1120 |
for (int i=0; i<w; i++) { |
|
1121 |
*row = qRed(cmap[*pix]); |
|
1122 |
++row; ++pix; |
|
1123 |
} |
|
1124 |
} else { |
|
1125 |
const uchar* pix = image.scanLine(cinfo.next_scanline); |
|
1126 |
for (int i=0; i<w; i++) { |
|
1127 |
*row++ = qRed(cmap[*pix]); |
|
1128 |
*row++ = qGreen(cmap[*pix]); |
|
1129 |
*row++ = qBlue(cmap[*pix]); |
|
1130 |
++pix; |
|
1131 |
} |
|
1132 |
} |
|
1133 |
break; |
|
1134 |
case QImage::Format_RGB888: |
|
1135 |
memcpy(row, image.scanLine(cinfo.next_scanline), w * 3); |
|
1136 |
break; |
|
1137 |
case QImage::Format_RGB32: |
|
1138 |
case QImage::Format_ARGB32: |
|
1139 |
case QImage::Format_ARGB32_Premultiplied: { |
|
1140 |
QRgb* rgb = (QRgb*)image.scanLine(cinfo.next_scanline); |
|
1141 |
for (int i=0; i<w; i++) { |
|
1142 |
*row++ = qRed(*rgb); |
|
1143 |
*row++ = qGreen(*rgb); |
|
1144 |
*row++ = qBlue(*rgb); |
|
1145 |
++rgb; |
|
1146 |
} |
|
1147 |
break; |
|
1148 |
} |
|
1149 |
default: |
|
1150 |
qWarning("QJpegHandler: unable to write image of format %i", |
|
1151 |
image.format()); |
|
1152 |
break; |
|
1153 |
} |
|
1154 |
jpeg_write_scanlines(&cinfo, row_pointer, 1); |
|
1155 |
} |
|
1156 |
||
1157 |
jpeg_finish_compress(&cinfo); |
|
1158 |
jpeg_destroy_compress(&cinfo); |
|
1159 |
success = true; |
|
1160 |
} else { |
|
1161 |
jpeg_destroy_compress(&cinfo); |
|
1162 |
success = false; |
|
1163 |
} |
|
1164 |
||
1165 |
delete iod_dest; |
|
1166 |
delete [] row_pointer[0]; |
|
1167 |
return success; |
|
1168 |
} |
|
1169 |
||
1170 |
QJpegHandler::QJpegHandler() |
|
1171 |
{ |
|
1172 |
quality = 75; |
|
1173 |
} |
|
1174 |
||
1175 |
bool QJpegHandler::canRead() const |
|
1176 |
{ |
|
1177 |
if (canRead(device())) { |
|
1178 |
setFormat("jpeg"); |
|
1179 |
return true; |
|
1180 |
} |
|
1181 |
return false; |
|
1182 |
} |
|
1183 |
||
1184 |
bool QJpegHandler::canRead(QIODevice *device) |
|
1185 |
{ |
|
1186 |
if (!device) { |
|
1187 |
qWarning("QJpegHandler::canRead() called with no device"); |
|
1188 |
return false; |
|
1189 |
} |
|
1190 |
||
8
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1191 |
char buffer[2]; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1192 |
if (device->peek(buffer, 2) != 2) |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1193 |
return false; |
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1194 |
|
3f74d0d4af4c
qt:70947f0f93d948bc89b3b43d00da758a51f1ef84
Eckhart Koeppen <eckhart.koppen@nokia.com>
parents:
4
diff
changeset
|
1195 |
return uchar(buffer[0]) == 0xff && uchar(buffer[1]) == 0xd8; |
0 | 1196 |
} |
1197 |
||
1198 |
bool QJpegHandler::read(QImage *image) |
|
1199 |
{ |
|
1200 |
if (!canRead()) |
|
1201 |
return false; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1202 |
return read_jpeg_image(device(), image, scaledSize, scaledClipRect, clipRect, quality); |
0 | 1203 |
} |
1204 |
||
1205 |
bool QJpegHandler::write(const QImage &image) |
|
1206 |
{ |
|
1207 |
return write_jpeg_image(image, device(), quality); |
|
1208 |
} |
|
1209 |
||
1210 |
bool QJpegHandler::supportsOption(ImageOption option) const |
|
1211 |
{ |
|
1212 |
return option == Quality |
|
1213 |
|| option == ScaledSize |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1214 |
|| option == ScaledClipRect |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1215 |
|| option == ClipRect |
0 | 1216 |
|| option == Size |
1217 |
|| option == ImageFormat; |
|
1218 |
} |
|
1219 |
||
1220 |
QVariant QJpegHandler::option(ImageOption option) const |
|
1221 |
{ |
|
1222 |
if (option == Quality) { |
|
1223 |
return quality; |
|
1224 |
} else if (option == ScaledSize) { |
|
1225 |
return scaledSize; |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1226 |
} else if (option == ScaledClipRect) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1227 |
return scaledClipRect; |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1228 |
} else if (option == ClipRect) { |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1229 |
return clipRect; |
0 | 1230 |
} else if (option == Size) { |
1231 |
if (canRead() && !device()->isSequential()) { |
|
1232 |
qint64 pos = device()->pos(); |
|
1233 |
int width = 0; |
|
1234 |
int height = 0; |
|
1235 |
read_jpeg_size(device(), width, height); |
|
1236 |
device()->seek(pos); |
|
1237 |
return QSize(width, height); |
|
1238 |
} |
|
1239 |
} else if (option == ImageFormat) { |
|
1240 |
if (canRead() && !device()->isSequential()) { |
|
1241 |
qint64 pos = device()->pos(); |
|
1242 |
QImage::Format format = QImage::Format_Invalid; |
|
1243 |
read_jpeg_format(device(), format); |
|
1244 |
device()->seek(pos); |
|
1245 |
return format; |
|
1246 |
} |
|
1247 |
return QImage::Format_Invalid; |
|
1248 |
} |
|
1249 |
return QVariant(); |
|
1250 |
} |
|
1251 |
||
1252 |
void QJpegHandler::setOption(ImageOption option, const QVariant &value) |
|
1253 |
{ |
|
1254 |
if (option == Quality) |
|
1255 |
quality = value.toInt(); |
|
1256 |
else if ( option == ScaledSize ) |
|
1257 |
scaledSize = value.toSize(); |
|
4
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1258 |
else if ( option == ScaledClipRect ) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1259 |
scaledClipRect = value.toRect(); |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1260 |
else if ( option == ClipRect ) |
3b1da2848fc7
Revision: 201003
Dremov Kirill (Nokia-D-MSW/Tampere) <kirill.dremov@nokia.com>
parents:
3
diff
changeset
|
1261 |
clipRect = value.toRect(); |
0 | 1262 |
} |
1263 |
||
1264 |
QByteArray QJpegHandler::name() const |
|
1265 |
{ |
|
1266 |
return "jpeg"; |
|
1267 |
} |
|
1268 |
||
1269 |
QT_END_NAMESPACE |