367 lines
9.6 KiB
C++
367 lines
9.6 KiB
C++
/***************************************************************************
|
|
* 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 {
|
|
static void boolsFromImage(bool ** & data, const QImage &image) {
|
|
int cols = image.width(), rows = image.height();
|
|
data = new bool*[cols];
|
|
for (int col = 0; col < cols; ++col) data[col] = new bool[rows];
|
|
for (int row = 0; row < rows; ++row) {
|
|
for (int col = 0; col < cols; ++col) {
|
|
data[col][row] = 0 == (image.pixel(col, row) & 0x00FFFFFF);
|
|
}
|
|
}
|
|
}
|
|
|
|
static void freeBools(const QSize & size, bool ** data) {
|
|
int cols = size.width();
|
|
for (int col = 0; col < cols; ++col) delete [] data[col];
|
|
delete[] data;
|
|
}
|
|
|
|
static const quint64 CNonogramNumbers_MAGIC = Q_UINT64_C(0xd33efcacc0050164);
|
|
|
|
CNonogramNumbers::CNonogramNumbers(const vv_num & rows, const vv_num & columns)
|
|
: m_rows(rows), m_columns(columns) {
|
|
}
|
|
|
|
CNonogramNumbers::CNonogramNumbers(const CNonogram & source)
|
|
: m_rows(source.height()), m_columns(source.width()) {
|
|
// remove the "0" blocks (they are used for otherwise empty lines)
|
|
for (int i = 0; i < source.height(); ++i) {
|
|
m_rows[i] = source.rowNumbers(i);
|
|
if (1 == m_rows[i].count() && 0 == m_rows[i][0]) m_rows[i].clear();
|
|
}
|
|
for (int i = 0; i < source.width(); ++i) {
|
|
m_columns[i] = source.columnNumbers(i);
|
|
if (1 == m_columns[i].count() && 0 == m_columns[i][0]) m_columns[i].clear();
|
|
}
|
|
}
|
|
|
|
CNonogramNumbers::CNonogramNumbers(QSize size, bool **data) {
|
|
calcFromImage(size, data);
|
|
}
|
|
|
|
CNonogramNumbers::CNonogramNumbers(const QImage & image) {
|
|
bool ** data = 0;
|
|
boolsFromImage(data, image);
|
|
calcFromImage(image.size(), data);
|
|
freeBools(image.size(), data);
|
|
}
|
|
|
|
void CNonogramNumbers::calcFromImage(QSize size, bool **data) {
|
|
int rows = size.height(), cols = size.width();
|
|
m_rows.clear(); m_rows.resize(rows);
|
|
m_columns.clear(); m_columns.resize(cols);
|
|
for (int row = 0; row < rows; ++row) {
|
|
quint16 *val = 0;
|
|
for (int col = 0; col < cols; ++col) {
|
|
bool b = data[col][row];
|
|
if (!val && b) {
|
|
m_rows[row].append(1);
|
|
val = &m_rows[row].last();
|
|
} else if (b) {
|
|
++(*val);
|
|
} else {
|
|
val = 0;
|
|
}
|
|
}
|
|
}
|
|
for (int col = 0; col < cols; ++col) {
|
|
quint16 *val = 0;
|
|
for (int row = 0; row < rows; ++row) {
|
|
bool b = data[col][row];
|
|
if (!val && b) {
|
|
m_columns[col].append(1);
|
|
val = &m_columns[col].last();
|
|
} else if (b) {
|
|
++(*val);
|
|
} else {
|
|
val = 0;
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
bool CNonogramNumbers::readFromStream(QDataStream & stream) {
|
|
quint64 magic;
|
|
stream >> magic;
|
|
if (CNonogramNumbers_MAGIC != magic || QDataStream::Ok != stream.status()) {
|
|
if (QDataStream::ReadPastEnd != stream.status()) stream.setStatus(QDataStream::ReadCorruptData);
|
|
return false;
|
|
}
|
|
stream >> m_rows >> m_columns;
|
|
return QDataStream::Ok == stream.status();
|
|
}
|
|
|
|
void CNonogramNumbers::writeToStream(QDataStream & stream) {
|
|
stream << CNonogramNumbers_MAGIC << m_rows << m_columns;
|
|
}
|
|
|
|
bool CNonogramNumbers::operator==(const CNonogramNumbers &other) const {
|
|
return m_rows == other.m_rows && m_columns == other.m_columns;
|
|
}
|
|
|
|
bool CNonogramNumbers::check(bool **data) const {
|
|
CNonogramNumbers tmp(size(), data);
|
|
return *this == tmp;
|
|
}
|
|
|
|
bool CNonogramNumbers::check(const QImage & image) const {
|
|
if (size() != image.size()) return FALSE;
|
|
CNonogramNumbers tmp(image);
|
|
return *this == tmp;
|
|
}
|
|
|
|
QDataStream & operator<<(QDataStream & stream, CNonogramNumbers & numbers) {
|
|
numbers.writeToStream(stream);
|
|
return stream;
|
|
}
|
|
|
|
QDataStream & operator>>(QDataStream & stream, CNonogramNumbers & numbers) {
|
|
numbers.readFromStream(stream);
|
|
return stream;
|
|
}
|
|
|
|
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);
|
|
}
|
|
|
|
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);
|
|
}
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|