From b03a2a25f125614d7e14ba8fab8de11be9af6243 Mon Sep 17 00:00:00 2001 From: Boki Date: Thu, 12 Feb 2026 22:28:18 -0500 Subject: [PATCH] chatgpt fix --- tools/OcrDaemon/EdgeCropHandler.cs | 67 +++++++++++++++++++++++------- 1 file changed, 53 insertions(+), 14 deletions(-) diff --git a/tools/OcrDaemon/EdgeCropHandler.cs b/tools/OcrDaemon/EdgeCropHandler.cs index 28c2c89..03334dc 100644 --- a/tools/OcrDaemon/EdgeCropHandler.cs +++ b/tools/OcrDaemon/EdgeCropHandler.cs @@ -31,6 +31,9 @@ class EdgeCropHandler int w = fullCapture.Width; int h = fullCapture.Height; + cursorX = Math.Clamp(cursorX, 0, w - 1); + cursorY = Math.Clamp(cursorY, 0, h - 1); + var bmpData = fullCapture.LockBits(new Rectangle(0, 0, w, h), ImageLockMode.ReadOnly, PixelFormat.Format32bppArgb); byte[] px = new byte[bmpData.Stride * h]; Marshal.Copy(bmpData.Scan0, px, 0, px.Length); @@ -54,13 +57,12 @@ class EdgeCropHandler for (int y = bandTop; y <= bandBot; y++) { int rowOff = y * stride; - int ci = rowOff + cursorX * 4; - int cBright = (px[ci] + px[ci + 1] + px[ci + 2]) / 3; - if (cBright >= darkThresh) continue; + int seedX = FindDarkSeedInRow(px, stride, w, rowOff, cursorX, darkThresh, seedRadius: 6); + if (seedX < 0) continue; - int leftEdge = cursorX; + int leftEdge = seedX; int gap = 0; - for (int x = cursorX - 1; x >= 0; x--) + for (int x = seedX - 1; x >= 0; x--) { int i = rowOff + x * 4; int brightness = (px[i] + px[i + 1] + px[i + 2]) / 3; @@ -68,9 +70,9 @@ class EdgeCropHandler else if (++gap > colGap) break; } - int rightEdge = cursorX; + int rightEdge = seedX; gap = 0; - for (int x = cursorX + 1; x < w; x++) + for (int x = seedX + 1; x < w; x++) { int i = rowOff + x * 4; int brightness = (px[i] + px[i + 1] + px[i + 2]) / 3; @@ -124,13 +126,12 @@ class EdgeCropHandler for (int x = colBandLeft; x <= colBandRight; x++) { - int ci = cursorY * stride + x * 4; - int cBright = (px[ci] + px[ci + 1] + px[ci + 2]) / 3; - if (cBright >= darkThresh) continue; + int seedY = FindDarkSeedInColumn(px, stride, h, x, cursorY, darkThresh, seedRadius: 6); + if (seedY < 0) continue; - int topEdge = cursorY; + int topEdge = seedY; int gap = 0; - for (int y = cursorY - 1; y >= 0; y--) + for (int y = seedY - 1; y >= 0; y--) { int i = y * stride + x * 4; int brightness = (px[i] + px[i + 1] + px[i + 2]) / 3; @@ -138,9 +139,9 @@ class EdgeCropHandler else if (++gap > maxGapUp) break; } - int bottomEdge = cursorY; + int bottomEdge = seedY; gap = 0; - for (int y = cursorY + 1; y < h; y++) + for (int y = seedY + 1; y < h; y++) { int i = y * stride + x * 4; int brightness = (px[i] + px[i + 1] + px[i + 2]) / 3; @@ -202,4 +203,42 @@ class EdgeCropHandler return (cropped, fullCapture, region); } + + private static int FindDarkSeedInRow(byte[] px, int stride, int w, int rowOff, int cursorX, int darkThresh, int seedRadius) + { + int maxR = Math.Min(seedRadius, Math.Min(cursorX, w - 1 - cursorX)); + for (int r = 0; r <= maxR; r++) + { + int x1 = cursorX - r; + int i1 = rowOff + x1 * 4; + int b1 = (px[i1] + px[i1 + 1] + px[i1 + 2]) / 3; + if (b1 < darkThresh) return x1; + + int x2 = cursorX + r; + int i2 = rowOff + x2 * 4; + int b2 = (px[i2] + px[i2 + 1] + px[i2 + 2]) / 3; + if (b2 < darkThresh) return x2; + } + + return -1; + } + + private static int FindDarkSeedInColumn(byte[] px, int stride, int h, int x, int cursorY, int darkThresh, int seedRadius) + { + int maxR = Math.Min(seedRadius, Math.Min(cursorY, h - 1 - cursorY)); + for (int r = 0; r <= maxR; r++) + { + int y1 = cursorY - r; + int i1 = y1 * stride + x * 4; + int b1 = (px[i1] + px[i1 + 1] + px[i1 + 2]) / 3; + if (b1 < darkThresh) return y1; + + int y2 = cursorY + r; + int i2 = y2 * stride + x * 4; + int b2 = (px[i2] + px[i2 + 1] + px[i2 + 2]) / 3; + if (b2 < darkThresh) return y2; + } + + return -1; + } }