Randoloot/assets/alien_ships/extract_sprites.ps1

44 lines
3.1 KiB
PowerShell

# Rebuild the game sprites from the transparent ImageGen atlas.
Add-Type -AssemblyName System.Drawing
$taskAtlas = [Drawing.Bitmap]::new((Join-Path $PSScriptRoot 'atlas.png'))
$taskRows = @(0, 220, 435, 615, 842, 1084)
try {
for ($taskRow = 0; $taskRow -lt 5; $taskRow++) {
for ($taskColumn = 0; $taskColumn -lt 8; $taskColumn++) {
$taskIndex = $taskRow * 8 + $taskColumn + 1
$taskLeft = [int][Math]::Round($taskColumn * $taskAtlas.Width / 8.0)
$taskRight = [int][Math]::Round(($taskColumn + 1) * $taskAtlas.Width / 8.0)
$taskRect = [Drawing.Rectangle]::new($taskLeft, $taskRows[$taskRow], $taskRight - $taskLeft, $taskRows[$taskRow + 1] - $taskRows[$taskRow])
$taskCell = $taskAtlas.Clone($taskRect, [Drawing.Imaging.PixelFormat]::Format32bppArgb)
# Find occupied bounds; ignore almost-transparent extraction fringe.
$taskMinX = $taskCell.Width; $taskMinY = $taskCell.Height
$taskMaxX = -1; $taskMaxY = -1
for ($taskY = 0; $taskY -lt $taskCell.Height; $taskY++) {
for ($taskX = 0; $taskX -lt $taskCell.Width; $taskX++) {
if ($taskCell.GetPixel($taskX, $taskY).A -gt 32) {
$taskMinX = [Math]::Min($taskMinX, $taskX); $taskMinY = [Math]::Min($taskMinY, $taskY)
$taskMaxX = [Math]::Max($taskMaxX, $taskX); $taskMaxY = [Math]::Max($taskMaxY, $taskY)
}
}
}
if ($taskMaxX -lt 0) { throw "Empty sprite $taskIndex" }
$taskBounds = [Drawing.Rectangle]::new($taskMinX, $taskMinY, $taskMaxX - $taskMinX + 1, $taskMaxY - $taskMinY + 1)
$taskSprite = $taskCell.Clone($taskBounds, [Drawing.Imaging.PixelFormat]::Format32bppArgb)
$taskCell.Dispose()
if ($taskIndex -in @(17, 21, 24)) { $taskSprite.RotateFlip([Drawing.RotateFlipType]::Rotate270FlipNone) }
if ($taskIndex -in @(9, 13, 14)) { $taskSprite.RotateFlip([Drawing.RotateFlipType]::Rotate180FlipNone) }
$taskOutput = [Drawing.Bitmap]::new(256, 256, [Drawing.Imaging.PixelFormat]::Format32bppArgb)
$taskGraphics = [Drawing.Graphics]::FromImage($taskOutput)
$taskGraphics.Clear([Drawing.Color]::Transparent)
$taskGraphics.InterpolationMode = [Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic
$taskScale = 224.0 / [Math]::Max($taskSprite.Width, $taskSprite.Height)
$taskWidth = [int][Math]::Round($taskSprite.Width * $taskScale)
$taskHeight = [int][Math]::Round($taskSprite.Height * $taskScale)
$taskGraphics.DrawImage($taskSprite, [Drawing.Rectangle]::new([int]((256 - $taskWidth) / 2), [int]((256 - $taskHeight) / 2), $taskWidth, $taskHeight))
$taskOutput.Save((Join-Path $PSScriptRoot ('alien_{0:D2}.png' -f $taskIndex)), [Drawing.Imaging.ImageFormat]::Png)
$taskGraphics.Dispose(); $taskOutput.Dispose(); $taskSprite.Dispose()
}
}
} finally { $taskAtlas.Dispose() }
Write-Output 'Extracted 40 transparent sprites, 256 x 256 pixels.'