186 lines
6.9 KiB
PowerShell
186 lines
6.9 KiB
PowerShell
param(
|
|
[string]$WorkspaceRoot = (Split-Path -Parent $PSScriptRoot),
|
|
[string]$InputPath = '',
|
|
[string]$OutputStem = 'xinyin-front-right-parts',
|
|
[int]$AlphaThreshold = 16,
|
|
[int]$MinimumArea = 80
|
|
)
|
|
|
|
Add-Type -AssemblyName System.Drawing
|
|
|
|
$helperSource = @'
|
|
using System;
|
|
using System.Collections;
|
|
using System.Drawing;
|
|
|
|
public sealed class AlphaComponent
|
|
{
|
|
public int Id { get; set; }
|
|
public int Area { get; set; }
|
|
public Rectangle Bounds { get; set; }
|
|
}
|
|
|
|
public static class AlphaComponentScanner
|
|
{
|
|
public static AlphaComponent[] Scan(string path, int alphaThreshold, int minimumArea)
|
|
{
|
|
using (var image = new Bitmap(path))
|
|
{
|
|
int width = image.Width;
|
|
int height = image.Height;
|
|
var foreground = new bool[width * height];
|
|
var visited = new bool[width * height];
|
|
var queue = new int[width * height];
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
int row = y * width;
|
|
for (int x = 0; x < width; x++)
|
|
foreground[row + x] = image.GetPixel(x, y).A >= alphaThreshold;
|
|
}
|
|
|
|
var components = new ArrayList();
|
|
int[] dx = { -1, 0, 1, -1, 1, -1, 0, 1 };
|
|
int[] dy = { -1, -1, -1, 0, 0, 1, 1, 1 };
|
|
|
|
for (int y = 0; y < height; y++)
|
|
{
|
|
for (int x = 0; x < width; x++)
|
|
{
|
|
int start = y * width + x;
|
|
if (visited[start] || !foreground[start]) continue;
|
|
|
|
int head = 0, tail = 0, area = 0;
|
|
int minX = x, minY = y, maxX = x, maxY = y;
|
|
visited[start] = true;
|
|
queue[tail++] = start;
|
|
|
|
while (head < tail)
|
|
{
|
|
int index = queue[head++];
|
|
int px = index % width;
|
|
int py = index / width;
|
|
area++;
|
|
if (px < minX) minX = px;
|
|
if (px > maxX) maxX = px;
|
|
if (py < minY) minY = py;
|
|
if (py > maxY) maxY = py;
|
|
|
|
for (int i = 0; i < 8; i++)
|
|
{
|
|
int nx = px + dx[i];
|
|
int ny = py + dy[i];
|
|
if (nx < 0 || nx >= width || ny < 0 || ny >= height) continue;
|
|
int next = ny * width + nx;
|
|
if (visited[next] || !foreground[next]) continue;
|
|
visited[next] = true;
|
|
queue[tail++] = next;
|
|
}
|
|
}
|
|
|
|
if (area >= minimumArea)
|
|
{
|
|
components.Add(new AlphaComponent {
|
|
Area = area,
|
|
Bounds = Rectangle.FromLTRB(minX, minY, maxX + 1, maxY + 1)
|
|
});
|
|
}
|
|
}
|
|
}
|
|
|
|
var result = new AlphaComponent[components.Count];
|
|
for (int i = 0; i < components.Count; i++) result[i] = (AlphaComponent)components[i];
|
|
Array.Sort(result, (a, b) => {
|
|
int rowCompare = a.Bounds.Top.CompareTo(b.Bounds.Top);
|
|
return rowCompare != 0 ? rowCompare : a.Bounds.Left.CompareTo(b.Bounds.Left);
|
|
});
|
|
for (int i = 0; i < result.Length; i++) result[i].Id = i + 1;
|
|
return result;
|
|
}
|
|
}
|
|
}
|
|
'@
|
|
|
|
$compilerReferences = @(
|
|
[System.Drawing.Bitmap].Assembly.Location,
|
|
[System.Drawing.Color].Assembly.Location,
|
|
[System.Reflection.Assembly]::Load('System.Private.Windows.GdiPlus').Location,
|
|
[System.Reflection.Assembly]::Load('System.Private.Windows.Core').Location,
|
|
[System.Reflection.Assembly]::Load('System.Collections').Location,
|
|
[System.Reflection.Assembly]::Load('System.Runtime').Location,
|
|
[System.Object].Assembly.Location
|
|
) | Select-Object -Unique
|
|
Add-Type -TypeDefinition $helperSource -ReferencedAssemblies $compilerReferences
|
|
|
|
if ([string]::IsNullOrWhiteSpace($InputPath)) {
|
|
$InputPath = Join-Path $WorkspaceRoot 'assets\images\2d-game-characters\02-xinyin\source\xinyin-front-right-parts-sheet-v1.png'
|
|
}
|
|
elseif (-not [IO.Path]::IsPathRooted($InputPath)) {
|
|
$InputPath = Join-Path $WorkspaceRoot $InputPath
|
|
}
|
|
$InputPath = (Resolve-Path -LiteralPath $InputPath).Path
|
|
$diagnosticDirectory = Join-Path $WorkspaceRoot 'assets\images\2d-game-characters\02-xinyin\diagnostics'
|
|
$diagnosticImage = Join-Path $diagnosticDirectory "$OutputStem-components.png"
|
|
$diagnosticJson = Join-Path $diagnosticDirectory "$OutputStem-components.json"
|
|
New-Item -ItemType Directory -Force -Path $diagnosticDirectory | Out-Null
|
|
|
|
$components = [AlphaComponentScanner]::Scan($InputPath, $AlphaThreshold, $MinimumArea)
|
|
$source = [Drawing.Bitmap]::new($InputPath)
|
|
$canvasWidth = $source.Width
|
|
$canvasHeight = $source.Height
|
|
$canvas = [Drawing.Bitmap]::new($source.Width, $source.Height, [Drawing.Imaging.PixelFormat]::Format32bppArgb)
|
|
$graphics = [Drawing.Graphics]::FromImage($canvas)
|
|
$graphics.Clear([Drawing.Color]::FromArgb(255, 22, 25, 34))
|
|
$graphics.DrawImageUnscaled($source, 0, 0)
|
|
$pen = [Drawing.Pen]::new([Drawing.Color]::FromArgb(255, 52, 220, 255), 2)
|
|
$labelFill = [Drawing.SolidBrush]::new([Drawing.Color]::FromArgb(230, 8, 12, 19))
|
|
$labelText = [Drawing.SolidBrush]::new([Drawing.Color]::White)
|
|
$font = [Drawing.Font]::new('Segoe UI Semibold', 11)
|
|
|
|
try {
|
|
foreach ($component in $components) {
|
|
$bounds = $component.Bounds
|
|
$graphics.DrawRectangle($pen, $bounds.X, $bounds.Y, $bounds.Width - 1, $bounds.Height - 1)
|
|
$label = [string]$component.Id
|
|
$size = $graphics.MeasureString($label, $font)
|
|
$labelX = [Math]::Max(0, $bounds.X)
|
|
$labelY = [Math]::Max(0, $bounds.Y - [int]$size.Height)
|
|
$graphics.FillRectangle($labelFill, $labelX, $labelY, [int]$size.Width + 8, [int]$size.Height)
|
|
$graphics.DrawString($label, $font, $labelText, $labelX + 4, $labelY)
|
|
}
|
|
$canvas.Save($diagnosticImage, [Drawing.Imaging.ImageFormat]::Png)
|
|
}
|
|
finally {
|
|
$font.Dispose()
|
|
$labelText.Dispose()
|
|
$labelFill.Dispose()
|
|
$pen.Dispose()
|
|
$graphics.Dispose()
|
|
$canvas.Dispose()
|
|
$source.Dispose()
|
|
}
|
|
|
|
$records = foreach ($component in $components) {
|
|
[ordered]@{
|
|
id = $component.Id
|
|
area = $component.Area
|
|
x = $component.Bounds.X
|
|
y = $component.Bounds.Y
|
|
width = $component.Bounds.Width
|
|
height = $component.Bounds.Height
|
|
}
|
|
}
|
|
[ordered]@{
|
|
source = $InputPath
|
|
imageWidth = $canvasWidth
|
|
imageHeight = $canvasHeight
|
|
alphaThreshold = $AlphaThreshold
|
|
minimumArea = $MinimumArea
|
|
componentCount = $components.Count
|
|
components = $records
|
|
} | ConvertTo-Json -Depth 4 | Set-Content -LiteralPath $diagnosticJson -Encoding utf8
|
|
|
|
Write-Output "Detected $($components.Count) alpha components."
|
|
Write-Output $diagnosticImage
|
|
Write-Output $diagnosticJson
|