78 lines
3.7 KiB
PowerShell
78 lines
3.7 KiB
PowerShell
# Writes the images `tests/` reads into `tests/images/`, with Windows' own encoders, which are no
|
|||
|
|
# part of image's decoders. Each is 24 by 20 pixels of a pattern the tests work out again, as
|
||
|
|
# `tests/support.sakura` does, red rising across, green down, and blue along the diagonal:
|
||
|
|
#
|
||
|
|
# r = 10x, g = 12y, b = 5(x + y), a = 55 + 8x where it has alpha
|
||
|
|
#
|
||
|
|
# powershell -File tools/test_images.ps1
|
||
|
|
Add-Type -AssemblyName PresentationCore
|
||
|
|
$out = Join-Path $PSScriptRoot "..\tests\images"
|
||
|
|
New-Item -ItemType Directory -Force $out | Out-Null
|
||
|
|
$width = 24; $height = 20
|
||
|
|
$formats = [System.Windows.Media.PixelFormats]
|
||
|
|
|
||
|
|
# The pattern as BGRA, and as the samples of the other formats.
|
||
|
|
function Pattern([string]$kind) {
|
||
|
|
$bytes = New-Object System.Collections.Generic.List[byte]
|
||
|
|
for ($y = 0; $y -lt $height; $y++) {
|
||
|
|
for ($x = 0; $x -lt $width; $x++) {
|
||
|
|
$r = 10 * $x; $g = 12 * $y; $b = 5 * ($x + $y); $a = 55 + 8 * $x
|
||
|
|
switch ($kind) {
|
||
|
|
"bgra" { $bytes.Add($b); $bytes.Add($g); $bytes.Add($r); $bytes.Add($a) }
|
||
|
|
"bgr" { $bytes.Add($b); $bytes.Add($g); $bytes.Add($r) }
|
||
|
|
"gray" { $bytes.Add($g) }
|
||
|
|
"gray16" { $v = $g * 257; $bytes.Add($v -band 0xFF); $bytes.Add($v -shr 8) }
|
||
|
|
"rgba64" { foreach ($c in @($r, $g, $b, $a)) { $v = $c * 257; $bytes.Add($v -band 0xFF); $bytes.Add($v -shr 8) } }
|
||
|
|
"index" { $bytes.Add([byte]((($x -shr 2) + ($y -shr 2) * 6) % 16)) }
|
||
|
|
"bw" { }
|
||
|
|
}
|
||
|
|
}
|
||
|
|
}
|
||
|
|
return ,$bytes.ToArray()
|
||
|
|
}
|
||
|
|
|
||
|
|
function Save($encoder, [string]$name, $source) {
|
||
|
|
$encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($source))
|
||
|
|
$stream = [System.IO.File]::Create((Join-Path $out $name))
|
||
|
|
$encoder.Save($stream)
|
||
|
|
$stream.Close()
|
||
|
|
}
|
||
|
|
|
||
|
|
function Source($format, [byte[]]$pixels, [int]$bitsPerPixel, $palette = $null) {
|
||
|
|
$stride = [int][Math]::Ceiling($width * $bitsPerPixel / 8.0)
|
||
|
|
return [System.Windows.Media.Imaging.BitmapSource]::Create($width, $height, 96, 96, $format, $palette, $pixels, $stride)
|
||
|
|
}
|
||
|
|
|
||
|
|
function Png([string]$name, $source, [bool]$interlaced = $false) {
|
||
|
|
$encoder = New-Object System.Windows.Media.Imaging.PngBitmapEncoder
|
||
|
|
if ($interlaced) { $encoder.Interlace = [System.Windows.Media.Imaging.PngInterlaceOption]::On }
|
||
|
|
Save $encoder $name $source
|
||
|
|
}
|
||
|
|
|
||
|
|
Png "rgba8.png" (Source $formats::Bgra32 (Pattern "bgra") 32)
|
||
|
|
Png "rgb8.png" (Source $formats::Bgr24 (Pattern "bgr") 24)
|
||
|
|
Png "gray8.png" (Source $formats::Gray8 (Pattern "gray") 8)
|
||
|
|
Png "gray16.png" (Source $formats::Gray16 (Pattern "gray16") 16)
|
||
|
|
Png "rgba16.png" (Source $formats::Rgba64 (Pattern "rgba64") 64)
|
||
|
|
Png "interlaced.png" (Source $formats::Bgra32 (Pattern "bgra") 32) $true
|
||
|
|
|
||
|
|
# Sixteen colors, entry i being (16i, 255 - 16i, 8i), each with alpha 255 - 8i.
|
||
|
|
$colors = New-Object System.Collections.Generic.List[System.Windows.Media.Color]
|
||
|
|
for ($i = 0; $i -lt 16; $i++) { $colors.Add([System.Windows.Media.Color]::FromArgb(255 - 8 * $i, 16 * $i, 255 - 16 * $i, 8 * $i)) }
|
||
|
|
$palette = New-Object System.Windows.Media.Imaging.BitmapPalette -ArgumentList (,$colors)
|
||
|
|
$indices = Pattern "index"
|
||
|
|
$packed = New-Object byte[] (12 * $height)
|
||
|
|
for ($i = 0; $i -lt $indices.Length; $i++) {
|
||
|
|
$row = [Math]::Floor($i / $width); $column = $i % $width
|
||
|
|
$packed[$row * 12 + ($column -shr 1)] = $packed[$row * 12 + ($column -shr 1)] -bor ($indices[$i] -shl (4 * (1 - ($column -band 1))))
|
||
|
|
}
|
||
|
|
Png "palette4.png" (Source $formats::Indexed4 $packed 4 $palette)
|
||
|
|
|
||
|
|
$jpeg = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
|
||
|
|
$jpeg.QualityLevel = 95
|
||
|
|
Save $jpeg "rgb.jpg" (Source $formats::Bgr24 (Pattern "bgr") 24)
|
||
|
|
$gray = New-Object System.Windows.Media.Imaging.JpegBitmapEncoder
|
||
|
|
$gray.QualityLevel = 95
|
||
|
|
Save $gray "gray.jpg" (Source $formats::Gray8 (Pattern "gray") 8)
|
||
|
|
|
||
|
|
Get-ChildItem $out | ForEach-Object { "$($_.Name) $($_.Length)" }
|