Files
hrynco-kswitcher/tests/ocr.cpp
T

161 lines
8.9 KiB
C++

#include "ocr.h"
#include "app.h"
#include <algorithm>
#include <set>
#include <tlhelp32.h>
#include <iostream>
namespace ks {
struct InputRegressionAccess {
static void scheduleOcr(App &app) { app.scheduleHistoryOcr(app.settings); }
};
}
std::set<DWORD> historyImageHelpers()
{
const auto snapshot=CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS,0);
if(snapshot==INVALID_HANDLE_VALUE) throw std::runtime_error("Cannot enumerate OCR fixtures");
struct Cleanup {HANDLE handle;~Cleanup(){CloseHandle(handle);}} cleanup{snapshot};
std::set<DWORD> children;
PROCESSENTRY32W entry{sizeof(entry)};
for(auto ok=Process32FirstW(snapshot,&entry);ok;ok=Process32NextW(snapshot,&entry))
if(entry.th32ParentProcessID==GetCurrentProcessId() && lstrcmpiW(entry.szExeFile,L"KSwitcher.Screenshot.exe")==0)
children.insert(entry.th32ProcessID);
return children;
}
int main(int argc, char **argv)
{
const bool tesseract = argc == 2 && std::string(argv[1]) == "--tesseract";
CoInitializeEx(nullptr, COINIT_MULTITHREADED);
try
{
BITMAPINFO info{};
info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
info.bmiHeader.biWidth = 800;
info.bmiHeader.biHeight = -150;
info.bmiHeader.biPlanes = 1;
info.bmiHeader.biBitCount = 32;
void *data{};
HBITMAP bitmap = CreateDIBSection(nullptr, &info, DIB_RGB_COLORS, &data, nullptr, 0);
if (!bitmap)
throw std::runtime_error("Cannot create OCR fixture");
HDC dc = CreateCompatibleDC(nullptr);
auto old = SelectObject(dc, bitmap);
RECT rect{0, 0, 800, 150};
FillRect(dc, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
HFONT font = CreateFontW(-54, 0, 0, 0, FW_NORMAL, FALSE, FALSE, FALSE, DEFAULT_CHARSET, 0, 0,
ANTIALIASED_QUALITY, 0, L"Arial");
auto oldFont = SelectObject(dc, font);
SetTextColor(dc, RGB(0, 0, 0));
SetBkColor(dc, RGB(255, 255, 255));
std::wstring fixture = tesseract ? L"Привіт Україно HELLO" : L"HELLO KSWITCHER";
TextOutW(dc, 20, 40, fixture.c_str(), (int)fixture.size());
GdiFlush();
ks::Pixels pixels{800, 150, ks::Bytes((BYTE *)data, (BYTE *)data + 800 * 150 * 4)};
SelectObject(dc, oldFont);
SelectObject(dc, old);
DeleteObject(font);
DeleteObject(bitmap);
DeleteDC(dc);
ks::Clip clip{{ks::imageDib(pixels)}};
auto decoded = ks::decodeImage(clip);
if (decoded.width != 800 || decoded.height != 150)
throw std::runtime_error("WIC decode size mismatch");
ks::Settings config;
config.useTesseract = tesseract;
auto text = ks::recognizeText(clip, config);
std::cout << "OCR: " << ks::utf8(text) << '\n';
if (ks::upper(text).find(L"HELLO") == std::wstring::npos)
throw std::runtime_error("OCR fixture was not recognized");
if (tesseract && ks::lower(text).find(L"привіт") == std::wstring::npos)
throw std::runtime_error("Ukrainian OCR fixture was not recognized");
if (!tesseract)
{
constexpr UINT width=20000;
ks::Pixels wideFixture{width,pixels.height,ks::Bytes(size_t(width)*pixels.height*4,255)};
for(UINT y=0;y<pixels.height;++y)
std::copy_n(pixels.bgra.data()+size_t(y)*pixels.width*4,pixels.width*4,
wideFixture.bgra.data()+size_t(y)*width*4);
if(ks::upper(ks::recognizeText(ks::Clip{{ks::imageDib(wideFixture)}},config)).find(L"HELLO")==std::wstring::npos)
throw std::runtime_error("OCR did not recognize the downscaled wide image");
}
auto temp = std::filesystem::temp_directory_path() / (L"KSwitcher-auto-ocr-" + std::to_wstring(GetCurrentProcessId()));
auto sequence = GetClipboardSequenceNumber();
{
ks::App app(GetModuleHandleW(nullptr), true, temp);
app.settings.history = true;
app.history->add(clip, L"OCR fixture", app.settings);
auto id = app.history->entries().front().id;
auto isolated = app.history->recognizeImage(id, config);
if (isolated != text) throw std::runtime_error("Helper and in-process OCR results differ");
const auto recognitionWorker=historyImageHelpers();
if(recognitionWorker.size()!=1 || app.history->recognizeImage(id,config)!=text ||
historyImageHelpers()!=recognitionWorker)
throw std::runtime_error("Successive history requests did not reuse the OCR process");
const auto thumbnail=app.history->thumbnail(id);
const auto previewWorkers=historyImageHelpers();
const auto repeated=app.history->thumbnail(id);
if(previewWorkers.size()!=2 || historyImageHelpers()!=previewWorkers ||
thumbnail.width!=repeated.width || thumbnail.height!=repeated.height || thumbnail.bgra!=repeated.bgra)
throw std::runtime_error("Successive thumbnail requests did not reuse the independent preview worker");
bool cancelled = false;
try { app.history->recognizeImage(id, config, [] { return true; }); }
catch (const std::exception &) { cancelled = true; }
if (!cancelled) throw std::runtime_error("Cancelled OCR request was executed");
auto invalid = config;
invalid.useTesseract = true;
invalid.tesseractPath = (temp / L"missing-tesseract.exe").wstring();
bool failed = false;
try { app.history->recognizeImage(id, invalid); }
catch (const std::exception &) { failed = true; }
if(historyImageHelpers()!=previewWorkers || app.history->recognizeImage(id,config)!=text)
throw std::runtime_error("An OCR job error ended the reusable helper");
const auto child=OpenProcess(PROCESS_TERMINATE|SYNCHRONIZE,FALSE,*recognitionWorker.begin());
if(!child) throw std::runtime_error("Cannot open OCR crash fixture");
const auto terminated=TerminateProcess(child,ERROR_PROCESS_ABORTED) &&
WaitForSingleObject(child,5000)==WAIT_OBJECT_0;
CloseHandle(child);
if(!terminated || app.history->recognizeImage(id,config)!=text || historyImageHelpers()==previewWorkers)
throw std::runtime_error("History OCR did not restart its exited helper");
std::cout << "PASS OCR and thumbnail process reuse and restart after worker exit\n";
if (!failed || app.history->entries().front().ocrState != 1)
throw std::runtime_error("Failed helper modified history metadata");
for (const auto &file : std::filesystem::directory_iterator(temp / L"history"))
if (file.path().filename().wstring().starts_with(L"ocr-"))
throw std::runtime_error("OCR helper left temporary files");
std::cout << "PASS isolated OCR equivalence, cancellation, failure and encrypted IPC cleanup\n";
if (tesseract)
{
app.history->saveOcr(id, L"old Windows result", app.settings);
app.settings.useTesseract = true;
}
ks::InputRegressionAccess::scheduleOcr(app);
auto deadline = GetTickCount64() + 35000;
while ((app.history->entries().front().ocrState == 1 ||
app.history->entries().front().ocrProfile != ks::ocrProfile(app.settings)) &&
GetTickCount64() < deadline) Sleep(20);
if (app.history->search(L"HELLO").size() != 1)
throw std::runtime_error("Background OCR did not make the image searchable");
ks::History restored(temp / L"history");
if (!restored.ocrText(id) || restored.search(L"HELLO").size() != 1)
throw std::runtime_error("Background OCR did not persist its result");
if (tesseract && (restored.search(L"привіт").size() != 1 || !restored.search(L"old Windows result").empty()))
throw std::runtime_error("Engine change did not replace cached Windows OCR with Ukrainian text");
if (GetClipboardSequenceNumber() != sequence)
throw std::runtime_error("Background OCR changed the clipboard");
std::cout << "PASS automatic background OCR, persisted search and unchanged clipboard\n";
}
if(!historyImageHelpers().empty()) throw std::runtime_error("Closing history left OCR workers running");
std::filesystem::remove_all(temp);
std::cout << (tesseract ? "PASS optional Tesseract, mixed Ukrainian/English and cache migration\n" :
"PASS native Windows OCR, DIB decoding and async result retrieval\n");
CoUninitialize();
return 0;
}
catch (const std::exception &e)
{
std::cerr << e.what() << '\n';
CoUninitialize();
return 1;
}
}