Score how strongly an image looks like a black+white nested frame/border: a border ring carrying BOTH near-black and near-white pixels (the photo-mat look), with the center noticeably less black-and-white than the ring. Returns {score, ringBlackFrac, ringWhiteFrac}.
| 379 | // carrying BOTH near-black and near-white pixels (the photo-mat look), with the center |
| 380 | // noticeably less black-and-white than the ring. Returns {score, ringBlackFrac, ringWhiteFrac}. |
| 381 | static std::tuple<double, double, double> FrameBorderScore(const Poseidon::DecodedImage& img) |
| 382 | { |
| 383 | const int w = img.width, h = img.height; |
| 384 | if (w < 8 || h < 8) |
| 385 | return {0.0, 0.0, 0.0}; |
| 386 | const uint8_t* p = img.rgba.data(); |
| 387 | auto luma = [&](int x, int y) |
| 388 | { |
| 389 | const uint8_t* q = p + (static_cast<size_t>(y) * w + x) * 4; |
| 390 | return (q[0] * 299 + q[1] * 587 + q[2] * 114) / 1000; |
| 391 | }; |
| 392 | const int bw = std::max(1, w / 8), bh = std::max(1, h / 8); // ~12.5% border ring |
| 393 | long ringN = 0, ringBlack = 0, ringWhite = 0, ctrN = 0, ctrBW = 0; |
| 394 | for (int y = 0; y < h; y++) |
| 395 | for (int x = 0; x < w; x++) |
| 396 | { |
| 397 | const int L = luma(x, y); |
| 398 | const bool border = (x < bw || x >= w - bw || y < bh || y >= h - bh); |
| 399 | if (border) |
| 400 | { |
| 401 | ringN++; |
| 402 | if (L < 50) |
| 403 | ringBlack++; |
| 404 | else if (L > 205) |
| 405 | ringWhite++; |
| 406 | } |
| 407 | else |
| 408 | { |
| 409 | ctrN++; |
| 410 | if (L < 50 || L > 205) |
| 411 | ctrBW++; |
| 412 | } |
| 413 | } |
| 414 | const double rb = ringN ? double(ringBlack) / ringN : 0.0; |
| 415 | const double rw = ringN ? double(ringWhite) / ringN : 0.0; |
| 416 | const double cbw = ctrN ? double(ctrBW) / ctrN : 0.0; |
| 417 | // Both black and white must populate the ring; reward a center that is less extreme. |
| 418 | const double score = std::min(rb, rw) * (1.0 - 0.5 * cbw); |
| 419 | return {score, rb, rw}; |
| 420 | } |
| 421 | |
| 422 | static void setupImageScan(CLI::App& image) |
| 423 | { |