forked from Ramblurr/PietCreator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ImageModel.cpp
286 lines (248 loc) · 8.27 KB
/
ImageModel.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*
Copyright (C) 2010 Casey Link <unnamedrambler@gmail.com>
This library is free software; you can redistribute it and/or modify it
under the terms of the GNU Library General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at your
option) any later version.
This library 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 Library General Public
License for more details.
You should have received a copy of the GNU Library General Public License
along with this library; see the file COPYING.LIB. If not, write to the
Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
02110-1301, USA.
*/
#include "ImageModel.h"
extern "C"
{
#include "npiet.h"
}
#include <QtGui>
#include <QMessageBox>
#include <QDebug>
ImageModel::ImageModel( QObject *parent ) :
QAbstractTableModel( parent ), mPixelSize( 1 ), mDebugPixel( -1, -1 )
{
}
ImageModel::~ImageModel()
{
}
// helper function to check codel size
void
cc_check( int i, QRgb c, QRgb *last_c, int *last_p, int *min_w )
{
if ( i == 0 ) {
*last_c = c;
*last_p = i;
} else if ( *last_c != c ) {
int w = i - *last_p;
if ( w < *min_w ) {
*min_w = w;
}
*last_c = c;
*last_p = i;
}
}
QImage ImageModel::autoScale(const QImage& _image, int codel_size)
{
QImage image;
if( _image.format() != QImage::Format_ARGB32_Premultiplied ) {
image = _image.convertToFormat( QImage::Format_ARGB32_Premultiplied );
} else {
image = _image;
}
qDebug() << image.width() << image.height();
if ( codel_size < 0 ) {
/* Begin: modified part taken from npiet.c, cleanup_input () */
// (C) 2010 Erik Schoenfelder <schoenfr@web.de>
int i, j, last_p;
QRgb last_cc;
int min_w = image.width() + 1;
int min_ww = image.width() + 1;
int *o_cells;
QRgb *cells = ( QRgb* ) image.bits();
// scan image to guess codel size
/* left to right: */
for ( j = 0; j < image.height(); j++ ) {
for ( i = 0; i < image.width(); i++ ) {
cc_check( i, cells [j * image.width() + i], &last_cc, &last_p, &min_w );
}
cc_check( i, c_mark_index, &last_cc, &last_p, &min_w );
}
qDebug() << "his min_w" << min_w;
/* top to bottom: */
for ( i = 0; i < image.width(); i++ ) {
for ( j = 0; j < image.height(); j++ ) {
cc_check( j, cells [j * image.width() + i], &last_cc, &last_p, &min_w );
}
cc_check( j, c_mark_index, &last_cc, &last_p, &min_w );
}
codel_size = min_w;
/* End: part taken from npiet.c, cleanup_input () */
qDebug() << "Guessed codel size: " << codel_size;
}
// scale image so 1 codel == 1 pixel
if ( codel_size > 1 ) {
int width = image.width() / codel_size;
int height = image.height() / codel_size;
return image.scaled( width, height );
} else {
return image;
}
}
void ImageModel::setImage( const QImage& image, int codel_size )
{
mImage = autoScale(image, codel_size);
qDebug() << mImage.width() << mImage.height();
// reset();
}
void ImageModel::newImage(int w, int h)
{
QImage image( w, h, QImage::Format_ARGB32_Premultiplied );
image.fill( QColor( Qt::white ).rgb() );
setImage( image, 1 );
}
QImage ImageModel::image() const
{
return mImage;
}
void ImageModel::insertImage(const QImage& _image, int x, int y)
{
QPainter p( &mImage );
p.drawImage( x, y, _image );
// reset();
}
void ImageModel::setDebuggedPixel( int x, int y )
{
emitNeighborsChanged( mDebugPixel.y(), mDebugPixel.x() );
mDebugPixel.setY( y );
mDebugPixel.setX( x );
emitNeighborsChanged( mDebugPixel.y(), mDebugPixel.x() );
}
void ImageModel::emitNeighborsChanged( int row, int col )
{
QModelIndex topLeft = index( row - 1, col - 1 );
QModelIndex bottomRight = index( row + 1, col + 1 );
emit dataChanged( topLeft, bottomRight );
}
int ImageModel::columnCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent )
return mImage.width();
}
int ImageModel::rowCount( const QModelIndex& parent ) const
{
Q_UNUSED( parent )
return mImage.height();
}
QVariant ImageModel::data( const QModelIndex& index, int role ) const
{
if ( !index.isValid() )
return QVariant();
switch ( role ) {
case Qt::DisplayRole: {
QColor c;
c.setRgb( mImage.pixel( index.column(), index.row() ) );
return c;
}
case Qt::StatusTipRole:
return statusString( index );
case ImageModel::ContiguousBlocksRole:
return contiguousBlocks( index.column(), index.row() );
case ImageModel::IsCurrentDebugRole:
return mDebugPixel.x() == index.column() && mDebugPixel.y() == index.row();
default:
return QVariant();
}
}
QVariant ImageModel::headerData( int section, Qt::Orientation orientation, int role ) const
{
Q_UNUSED( orientation )
switch ( role ) {
case Qt::SizeHintRole:
return QSize( mPixelSize, mPixelSize );
case Qt::DisplayRole:
return QString::number( section );
default:
return QVariant();
}
}
bool ImageModel::setData( const QModelIndex& index, const QVariant& value, int role )
{
if ( role != Qt::DisplayRole )
return false;
if ( !value.canConvert<QColor>() )
return false;
QColor c = value.value<QColor>();
mImage.setPixel( index.column(), index.row(), c.rgb() );
emit dataChanged( index, index );
emit pixelChanged( index.column(), index.row(), c.rgb() );
return true;
}
void ImageModel::slotPixelSizeChange( int size )
{
mPixelSize = size;
}
QString ImageModel::statusString( QModelIndex index ) const
{
QString coords;
coords = QString( "X: %1 Y: %2" ).arg( index.column(), 3 ).arg( index.row(), 3 );
quint64 connected = contiguousBlocks( index.column(), index.row() );
QString character;
if ( connected >= 32 && connected <= 126 )
character = QString( "(char: '%1')" ).arg( ( char ) connected );
return QString( "%1, contiguous: %2 %3" ).arg( coords ).arg( connected ).arg( character );
}
struct Frame {
Frame( int _x, int _y ) : x( _x ), y( _y ) {}
int x, y;
};
// emulate stack based iterative recursion to traverse the image data
// and find the number of contiguous blocks of the same color
// converted from an earlier 'actually' recursive algorithm.
// could probably use some optimization, however it seems to perform fine.
quint64 ImageModel::contiguousBlocks( int x, int y ) const
{
if ( x < 0 || x >= mImage.width() || y < 0 || y >= mImage.height() )
return 0;
// array used to mark pixels as visited.
// the image is mapped in row major fashion
QScopedPointer<QStack<Frame*> > stack( new QStack<Frame*>() );
QScopedPointer<QBitArray> markedArray( new QBitArray( mImage.width() * mImage.height() ) );
quint64 result = 0;
QRgb color = mImage.pixel( x, y );
stack->push( new Frame( x, y ) );
while( !stack->isEmpty() ) {
Frame* frame = stack->pop();
if ( frame->x < 0 || frame->x >= mImage.width() || frame->y < 0 || frame->y >= mImage.height() ) {
delete frame;
continue;
}
if ( ( *markedArray )[frame->x*mImage.height()+frame->y] || mImage.pixel( frame->x, frame->y ) != color ) {
delete frame;
continue;
}
( *markedArray )[frame->x*mImage.height()+frame->y] = 1;
stack->push( new Frame( frame->x + 1, frame->y ) );
stack->push( new Frame( frame->x - 1, frame->y ) );
stack->push( new Frame( frame->x, frame->y + 1 ) );
stack->push( new Frame( frame->x, frame->y - 1 ) );
delete frame;
result += 1;
}
return result;
}
void ImageModel::scaleImage( const QSize& size )
{
QImage newImage(size, QImage::Format_ARGB32_Premultiplied);
newImage.fill( QColor( Qt::white ).rgb() );
QImage oldImage(mImage);
mImage = newImage;
insertImage(oldImage, 0,0);
}
QSize ImageModel::imageSize() const
{
return mImage.size();
}
#include "ImageModel.moc"