/*************************************************************************** * 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 namespace libqcross { 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() { cleanup(); } void CNonogram::loadFromImage(QImage & image) { if (image.isNull()) return; resize(image.size()); for (int i = 0; i < m_Size.width(); i++) for (int j = 0; j < m_Size.height(); j++) m_Data[i][j] = !(image.pixel(i, j) & 0x00FFFFFF); } 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()]; } }