Forráskód Böngészése

Fix alignment HTTP blocking and publish complete ROI JPEG snapshots

SybexXx 1 napja
szülő
commit
24e7459794
1 módosított fájl, 36 hozzáadás és 36 törlés
  1. 36 36
      code/components/jomjol_image_proc/CAlignAndCutImage.cpp

+ 36 - 36
code/components/jomjol_image_proc/CAlignAndCutImage.cpp

@@ -16,22 +16,6 @@
 
 static const char* TAG = "c_align_and_cut_image";
 
-class ScopedImageAccess
-{
-    CImageBasis* image;
-    bool locked;
-public:
-    explicit ScopedImageAccess(CImageBasis* value) : image(value), locked(false)
-    {
-        if (image != NULL) locked = image->RGBImageLock() != NULL;
-    }
-    ~ScopedImageAccess()
-    {
-        if (locked) image->RGBImageRelease();
-    }
-    bool isLocked() const { return locked; }
-};
-
 static bool getCutRectangle(int imageWidth, int imageHeight,
                              int x, int y, int requestedWidth, int requestedHeight,
                              int* right, int* bottom)
@@ -82,16 +66,41 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
     int r0_x, r0_y, r1_x, r1_y;
     bool isSimilar1, isSimilar2;
 
-    if (_temp1 == NULL || _temp2 == NULL || rgb_image == NULL) {
-        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: source image or reference is unavailable");
+    if (_temp1 == NULL || _temp2 == NULL || rgb_image == NULL ||
+        ImageTMP == NULL || !ImageTMP->ImageOkay() ||
+        ImageTMP->width != width || ImageTMP->height != height ||
+        ImageTMP->channels != channels) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: source image or snapshot workspace is unavailable");
+        return false;
+    }
+
+    // Copy the source while briefly holding the image lock. The template search
+    // then reads only this immutable snapshot and does not block HTTP image access.
+    size_t snapshotSize = 0;
+    if (!getCutBufferSize(width, height, channels, &snapshotSize)) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: invalid source image dimensions");
+        return false;
+    }
+
+    uint8_t* source = RGBImageLock();
+    if (source == NULL) {
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock source image for snapshot");
         return false;
     }
-    ScopedImageAccess sourceAccess(this);
-    if (!sourceAccess.isLocked()) {
-        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock source image");
+
+    uint8_t* snapshot = ImageTMP->RGBImageLock();
+    if (snapshot == NULL) {
+        RGBImageRelease();
+        LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to lock snapshot workspace");
         return false;
     }
-    CFindTemplate* ft = new (std::nothrow) CFindTemplate("align", rgb_image, channels, width, height, bpp);
+
+    std::memcpy(snapshot, source, snapshotSize);
+    ImageTMP->RGBImageRelease();
+    RGBImageRelease();
+
+    CFindTemplate* ft = new (std::nothrow) CFindTemplate(
+        "align", snapshot, channels, width, height, bpp);
     if (ft == NULL) {
         LogFile.WriteToFile(ESP_LOG_ERROR, TAG, "Align: unable to allocate template matcher");
         return false;
@@ -100,16 +109,16 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
     r0_x = _temp1->target_x;
     r0_y = _temp1->target_y;
     ESP_LOGD(TAG, "Before ft->FindTemplate(_temp1); %s", _temp1->image_file.c_str());
-    isSimilar1 = ft->FindTemplate(_temp1);
+    isSimilar1 = ft->FindTemplate(_temp1, true);
     _temp1->width = ft->tpl_width;
-    _temp1->height = ft->tpl_height; 
+    _temp1->height = ft->tpl_height;
 
     r1_x = _temp2->target_x;
     r1_y = _temp2->target_y;
     ESP_LOGD(TAG, "Before ft->FindTemplate(_temp2); %s", _temp2->image_file.c_str());
-    isSimilar2 = ft->FindTemplate(_temp2);
+    isSimilar2 = ft->FindTemplate(_temp2, true);
     _temp2->width = ft->tpl_width;
-    _temp2->height = ft->tpl_height; 
+    _temp2->height = ft->tpl_height;
 
     delete ft;
 
@@ -124,20 +133,11 @@ bool CAlignAndCutImage::Align(RefInfo *_temp1, RefInfo *_temp2)
 
     float w_org, w_ist, d_winkel;
 
-	w_org = atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
+    w_org = atan2((float)(_temp2->found_y - _temp1->found_y), (float)(_temp2->found_x - _temp1->found_x));
     w_ist = atan2((float)(r1_y - r0_y), (float)(r1_x - r0_x));
 
     d_winkel = (w_ist - w_org) * 180 / M_PI;
 
-/*#ifdef DEBUG_DETAIL_ON
-    std::string zw = "\tdx:\t" + std::to_string(dx) + "\tdy:\t" + std::to_string(dy) + "\td_winkel:\t" + std::to_string(d_winkel);
-    zw = zw + "\tt1_x_y:\t" + std::to_string(_temp1->found_x) + "\t" + std::to_string(_temp1->found_y);
-    zw = zw + "\tpara1_found_min_avg_max_SAD:\t" + std::to_string(_temp1->fastalg_min) + "\t" + std::to_string(_temp1->fastalg_avg) + "\t" + std::to_string(_temp1->fastalg_max) + "\t"+ std::to_string(_temp1->fastalg_SAD);
-    zw = zw + "\tt2_x_y:\t" + std::to_string(_temp2->found_x) + "\t" + std::to_string(_temp2->found_y);
-    zw = zw + "\tpara2_found_min_avg_max:\t" + std::to_string(_temp2->fastalg_min) + "\t" + std::to_string(_temp2->fastalg_avg) + "\t" + std::to_string(_temp2->fastalg_max) + "\t"+ std::to_string(_temp2->fastalg_SAD);
-    LogFile.WriteToDedicatedFile("/sdcard/alignment.txt", zw);
-#endif*/
-
     CRotateImage rt("Align", this, ImageTMP);
     rt.Translate(dx, dy);
     rt.Rotate(d_winkel, _temp1->target_x, _temp1->target_y);