r22457 by jghali - make a few ScImage methods const to avoid some potential memory waste due to image data being detached
scribus-commit
scribus-commit at lists.scribus.net
Fri Mar 30 16:10:31 UTC 2018
Author: jghali
Date: Fri Mar 30 16:10:31 2018
New Revision: 22457
URL: http://scribus.net/websvn/listing.php?repname=Scribus&sc=1&rev=22457
Log:
make a few ScImage methods const to avoid some potential memory waste due to image data being detached
Modified:
trunk/Scribus/scribus/scimage.cpp
trunk/Scribus/scribus/scimage.h
Modified: trunk/Scribus/scribus/scimage.cpp
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22457&path=/trunk/Scribus/scribus/scimage.cpp
==============================================================================
--- trunk/Scribus/scribus/scimage.cpp (original)
+++ trunk/Scribus/scribus/scimage.cpp Fri Mar 30 16:10:31 2018
@@ -1223,29 +1223,28 @@
return success;
}
-QByteArray ScImage::ImageToArray()
+QByteArray ScImage::ImageToArray() const
{
int i = 0;
int h = height();
int w = width();
unsigned char u;
- QRgb *s;
- QRgb r;
+ const QRgb *rgb;
QByteArray imgArray(3 * h * w, ' ');
if (imgArray.isNull())
return imgArray;
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
- for( int xi=0; xi < w; ++xi )
- {
- r = *s++;
- u=qRed(r);
+ for (int yi = 0; yi < h; ++yi)
+ {
+ rgb = (QRgb*) this->constScanLine(yi);
+ for (int xi = 0; xi < w; ++xi)
+ {
+ u = qRed(*rgb);
imgArray[i++] = u;
- u=qGreen(r);
+ u = qGreen(*rgb);
imgArray[i++] = u;
- u=qBlue(r);
+ u = qBlue(*rgb);
imgArray[i++] = u;
+ ++rgb;
}
}
return imgArray;
@@ -1269,9 +1268,10 @@
}
}
-bool ScImage::writeRGBDataToFilter(ScStreamFilter* filter)
-{
- QRgb r, *s;
+bool ScImage::writeRGBDataToFilter(ScStreamFilter* filter) const
+{
+ QRgb r;
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int h = height();
@@ -1282,9 +1282,9 @@
buffer.resize(bufferSize + 16);
if (buffer.isNull()) // Memory allocation failure
return false;
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
+ for (int yi = 0; yi < h; ++yi)
+ {
+ s = (const QRgb*) constScanLine(yi);
for( int xi=0; xi < w; ++xi )
{
r = *s++;
@@ -1303,9 +1303,10 @@
return success;
}
-bool ScImage::writeGrayDataToFilter(ScStreamFilter* filter, bool precal)
-{
- QRgb r, *s;
+bool ScImage::writeGrayDataToFilter(ScStreamFilter* filter, bool precal) const
+{
+ QRgb r;
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int h = height();
@@ -1316,12 +1317,12 @@
buffer.resize(bufferSize + 16);
if (buffer.isNull()) // Memory allocation failure
return false;
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
+ for (int yi = 0; yi < h; ++yi)
+ {
+ s = (const QRgb*) constScanLine(yi);
if (precal) // image data is already grayscale, no need for weighted conversion
{
- for( int xi=0; xi < w; ++xi )
+ for (int xi = 0; xi < w; ++xi)
{
r = *s;
k = qRed(r);
@@ -1331,7 +1332,7 @@
}
else
{
- for( int xi=0; xi < w; ++xi )
+ for (int xi = 0; xi < w; ++xi)
{
r = *s;
k = qMin(qRound(0.3 * qRed(r) + 0.59 * qGreen(r) + 0.11 * qBlue(r)), 255);
@@ -1350,9 +1351,9 @@
return success;
}
-bool ScImage::writeMonochromeDataToFilter(ScStreamFilter* filter, bool fromCmyk)
-{
- QRgb *s;
+bool ScImage::writeMonochromeDataToFilter(ScStreamFilter* filter, bool fromCmyk) const
+{
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int h = height();
@@ -1368,7 +1369,7 @@
{
char curByte = 0;
int bitCount = 0;
- s = (QRgb*)(scanLine( yi ));
+ s = (const QRgb*) constScanLine(yi);
for (int xi = 0; xi < w; ++xi)
{
curByte <<= 1;
@@ -1395,9 +1396,10 @@
return success;
}
-bool ScImage::writeCMYKDataToFilter(ScStreamFilter* filter)
-{
- QRgb r, *s;
+bool ScImage::writeCMYKDataToFilter(ScStreamFilter* filter) const
+{
+ QRgb r;
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int h = height();
@@ -1410,7 +1412,7 @@
return false;
for( int yi=0; yi < h; ++yi )
{
- s = (QRgb*)(scanLine( yi ));
+ s = (const QRgb*) constScanLine(yi);
for( int xi=0; xi < w; ++xi )
{
r = *s++;
@@ -1430,9 +1432,10 @@
return success;
}
-bool ScImage::writePSImageToFilter(ScStreamFilter* filter, int pl)
-{
- QRgb r, *s;
+bool ScImage::writePSImageToFilter(ScStreamFilter* filter, int pl) const
+{
+ QRgb r;
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int c, m, y, k;
@@ -1444,10 +1447,10 @@
buffer.resize(bufferSize + 16);
if (buffer.isNull()) // Memory allocation failure
return false;
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
- for( int xi=0; xi < w; ++xi )
+ for (int yi = 0; yi < h; ++yi)
+ {
+ s = (const QRgb*) constScanLine(yi);
+ for (int xi = 0; xi < w; ++xi)
{
r = *s++;
c = qRed(r);
@@ -1486,9 +1489,10 @@
return success;
}
-bool ScImage::writePSImageToFilter(ScStreamFilter* filter, const QByteArray& mask, int pl)
-{
- QRgb r, *s;
+bool ScImage::writePSImageToFilter(ScStreamFilter* filter, const QByteArray& mask, int pl) const
+{
+ QRgb r;
+ const QRgb *s;
QByteArray buffer;
bool success = true;
int c, m, y, k;
@@ -1503,10 +1507,10 @@
if (buffer.isNull()) // Check for memory allocation failure
return false;
unsigned char* maskData = (unsigned char*) mask.constData();
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
- for( int xi=0; xi < w; ++xi )
+ for (int yi = 0; yi < h; ++yi)
+ {
+ s = (const QRgb*) constScanLine(yi);
+ for (int xi = 0; xi < w; ++xi)
{
r = *s++;
c = qRed(r);
@@ -2724,21 +2728,21 @@
return true;
}
-bool ScImage::hasSmoothAlpha()
+bool ScImage::hasSmoothAlpha() const
{
int h = height();
int w = width();
QSet<int> alpha;
- QRgb *s, r;
- for( int yi=0; yi < h; ++yi )
- {
- s = (QRgb*)(scanLine( yi ));
- for( int xi=0; xi < w; ++xi )
- {
- r = *s++;
- alpha.insert(qAlpha(r));
+ const QRgb *s;
+ for (int yi = 0; yi < h; ++yi)
+ {
+ s = (const QRgb*) constScanLine(yi);
+ for (int xi = 0; xi < w; ++xi)
+ {
+ alpha.insert(qAlpha(*s));
if (alpha.count() > 2)
return true;
+ s++;
}
}
return (alpha.count() > 2);
Modified: trunk/Scribus/scribus/scimage.h
URL: http://scribus.net/websvn/diff.php?repname=Scribus&rev=22457&path=/trunk/Scribus/scribus/scimage.h
==============================================================================
--- trunk/Scribus/scribus/scimage.h (original)
+++ trunk/Scribus/scribus/scimage.h Fri Mar 30 16:10:31 2018
@@ -76,20 +76,20 @@
int height() const { return QImage::height(); }
int width() const { return QImage::width(); }
bool hasAlpha() const { return QImage::hasAlphaChannel(); }
- bool hasSmoothAlpha();
+ bool hasSmoothAlpha() const;
// Routines for PDF/PS output of images
- QByteArray ImageToArray();
+ QByteArray ImageToArray() const;
void convertToGray(void);
- bool writeRGBDataToFilter(ScStreamFilter* filter);
- bool writeGrayDataToFilter(ScStreamFilter* filter, bool precal);
- bool writeMonochromeDataToFilter(ScStreamFilter* filter, bool fromCmyk);
- bool writeCMYKDataToFilter(ScStreamFilter* filter);
+ bool writeRGBDataToFilter(ScStreamFilter* filter) const;
+ bool writeGrayDataToFilter(ScStreamFilter* filter, bool precal) const;
+ bool writeMonochromeDataToFilter(ScStreamFilter* filter, bool fromCmyk) const;
+ bool writeCMYKDataToFilter(ScStreamFilter* filter) const;
- bool writePSImageToFilter(ScStreamFilter* filter, int pl);
- bool writePSImageToFilter(ScStreamFilter* filter, const QByteArray& mask, int pl);
+ bool writePSImageToFilter(ScStreamFilter* filter, int pl) const;
+ bool writePSImageToFilter(ScStreamFilter* filter, const QByteArray& mask, int pl) const;
bool getAlpha(QString fn, int page, QByteArray& alpha, bool PDF, bool pdf14, int gsRes = 72, int scaleXSize = 0, int scaleYSize = 0);
bool convert2JPG(QString fn, int Quality, bool isCMYK, bool isGray);
More information about the scribus-commit
mailing list