diff --git a/PARITY.md b/PARITY.md
index 9c36963..5781e49 100644
--- a/PARITY.md
+++ b/PARITY.md
@@ -17,8 +17,8 @@ Status of each Windows feature on Linux. This file describes the tree it ships w
| Esc and right-click cancel the snip | partial | `linux/SnipGesture.cs`, `linux/OverlayWindow.cs` |
| Mixed-DPI placement | working on X11, with one window per monitor. Wayland cross-monitor drags are exact when scales match. | `linux/OverlayWindow.cs`, `linux/MonitorMap.cs`, `linux/X11Pointer.cs` |
| X11 outputs left of or above the origin | working. Capture falls back to the root at (0, 0) if the server rejects the bounding box. | `linux/X11Randr.cs`, `linux/Capture.cs` |
-| Magnifier with coordinates | working | `linux/OverlayWindow.cs` |
-| Magnifier color | working | `linux/OverlayWindow.cs` |
+| Pixel-grid magnifier, coordinate, and color | working. 15×15 nearest-neighbor cells stay centered on the cursor pixel at frame edges; the strip shows a swatch and `#RRGGBB`. | `linux/Magnifier.cs`, `linux/OverlayWindow.cs` |
+| Magnifier can be turned off | working | `linux/SettingsWindow.cs` |
| X11 capture | working | `linux/Capture.cs` |
| Wayland capture (portal screenshot) | working | `linux/Capture.cs`, `linux/DBus.cs` |
| Copy PNG to the clipboard | working. `xclip` on X11, `wl-copy` on Wayland, then Avalonia. | `linux/ClipboardService.cs` |
diff --git a/README.md b/README.md
index 4767e98..8e6495d 100644
--- a/README.md
+++ b/README.md
@@ -117,7 +117,7 @@ Run `OpenSnip.exe --diagnostics` to write a report (displays, audio devices, and
The Linux app is a separate native Avalonia build. It covers screenshots, annotation, pinned images, global shortcuts and screen recording on X11.
-**What works:** X11 capture through libX11, with `maim`, ImageMagick `import` and `scrot` as fallbacks; Wayland capture through the `xdg-desktop-portal` Screenshot API, with `grim`, `gnome-screenshot` and `spectacle` as fallbacks; region, window, monitor and all-monitor selection with a magnifier and one overlay window per screen at its scale; clipboard as PNG; auto-save as PNG or JPG; preview; annotation; pinned images (under the pointer on X11, centered on Wayland); tray icon; global shortcuts and MP4 recording on X11; and settings. X11 capture includes outputs left of or above the origin.
+**What works:** X11 capture through libX11, with `maim`, ImageMagick `import` and `scrot` as fallbacks; Wayland capture through the `xdg-desktop-portal` Screenshot API, with `grim`, `gnome-screenshot` and `spectacle` as fallbacks; region, window, monitor and all-monitor selection with a pixel-grid magnifier showing coordinates and color, and one overlay window per screen at its scale; clipboard as PNG; auto-save as PNG or JPG; preview; annotation; pinned images (under the pointer on X11, centered on Wayland); tray icon; global shortcuts and MP4 recording on X11; and settings. X11 capture includes outputs left of or above the origin.
**Install:** download `OpenSnip-linux-x64.tar.gz` from [Releases](https://github.com/DevanMetz/OpenSnip/releases), then:
@@ -228,7 +228,7 @@ Issues and pull requests are welcome. The code is plain WinForms + GDI+; recordi
| `linux/Program.cs` / `linux/App.cs` | Linux app startup, single instance, tray and snip flow |
| `linux/Capture.cs` / `linux/DBus.cs` | X11, Wayland portal and external capture backends |
| `linux/X11Windows.cs` | Top-level X11 windows for click-to-capture |
-| `linux/OverlayWindow.cs` | Per-monitor Linux selection overlays and magnifier |
+| `linux/OverlayWindow.cs` / `linux/Magnifier.cs` | Per-monitor Linux selection overlays and pixel magnifier |
| `linux/MonitorMap.cs` | Desktop-pixel mapping for mixed-DPI snips |
| `linux/RecordingSession.cs` | X11 recording, red outline, and control bar |
| `linux/PinWindow.cs` | Linux pinned images |
diff --git a/linux/Magnifier.cs b/linux/Magnifier.cs
new file mode 100644
index 0000000..520b4a1
--- /dev/null
+++ b/linux/Magnifier.cs
@@ -0,0 +1,161 @@
+namespace OpenSnip;
+
+///
+/// The snip loupe: a 15×15 grid of the frozen frame, centered on the cursor pixel.
+/// Cells are sampled from that pixel so the center stays put when the cursor
+/// reaches the edge of the frame. A clamped source rectangle would slide it.
+///
+static class Magnifier
+{
+ public const int Cells = 15;
+ public const int Center = 7;
+
+ public readonly record struct Box(double X, double Y, double Width, double Height)
+ {
+ public double Right => X + Width;
+ public double Bottom => Y + Height;
+
+ public bool Overlaps(Box other) =>
+ X < other.Right && Right > other.X && Y < other.Bottom && Bottom > other.Y;
+ }
+
+ public static int ImageCoord(double local, double span, int pixels)
+ {
+ if (pixels <= 1 || span <= 0) return 0;
+ int value = (int)(local * pixels / span);
+ if (value < 0) return 0;
+ if (value >= pixels) return pixels - 1;
+ return value;
+ }
+
+ public static bool TryPixel(CapturedFrame frame, int x, int y, out byte r, out byte g, out byte b)
+ {
+ r = g = b = 0;
+ if (frame == null || (uint)x >= (uint)frame.Width || (uint)y >= (uint)frame.Height) return false;
+ int i = (y * frame.Width + x) * 4;
+ b = frame.Bgra[i];
+ g = frame.Bgra[i + 1];
+ r = frame.Bgra[i + 2];
+ return true;
+ }
+
+ public static string Label(int x, int y, byte r, byte g, byte b) =>
+ $"{x}, {y} #{r:X2}{g:X2}{b:X2}";
+
+ /// Packs a square of BGRA pixels. Outside the frame stays black.
+ public static void Fill(CapturedFrame frame, int x, int y, byte[] bgra)
+ {
+ if (frame == null) throw new ArgumentNullException(nameof(frame));
+ if (bgra == null || bgra.Length < Cells * Cells * 4)
+ throw new ArgumentException("The loupe buffer is too small.", nameof(bgra));
+
+ int count = Cells * Cells * 4;
+ for (int i = 0; i < count; i += 4)
+ {
+ bgra[i] = 0;
+ bgra[i + 1] = 0;
+ bgra[i + 2] = 0;
+ bgra[i + 3] = 255;
+ }
+
+ for (int row = 0; row < Cells; row++)
+ {
+ int sy = y + row - Center;
+ if ((uint)sy >= (uint)frame.Height) continue;
+ for (int col = 0; col < Cells; col++)
+ {
+ int sx = x + col - Center;
+ if ((uint)sx >= (uint)frame.Width) continue;
+ int src = (sy * frame.Width + sx) * 4;
+ int dst = (row * Cells + col) * 4;
+ bgra[dst] = frame.Bgra[src];
+ bgra[dst + 1] = frame.Bgra[src + 1];
+ bgra[dst + 2] = frame.Bgra[src + 2];
+ bgra[dst + 3] = 255;
+ }
+ }
+ }
+
+ /// Nearest-neighbor scale of the cell grid into screen pixels per cell.
+ public static void Expand(byte[] cells, int cell, byte[] dest)
+ {
+ if (cell < 1) throw new ArgumentOutOfRangeException(nameof(cell));
+ if (cells == null || cells.Length < Cells * Cells * 4)
+ throw new ArgumentException("The loupe buffer is too small.", nameof(cells));
+ int size = Cells * cell;
+ if (dest == null || dest.Length < size * size * 4)
+ throw new ArgumentException("The scaled buffer is too small.", nameof(dest));
+
+ int dstStride = size * 4;
+ for (int row = 0; row < Cells; row++)
+ {
+ for (int col = 0; col < Cells; col++)
+ {
+ int src = (row * Cells + col) * 4;
+ byte b = cells[src];
+ byte g = cells[src + 1];
+ byte r = cells[src + 2];
+ byte a = cells[src + 3];
+ int row0 = row * cell;
+ int col0 = col * cell;
+ for (int dy = 0; dy < cell; dy++)
+ {
+ int dst = (row0 + dy) * dstStride + col0 * 4;
+ for (int dx = 0; dx < cell; dx++)
+ {
+ dest[dst++] = b;
+ dest[dst++] = g;
+ dest[dst++] = r;
+ dest[dst++] = a;
+ }
+ }
+ }
+ }
+ }
+
+ ///
+ /// Puts the loupe beside the cursor, flipping to stay on screen and off .
+ ///
+ public static Box Place(double cursorX, double cursorY, double width, double height,
+ double boundsWidth, double boundsHeight, Box? avoid)
+ {
+ const double gap = 22;
+ const double margin = 4;
+
+ Box? TryAt(double x, double y)
+ {
+ if (x < margin || y < margin ||
+ x + width > boundsWidth - margin ||
+ y + height > boundsHeight - margin)
+ return null;
+ var rect = new Box(x, y, width, height);
+ if (avoid is { } blocked && rect.Overlaps(blocked)) return null;
+ return rect;
+ }
+
+ double right = cursorX + gap;
+ double left = cursorX - width - gap;
+ double down = cursorY + gap;
+ double up = cursorY - height - gap;
+
+ var found = TryAt(right, down) ?? TryAt(left, down) ?? TryAt(right, up) ?? TryAt(left, up);
+ if (found == null && avoid is { } blocked)
+ {
+ found = TryAt(blocked.Right + gap, down)
+ ?? TryAt(blocked.X - width - gap, down)
+ ?? TryAt(right, blocked.Bottom + gap)
+ ?? TryAt(right, blocked.Y - height - gap)
+ ?? TryAt(left, blocked.Bottom + gap)
+ ?? TryAt(left, blocked.Y - height - gap);
+ }
+ if (found is { } placed) return placed;
+
+ double fx = right;
+ double fy = down;
+ if (fx + width > boundsWidth - margin) fx = left;
+ if (fy + height > boundsHeight - margin) fy = up;
+ fx = Math.Clamp(fx, margin, Math.Max(margin, boundsWidth - width - margin));
+ fy = Math.Clamp(fy, margin, Math.Max(margin, boundsHeight - height - margin));
+ return new Box(fx, fy, width, height);
+ }
+}
diff --git a/linux/OverlayWindow.cs b/linux/OverlayWindow.cs
index 5200e14..d52cd2a 100644
--- a/linux/OverlayWindow.cs
+++ b/linux/OverlayWindow.cs
@@ -1,11 +1,14 @@
using System.Globalization;
+using System.Runtime.InteropServices;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Media;
using Avalonia.Media.Imaging;
+using Avalonia.Platform;
using Avalonia.Threading;
+using Avalonia.VisualTree;
namespace OpenSnip;
@@ -289,10 +292,18 @@ sealed class CaptureCanvas : Control
readonly int _index;
readonly Typeface _typeface = new("Inter");
+ byte[] _loupePixels;
+ byte[] _loupeScaled;
+ WriteableBitmap _loupe;
+ int _loupeCell;
+
static readonly IBrush DimBrush = new SolidColorBrush(Color.FromArgb(150, 0, 0, 0));
static readonly IBrush ReadoutBrush = new SolidColorBrush(Color.FromArgb(238, 38, 38, 44));
+ static readonly IBrush GridTint = new SolidColorBrush(Color.FromArgb(60, AppTheme.AccentColor.R, AppTheme.AccentColor.G, AppTheme.AccentColor.B));
static readonly IPen AccentPen = new Pen(AppTheme.Accent, 2);
static readonly IPen CrosshairPen = new Pen(AppTheme.Accent, 1);
+ static readonly IPen CenterWhite = new Pen(Brushes.White, 1);
+ static readonly IPen CenterBlack = new Pen(Brushes.Black, 1);
public CaptureCanvas(OverlaySession session, int index)
{
@@ -431,77 +442,78 @@ Rect DrawReadout(DrawingContext context, Rect box, PixelRect selection)
void DrawMagnifier(DrawingContext context, Point cursor, Rect? readout, Rect view)
{
- if (!_session.Map.TryToImage(new PixelRect(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, 1, 1), out var pixel))
- return;
-
- const double inner = 104;
- const double pad = 3;
- const double labelHeight = 20;
- double total = inner + pad * 2;
- var box = PlaceMagnifier(cursor, total, total + labelHeight, readout, view);
- int imageX = Math.Clamp(pixel.X, 0, Math.Max(0, _session.Bitmap.PixelSize.Width - 1));
- int imageY = Math.Clamp(pixel.Y, 0, Math.Max(0, _session.Bitmap.PixelSize.Height - 1));
- int frameW = _session.Bitmap.PixelSize.Width;
- int frameH = _session.Bitmap.PixelSize.Height;
- const int sourceSize = 24;
- int left = imageX - sourceSize / 2;
- int top = imageY - sourceSize / 2;
- int srcL = Math.Max(0, left), srcT = Math.Max(0, top);
- int srcR = Math.Min(frameW, left + sourceSize), srcB = Math.Min(frameH, top + sourceSize);
- var source = new Rect(srcL, srcT, srcR - srcL, srcB - srcT);
- var dest = new Rect(box.X + pad, box.Y + pad, inner, inner);
-
- context.DrawRectangle(ReadoutBrush, new Pen(AppTheme.Accent, 2), new Rect(box.X, box.Y, total, total + labelHeight), 7, 7);
- using (context.PushClip(dest))
- context.DrawImage(_session.Bitmap, source, new Rect(dest.X + (srcL - left) * inner / sourceSize, dest.Y + (srcT - top) * inner / sourceSize, source.Width * inner / sourceSize, source.Height * inner / sourceSize));
- var center = new Point(dest.X + (imageX - left + 0.5) * inner / sourceSize, dest.Y + (imageY - top + 0.5) * inner / sourceSize);
- context.DrawLine(CrosshairPen, new Point(center.X - 7, center.Y), new Point(center.X + 7, center.Y));
- context.DrawLine(CrosshairPen, new Point(center.X, center.Y - 7), new Point(center.X, center.Y + 7));
-
- var label = new FormattedText($"{_session.Snip.Cursor.X}, {_session.Snip.Cursor.Y} {_session.Frame.ColorLabel(imageX, imageY)}", CultureInfo.InvariantCulture,
- FlowDirection.LeftToRight, _typeface, 11, AppTheme.Muted);
- context.DrawText(label, new Point(box.X + (total - label.Width) / 2, box.Y + total + 2));
+ const double pad = 7;
+ const double swatch = 10;
+ const double infoH = 24;
+
+ if (!_session.Map.TryToImage(new PixelRect(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, 1, 1), out var pixel)) return;
+ int imageX = pixel.X;
+ int imageY = pixel.Y;
+ Magnifier.TryPixel(_session.Frame, imageX, imageY, out byte red, out byte green, out byte blue);
+ var label = new FormattedText(Magnifier.Label(_session.Snip.Cursor.X, _session.Snip.Cursor.Y, red, green, blue), CultureInfo.InvariantCulture,
+ FlowDirection.LeftToRight, _typeface, 12, AppTheme.Text);
+
+ int cell = Math.Max(8, (int)Math.Ceiling((label.Width + swatch + pad * 3) / Magnifier.Cells));
+ double grid = Magnifier.Cells * cell;
+ double boxW = Math.Max(grid, label.Width + swatch + pad * 3);
+ double boxH = grid + infoH;
+ Magnifier.Box? avoid = readout is { } block
+ ? new Magnifier.Box(block.X, block.Y, block.Width, block.Height)
+ : null;
+ var placed = Magnifier.Place(cursor.X, cursor.Y, boxW, boxH, view.Width, view.Height, avoid);
+ var box = new Rect(placed.X, placed.Y, placed.Width, placed.Height);
+
+ context.FillRectangle(Brushes.Black, box);
+
+ PaintLoupe(context, imageX, imageY, box.X + Math.Round((boxW - grid) / 2), box.Y, grid, cell);
+
+ double infoY = box.Y + grid;
+ context.FillRectangle(new SolidColorBrush(Color.FromRgb(red, green, blue)),
+ new Rect(box.X + pad, infoY + (infoH - swatch) / 2, swatch, swatch));
+ context.DrawText(label, new Point(box.X + pad + swatch + pad, infoY + (infoH - label.Height) / 2));
+ context.DrawRectangle(null, AccentPen, box);
}
- static Rect PlaceMagnifier(Point cursor, double width, double height, Rect? readout, Rect view)
+ void PaintLoupe(DrawingContext context, int imageX, int imageY, double gridX, double gridY, double grid, int cell)
{
- const double gap = 22;
- const double margin = 4;
-
- Rect? TryAt(double x, double y)
+ _loupePixels ??= new byte[Magnifier.Cells * Magnifier.Cells * 4];
+ Magnifier.Fill(_session.Frame, imageX, imageY, _loupePixels);
+ int size = Magnifier.Cells * cell;
+ if (_loupe == null || _loupeCell != cell)
{
- if (x < margin || y < margin || x + width > view.Width - margin || y + height > view.Height - margin)
- return null;
- var rect = new Rect(x, y, width, height);
- if (readout is { } box && Overlaps(rect, box)) return null;
- return rect;
+ _loupe?.Dispose();
+ _loupe = new WriteableBitmap(new PixelSize(size, size), new Vector(96, 96), PixelFormat.Bgra8888, AlphaFormat.Opaque);
+ _loupeScaled = new byte[size * size * 4];
+ _loupeCell = cell;
}
-
- double right = cursor.X + gap;
- double left = cursor.X - width - gap;
- double down = cursor.Y + gap;
- double up = cursor.Y - height - gap;
- var found = TryAt(right, down) ?? TryAt(left, down) ?? TryAt(right, up) ?? TryAt(left, up);
- if (found == null && readout is { } blocked)
+ Magnifier.Expand(_loupePixels, cell, _loupeScaled);
+ using (var buffer = _loupe.Lock())
{
- found = TryAt(blocked.Right + gap, down)
- ?? TryAt(blocked.Left - width - gap, down)
- ?? TryAt(right, blocked.Bottom + gap)
- ?? TryAt(right, blocked.Top - height - gap)
- ?? TryAt(left, blocked.Bottom + gap)
- ?? TryAt(left, blocked.Top - height - gap);
+ int stride = size * 4;
+ for (int row = 0; row < size; row++)
+ Marshal.Copy(_loupeScaled, row * stride, IntPtr.Add(buffer.Address, row * buffer.RowBytes), stride);
}
- if (found is { } placed) return placed;
-
- double fx = right;
- double fy = down;
- if (fx + width > view.Width - margin) fx = left;
- if (fy + height > view.Height - margin) fy = up;
- fx = Math.Clamp(fx, margin, Math.Max(margin, view.Width - width - margin));
- fy = Math.Clamp(fy, margin, Math.Max(margin, view.Height - height - margin));
- return new Rect(fx, fy, width, height);
+
+ // Already nearest-neighbor. Draw 1:1 so the compositor does not blend the cells.
+ var dest = new Rect(gridX, gridY, grid, grid);
+ context.DrawImage(_loupe, dest);
+
+ double c0 = Magnifier.Center * cell;
+ double cellSize = cell;
+ context.FillRectangle(GridTint, new Rect(dest.X, dest.Y + c0, c0, cellSize));
+ context.FillRectangle(GridTint, new Rect(dest.X + c0 + cellSize, dest.Y + c0, grid - c0 - cellSize, cellSize));
+ context.FillRectangle(GridTint, new Rect(dest.X + c0, dest.Y, cellSize, c0));
+ context.FillRectangle(GridTint, new Rect(dest.X + c0, dest.Y + c0 + cellSize, cellSize, grid - c0 - cellSize));
+
+ var center = new Rect(dest.X + c0, dest.Y + c0, cellSize, cellSize);
+ context.DrawRectangle(null, CenterBlack, new Rect(center.X - 1, center.Y - 1, center.Width + 2, center.Height + 2));
+ context.DrawRectangle(null, CenterWhite, center);
}
- static bool Overlaps(Rect a, Rect b) =>
- a.Left < b.Right && a.Right > b.Left && a.Top < b.Bottom && a.Bottom > b.Top;
+ protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
+ {
+ base.OnDetachedFromVisualTree(e);
+ _loupe?.Dispose();
+ _loupe = null;
+ }
}
diff --git a/tests/OpenSnip.Tests/MagnifierTests.cs b/tests/OpenSnip.Tests/MagnifierTests.cs
new file mode 100644
index 0000000..fd00132
--- /dev/null
+++ b/tests/OpenSnip.Tests/MagnifierTests.cs
@@ -0,0 +1,136 @@
+namespace OpenSnip.Tests;
+
+public class MagnifierTests
+{
+ [Fact]
+ public void CenterCellIsTheCursorPixel()
+ {
+ var frame = Frame(5, 5,
+ (2, 2, 255, 0, 0),
+ (1, 2, 0, 255, 0),
+ (3, 3, 0, 0, 255));
+ var loupe = new byte[Magnifier.Cells * Magnifier.Cells * 4];
+ Magnifier.Fill(frame, 2, 2, loupe);
+
+ Assert.Equal((255, 0, 0, 255), At(loupe, Magnifier.Center, Magnifier.Center));
+ Assert.Equal((0, 255, 0, 255), At(loupe, Magnifier.Center - 1, Magnifier.Center));
+ Assert.Equal((0, 0, 255, 255), At(loupe, Magnifier.Center + 1, Magnifier.Center + 1));
+ }
+
+ [Fact]
+ public void EdgeDoesNotSlideTheCenterCell()
+ {
+ var frame = Frame(4, 3, (0, 0, 10, 20, 30), (3, 2, 200, 10, 1));
+ var loupe = new byte[Magnifier.Cells * Magnifier.Cells * 4];
+
+ Magnifier.Fill(frame, 0, 0, loupe);
+ Assert.Equal((10, 20, 30, 255), At(loupe, Magnifier.Center, Magnifier.Center));
+ Assert.Equal((0, 0, 0, 255), At(loupe, Magnifier.Center - 1, Magnifier.Center));
+ Assert.Equal((0, 0, 0, 255), At(loupe, Magnifier.Center, Magnifier.Center - 1));
+
+ Magnifier.Fill(frame, 3, 2, loupe);
+ Assert.Equal((200, 10, 1, 255), At(loupe, Magnifier.Center, Magnifier.Center));
+ Assert.Equal((0, 0, 0, 255), At(loupe, Magnifier.Center + 1, Magnifier.Center));
+ Assert.Equal((0, 0, 0, 255), At(loupe, Magnifier.Center, Magnifier.Center + 1));
+ }
+
+ [Fact]
+ public void ExpandKeepsCellsCrisp()
+ {
+ var frame = Frame(3, 3, (1, 1, 255, 0, 0), (2, 2, 0, 0, 255));
+ var cells = new byte[Magnifier.Cells * Magnifier.Cells * 4];
+ Magnifier.Fill(frame, 1, 1, cells);
+ const int cell = 3;
+ var scaled = new byte[Magnifier.Cells * cell * Magnifier.Cells * cell * 4];
+ Magnifier.Expand(cells, cell, scaled);
+
+ int origin = Magnifier.Center * cell;
+ for (int dy = 0; dy < cell; dy++)
+ for (int dx = 0; dx < cell; dx++)
+ Assert.Equal((255, 0, 0, 255), ScaledAt(scaled, cell, origin + dx, origin + dy));
+
+ int neighbor = (Magnifier.Center + 1) * cell;
+ Assert.Equal((0, 0, 255, 255), ScaledAt(scaled, cell, neighbor, neighbor));
+ Assert.Equal((255, 0, 0, 255), ScaledAt(scaled, cell, origin + cell - 1, origin + cell - 1));
+ Assert.NotEqual((255, 0, 0, 255), ScaledAt(scaled, cell, origin + cell, origin));
+ }
+
+ [Fact]
+ public void LabelMatchesTheWindowsReadout()
+ {
+ Assert.True(Magnifier.TryPixel(Frame(2, 2, (1, 0, 0x0A, 0xB0, 0xC3)), 1, 0, out byte r, out byte g, out byte b));
+ Assert.Equal("1, 0 #0AB0C3", Magnifier.Label(1, 0, r, g, b));
+ Assert.False(Magnifier.TryPixel(Frame(2, 2), -1, 0, out _, out _, out _));
+ }
+
+ [Theory]
+ [InlineData(0, 100, 100, 0)]
+ [InlineData(50, 100, 200, 100)]
+ [InlineData(99.9, 100, 100, 99)]
+ [InlineData(100, 100, 100, 99)]
+ [InlineData(-4, 100, 100, 0)]
+ [InlineData(10, 0, 100, 0)]
+ [InlineData(10, 100, 1, 0)]
+ public void ImageCoordMatchesTheFrameMapping(double local, double span, int pixels, int expected)
+ {
+ Assert.Equal(expected, Magnifier.ImageCoord(local, span, pixels));
+ }
+
+ [Fact]
+ public void LoupeSitsDownAndRightOfTheCursor()
+ {
+ var box = Magnifier.Place(10, 10, 120, 140, 800, 600, null);
+ Assert.Equal(new Magnifier.Box(32, 32, 120, 140), box);
+ }
+
+ [Fact]
+ public void LoupeFlipsAtTheBottomRightEdge()
+ {
+ var box = Magnifier.Place(790, 590, 120, 140, 800, 600, null);
+ Assert.Equal(new Magnifier.Box(648, 428, 120, 140), box);
+ }
+
+ [Fact]
+ public void LoupeMovesOffTheSizeReadout()
+ {
+ var readout = new Magnifier.Box(122, 122, 120, 140);
+ var box = Magnifier.Place(100, 100, 120, 140, 800, 600, readout);
+ Assert.False(box.Overlaps(readout));
+ Assert.Equal(new Magnifier.Box(264, 122, 120, 140), box);
+ }
+
+ [Fact]
+ public void LoupeClampsWhenItCannotFit()
+ {
+ var box = Magnifier.Place(50, 50, 200, 200, 100, 100, null);
+ Assert.Equal(new Magnifier.Box(4, 4, 200, 200), box);
+ }
+
+ static (byte R, byte G, byte B, byte A) ScaledAt(byte[] bgra, int cell, int x, int y)
+ {
+ int size = Magnifier.Cells * cell;
+ int i = (y * size + x) * 4;
+ return (bgra[i + 2], bgra[i + 1], bgra[i], bgra[i + 3]);
+ }
+
+ static (byte R, byte G, byte B, byte A) At(byte[] bgra, int col, int row)
+ {
+ int i = (row * Magnifier.Cells + col) * 4;
+ return (bgra[i + 2], bgra[i + 1], bgra[i], bgra[i + 3]);
+ }
+
+ static CapturedFrame Frame(int width, int height, params (int X, int Y, byte R, byte G, byte B)[] pixels)
+ {
+ var data = new byte[width * height * 4];
+ for (int i = 3; i < data.Length; i += 4) data[i] = 255;
+ foreach (var pixel in pixels)
+ {
+ int i = (pixel.Y * width + pixel.X) * 4;
+ data[i] = pixel.B;
+ data[i + 1] = pixel.G;
+ data[i + 2] = pixel.R;
+ data[i + 3] = 255;
+ }
+ return new CapturedFrame(width, height, data);
+ }
+}