|
| 1 | +/* |
| 2 | + * dump_patt.c |
| 3 | + * WebARKitLib-rs |
| 4 | + * |
| 5 | + * This file is part of WebARKitLib-rs - WebARKit. |
| 6 | + * |
| 7 | + * WebARKitLib-rs is free software: you can redistribute it and/or modify |
| 8 | + * it under the terms of the GNU Lesser General Public License as published by |
| 9 | + * the Free Software Foundation, either version 3 of the License, or |
| 10 | + * (at your option) any later version. |
| 11 | + * |
| 12 | + * WebARKitLib-rs is distributed in the hope that it will be useful, |
| 13 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | + * GNU Lesser General Public License for more details. |
| 16 | + * |
| 17 | + * You should have received a copy of the GNU Lesser General Public License |
| 18 | + * along with WebARKitLib-rs. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + * |
| 20 | + * As a special exception, the copyright holders of this library give you |
| 21 | + * permission to link this library with independent modules to produce an |
| 22 | + * executable, regardless of the license terms of these independent modules, |
| 23 | + * and to copy and distribute the resulting executable under terms of your |
| 24 | + * choice, provided that you also meet, for each linked independent module, |
| 25 | + * the terms and conditions of the license of that module. An independent |
| 26 | + * module is a module which is neither derived from nor based on this |
| 27 | + * library. If you modify this library, you may extend this exception to |
| 28 | + * your version of the library, but you are not obligated to do so. If you |
| 29 | + * do not wish to do so, delete this exception statement from your version. |
| 30 | + * |
| 31 | + * Copyright 2026 WebARKit. |
| 32 | + * |
| 33 | + * Author(s): Walter Perdan @kalwalt https://github.com/kalwalt |
| 34 | + * |
| 35 | + */ |
| 36 | + |
| 37 | +/* |
| 38 | + * Diagnostic harness for issue #103 (low CF from template matching). |
| 39 | + * |
| 40 | + * Runs `arDetectMarker` once on a luma raw image, then re-extracts the same |
| 41 | + * pattern patch via `arPattGetImage` using the contour data preserved in |
| 42 | + * `arHandle->markerInfo2[0]`. The extracted patch is dumped to disk as raw |
| 43 | + * bytes for byte-level comparison with the Rust port. |
| 44 | + * |
| 45 | + * NOTE: this is a sibling of `main.c`; it does NOT modify the timing |
| 46 | + * benchmark. Outputs: |
| 47 | + * - <out_dir>/c_ext_patt.bin raw ext_patt bytes (patt_size^2 * channels) |
| 48 | + * - <out_dir>/c_ext_patt_meta.txt patt_size, channels, mode, vertices, cf, id, dir |
| 49 | + */ |
| 50 | + |
| 51 | +#include <AR/ar.h> |
| 52 | +#include <stdio.h> |
| 53 | +#include <stdlib.h> |
| 54 | +#include <string.h> |
| 55 | + |
| 56 | +/* Compatibility helpers required by some WebARKitLib builds. */ |
| 57 | +char *cat(const char *file, size_t *bufSize_p) { |
| 58 | + FILE *fp; |
| 59 | + size_t len; |
| 60 | + char *s; |
| 61 | + if (!file) |
| 62 | + return NULL; |
| 63 | + fp = fopen(file, "rb"); |
| 64 | + if (!fp) |
| 65 | + return NULL; |
| 66 | + fseek(fp, 0, SEEK_END); |
| 67 | + len = ftell(fp); |
| 68 | + fseek(fp, 0, SEEK_SET); |
| 69 | + if (!(s = (char *)malloc(len + 1))) { |
| 70 | + fclose(fp); |
| 71 | + return NULL; |
| 72 | + } |
| 73 | + if (fread(s, len, 1, fp) < 1 && len > 0) { |
| 74 | + free(s); |
| 75 | + fclose(fp); |
| 76 | + return NULL; |
| 77 | + } |
| 78 | + s[len] = '\0'; |
| 79 | + fclose(fp); |
| 80 | + if (bufSize_p) |
| 81 | + *bufSize_p = len + 1; |
| 82 | + return s; |
| 83 | +} |
| 84 | +int test_d(const char *dir) { (void)dir; return 0; } |
| 85 | +int mkdir_p(const char *path) { (void)path; return 0; } |
| 86 | + |
| 87 | +static const char *pixfmt_name(AR_PIXEL_FORMAT f) { |
| 88 | + switch (f) { |
| 89 | + case AR_PIXEL_FORMAT_RGB: return "AR_PIXEL_FORMAT_RGB"; |
| 90 | + case AR_PIXEL_FORMAT_BGR: return "AR_PIXEL_FORMAT_BGR"; |
| 91 | + case AR_PIXEL_FORMAT_RGBA: return "AR_PIXEL_FORMAT_RGBA"; |
| 92 | + case AR_PIXEL_FORMAT_BGRA: return "AR_PIXEL_FORMAT_BGRA"; |
| 93 | + case AR_PIXEL_FORMAT_ABGR: return "AR_PIXEL_FORMAT_ABGR"; |
| 94 | + case AR_PIXEL_FORMAT_ARGB: return "AR_PIXEL_FORMAT_ARGB"; |
| 95 | + case AR_PIXEL_FORMAT_MONO: return "AR_PIXEL_FORMAT_MONO"; |
| 96 | + default: return "<unknown>"; |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +static const char *mode_name(int m) { |
| 101 | + switch (m) { |
| 102 | + case AR_TEMPLATE_MATCHING_COLOR: return "AR_TEMPLATE_MATCHING_COLOR"; |
| 103 | + case AR_TEMPLATE_MATCHING_MONO: return "AR_TEMPLATE_MATCHING_MONO"; |
| 104 | + case AR_MATRIX_CODE_DETECTION: return "AR_MATRIX_CODE_DETECTION"; |
| 105 | + case AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX: return "AR_TEMPLATE_MATCHING_COLOR_AND_MATRIX"; |
| 106 | + case AR_TEMPLATE_MATCHING_MONO_AND_MATRIX: return "AR_TEMPLATE_MATCHING_MONO_AND_MATRIX"; |
| 107 | + default: return "<unknown>"; |
| 108 | + } |
| 109 | +} |
| 110 | + |
| 111 | +int main(int argc, char *argv[]) { |
| 112 | + if (argc < 5) { |
| 113 | + printf("Usage: %s <camera_para.dat> <patt.hiro> <luma.raw> <out_dir> [width] [height]\n", |
| 114 | + argv[0]); |
| 115 | + printf(" out_dir defaults expected: ../data\n"); |
| 116 | + return 1; |
| 117 | + } |
| 118 | + |
| 119 | + const char *cparam_name = argv[1]; |
| 120 | + const char *patt_name = argv[2]; |
| 121 | + const char *luma_name = argv[3]; |
| 122 | + const char *out_dir = argv[4]; |
| 123 | + int width = (argc > 5) ? atoi(argv[5]) : 429; |
| 124 | + int height = (argc > 6) ? atoi(argv[6]) : 317; |
| 125 | + |
| 126 | + /* ---- ARParam + ARHandle setup, mirrors main.c ---- */ |
| 127 | + ARParam cparam; |
| 128 | + if (arParamLoad(cparam_name, 1, &cparam) < 0) { |
| 129 | + fprintf(stderr, "ERROR: arParamLoad failed for %s\n", cparam_name); |
| 130 | + return 1; |
| 131 | + } |
| 132 | + arParamChangeSize(&cparam, width, height, &cparam); |
| 133 | + ARParamLT *cparamLT = arParamLTCreate(&cparam, AR_PARAM_LT_DEFAULT_OFFSET); |
| 134 | + |
| 135 | + ARHandle *arHandle = arCreateHandle(cparamLT); |
| 136 | + if (!arHandle) { |
| 137 | + fprintf(stderr, "ERROR: arCreateHandle failed\n"); |
| 138 | + return 1; |
| 139 | + } |
| 140 | + arHandle->arPixelFormat = AR_PIXEL_FORMAT_MONO; |
| 141 | + /* arPatternDetectionMode is left at the default (AR_TEMPLATE_MATCHING_COLOR), which |
| 142 | + matches the simple Rust example; do not override. */ |
| 143 | + |
| 144 | + ARPattHandle *pattHandle = arPattCreateHandle(); |
| 145 | + if (!pattHandle || arPattLoad(pattHandle, patt_name) < 0) { |
| 146 | + fprintf(stderr, "ERROR: arPattLoad failed for %s\n", patt_name); |
| 147 | + return 1; |
| 148 | + } |
| 149 | + arPattAttach(arHandle, pattHandle); |
| 150 | + |
| 151 | + /* ---- Load raw luma image ---- */ |
| 152 | + size_t data_size = (size_t)width * (size_t)height; |
| 153 | + unsigned char *luma_buffer = (unsigned char *)malloc(data_size); |
| 154 | + FILE *f = fopen(luma_name, "rb"); |
| 155 | + if (!f || fread(luma_buffer, 1, data_size, f) != data_size) { |
| 156 | + fprintf(stderr, "ERROR: could not read %zu bytes from %s\n", data_size, luma_name); |
| 157 | + if (f) fclose(f); |
| 158 | + free(luma_buffer); |
| 159 | + return 1; |
| 160 | + } |
| 161 | + fclose(f); |
| 162 | + |
| 163 | + AR2VideoBufferT vbuf; |
| 164 | + memset(&vbuf, 0, sizeof(vbuf)); |
| 165 | + vbuf.buff = luma_buffer; |
| 166 | + vbuf.buffLuma = luma_buffer; |
| 167 | + vbuf.fillFlag = 1; |
| 168 | + |
| 169 | + /* ---- Run detection once ---- */ |
| 170 | + if (arDetectMarker(arHandle, &vbuf) < 0) { |
| 171 | + fprintf(stderr, "ERROR: arDetectMarker failed\n"); |
| 172 | + return 1; |
| 173 | + } |
| 174 | + |
| 175 | + int marker_num = arGetMarkerNum(arHandle); |
| 176 | + if (marker_num <= 0) { |
| 177 | + fprintf(stderr, "ERROR: no markers detected (marker_num=%d)\n", marker_num); |
| 178 | + return 1; |
| 179 | + } |
| 180 | + ARMarkerInfo *minfo = arGetMarker(arHandle); |
| 181 | + /* Pick the first marker with id >= 0; otherwise fall back to index 0. */ |
| 182 | + int sel = 0; |
| 183 | + for (int i = 0; i < marker_num; i++) { |
| 184 | + if (minfo[i].id >= 0) { sel = i; break; } |
| 185 | + } |
| 186 | + |
| 187 | + /* The candidate's contour data lives in markerInfo2[sel] (assumes marker_info[i] |
| 188 | + corresponds to markerInfo2[i] for our single-detection case — true when no |
| 189 | + candidates were dropped between detection stages). */ |
| 190 | + ARMarkerInfo2 *m2 = &arHandle->markerInfo2[sel]; |
| 191 | + |
| 192 | + int patt_size = pattHandle->pattSize; |
| 193 | + int patt_max_size = patt_size * 2; |
| 194 | + int channels = (arHandle->arPatternDetectionMode == AR_TEMPLATE_MATCHING_COLOR) ? 3 : 1; |
| 195 | + size_t ext_len = (size_t)patt_size * (size_t)patt_size * (size_t)channels; |
| 196 | + unsigned char *ext_patt = (unsigned char *)malloc(ext_len); |
| 197 | + memset(ext_patt, 0, ext_len); |
| 198 | + |
| 199 | + /* Re-extract using the same contour data the matcher used internally. |
| 200 | + If the prototype differs in your AR/ar.h variant, adjust here. */ |
| 201 | + int rv = arPattGetImage(arHandle->arImageProcMode, arHandle->arPatternDetectionMode, |
| 202 | + patt_size, patt_max_size, |
| 203 | + luma_buffer, width, height, arHandle->arPixelFormat, |
| 204 | + m2->x_coord, m2->y_coord, m2->vertex, |
| 205 | + arHandle->pattRatio, ext_patt); |
| 206 | + if (rv < 0) { |
| 207 | + fprintf(stderr, "ERROR: arPattGetImage returned %d\n", rv); |
| 208 | + free(ext_patt); |
| 209 | + return 1; |
| 210 | + } |
| 211 | + |
| 212 | + /* ---- Write outputs ---- */ |
| 213 | + char path_bin[1024], path_meta[1024]; |
| 214 | + snprintf(path_bin, sizeof(path_bin), "%s/c_ext_patt.bin", out_dir); |
| 215 | + snprintf(path_meta, sizeof(path_meta), "%s/c_ext_patt_meta.txt", out_dir); |
| 216 | + |
| 217 | + FILE *fbin = fopen(path_bin, "wb"); |
| 218 | + if (!fbin) { |
| 219 | + fprintf(stderr, "ERROR: could not open %s for writing\n", path_bin); |
| 220 | + return 1; |
| 221 | + } |
| 222 | + fwrite(ext_patt, 1, ext_len, fbin); |
| 223 | + fclose(fbin); |
| 224 | + |
| 225 | + FILE *fmeta = fopen(path_meta, "w"); |
| 226 | + if (!fmeta) { |
| 227 | + fprintf(stderr, "ERROR: could not open %s for writing\n", path_meta); |
| 228 | + return 1; |
| 229 | + } |
| 230 | + fprintf(fmeta, "patt_size = %d\n", patt_size); |
| 231 | + fprintf(fmeta, "channels = %d\n", channels); |
| 232 | + fprintf(fmeta, "mode = %s\n", mode_name(arHandle->arPatternDetectionMode)); |
| 233 | + fprintf(fmeta, "pixfmt = %s\n", pixfmt_name(arHandle->arPixelFormat)); |
| 234 | + fprintf(fmeta, "xsize = %d\n", width); |
| 235 | + fprintf(fmeta, "ysize = %d\n", height); |
| 236 | + fprintf(fmeta, "patt_ratio = %.6f\n", arHandle->pattRatio); |
| 237 | + fprintf(fmeta, "selected = %d (of %d markers)\n", sel, marker_num); |
| 238 | + /* The 4 contour vertices used by arPattGetImage. */ |
| 239 | + for (int i = 0; i < 4; i++) { |
| 240 | + int idx = m2->vertex[i]; |
| 241 | + fprintf(fmeta, "vertex[%d] = (%d, %d)\n", i, m2->x_coord[idx], m2->y_coord[idx]); |
| 242 | + } |
| 243 | + /* Ideal-space marker corners (post-distortion-correction). */ |
| 244 | + for (int i = 0; i < 4; i++) { |
| 245 | + fprintf(fmeta, "marker_vertex[%d] = (%.4f, %.4f)\n", i, minfo[sel].vertex[i][0], minfo[sel].vertex[i][1]); |
| 246 | + } |
| 247 | + fprintf(fmeta, "id = %d\n", minfo[sel].id); |
| 248 | + fprintf(fmeta, "id_patt = %d\n", minfo[sel].idPatt); |
| 249 | + fprintf(fmeta, "cf = %.6f\n", minfo[sel].cf); |
| 250 | + fprintf(fmeta, "cf_patt = %.6f\n", minfo[sel].cfPatt); |
| 251 | + fprintf(fmeta, "dir = %d\n", minfo[sel].dir); |
| 252 | + fclose(fmeta); |
| 253 | + |
| 254 | + printf("Wrote %s (%zu bytes)\n", path_bin, ext_len); |
| 255 | + printf("Wrote %s\n", path_meta); |
| 256 | + printf("cf=%.4f id=%d dir=%d\n", minfo[sel].cf, minfo[sel].id, minfo[sel].dir); |
| 257 | + |
| 258 | + /* Cleanup */ |
| 259 | + free(ext_patt); |
| 260 | + free(luma_buffer); |
| 261 | + arPattDetach(arHandle); |
| 262 | + arPattDeleteHandle(pattHandle); |
| 263 | + arDeleteHandle(arHandle); |
| 264 | + arParamLTFree(&cparamLT); |
| 265 | + |
| 266 | + return 0; |
| 267 | +} |
0 commit comments