|
@@ -29,7 +29,7 @@ static bool getTemplateBufferSize(int width, int height, int channels, size_t* s
|
|
|
// #define DEBUG_DETAIL_ON
|
|
// #define DEBUG_DETAIL_ON
|
|
|
|
|
|
|
|
|
|
|
|
|
-bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
|
|
|
|
+bool CFindTemplate::FindTemplate(RefInfo *_ref, bool sourceIsStableSnapshot)
|
|
|
{
|
|
{
|
|
|
if (_ref == NULL || rgb_image == NULL || width <= 0 || height <= 0 || channels <= 0) {
|
|
if (_ref == NULL || rgb_image == NULL || width <= 0 || height <= 0 || channels <= 0) {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: source image or reference is unavailable");
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: source image or reference is unavailable");
|
|
@@ -54,11 +54,17 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
- uint8_t* source = RGBImageLock();
|
|
|
|
|
- if (source == NULL) {
|
|
|
|
|
- LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: source image is locked");
|
|
|
|
|
- stbi_image_free(rgb_template);
|
|
|
|
|
- return false;
|
|
|
|
|
|
|
+ // Alignment passes an immutable copy in the shared tmpImage workspace.
|
|
|
|
|
+ // Searching that snapshot must not hold the global image mutex for seconds.
|
|
|
|
|
+ uint8_t* source = rgb_image;
|
|
|
|
|
+ const bool sourceLocked = !sourceIsStableSnapshot;
|
|
|
|
|
+ if (sourceLocked) {
|
|
|
|
|
+ source = RGBImageLock();
|
|
|
|
|
+ if (source == NULL) {
|
|
|
|
|
+ LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: source image is locked");
|
|
|
|
|
+ stbi_image_free(rgb_template);
|
|
|
|
|
+ return false;
|
|
|
|
|
+ }
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
if (_ref->search_x == 0) {
|
|
if (_ref->search_x == 0) {
|
|
@@ -81,7 +87,7 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
|
|
|
|
|
if (ow_stop < ow_start || oh_stop < oh_start) {
|
|
if (ow_stop < ow_start || oh_stop < oh_start) {
|
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: search window does not intersect source image");
|
|
LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "FindTemplate: search window does not intersect source image");
|
|
|
- RGBImageRelease();
|
|
|
|
|
|
|
+ if (sourceLocked) RGBImageRelease();
|
|
|
stbi_image_free(rgb_template);
|
|
stbi_image_free(rgb_template);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|
|
@@ -100,7 +106,7 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
#endif
|
|
#endif
|
|
|
_ref->found_x = _ref->fastalg_x;
|
|
_ref->found_x = _ref->fastalg_x;
|
|
|
_ref->found_y = _ref->fastalg_y;
|
|
_ref->found_y = _ref->fastalg_y;
|
|
|
- RGBImageRelease();
|
|
|
|
|
|
|
+ if (sourceLocked) RGBImageRelease();
|
|
|
stbi_image_free(rgb_template);
|
|
stbi_image_free(rgb_template);
|
|
|
return true;
|
|
return true;
|
|
|
}
|
|
}
|
|
@@ -154,7 +160,7 @@ bool CFindTemplate::FindTemplate(RefInfo *_ref)
|
|
|
_ref->fastalg_max = max;
|
|
_ref->fastalg_max = max;
|
|
|
_ref->fastalg_SAD = SAD;
|
|
_ref->fastalg_SAD = SAD;
|
|
|
|
|
|
|
|
- RGBImageRelease();
|
|
|
|
|
|
|
+ if (sourceLocked) RGBImageRelease();
|
|
|
stbi_image_free(rgb_template);
|
|
stbi_image_free(rgb_template);
|
|
|
return false;
|
|
return false;
|
|
|
}
|
|
}
|