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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
|
#include <json.hpp>
#include <fstream>
#include <sstream>
#include <list>
#include <iostream>
#include <vector>
#include <tuple>
#include <random>
#include <set>
#include <hkutil/string.h>
#include <tesseract/baseapi.h>
#include <leptonica/allheaders.h>
#include <curl_easy.h>
#include <Magick++.h>
#include "prefix_search.h"
std::string stripSpaces(std::string in)
{
in.erase(
std::remove_if(
std::begin(in),
std::end(in),
::isspace),
std::end(in));
return in;
}
using ps_type = prefix_search<std::tuple<size_t, size_t>>;
enum class card_frame {
m2015,
modern
};
struct card {
size_t id;
std::string name;
std::string imageUri;
card_frame frame;
card(
size_t id,
std::string name,
std::string imageUri,
card_frame frame) :
id(id),
name(std::move(name)),
imageUri(std::move(imageUri)),
frame(frame)
{
}
};
struct usage {
size_t cardId;
size_t strIndex;
size_t strLen;
usage(
size_t ci,
size_t si,
size_t sl) :
cardId(ci),
strIndex(si),
strLen(sl)
{
}
};
struct solution {
const ps_type& prefix;
std::vector<size_t> lengths;
size_t score;
};
class designer {
public:
designer(
std::string text,
const ps_type& titles) :
text_(std::move(text)),
titles_(titles),
solutions_(text_.length() + 1)
{
}
std::list<usage> generate(std::mt19937& rng) const;
private:
const solution& get(size_t i) const;
solution calculate(size_t i) const;
const std::string text_;
const ps_type& titles_;
mutable std::vector<std::unique_ptr<solution>> solutions_;
};
std::list<usage> designer::generate(std::mt19937& rng) const
{
std::list<usage> result;
size_t cur = 0;
while (cur < text_.length())
{
const solution& curSol = get(cur);
const std::vector<size_t>& posLens = curSol.lengths;
std::uniform_int_distribution<size_t> lenDist(0, posLens.size() - 1);
size_t len = posLens.at(lenDist(rng));
const ps_type& prefix = curSol.prefix;
std::uniform_int_distribution<size_t> cardDist(0, prefix.getCount() - 1);
size_t cardIndex = cardDist(rng);
std::tuple<size_t, size_t> pd = prefix.at(cardIndex);
result.emplace_back(std::get<0>(pd), std::get<1>(pd), len);
cur += len;
}
return result;
}
solution designer::calculate(size_t i) const
{
if (i == text_.length())
{
return {
titles_,
{},
0
};
}
const ps_type& prefix = titles_.find(text_, i);
bool foundScore = false;
size_t bestScore;
std::vector<size_t> bestLens;
for (int j = 1;
(j <= prefix.getDepth()) && (i + j <= text_.length());
j++)
{
const solution& subSol = get(i + j);
if (subSol.score > 0 || (i + j == text_.length()))
{
size_t tempScore = subSol.score + 1;
if (!foundScore || tempScore < bestScore)
{
foundScore = true;
bestScore = tempScore;
bestLens.clear();
bestLens.push_back(j);
} else if (tempScore == bestScore)
{
bestLens.push_back(j);
}
}
}
if (!foundScore)
{
return {
titles_,
{},
0
};
} else {
return {
prefix,
std::move(bestLens),
bestScore
};
}
}
const solution& designer::get(size_t i) const
{
if (!solutions_.at(i))
{
solutions_[i] = std::make_unique<solution>(calculate(i));
}
return *solutions_.at(i);
}
Magick::Image downloadImage(const std::string& url)
{
std::ostringstream imgbuf;
curl::curl_ios<std::ostringstream> imgios(imgbuf);
curl::curl_easy imghandle(imgios);
imghandle.add<CURLOPT_URL>(url.c_str());
imghandle.add<CURLOPT_CONNECTTIMEOUT>(30);
imghandle.add<CURLOPT_TIMEOUT>(300);
imghandle.perform();
if (imghandle.get_info<CURLINFO_RESPONSE_CODE>().get() != 200)
{
throw std::runtime_error("Could not download image");
}
std::string content_type = imghandle.get_info<CURLINFO_CONTENT_TYPE>().get();
if (content_type.substr(0, 6) != "image/")
{
throw std::runtime_error("Could not download image");
}
std::string imgstr = imgbuf.str();
Magick::Blob img(imgstr.c_str(), imgstr.length());
Magick::Image pic;
try
{
pic.read(img);
} catch (const Magick::ErrorOption& e)
{
// Occurs when the the data downloaded from the server is malformed
std::cout << "Magick: " << e.what() << std::endl;
throw std::runtime_error("Could not download image");
}
return pic;
}
int main(int argc, char** argv)
{
Magick::InitializeMagick(nullptr);
std::random_device randomDevice;
std::mt19937 rng(randomDevice());
std::cout << "Compiling prefix search..." << std::endl;
std::vector<card> cards;
ps_type titles;
std::set<char> chars;
{
std::ifstream in(
"/Users/hatkirby/Downloads/scryfall-default-cards.json",
std::ios::in | std::ios::binary);
std::ostringstream contents;
contents << in.rdbuf();
nlohmann::json cardsJson = nlohmann::json::parse(contents.str());
for (const auto& cardJson : cardsJson)
{
if (
// The object needs to be a card
cardJson["object"] == "card" &&
// It needs to have a downloadable image
cardJson.count("image_uris") &&
// Make sure we can support the card layout
(
cardJson["layout"] == "normal" ||
cardJson["layout"] == "leveler" ||
cardJson["layout"] == "saga"
) &&
// Digital cards look slightly different so ignore them
!cardJson["digital"] &&
// Only use english printings
cardJson["lang"] == "en" &&
// Currently not supporting silver bordered cards
cardJson["border_color"] != "silver" &&
// It is hard to read the name of a planeswalker
cardJson["type_line"].get<std::string>()
.find("Planeswalker") == std::string::npos &&
// This cuts out checklists and special tokens
cardJson["type_line"] != "Card" &&
// Amonkhet invocations are impossible
cardJson["set"] != "mp2")
{
card_frame frame;
if (cardJson["frame"] == "2015")
{
frame = card_frame::m2015;
} else if (cardJson["frame"] == "2003")
{
frame = card_frame::modern;
} else {
continue;
}
size_t cardId = cards.size();
cards.emplace_back(
cardId,
cardJson["name"],
cardJson["image_uris"]["png"],
frame);
std::string canon = hatkirby::lowercase(cardJson["name"]);
for (int i = 0; i < canon.length(); i++)
{
titles.add(canon, {cardId, i}, i);
chars.insert(canon.at(i));
}
}
}
}
std::cout << "Characters: ";
for (char ch : chars)
{
std::cout << ch;
}
std::cout << std::endl;
std::cout << "Calculating card list..." << std::endl;
std::string text = "is it pretentious that i'm sending someone over 100 common and uncommon magic cards in an iphone six box";
std::string canonText = hatkirby::lowercase(text);
designer des(canonText, titles);
std::list<usage> res = des.generate(rng);
Magick::Image endImage;
bool firstSlice = false;
for (const usage& u : res)
{
const card& theCard = cards.at(u.cardId);
const std::string& cardName = theCard.name;
std::cout << cardName.substr(0, u.strIndex)
<< "[" << cardName.substr(u.strIndex, u.strLen)
<< "]" << cardName.substr(u.strIndex + u.strLen)
<< std::endl;
std::cout << "Downloading image..." << std::endl;
Magick::Image cardImg = downloadImage(theCard.imageUri);
std::cout << "Reading text..." << std::endl;
Magick::Image titleImg = cardImg;
titleImg.magick("TIFF");
//titleImg.threshold(MaxRGB / 2);
titleImg.write("pre.tif");
Magick::Geometry margin;
if (theCard.frame == card_frame::m2015)
{
margin = Magick::Geometry { 595, 46, 57, 54 };
} else if (theCard.frame == card_frame::modern)
{
margin = Magick::Geometry { 581, 50, 63, 57 };
}
titleImg.crop(margin);
titleImg.zoom({ margin.width() * 5, margin.height() * 5 });
//titleImg.quantizeColorSpace(Magick::GRAYColorspace);
//titleImg.quantizeColors(2);
//titleImg.quantize();
titleImg.backgroundColor("white");
titleImg.matte(false);
titleImg.resolutionUnits(Magick::PixelsPerInchResolution);
titleImg.density({ 300, 300 });
titleImg.type(Magick::GrayscaleType);
titleImg.write("title.tif");
Magick::Blob titleBlob;
titleImg.write(&titleBlob);
Pix* titlePix = pixReadMemTiff(
reinterpret_cast<const unsigned char*>(titleBlob.data()),
titleBlob.length(),
0);
tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI();
if (api->Init(nullptr, "eng"))
{
throw std::runtime_error("Could not initialize tesseract");
}
api->SetImage(titlePix);
api->Recognize(nullptr);
tesseract::ResultIterator* ri = api->GetIterator();
tesseract::PageIteratorLevel level = tesseract::RIL_TEXTLINE;
bool foundName = false;
if (ri)
{
do
{
const char* line = ri->GetUTF8Text(level);
if (line)
{
std::string lineStr(line);
//if (stripSpaces(hatkirby::lowercase(lineStr)).find(stripSpaces(hatkirby::lowercase((cardName)))) == 0)
{
foundName = true;
break;
}/* else {
std::cout << "WRONG: " << lineStr << std::endl;
}*/
}
} while (ri->Next(level));
}
if (foundName)
{
level = tesseract::RIL_SYMBOL;
std::vector<std::tuple<unsigned int, unsigned int>> characters;
size_t cur = 0;
do
{
int x1, y1, x2, y2;
ri->BoundingBox(level, &x1, &y1, &x2, &y2);
x1 /= 5;
x2 /= 5;
if (cardName.at(cur) == ' ')
{
if (cur == 0)
{
characters.emplace_back(0, x1);
} else {
const auto& prev = characters.back();
characters.emplace_back(std::get<1>(characters.back()), x1);
}
cur++;
}
characters.emplace_back(x1, x2);
cur++;
} while (ri->Next(level) && (cur < cardName.length()));
if (cur != cardName.length())
{
throw std::runtime_error("Error detecting character bounds");
}
cardImg.crop({
std::get<1>(characters[u.strIndex + u.strLen - 1])
- std::get<0>(characters[u.strIndex]),
cardImg.rows(),
margin.xOff() + std::get<0>(characters[u.strIndex]),
0
});
cardImg.magick("PNG");
cardImg.write("slice.png");
} else {
std::cout << "Didn't find name" << std::endl;
}
api->End();
pixDestroy(&titlePix);
if (!firstSlice)
{
firstSlice = true;
endImage = cardImg;
} else {
int xoff = endImage.columns();
endImage.backgroundColor("black");
endImage.extent(
{endImage.columns() + cardImg.columns(), cardImg.rows()},
Magick::WestGravity);
endImage.composite(
cardImg,
xoff,
(theCard.frame == card_frame::m2015) ? 6 : 0);
}
//break;
}
endImage.magick("PNG");
endImage.write("output.png");
}
|