| 1e01a22 by Enrico Ros at 2009-02-21 |
1 |
/*************************************************************************** |
| 0e5745d by Enrico Ros at 2009-05-31 |
2 |
* Copyright (c) 2009 Enrico Ros * |
|
3 |
* 2009 Enrico Ros <enrico.ros@email.it> * |
|
4 |
* 2009 Alberto Scarpa <skaal.sl@gmail.com> * |
| 1e01a22 by Enrico Ros at 2009-02-21 |
5 |
* * |
|
6 |
* Permission is hereby granted, free of charge, to any person * |
|
7 |
* obtaining a copy of this software and associated documentation * |
|
8 |
* files (the "Software"), to deal in the Software without * |
|
9 |
* restriction, including without limitation the rights to use, * |
|
10 |
* copy, modify, merge, publish, distribute, sublicense, and/or sell * |
|
11 |
* copies of the Software, and to permit persons to whom the * |
|
12 |
* Software is furnished to do so, subject to the following * |
|
13 |
* conditions: * |
|
14 |
* * |
|
15 |
* The above copyright notice and this permission notice shall be * |
|
16 |
* included in all copies or substantial portions of the Software. * |
|
17 |
* * |
|
18 |
***************************************************************************/ |
|
19 |
|
| e45848b by Enrico Ros at 2009-05-31 |
20 |
#include "WCGame.h" |
| 023fc63 by Alberto Scarpa at 2009-02-21 |
21 |
|
|
22 |
#include "Scrambler.h" |
| e45848b by Enrico Ros at 2009-05-31 |
23 |
#include "ScreenCapture.h" |
| a6ab1de by Enrico Ros at 2009-02-21 |
24 |
#include "InputUtils.h" |
| 9d1b99e by Enrico Ros at 2009-02-25 |
25 |
#include "ocr/Ocr.h" |
| e45848b by Enrico Ros at 2009-05-31 |
26 |
#include "ui_AppWidget.h" |
| 1e01a22 by Enrico Ros at 2009-02-21 |
27 |
#include <QTimer> |
| a6ab1de by Enrico Ros at 2009-02-21 |
28 |
#include <QDebug> |
| e45848b by Enrico Ros at 2009-05-31 |
29 |
#include <QPainter> |
| c03b1c8 by Enrico Ros on Win32 at 2009-06-03 |
30 |
|
|
31 |
//BEGIN portableMSleep |
|
32 |
#ifdef Q_OS_UNIX |
| e45848b by Enrico Ros at 2009-05-31 |
33 |
#include <unistd.h> |
| c03b1c8 by Enrico Ros on Win32 at 2009-06-03 |
34 |
void portableMSleep( int ms ) { |
|
35 |
::usleep( ms * 1000 ); |
|
36 |
} |
|
37 |
#else |
|
38 |
#ifdef Q_OS_WIN32 |
|
39 |
#include <windows.h> |
|
40 |
void portableMSleep( int ms ) { |
|
41 |
Sleep( ms ); |
|
42 |
} |
|
43 |
#endif |
|
44 |
#endif |
|
45 |
//END portableMSleep |
|
46 |
|
| 1e01a22 by Enrico Ros at 2009-02-21 |
47 |
|
| e45848b by Enrico Ros at 2009-05-31 |
48 |
//static const int WC_CAP_L = 114; |
|
49 |
//static const int WC_CAP_T = 245; |
|
50 |
static const int WC_BASELINE_H[] = { 7, 61, 116, 170, 224, 279 }; |
|
51 |
static const int WC_BASELINE_V[] = { 3, 3, 3, 3, 3, 3 }; |
|
52 |
static const int WC_LETTER_W = 35; |
|
53 |
static const int WC_LETTER_H = 37; |
|
54 |
|
|
55 |
WCGame::WCGame( QObject * parent ) |
|
56 |
: AbstractGame( parent ) |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
57 |
, m_wordsDictionary( new Scrambler() ) |
|
58 |
, m_namesDictionary( new Scrambler() ) |
| 1e01a22 by Enrico Ros at 2009-02-21 |
59 |
{ |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
60 |
// load the dictionaries |
|
61 |
m_wordsDictionary->addDictionary( "dic-it.txt" ); |
|
62 |
m_namesDictionary->addDictionary( "dic-enrico.txt" ); |
| 6336a38 by Enrico Ros at 2009-02-25 |
63 |
} |
|
64 |
|
| e45848b by Enrico Ros at 2009-05-31 |
65 |
WCGame::~WCGame() |
| 6336a38 by Enrico Ros at 2009-02-25 |
66 |
{ |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
67 |
delete m_wordsDictionary; |
|
68 |
delete m_namesDictionary; |
| 6336a38 by Enrico Ros at 2009-02-25 |
69 |
} |
| 9d1b99e by Enrico Ros at 2009-02-25 |
70 |
|
| e45848b by Enrico Ros at 2009-05-31 |
71 |
QPixmap WCGame::highlightPixmap( const QPixmap & pixmap ) const |
|
72 |
{ |
|
73 |
QPixmap pix = pixmap; |
|
74 |
QPainter pp( &pix ); |
|
75 |
pp.setPen( Qt::red ); |
|
76 |
for ( int i = 0; i < 6; i++ ) |
|
77 |
pp.drawRect( WC_BASELINE_H[ i ], WC_BASELINE_V[ i ], WC_LETTER_W - 1, WC_LETTER_H - 1 ); |
|
78 |
pp.end(); |
|
79 |
return pix; |
|
80 |
} |
|
81 |
|
|
82 |
void WCGame::train( Ocr * ocr, QString lettersText, const QImage & gamePixmap ) const |
|
83 |
{ |
|
84 |
for ( int i = 0; i < 6; i++ ) { |
|
85 |
QImage letterImage = gamePixmap.copy( WC_BASELINE_H[ i ], WC_BASELINE_V[ i ], WC_LETTER_W, WC_LETTER_H ); |
|
86 |
QChar character = lettersText.at( i ); |
|
87 |
letterImage.save( QString( "wc-glyphs/glyph_%1.png" ).arg( character.toLatin1() ), "PNG" ); |
|
88 |
ocr->trainGlyph( letterImage, character ); |
|
89 |
} |
|
90 |
} |
|
91 |
|
|
92 |
void WCGame::run( Ui::AppWidgetClass * ui, const ScreenCapture * capture, const Ocr * ocr ) |
| 6336a38 by Enrico Ros at 2009-02-25 |
93 |
{ |
| 7ee4d03 by Enrico Ros at 2009-02-25 |
94 |
if ( m_time.isNull() ) |
|
95 |
m_time.start(); |
|
96 |
if ( m_time.elapsed() > (60 * 60 * 1000) ) |
|
97 |
return; |
|
98 |
|
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
99 |
// OCR screen -> 6Chars + mean color identification |
| 9d1b99e by Enrico Ros at 2009-02-25 |
100 |
QString letters; |
| e45848b by Enrico Ros at 2009-05-31 |
101 |
QImage gamePixmap = capture->lastPixmap().toImage(); |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
102 |
int meanR = 0, meanG = 0, meanB = 0; |
| 9d1b99e by Enrico Ros at 2009-02-25 |
103 |
for ( int i = 0; i < 6; i++ ) { |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
104 |
// accumulate intermediate color (yellow for normal, pink for name) |
|
105 |
if ( i > 1 ) { |
|
106 |
int sampleX = (WC_BASELINE_H[ i ] + WC_BASELINE_H[ i - 1 ] + WC_LETTER_W) >> 1; |
|
107 |
int sampleY = (WC_BASELINE_V[ i ] + WC_LETTER_H) >> 1; |
|
108 |
QRgb sampleColor = gamePixmap.pixel( sampleX, sampleY ); |
|
109 |
meanR += qRed( sampleColor ); |
|
110 |
meanG += qGreen( sampleColor ); |
|
111 |
meanB += qBlue( sampleColor ); |
|
112 |
} |
| e45848b by Enrico Ros at 2009-05-31 |
113 |
QImage letterImage = gamePixmap.copy( WC_BASELINE_H[ i ], WC_BASELINE_V[ i ], WC_LETTER_W, WC_LETTER_H ); |
|
114 |
OcrResult res = ocr->recognizeGlyph( letterImage ); |
| 9d1b99e by Enrico Ros at 2009-02-25 |
115 |
if ( res.confidence > 0.1 ) |
|
116 |
letters.append( res.character.toLower() ); |
|
117 |
//qWarning() << res.character << res.confidence; |
|
118 |
} |
| e45848b by Enrico Ros at 2009-05-31 |
119 |
ui->currentLetters->setText( letters ); |
|
120 |
if ( letters.length() != 6 ) |
|
121 |
return; |
| 023fc63 by Alberto Scarpa at 2009-02-21 |
122 |
|
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
123 |
// select the dictionary based on the game type |
| e36409a by Enrico Ros on Win32 at 2009-06-03 |
124 |
qreal meanHue = QColor( meanR / 5, meanG / 5, meanB / 5 ).hueF(); |
|
125 |
Scrambler * scrambler = (meanHue > 0.89 && meanHue < 0.99) ? m_namesDictionary : m_wordsDictionary; |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
126 |
|
| e45848b by Enrico Ros at 2009-05-31 |
127 |
// Do Anagram |
| 7e4e8a8 by Enrico Ros at 2009-06-03 |
128 |
QStringList words = scrambler->words( letters, ui->minLetters->value(), ui->maxLetters->value() ); |
|
129 |
ui->anagramWords->setText(QString::number(words.size())); |
| a6ab1de by Enrico Ros at 2009-02-21 |
130 |
|
| e45848b by Enrico Ros at 2009-05-31 |
131 |
// Write Words |
|
132 |
InputUtils::mouseMove( capture->geometry().center() ); |
| a6ab1de by Enrico Ros at 2009-02-21 |
133 |
InputUtils::mouseLeftClick(); |
| c03b1c8 by Enrico Ros on Win32 at 2009-06-03 |
134 |
portableMSleep( 200 ); |
| a6ab1de by Enrico Ros at 2009-02-21 |
135 |
int count = words.size(); |
|
136 |
for ( int i = 0; i < count; i++ ) { |
|
137 |
InputUtils::keyWrite( words.at( i ) + '\n' ); |
| e36409a by Enrico Ros on Win32 at 2009-06-03 |
138 |
portableMSleep( 10 ); |
| a6ab1de by Enrico Ros at 2009-02-21 |
139 |
} |
|
140 |
InputUtils::keyClickSpecial( Qt::Key_Control ); |
| 1e01a22 by Enrico Ros at 2009-02-21 |
141 |
} |