|
| 1 | +param( |
| 2 | + [string]$Source = 'C:\Users\kaitao\Downloads\intelligent terminal.png', |
| 3 | + [string]$Dir = 'res\terminal\images-Dev', |
| 4 | + [int]$MaxBytes = 204800 |
| 5 | +) |
| 6 | +$ErrorActionPreference = 'Stop' |
| 7 | + |
| 8 | +Add-Type -AssemblyName System.Drawing |
| 9 | +Add-Type -AssemblyName PresentationCore |
| 10 | + |
| 11 | +$src = [System.Drawing.Image]::FromFile($Source) |
| 12 | +Write-Host "Source: $($src.Width)x$($src.Height) $Source" |
| 13 | + |
| 14 | +$files = Get-ChildItem -Path $Dir -Filter *.png -File |
| 15 | +Write-Host "Regenerating $($files.Count) files into $Dir (max bytes: $MaxBytes)" |
| 16 | + |
| 17 | +function Save-Indexed-Via-WPF { |
| 18 | + param([System.Drawing.Bitmap]$Bitmap, [string]$Path, [int]$Colors) |
| 19 | + # Convert System.Drawing.Bitmap to BitmapSource |
| 20 | + $ms = New-Object System.IO.MemoryStream |
| 21 | + $Bitmap.Save($ms, [System.Drawing.Imaging.ImageFormat]::Png) |
| 22 | + $ms.Position = 0 |
| 23 | + $bs = [System.Windows.Media.Imaging.BitmapFrame]::Create($ms, |
| 24 | + [System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat, |
| 25 | + [System.Windows.Media.Imaging.BitmapCacheOption]::OnLoad) |
| 26 | + |
| 27 | + # Quantize to indexed by converting to Indexed8 format |
| 28 | + $palette = [System.Windows.Media.Imaging.BitmapPalettes]::Halftone256Transparent |
| 29 | + $converted = New-Object System.Windows.Media.Imaging.FormatConvertedBitmap |
| 30 | + $converted.BeginInit() |
| 31 | + $converted.Source = $bs |
| 32 | + $converted.DestinationFormat = [System.Windows.Media.PixelFormats]::Indexed8 |
| 33 | + $converted.DestinationPalette = $palette |
| 34 | + $converted.EndInit() |
| 35 | + |
| 36 | + $encoder = New-Object System.Windows.Media.Imaging.PngBitmapEncoder |
| 37 | + $encoder.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($converted)) |
| 38 | + $fs = [System.IO.File]::Open($Path, [System.IO.FileMode]::Create) |
| 39 | + $encoder.Save($fs) |
| 40 | + $fs.Dispose() |
| 41 | + $ms.Dispose() |
| 42 | +} |
| 43 | + |
| 44 | +foreach ($f in $files) { |
| 45 | + $existing = [System.Drawing.Image]::FromFile($f.FullName) |
| 46 | + $w = $existing.Width |
| 47 | + $h = $existing.Height |
| 48 | + $existing.Dispose() |
| 49 | + |
| 50 | + $srcAR = $src.Width / $src.Height |
| 51 | + $dstAR = $w / $h |
| 52 | + if ($srcAR -gt $dstAR) { |
| 53 | + $dw = $w |
| 54 | + $dh = [int]($w / $srcAR) |
| 55 | + } else { |
| 56 | + $dh = $h |
| 57 | + $dw = [int]($h * $srcAR) |
| 58 | + } |
| 59 | + $dx = [int](($w - $dw) / 2) |
| 60 | + $dy = [int](($h - $dh) / 2) |
| 61 | + |
| 62 | + $bmp = New-Object System.Drawing.Bitmap $w, $h, ([System.Drawing.Imaging.PixelFormat]::Format32bppArgb) |
| 63 | + $g = [System.Drawing.Graphics]::FromImage($bmp) |
| 64 | + $g.Clear([System.Drawing.Color]::Transparent) |
| 65 | + $g.InterpolationMode = [System.Drawing.Drawing2D.InterpolationMode]::HighQualityBicubic |
| 66 | + $g.SmoothingMode = [System.Drawing.Drawing2D.SmoothingMode]::HighQuality |
| 67 | + $g.PixelOffsetMode = [System.Drawing.Drawing2D.PixelOffsetMode]::HighQuality |
| 68 | + $rect = New-Object System.Drawing.Rectangle $dx, $dy, $dw, $dh |
| 69 | + $g.DrawImage($src, $rect) |
| 70 | + $g.Dispose() |
| 71 | + |
| 72 | + $bmp.Save($f.FullName, [System.Drawing.Imaging.ImageFormat]::Png) |
| 73 | + |
| 74 | + # If too big, re-save as indexed with progressively smaller palettes |
| 75 | + $size = (Get-Item $f.FullName).Length |
| 76 | + $palettes = @( |
| 77 | + [System.Windows.Media.Imaging.BitmapPalettes]::Halftone256Transparent, |
| 78 | + [System.Windows.Media.Imaging.BitmapPalettes]::Halftone125Transparent, |
| 79 | + [System.Windows.Media.Imaging.BitmapPalettes]::Halftone64Transparent, |
| 80 | + [System.Windows.Media.Imaging.BitmapPalettes]::Halftone27Transparent, |
| 81 | + [System.Windows.Media.Imaging.BitmapPalettes]::Halftone8Transparent |
| 82 | + ) |
| 83 | + if ($size -gt $MaxBytes) { |
| 84 | + foreach ($pal in $palettes) { |
| 85 | + $ms2 = New-Object System.IO.MemoryStream |
| 86 | + $bmp.Save($ms2, [System.Drawing.Imaging.ImageFormat]::Png) |
| 87 | + $ms2.Position = 0 |
| 88 | + $bs2 = [System.Windows.Media.Imaging.BitmapFrame]::Create($ms2, |
| 89 | + [System.Windows.Media.Imaging.BitmapCreateOptions]::PreservePixelFormat, |
| 90 | + [System.Windows.Media.Imaging.BitmapCacheOption]::OnLoad) |
| 91 | + $conv = New-Object System.Windows.Media.Imaging.FormatConvertedBitmap |
| 92 | + $conv.BeginInit() |
| 93 | + $conv.Source = $bs2 |
| 94 | + $conv.DestinationFormat = [System.Windows.Media.PixelFormats]::Indexed8 |
| 95 | + $conv.DestinationPalette = $pal |
| 96 | + $conv.EndInit() |
| 97 | + $enc = New-Object System.Windows.Media.Imaging.PngBitmapEncoder |
| 98 | + $enc.Frames.Add([System.Windows.Media.Imaging.BitmapFrame]::Create($conv)) |
| 99 | + $fs = [System.IO.File]::Open($f.FullName, [System.IO.FileMode]::Create) |
| 100 | + $enc.Save($fs) |
| 101 | + $fs.Dispose() |
| 102 | + $ms2.Dispose() |
| 103 | + $size = (Get-Item $f.FullName).Length |
| 104 | + if ($size -le $MaxBytes) { break } |
| 105 | + } |
| 106 | + if ($size -gt $MaxBytes) { |
| 107 | + Write-Warning "$($f.Name) still $size bytes after all palette reductions" |
| 108 | + } |
| 109 | + } |
| 110 | + $bmp.Dispose() |
| 111 | +} |
| 112 | + |
| 113 | +$src.Dispose() |
| 114 | +Write-Host "Done." |
0 commit comments