From 3b8408f7ac3eb3f43538c0b98deab58534fb5e52 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stefan=20B=C3=BChler?= Date: Fri, 20 Apr 2012 15:00:02 +0200 Subject: [PATCH] marker -> image conversion --- libqnono/nonogramimage.cpp | 40 +++++++++++++++++++++++++++++--------- libqnono/nonogramimage.h | 5 +++++ libqnono/nonogrammarker.h | 6 ++++++ 3 files changed, 42 insertions(+), 9 deletions(-) diff --git a/libqnono/nonogramimage.cpp b/libqnono/nonogramimage.cpp index 39a36d0..8cd32fe 100644 --- a/libqnono/nonogramimage.cpp +++ b/libqnono/nonogramimage.cpp @@ -17,6 +17,7 @@ * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * ***************************************************************************/ #include "nonogramimage.h" +#include "nonogrammarker.h" #include @@ -69,15 +70,8 @@ namespace libqnono { } NonogramImage::NonogramImage(const QImage & image) - : m_size(image.size()), m_data(0), m_blackPixels(0) { - int rows = image.height(), cols = image.width(); - m_data = new bool[rows*cols]; - for (int i = 0, y = 0; y < rows; ++y) { - for (int x = 0; x < cols; ++x, ++i) { - m_data[i] = (0 == (image.pixel(x, y) & 0x00FFFFFF)); - if (m_data[i]) ++m_blackPixels; - } - } + : m_size(0, 0), m_data(0), m_blackPixels(0) { + load(image); } NonogramImage::~NonogramImage() { @@ -196,6 +190,34 @@ namespace libqnono { stream.writeRawData(bits, bitcount); } + void NonogramImage::load(const QImage & image) { + delete [] m_data; m_data = 0; + m_blackPixels = 0; + + m_size = image.size(); + int rows = m_size.height(), cols = m_size.width(); + m_data = new bool[rows*cols]; + for (int i = 0, y = 0; y < rows; ++y) { + for (int x = 0; x < cols; ++x, ++i) { + m_data[i] = (0 == (image.pixel(x, y) & 0x00FFFFFF)); + if (m_data[i]) ++m_blackPixels; + } + } + } + + void NonogramImage::load(const NonogramMarker & marker) { + delete [] m_data; m_data = 0; + m_blackPixels = 0; + + m_size = marker.size(); + int n = m_size.height() * m_size.width(); + m_data = new bool[n]; + for (int i = 0; i < n; ++i) { + m_data[i] = (NonogramMarker::MARKED == marker.m_data[i]); + if (m_data[i]) ++m_blackPixels; + } + } + QDataStream & operator<<(QDataStream & stream, const NonogramImage & image) { image.writeToStream(stream); return stream; diff --git a/libqnono/nonogramimage.h b/libqnono/nonogramimage.h index 4dd9852..f8c6865 100644 --- a/libqnono/nonogramimage.h +++ b/libqnono/nonogramimage.h @@ -23,6 +23,8 @@ #include namespace libqnono { + class NonogramMarker; + class NonogramImageView { public: virtual bool pixel(int coord1, int coord2) const = 0; @@ -57,6 +59,9 @@ namespace libqnono { bool readFromStream(QDataStream & stream); void writeToStream(QDataStream & stream) const; + void load(const QImage & image); + void load(const NonogramMarker & marker); + private: QSize m_size; bool *m_data; diff --git a/libqnono/nonogrammarker.h b/libqnono/nonogrammarker.h index 87af2ab..d89b5ca 100644 --- a/libqnono/nonogrammarker.h +++ b/libqnono/nonogrammarker.h @@ -22,6 +22,8 @@ #include namespace libqnono { + class NonogramImage; + class NonogramMarker { public: enum Mark { NONE = 0, MARKED = 1, CROSSED = 2 }; @@ -38,11 +40,15 @@ namespace libqnono { void setPixel(int x, int y, Mark value) { m_data[y*m_size.width()+x] = value; } QSize size() const { return m_size; } + int width() const { return m_size.width(); } + int height() const { return m_size.height(); } bool readFromStream(QDataStream & stream); void writeToStream(QDataStream & stream) const; private: + friend class NonogramImage; + QSize m_size; Mark *m_data; };