qcross/libqnono/cnonogram.cpp

246 lines
6.2 KiB
C++
Raw Normal View History

2010-06-16 12:53:56 +00:00
/***************************************************************************
* Copyright (C) 2008 by Oliver Groß *
* z.o.gross@gmx.de *
* *
* This program is free software; you can redistribute it and/or modify *
* it under the terms of the GNU General Public License as published by *
* the Free Software Foundation; either version 2 of the License, or *
* (at your option) any later version. *
* *
* This program is distributed in the hope that it will be useful, *
* but WITHOUT ANY WARRANTY; without even the implied warranty of *
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
* GNU General Public License for more details. *
* *
* You should have received a copy of the GNU General Public License *
* along with this program; if not, write to the *
* Free Software Foundation, Inc., *
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "cnonogram.h"
#include <QImage>
namespace libqnono {
2010-06-16 12:53:56 +00:00
CNonogram::CNonogram()
: m_Size(0, 0),
m_Data(NULL),
m_Timeout(0),
m_MaximumNumberCount(0) {
}
CNonogram::CNonogram(QSize size) : m_Size(size), m_MaximumNumberCount(0) {
init();
}
CNonogram::CNonogram(QImage & image) : m_Data(NULL), m_MaximumNumberCount(0) {
loadFromImage(image);
}
CNonogram::CNonogram(QDataStream & stream) : m_Data(NULL), m_MaximumNumberCount(0) {
readFromStream(stream);
}
2010-06-16 12:53:56 +00:00
CNonogram::~CNonogram() {
cleanup();
}
void CNonogram::loadFromImage(QImage & image) {
if (image.isNull())
return;
resize(image.size());
for (int iy = 0; iy < m_Size.height(); iy++)
for (int ix = 0; ix < m_Size.width(); ix++)
m_Data[ix][iy] = !(image.pixel(ix, iy) & 0x00FFFFFF);
}
bool CNonogram::readFromStream(QDataStream & stream) {
unsigned char data;
QString stringBuffer;
qint32 intBuffer;
QSize sizeBuffer;
stream >> stringBuffer; qDebug("reading image: %s", qPrintable(stringBuffer));
if (stream.atEnd()) {
qCritical("invalid nonogram");
cleanup();
return false;
}
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);
}
}
2010-06-16 12:53:56 +00:00
}
void CNonogram::resize(QSize size) {
if (m_Data)
cleanup();
m_Size = size;
init();
}
void CNonogram::setPixel(int x, int y, bool value) {
m_Data[x][y] = value;
}
void CNonogram::updateNumbers() {
m_BlackPixels = 0;
int pixelCount;
m_MaximumNumberCount = 0;
for (int i = 0; i < m_Size.height(); i++) {
m_RowNumbers[i].clear();
pixelCount = 0;
for (int j = 0; j < m_Size.width(); j++) {
if (m_Data[j][i]) {
pixelCount++;
m_BlackPixels++;
}
else if (pixelCount) {
m_RowNumbers[i] << pixelCount;
pixelCount = 0;
}
}
if (pixelCount || m_RowNumbers[i].empty())
m_RowNumbers[i] << pixelCount;
if (m_RowNumbers[i].count() > m_MaximumNumberCount)
m_MaximumNumberCount = m_RowNumbers[i].count();
}
for (int j = 0; j < m_Size.width(); j++) {
m_ColumnNumbers[j].clear();
pixelCount = 0;
for (int i = 0; i < m_Size.height(); i++) {
if (m_Data[j][i]) {
pixelCount++;
}
else if (pixelCount) {
m_ColumnNumbers[j] << pixelCount;
pixelCount = 0;
}
}
if (pixelCount || m_ColumnNumbers[j].empty())
m_ColumnNumbers[j] << pixelCount;
if (m_ColumnNumbers[j].count() > m_MaximumNumberCount)
m_MaximumNumberCount = m_ColumnNumbers[j].count();
}
}
void CNonogram::fill(bool value) {
for (int i = 0; i < m_Size.width(); i++)
for (int j = 0; j < m_Size.height(); j++)
m_Data[i][j] = value;
}
void CNonogram::cleanup() {
delete[] m_RowNumbers;
delete[] m_ColumnNumbers;
for (int i = 0; i < m_Size.width(); i++)
delete[] m_Data[i];
delete[] m_Data;
m_Data = NULL;
}
void CNonogram::init() {
m_Data = new bool *[m_Size.width()];
for (int i = 0; i < m_Size.width(); i++)
m_Data[i] = new bool[m_Size.height()];
m_RowNumbers = new NumbersVector[m_Size.height()];
m_ColumnNumbers = new NumbersVector[m_Size.width()];
}
QDataStream & operator<<(QDataStream & stream, CNonogram & nonogram) {
nonogram.writeToStream(stream);
return stream;
}
QDataStream & operator>>(QDataStream & stream, CNonogram & nonogram) {
nonogram.readFromStream(stream);
return stream;
}
2010-06-16 12:53:56 +00:00
}