Code reformatted using kde-dev-scripts/astyle-kdelibs.

Use git blame -w 47df948 to show authorship as it was before this commit.
This commit is contained in:
David Faure
2013-12-18 09:54:11 +01:00
parent 47df9483fd
commit 02b5e97197
19 changed files with 950 additions and 731 deletions

View File

@ -7,7 +7,6 @@
// published by the Free Software Foundation; either version 2 of the
// License, or (at your option) any later version.
/* this code supports:
* reading:
* everything, except images with 1 dimension or images with
@ -34,13 +33,16 @@ class RLEData : public QVector<uchar>
{
public:
RLEData() {}
RLEData(const uchar *d, uint l, uint o) : _offset(o) {
for (uint i = 0; i < l; i++)
RLEData(const uchar *d, uint l, uint o) : _offset(o)
{
for (uint i = 0; i < l; i++) {
append(d[i]);
}
}
bool operator<(const RLEData&) const;
void write(QDataStream& s);
uint offset() const {
bool operator<(const RLEData &) const;
void write(QDataStream &s);
uint offset() const
{
return _offset;
}
@ -48,14 +50,14 @@ private:
uint _offset;
};
class RLEMap : public QMap<RLEData, uint>
{
public:
RLEMap() : _counter(0), _offset(0) {}
uint insert(const uchar *d, uint l);
QVector<const RLEData*> vector();
void setBaseOffset(uint o) {
QVector<const RLEData *> vector();
void setBaseOffset(uint o)
{
_offset = o;
}
@ -64,15 +66,14 @@ private:
uint _offset;
};
class SGIImage
{
public:
SGIImage(QIODevice *device);
~SGIImage();
bool readImage(QImage&);
bool writeImage(const QImage&);
bool readImage(QImage &);
bool writeImage(const QImage &);
private:
enum { NORMAL, DITHERED, SCREEN, COLORMAP }; // colormap
@ -95,16 +96,16 @@ private:
QByteArray _data;
QByteArray::Iterator _pos;
RLEMap _rlemap;
QVector<const RLEData*> _rlevector;
QVector<const RLEData *> _rlevector;
uint _numrows;
bool readData(QImage&);
bool readData(QImage &);
bool getRow(uchar *dest);
void writeHeader();
void writeRle();
void writeVerbatim(const QImage&);
bool scanData(const QImage&);
void writeVerbatim(const QImage &);
bool scanData(const QImage &);
uint compact(uchar *, uchar *);
uchar intensity(uchar);
};
@ -117,24 +118,22 @@ SGIImage::SGIImage(QIODevice *io) :
_stream.setDevice(_dev);
}
SGIImage::~SGIImage()
{
delete[] _starttab;
delete[] _lengthtab;
}
///////////////////////////////////////////////////////////////////////////////
bool SGIImage::getRow(uchar *dest)
{
int n, i;
if (!_rle) {
for (i = 0; i < _xsize; i++) {
if (_pos >= _data.end())
if (_pos >= _data.end()) {
return false;
}
dest[i] = uchar(*_pos);
_pos += _bpc;
}
@ -142,11 +141,13 @@ bool SGIImage::getRow(uchar *dest)
}
for (i = 0; i < _xsize;) {
if (_bpc == 2)
if (_bpc == 2) {
_pos++;
}
n = *_pos & 0x7f;
if (!n)
if (!n) {
break;
}
if (*_pos++ & 0x80) {
for (; i < _xsize && n--; i++) {
@ -154,8 +155,9 @@ bool SGIImage::getRow(uchar *dest)
_pos += _bpc;
}
} else {
for (; i < _xsize && n--; i++)
for (; i < _xsize && n--; i++) {
*dest++ = *_pos;
}
_pos += _bpc;
}
@ -163,8 +165,7 @@ bool SGIImage::getRow(uchar *dest)
return i == _xsize;
}
bool SGIImage::readData(QImage& img)
bool SGIImage::readData(QImage &img)
{
QRgb *c;
quint32 *start = _starttab;
@ -172,62 +173,76 @@ bool SGIImage::readData(QImage& img)
uchar *line = (uchar *)lguard.data();
unsigned x, y;
if (!_rle)
if (!_rle) {
_pos = _data.begin();
for (y = 0; y < _ysize; y++) {
if (_rle)
_pos = _data.begin() + *start++;
if (!getRow(line))
return false;
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
*c = qRgb(line[x], line[x], line[x]);
}
if (_zsize == 1)
for (y = 0; y < _ysize; y++) {
if (_rle) {
_pos = _data.begin() + *start++;
}
if (!getRow(line)) {
return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++) {
*c = qRgb(line[x], line[x], line[x]);
}
}
if (_zsize == 1) {
return true;
}
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
if (_rle)
if (_rle) {
_pos = _data.begin() + *start++;
if (!getRow(line))
}
if (!getRow(line)) {
return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
for (x = 0; x < _xsize; x++, c++) {
*c = qRgb(qRed(*c), line[x], line[x]);
}
}
for (y = 0; y < _ysize; y++) {
if (_rle)
if (_rle) {
_pos = _data.begin() + *start++;
if (!getRow(line))
}
if (!getRow(line)) {
return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
for (x = 0; x < _xsize; x++, c++) {
*c = qRgb(qRed(*c), qGreen(*c), line[x]);
}
}
if (_zsize == 3)
if (_zsize == 3) {
return true;
}
}
for (y = 0; y < _ysize; y++) {
if (_rle)
if (_rle) {
_pos = _data.begin() + *start++;
if (!getRow(line))
}
if (!getRow(line)) {
return false;
}
c = (QRgb *)img.scanLine(_ysize - y - 1);
for (x = 0; x < _xsize; x++, c++)
for (x = 0; x < _xsize; x++, c++) {
*c = qRgba(qRed(*c), qGreen(*c), qBlue(*c), line[x]);
}
}
return true;
}
bool SGIImage::readImage(QImage& img)
bool SGIImage::readImage(QImage &img)
{
qint8 u8;
qint16 u16;
@ -237,14 +252,16 @@ bool SGIImage::readImage(QImage& img)
// magic
_stream >> u16;
if (u16 != 0x01da)
if (u16 != 0x01da) {
return false;
}
// verbatim/rle
_stream >> _rle;
// qDebug() << (_rle ? "RLE" : "verbatim");
if (_rle > 1)
if (_rle > 1) {
return false;
}
// bytes per channel
_stream >> _bpc;
@ -253,14 +270,16 @@ bool SGIImage::readImage(QImage& img)
;
else if (_bpc == 2) {
// qDebug() << "dropping least significant byte";
} else
} else {
return false;
}
// number of dimensions
_stream >> _dim;
// qDebug() << "dimensions: " << _dim;
if (_dim < 1 || _dim > 3)
if (_dim < 1 || _dim > 3) {
return false;
}
_stream >> _xsize >> _ysize >> _zsize >> _pixmin >> _pixmax >> u32;
// qDebug() << "x: " << _xsize;
@ -273,27 +292,30 @@ bool SGIImage::readImage(QImage& img)
_stream >> _colormap;
// qDebug() << "colormap: " << _colormap;
if (_colormap != NORMAL)
return false; // only NORMAL supported
if (_colormap != NORMAL) {
return false; // only NORMAL supported
}
for (int i = 0; i < 404; i++)
for (int i = 0; i < 404; i++) {
_stream >> u8;
}
if (_dim == 1) {
// qDebug() << "1-dimensional images aren't supported yet";
return false;
}
if (_stream.atEnd())
if (_stream.atEnd()) {
return false;
}
_numrows = _ysize * _zsize;
img = QImage(_xsize, _ysize, QImage::Format_RGB32);
if (_zsize == 2 || _zsize == 4)
if (_zsize == 2 || _zsize == 4) {
img = img.convertToFormat(QImage::Format_ARGB32);
else if (_zsize > 4) {
} else if (_zsize > 4) {
// qDebug() << "using first 4 of " << _zsize << " channels";
}
@ -306,8 +328,9 @@ bool SGIImage::readImage(QImage& img)
}
_lengthtab = new quint32[_numrows];
for (l = 0; l < _numrows; l++)
for (l = 0; l < _numrows; l++) {
_stream >> _lengthtab[l];
}
}
_data = _dev->readAll();
@ -329,84 +352,87 @@ bool SGIImage::readImage(QImage& img)
return true;
}
///////////////////////////////////////////////////////////////////////////////
void RLEData::write(QDataStream& s)
void RLEData::write(QDataStream &s)
{
for (int i = 0; i < size(); i++)
for (int i = 0; i < size(); i++) {
s << at(i);
}
}
bool RLEData::operator<(const RLEData& b) const
bool RLEData::operator<(const RLEData &b) const
{
uchar ac, bc;
for (int i = 0; i < qMin(size(), b.size()); i++) {
ac = at(i);
bc = b[i];
if (ac != bc)
if (ac != bc) {
return ac < bc;
}
}
return size() < b.size();
}
uint RLEMap::insert(const uchar *d, uint l)
{
RLEData data = RLEData(d, l, _offset);
Iterator it = find(data);
if (it != end())
if (it != end()) {
return it.value();
}
_offset += l;
return QMap<RLEData, uint>::insert(data, _counter++).value();
}
QVector<const RLEData*> RLEMap::vector()
QVector<const RLEData *> RLEMap::vector()
{
QVector<const RLEData*> v(size());
for (Iterator it = begin(); it != end(); ++it)
QVector<const RLEData *> v(size());
for (Iterator it = begin(); it != end(); ++it) {
v.replace(it.value(), &it.key());
}
return v;
}
uchar SGIImage::intensity(uchar c)
{
if (c < _pixmin)
if (c < _pixmin) {
_pixmin = c;
if (c > _pixmax)
}
if (c > _pixmax) {
_pixmax = c;
}
return c;
}
uint SGIImage::compact(uchar *d, uchar *s)
{
uchar *dest = d, *src = s, patt, *t, *end = s + _xsize;
int i, n;
while (src < end) {
for (n = 0, t = src; t + 2 < end && !(*t == t[1] && *t == t[2]); t++)
for (n = 0, t = src; t + 2 < end && !(*t == t[1] && *t == t[2]); t++) {
n++;
}
while (n) {
i = n > 126 ? 126 : n;
n -= i;
*dest++ = 0x80 | i;
while (i--)
while (i--) {
*dest++ = *src++;
}
}
if (src == end)
if (src == end) {
break;
}
patt = *src++;
for (n = 1; src < end && *src == patt; src++)
for (n = 1; src < end && *src == patt; src++) {
n++;
}
while (n) {
i = n > 126 ? 126 : n;
@ -419,8 +445,7 @@ uint SGIImage::compact(uchar *d, uchar *s)
return dest - d;
}
bool SGIImage::scanData(const QImage& img)
bool SGIImage::scanData(const QImage &img)
{
quint32 *start = _starttab;
QByteArray lineguard(_xsize * 2, 0);
@ -433,40 +458,46 @@ bool SGIImage::scanData(const QImage& img)
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qRed(*c++));
}
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
if (_zsize == 1)
if (_zsize == 1) {
return true;
}
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qGreen(*c++));
}
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qBlue(*c++));
}
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
if (_zsize == 3)
if (_zsize == 3) {
return true;
}
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
buf[x] = intensity(qAlpha(*c++));
}
len = compact(line, buf);
*start++ = _rlemap.insert(line, len);
}
@ -474,7 +505,6 @@ bool SGIImage::scanData(const QImage& img)
return true;
}
void SGIImage::writeHeader()
{
_stream << quint16(0x01da);
@ -483,16 +513,17 @@ void SGIImage::writeHeader()
_stream << _pixmin << _pixmax;
_stream << quint32(0);
for (int i = 0; i < 80; i++)
for (int i = 0; i < 80; i++) {
_imagename[i] = '\0';
}
_stream.writeRawData(_imagename, 80);
_stream << _colormap;
for (int i = 0; i < 404; i++)
for (int i = 0; i < 404; i++) {
_stream << quint8(0);
}
}
void SGIImage::writeRle()
{
_rle = 1;
@ -501,20 +532,22 @@ void SGIImage::writeRle()
uint i;
// write start table
for (i = 0; i < _numrows; i++)
for (i = 0; i < _numrows; i++) {
_stream << quint32(_rlevector[_starttab[i]]->offset());
}
// write length table
for (i = 0; i < _numrows; i++)
for (i = 0; i < _numrows; i++) {
_stream << quint32(_rlevector[_starttab[i]]->size());
}
// write data
for (i = 0; (int)i < _rlevector.size(); i++)
const_cast<RLEData*>(_rlevector[i])->write(_stream);
for (i = 0; (int)i < _rlevector.size(); i++) {
const_cast<RLEData *>(_rlevector[i])->write(_stream);
}
}
void SGIImage::writeVerbatim(const QImage& img)
void SGIImage::writeVerbatim(const QImage &img)
{
_rle = 0;
// qDebug() << "writing verbatim data";
@ -525,49 +558,56 @@ void SGIImage::writeVerbatim(const QImage& img)
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
_stream << quint8(qRed(*c++));
}
}
if (_zsize == 1)
if (_zsize == 1) {
return;
}
if (_zsize != 2) {
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
_stream << quint8(qGreen(*c++));
}
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
_stream << quint8(qBlue(*c++));
}
}
if (_zsize == 3)
if (_zsize == 3) {
return;
}
}
for (y = 0; y < _ysize; y++) {
c = reinterpret_cast<const QRgb *>(img.scanLine(_ysize - y - 1));
for (x = 0; x < _xsize; x++)
for (x = 0; x < _xsize; x++) {
_stream << quint8(qAlpha(*c++));
}
}
}
bool SGIImage::writeImage(const QImage& image)
bool SGIImage::writeImage(const QImage &image)
{
// qDebug() << "writing "; // TODO add filename
QImage img = image;
if (img.allGray())
if (img.allGray()) {
_dim = 2, _zsize = 1;
else
} else {
_dim = 3, _zsize = 3;
}
if (img.format() == QImage::Format_ARGB32)
if (img.format() == QImage::Format_ARGB32) {
_dim = 3, _zsize++;
}
img = img.convertToFormat(QImage::Format_RGB32);
if (img.isNull()) {
@ -594,8 +634,9 @@ bool SGIImage::writeImage(const QImage& image)
long verbatim_size = _numrows * _xsize;
long rle_size = _numrows * 2 * sizeof(quint32);
for (int i = 0; i < _rlevector.size(); i++)
for (int i = 0; i < _rlevector.size(); i++) {
rle_size += _rlevector[i]->size();
}
// qDebug() << "minimum intensity: " << _pixmin;
// qDebug() << "maximum intensity: " << _pixmax;
@ -603,22 +644,20 @@ bool SGIImage::writeImage(const QImage& image)
// qDebug() << "total savings: " << (verbatim_size - rle_size) << " bytes";
// qDebug() << "compression: " << (rle_size * 100.0 / verbatim_size) << '%';
if (verbatim_size <= rle_size)
if (verbatim_size <= rle_size) {
writeVerbatim(img);
else
} else {
writeRle();
}
return true;
}
///////////////////////////////////////////////////////////////////////////////
RGBHandler::RGBHandler()
{
}
bool RGBHandler::canRead() const
{
if (canRead(device())) {
@ -628,21 +667,18 @@ bool RGBHandler::canRead() const
return false;
}
bool RGBHandler::read(QImage *outImage)
{
SGIImage sgi(device());
return sgi.readImage(*outImage);
}
bool RGBHandler::write(const QImage &image)
{
SGIImage sgi(device());
return sgi.writeImage(image);
}
bool RGBHandler::canRead(QIODevice *device)
{
if (!device) {
@ -655,8 +691,9 @@ bool RGBHandler::canRead(QIODevice *device)
int readBytes = head.size();
if (device->isSequential()) {
while (readBytes > 0)
while (readBytes > 0) {
device->ungetChar(head[readBytes-- - 1]);
}
} else {
device->seek(oldPos);
@ -668,29 +705,31 @@ bool RGBHandler::canRead(QIODevice *device)
return data.contains(regexp);
}
///////////////////////////////////////////////////////////////////////////////
QImageIOPlugin::Capabilities RGBPlugin::capabilities(QIODevice *device, const QByteArray &format) const
{
if (format == "rgb" || format == "rgba" ||
format == "bw" || format == "sgi")
format == "bw" || format == "sgi") {
return Capabilities(CanRead | CanWrite);
if (!format.isEmpty())
}
if (!format.isEmpty()) {
return 0;
if (!device->isOpen())
}
if (!device->isOpen()) {
return 0;
}
Capabilities cap;
if (device->isReadable() && RGBHandler::canRead(device))
if (device->isReadable() && RGBHandler::canRead(device)) {
cap |= CanRead;
if (device->isWritable())
}
if (device->isWritable()) {
cap |= CanWrite;
}
return cap;
}
QImageIOHandler *RGBPlugin::create(QIODevice *device, const QByteArray &format) const
{
QImageIOHandler *handler = new RGBHandler;