diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-10-07 15:28:07 -0400 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2018-10-07 15:28:07 -0400 |
commit | 5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a (patch) | |
tree | e70436d4620737dd8707e1db8bd8fb1f3d5f7bb1 /wizard.cpp | |
parent | a3056929ea8381884655a46f80b4c0e1fdb66341 (diff) | |
download | wizard-5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a.tar.gz wizard-5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a.tar.bz2 wizard-5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a.zip |
Smart pointers
Diffstat (limited to 'wizard.cpp')
-rw-r--r-- | wizard.cpp | 38 |
1 files changed, 32 insertions, 6 deletions
diff --git a/wizard.cpp b/wizard.cpp index ef4941f..e4b79e6 100644 --- a/wizard.cpp +++ b/wizard.cpp | |||
@@ -9,6 +9,7 @@ | |||
9 | #include <tuple> | 9 | #include <tuple> |
10 | #include <random> | 10 | #include <random> |
11 | #include <set> | 11 | #include <set> |
12 | #include <memory> | ||
12 | #include <hkutil/string.h> | 13 | #include <hkutil/string.h> |
13 | #include <tesseract/baseapi.h> | 14 | #include <tesseract/baseapi.h> |
14 | #include <leptonica/allheaders.h> | 15 | #include <leptonica/allheaders.h> |
@@ -265,6 +266,32 @@ Magick::Image downloadImage(const std::string& url) | |||
265 | 266 | ||
266 | 267 | ||
267 | 268 | ||
269 | class tesseract_deleter { | ||
270 | public: | ||
271 | |||
272 | void operator()(tesseract::TessBaseAPI* ptr) const | ||
273 | { | ||
274 | ptr->End(); | ||
275 | } | ||
276 | }; | ||
277 | |||
278 | using tesseract_ptr = | ||
279 | std::unique_ptr<tesseract::TessBaseAPI, tesseract_deleter>; | ||
280 | |||
281 | class pix_deleter { | ||
282 | public: | ||
283 | |||
284 | void operator()(Pix* ptr) const | ||
285 | { | ||
286 | pixDestroy(&ptr); | ||
287 | } | ||
288 | }; | ||
289 | |||
290 | using pix_ptr = std::unique_ptr<Pix, pix_deleter>; | ||
291 | |||
292 | |||
293 | |||
294 | |||
268 | 295 | ||
269 | 296 | ||
270 | int main(int argc, char** argv) | 297 | int main(int argc, char** argv) |
@@ -418,19 +445,19 @@ int main(int argc, char** argv) | |||
418 | Magick::Blob titleBlob; | 445 | Magick::Blob titleBlob; |
419 | titleImg.write(&titleBlob); | 446 | titleImg.write(&titleBlob); |
420 | 447 | ||
421 | Pix* titlePix = pixReadMemTiff( | 448 | pix_ptr titlePix { pixReadMemTiff( |
422 | reinterpret_cast<const unsigned char*>(titleBlob.data()), | 449 | reinterpret_cast<const unsigned char*>(titleBlob.data()), |
423 | titleBlob.length(), | 450 | titleBlob.length(), |
424 | 0); | 451 | 0) }; |
425 | 452 | ||
426 | tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI(); | 453 | tesseract_ptr api { new tesseract::TessBaseAPI() }; |
427 | 454 | ||
428 | if (api->Init(nullptr, "eng")) | 455 | if (api->Init(nullptr, "eng")) |
429 | { | 456 | { |
430 | throw std::runtime_error("Could not initialize tesseract"); | 457 | throw std::runtime_error("Could not initialize tesseract"); |
431 | } | 458 | } |
432 | 459 | ||
433 | api->SetImage(titlePix); | 460 | api->SetImage(titlePix.get()); |
434 | api->Recognize(nullptr); | 461 | api->Recognize(nullptr); |
435 | 462 | ||
436 | tesseract::ResultIterator* ri = api->GetIterator(); | 463 | tesseract::ResultIterator* ri = api->GetIterator(); |
@@ -520,8 +547,7 @@ int main(int argc, char** argv) | |||
520 | 547 | ||
521 | 548 | ||
522 | 549 | ||
523 | api->End(); | 550 | |
524 | pixDestroy(&titlePix); | ||
525 | 551 | ||
526 | if (!firstSlice) | 552 | if (!firstSlice) |
527 | { | 553 | { |