[libqnono] nonogram reading and writing from/to stream

This commit is contained in:
Oliver Groß 2012-02-28 11:33:20 +01:00
parent 082dd261bc
commit efe7150dc7
2 changed files with 96 additions and 24 deletions

View File

@ -37,7 +37,7 @@ namespace libqnono {
}
CNonogram::CNonogram(QDataStream & stream) : m_Data(NULL), m_MaximumNumberCount(0) {
loadFromStream(stream);
readFromStream(stream);
}
CNonogram::~CNonogram() {
@ -55,19 +55,98 @@ namespace libqnono {
m_Data[ix][iy] = !(image.pixel(ix, iy) & 0x00FFFFFF);
}
void CNonogram::loadFromStream(QDataStream & stream) {
qDebug("loading picture from stream...");
stream >> m_Name; qDebug("m_Name = %s", qPrintable(m_Name));
int imageWidth, imageHeight;
stream >> imageWidth; qDebug("imageWidth = %i", imageWidth);
stream >> imageHeight; qDebug("imageHeight = %i", imageHeight);
resize(QSize(imageWidth, imageHeight));
bool CNonogram::readFromStream(QDataStream & stream) {
unsigned char data;
QString stringBuffer;
qint32 intBuffer;
QSize sizeBuffer;
stream >> m_Timeout;
stream >> stringBuffer; qDebug("reading image: %s", qPrintable(stringBuffer));
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
for (int iy = 0; iy < m_Size.height(); iy++)
for (int ix = 0; ix < m_Size.width(); ix++)
stream >> m_Data[ix][iy];
stream >> intBuffer; qDebug("width %i", intBuffer);
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
sizeBuffer.setWidth(intBuffer);
stream >> intBuffer; qDebug("height %i", intBuffer);
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
sizeBuffer.setHeight(intBuffer);
m_Name = stringBuffer;
stream >> intBuffer; qDebug("timeout %i", intBuffer);
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
m_Timeout = intBuffer;
resize(sizeBuffer);
for (int x = 0; x < sizeBuffer.width(); x++) {
int y = 0;
while (y < sizeBuffer.height()) {
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
stream.readRawData((char *)&data, 1);
int b;
for (b = 0; b < 8; b++) {
if (y < sizeBuffer.height())
m_Data[x][y] = (bool)(data & 0x80);
data = data << 1;
y++;
}
}
}
return true;
}
void CNonogram::writeToStream(QDataStream & stream) {
stream << m_Name;
stream << qint32(m_Size.width());
stream << qint32(m_Size.height());
stream << qint32(m_Timeout);
unsigned char data;
for (int x = 0; x < m_Size.width(); x++) {
data = 0x00;
int y = 0;
while (y < m_Size.height()) {
int b;
for (b = 0; b < 8; b++) {
data = data << 1;
if (y < m_Size.height() && m_Data[x][y])
data |= 0x01;
y++;
}
stream.writeRawData((char *)&data, 1);
}
}
}
void CNonogram::resize(QSize size) {
@ -155,21 +234,12 @@ namespace libqnono {
}
QDataStream & operator<<(QDataStream & stream, CNonogram & nonogram) {
stream << nonogram.name();
stream << nonogram.width();
stream << nonogram.height();
stream << nonogram.timeout();
for (int iy = 0; iy < nonogram.height(); iy++)
for (int ix = 0; ix < nonogram.width(); ix++)
stream << nonogram.pixel(ix, iy);
nonogram.writeToStream(stream);
return stream;
}
QDataStream & operator>>(QDataStream & stream, CNonogram & nonogram) {
nonogram.loadFromStream(stream);
nonogram.readFromStream(stream);
return stream;
}
}

View File

@ -38,9 +38,11 @@ namespace libqnono {
~CNonogram();
void loadFromImage(QImage & image);
void loadFromStream(QDataStream & stream);
void resize(QSize size);
bool readFromStream(QDataStream & stream);
void writeToStream(QDataStream & stream);
inline QString name() const { return m_Name; }
inline void setName(QString value) { m_Name = value; }