marker -> image conversion

This commit is contained in:
Stefan Bühler 2012-04-20 15:00:02 +02:00
parent 71e31dcc22
commit 3b8408f7ac
3 changed files with 42 additions and 9 deletions

View File

@ -17,6 +17,7 @@
* 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
***************************************************************************/
#include "nonogramimage.h"
#include "nonogrammarker.h"
#include <QDataStream>
@ -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;

View File

@ -23,6 +23,8 @@
#include <QImage>
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;

View File

@ -22,6 +22,8 @@
#include <QSize>
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;
};