📄 qjpeghandler.cpp
字号:
/****************************************************************************
**
** Copyright (C) 2008 Nokia Corporation and/or its subsidiary(-ies).
** Contact: Qt Software Information (qt-info@nokia.com)
**
** This file is part of the plugins of the Qt Toolkit.
**
** Commercial Usage
** Licensees holding valid Qt Commercial licenses may use this file in
** accordance with the Qt Commercial License Agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and Nokia.
**
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License versions 2.0 or 3.0 as published by the Free
** Software Foundation and appearing in the file LICENSE.GPL included in
** the packaging of this file. Please review the following information
** to ensure GNU General Public Licensing requirements will be met:
** http://www.fsf.org/licensing/licenses/info/GPLv2.html and
** http://www.gnu.org/copyleft/gpl.html. In addition, as a special
** exception, Nokia gives you certain additional rights. These rights
** are described in the Nokia Qt GPL Exception version 1.3, included in
** the file GPL_EXCEPTION.txt in this package.
**
** Qt for Windows(R) Licensees
** As a special exception, Nokia, as the sole copyright holder for Qt
** Designer, grants users of the Qt/Eclipse Integration plug-in the
** right for the Qt/Eclipse Integration to link to functionality
** provided by Qt Designer and its related libraries.
**
** If you are unsure which license is appropriate for your use, please
** contact the sales department at qt-sales@nokia.com.
**
****************************************************************************/
#include "qjpeghandler.h"
#include <qimage.h>
#include <qvariant.h>
#include <qvector.h>
#include <stdio.h> // jpeglib needs this to be pre-included
#include <setjmp.h>
#ifdef FAR
#undef FAR
#endif
// including jpeglib.h seems to be a little messy
extern "C" {
// mingw includes rpcndr.h but does not define boolean
#if defined(Q_OS_WIN) && defined(Q_CC_GNU)
# if defined(__RPCNDR_H__) && !defined(boolean)
typedef unsigned char boolean;
# define HAVE_BOOLEAN
# endif
#endif
#define XMD_H // shut JPEGlib up
#if defined(Q_OS_UNIXWARE)
# define HAVE_BOOLEAN // libjpeg under Unixware seems to need this
#endif
#include <jpeglib.h>
#ifdef const
# undef const // remove crazy C hackery in jconfig.h
#endif
}
QT_BEGIN_NAMESPACE
//#define QT_NO_IMAGE_SMOOTHSCALE
#ifndef QT_NO_IMAGE_SMOOTHSCALE
class QImageSmoothScalerPrivate;
class QImageSmoothScaler
{
public:
QImageSmoothScaler(const int w, const int h, const QImage &src);
QImageSmoothScaler(const int srcWidth, const int srcHeight,
const char *parameters);
virtual ~QImageSmoothScaler(void);
QImage scale();
protected:
int scaledWidth(void) const;
private:
QImageSmoothScalerPrivate *d;
virtual QRgb *scanLine(const int line = 0, const QImage *src = 0);
};
class QImageSmoothScalerPrivate
{
public:
int cols;
int newcols;
int rows;
int newrows;
bool hasAlpha;
const QImage *src;
void setup(const int srcWidth, const int srcHeight, const int dstWidth,
const int dstHeight, bool hasAlphaChannel);
};
QImageSmoothScaler::QImageSmoothScaler(const int w, const int h,
const QImage &src)
{
d = new QImageSmoothScalerPrivate;
d->setup(src.width(), src.height(), w, h, src.hasAlphaChannel() );
this->d->src = &src;
}
QImageSmoothScaler::QImageSmoothScaler(const int srcWidth, const int srcHeight,
const char *parameters)
{
char sModeStr[1024];
int t1;
int t2;
int dstWidth;
int dstHeight;
sModeStr[0] = '\0';
d = new QImageSmoothScalerPrivate;
#if defined(Q_OS_WIN) && !defined(Q_OS_WINCE) && defined(_MSC_VER) && _MSC_VER >= 1400
sscanf_s(parameters, "Scale( %i, %i, %1023s )", &dstWidth, &dstHeight, sModeStr, sizeof(sModeStr));
#else
sscanf(parameters, "Scale( %i, %i, %s )", &dstWidth, &dstHeight, sModeStr);
#endif
QString sModeQStr = QString::fromLatin1(sModeStr);
t1 = srcWidth * dstHeight;
t2 = srcHeight * dstWidth;
if (((sModeQStr == QLatin1String("ScaleMin")) && (t1 > t2)) || ((sModeQStr == QLatin1String("ScaleMax")) && (t2 < t2))) {
dstHeight = t2 / srcWidth;
} else if (sModeQStr != QLatin1String("ScaleFree")) {
dstWidth = t1 / srcHeight;
}
d->setup(srcWidth, srcHeight, dstWidth, dstHeight, 0);
}
void QImageSmoothScalerPrivate::setup(const int srcWidth, const int srcHeight,
const int dstWidth, const int dstHeight,
bool hasAlphaChannel)
{
cols = srcWidth;
rows = srcHeight;
newcols = dstWidth;
newrows = dstHeight;
hasAlpha = hasAlphaChannel;
}
int QImageSmoothScaler::scaledWidth() const
{
return d->cols;
}
QImageSmoothScaler::~QImageSmoothScaler()
{
delete d;
}
inline QRgb *QImageSmoothScaler::scanLine(const int line, const QImage *src)
{
return (QRgb*)src->scanLine(line);
}
/*
This function uses code based on pnmscale.c by Jef Poskanzer.
pnmscale.c - read a portable anymap and scale it
Copyright (C) 1989, 1991 by Jef Poskanzer.
Permission to use, copy, modify, and distribute this software and its
documentation for any purpose and without fee is hereby granted, provided
that the above copyright notice appear in all copies and that both that
copyright notice and this permission notice appear in supporting
documentation. This software is provided "as is" without express or
implied warranty.
*/
QImage QImageSmoothScaler::scale()
{
long SCALE;
long HALFSCALE;
QRgb *xelrow = 0;
QRgb *tempxelrow = 0;
QRgb *xP;
QRgb *nxP;
int row, rowsread;
int col, needtoreadrow;
uchar maxval = 255;
double xscale, yscale;
long sxscale, syscale;
long fracrowtofill, fracrowleft;
long *as;
long *rs;
long *gs;
long *bs;
int rowswritten = 0;
QImage dst;
if (d->cols > 4096) {
SCALE = 4096;
HALFSCALE = 2048;
} else {
int fac = 4096;
while (d->cols * fac > 4096) {
fac /= 2;
}
SCALE = fac * d->cols;
HALFSCALE = fac * d->cols / 2;
}
xscale = (double) d->newcols / (double) d->cols;
yscale = (double) d->newrows / (double) d->rows;
sxscale = (long)(xscale * SCALE);
syscale = (long)(yscale * SCALE);
if ( d->newrows != d->rows ) /* shortcut Y scaling if possible */
tempxelrow = new QRgb[d->cols];
if ( d->hasAlpha ) {
as = new long[d->cols];
for ( col = 0; col < d->cols; ++col )
as[col] = HALFSCALE;
} else {
as = 0;
}
rs = new long[d->cols];
gs = new long[d->cols];
bs = new long[d->cols];
rowsread = 0;
fracrowleft = syscale;
needtoreadrow = 1;
for ( col = 0; col < d->cols; ++col )
rs[col] = gs[col] = bs[col] = HALFSCALE;
fracrowtofill = SCALE;
dst = QImage( d->newcols, d->newrows, d->hasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32 );
for ( row = 0; row < d->newrows; ++row ) {
/* First scale Y from xelrow into tempxelrow. */
if ( d->newrows == d->rows ) {
/* shortcut Y scaling if possible */
tempxelrow = xelrow = scanLine(rowsread++, d->src);
} else {
while ( fracrowleft < fracrowtofill ) {
if ( needtoreadrow && rowsread < d->rows ) {
xelrow = scanLine(rowsread++, d->src);
}
for ( col = 0, xP = xelrow; col < d->cols; ++col, ++xP ) {
if (as) {
as[col] += fracrowleft * qAlpha( *xP );
rs[col] += fracrowleft * qRed( *xP ) * qAlpha( *xP ) / 255;
gs[col] += fracrowleft * qGreen( *xP ) * qAlpha( *xP ) / 255;
bs[col] += fracrowleft * qBlue( *xP ) * qAlpha( *xP ) / 255;
} else {
rs[col] += fracrowleft * qRed( *xP );
gs[col] += fracrowleft * qGreen( *xP );
bs[col] += fracrowleft * qBlue( *xP );
}
}
fracrowtofill -= fracrowleft;
fracrowleft = syscale;
needtoreadrow = 1;
}
/* Now fracrowleft is >= fracrowtofill, so we can produce a row. */
if ( needtoreadrow && rowsread < d->rows) {
xelrow = scanLine(rowsread++, d->src);
needtoreadrow = 0;
}
for ( col = 0, xP = xelrow, nxP = tempxelrow;
col < d->cols; ++col, ++xP, ++nxP )
{
register long a, r, g, b;
if ( as ) {
r = rs[col] + fracrowtofill * qRed( *xP ) * qAlpha( *xP ) / 255;
g = gs[col] + fracrowtofill * qGreen( *xP ) * qAlpha( *xP ) / 255;
b = bs[col] + fracrowtofill * qBlue( *xP ) * qAlpha( *xP ) / 255;
a = as[col] + fracrowtofill * qAlpha( *xP );
if ( a ) {
r = r * 255 / a * SCALE;
g = g * 255 / a * SCALE;
b = b * 255 / a * SCALE;
}
} else {
r = rs[col] + fracrowtofill * qRed( *xP );
g = gs[col] + fracrowtofill * qGreen( *xP );
b = bs[col] + fracrowtofill * qBlue( *xP );
a = 0; // unwarn
}
r /= SCALE;
if ( r > maxval ) r = maxval;
g /= SCALE;
if ( g > maxval ) g = maxval;
b /= SCALE;
if ( b > maxval ) b = maxval;
if ( as ) {
a /= SCALE;
if ( a > maxval ) a = maxval;
*nxP = qRgba( (int)r, (int)g, (int)b, (int)a );
as[col] = HALFSCALE;
} else {
*nxP = qRgb( (int)r, (int)g, (int)b );
}
rs[col] = gs[col] = bs[col] = HALFSCALE;
}
fracrowleft -= fracrowtofill;
if ( fracrowleft == 0 ) {
fracrowleft = syscale;
needtoreadrow = 1;
}
fracrowtofill = SCALE;
}
/* Now scale X from tempxelrow into dst and write it out. */
if ( d->newcols == d->cols ) {
/* shortcut X scaling if possible */
memcpy(dst.scanLine(rowswritten++), tempxelrow, d->newcols*4);
} else {
register long a, r, g, b;
register long fraccoltofill, fraccolleft = 0;
register int needcol;
nxP = (QRgb*)dst.scanLine(rowswritten++);
fraccoltofill = SCALE;
a = r = g = b = HALFSCALE;
needcol = 0;
for ( col = 0, xP = tempxelrow; col < d->cols; ++col, ++xP ) {
fraccolleft = sxscale;
while ( fraccolleft >= fraccoltofill ) {
if ( needcol ) {
++nxP;
a = r = g = b = HALFSCALE;
}
if ( as ) {
r += fraccoltofill * qRed( *xP ) * qAlpha( *xP ) / 255;
g += fraccoltofill * qGreen( *xP ) * qAlpha( *xP ) / 255;
b += fraccoltofill * qBlue( *xP ) * qAlpha( *xP ) / 255;
a += fraccoltofill * qAlpha( *xP );
if ( a ) {
r = r * 255 / a * SCALE;
g = g * 255 / a * SCALE;
b = b * 255 / a * SCALE;
}
} else {
r += fraccoltofill * qRed( *xP );
g += fraccoltofill * qGreen( *xP );
b += fraccoltofill * qBlue( *xP );
}
r /= SCALE;
if ( r > maxval ) r = maxval;
g /= SCALE;
if ( g > maxval ) g = maxval;
b /= SCALE;
if ( b > maxval ) b = maxval;
if (as) {
a /= SCALE;
if ( a > maxval ) a = maxval;
*nxP = qRgba( (int)r, (int)g, (int)b, (int)a );
} else {
*nxP = qRgb( (int)r, (int)g, (int)b );
}
fraccolleft -= fraccoltofill;
fraccoltofill = SCALE;
needcol = 1;
}
if ( fraccolleft > 0 ) {
if ( needcol ) {
++nxP;
a = r = g = b = HALFSCALE;
needcol = 0;
}
if (as) {
a += fraccolleft * qAlpha( *xP );
r += fraccolleft * qRed( *xP ) * qAlpha( *xP ) / 255;
g += fraccolleft * qGreen( *xP ) * qAlpha( *xP ) / 255;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -