From 5a65625cb589b2cb5b336e1fa5748df8dcdb8f6a Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 7 Oct 2018 15:28:07 -0400 Subject: Smart pointers --- wizard.cpp | 38 ++++++++++++++++++++++++++++++++------ 1 file changed, 32 insertions(+), 6 deletions(-) (limited to 'wizard.cpp') diff --git a/wizard.cpp b/wizard.cpp index ef4941f..e4b79e6 100644 --- a/wizard.cpp +++ b/wizard.cpp @@ -9,6 +9,7 @@ #include #include #include +#include #include #include #include @@ -265,6 +266,32 @@ Magick::Image downloadImage(const std::string& url) +class tesseract_deleter { +public: + + void operator()(tesseract::TessBaseAPI* ptr) const + { + ptr->End(); + } +}; + +using tesseract_ptr = + std::unique_ptr; + +class pix_deleter { +public: + + void operator()(Pix* ptr) const + { + pixDestroy(&ptr); + } +}; + +using pix_ptr = std::unique_ptr; + + + + int main(int argc, char** argv) @@ -418,19 +445,19 @@ int main(int argc, char** argv) Magick::Blob titleBlob; titleImg.write(&titleBlob); - Pix* titlePix = pixReadMemTiff( + pix_ptr titlePix { pixReadMemTiff( reinterpret_cast(titleBlob.data()), titleBlob.length(), - 0); + 0) }; - tesseract::TessBaseAPI* api = new tesseract::TessBaseAPI(); + tesseract_ptr api { new tesseract::TessBaseAPI() }; if (api->Init(nullptr, "eng")) { throw std::runtime_error("Could not initialize tesseract"); } - api->SetImage(titlePix); + api->SetImage(titlePix.get()); api->Recognize(nullptr); tesseract::ResultIterator* ri = api->GetIterator(); @@ -520,8 +547,7 @@ int main(int argc, char** argv) - api->End(); - pixDestroy(&titlePix); + if (!firstSlice) { -- cgit 1.4.1