-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgif.ps1
More file actions
208 lines (180 loc) · 7.64 KB
/
Copy pathgif.ps1
File metadata and controls
208 lines (180 loc) · 7.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
[CmdletBinding()]
param(
[string]$ImagePath,
[int]$alphathreshold = 50,
[int]$COLUMNS = 35,
[int]$headerHeight = 2,
[int]$leftPadding = 1,
[switch] $ascii,
[bool] $resetCursorAtEnd = $true,
[single] $frameDelayScale = 1,
[int] $repeatAnimationCount = 1,
[string][ValidateSet("console", "script", "json")] $OutputKind = "console" # Valid values: console, script, json
)
function GifBitmapToAscii {
param(
$RawImage,
[int]$alphathreshold,
[int]$COLUMNS,
[switch] $ascii
);
$e = [char]0x1B;
# Divide scaled height by 2.2 to compensate for ASCII characters being taller than they are wide
[int]$ROWS = $RawImage.Height / $RawImage.Width * $COLUMNS / $(if ($ascii) { 2.2 } else { 1 })
$ScaledImage = New-Object System.Drawing.Bitmap @($RawImage, [Drawing.Size]"$COLUMNS,$ROWS")
if ($ascii) {
$chars = ' .,:;+iIH$@'
for ($i = 0; $i -lt $ScaledImage.Height; $i++) {
$currline = ""
for ($j = 0; $j -lt $ScaledImage.Width; $j++) {
$p = $ScaledImage.GetPixel($j, $i)
$currline += "$e[38;2;$($p.R);$($p.G);$($p.B)m$($chars[[math]::Floor($p.GetBrightness() * $chars.Length)])$e[0m"
}
$currline
}
} else {
for ($i = 0; $i -lt $ScaledImage.Height; $i += 2) {
$currline = ""
for ($j = 0; $j -lt $ScaledImage.Width; $j++) {
$pixel1 = $ScaledImage.GetPixel($j, $i)
$char = [char]0x2580
if ($i -ge $ScaledImage.Height - 1) {
if ($pixel1.A -lt $alphathreshold) {
$char = [char]0x2800
$ansi = "$e[49m"
} else {
$ansi = "$e[38;2;$($pixel1.R);$($pixel1.G);$($pixel1.B)m"
}
} else {
$pixel2 = $ScaledImage.GetPixel($j, $i + 1)
if ($pixel1.A -lt $alphathreshold -or $pixel2.A -lt $alphathreshold) {
if ($pixel1.A -lt $alphathreshold -and $pixel2.A -lt $alphathreshold) {
$char = [char]0x2800
$ansi = "$e[49m"
} elseif ($pixel1.A -lt $alphathreshold) {
$char = [char]0x2584
$ansi = "$e[49;38;2;$($pixel2.R);$($pixel2.G);$($pixel2.B)m"
} else {
$ansi = "$e[49;38;2;$($pixel1.R);$($pixel1.G);$($pixel1.B)m"
}
} else {
$ansi = "$e[38;2;$($pixel1.R);$($pixel1.G);$($pixel1.B);48;2;$($pixel2.R);$($pixel2.G);$($pixel2.B)m"
}
}
$currline += "$ansi$char$e[0m"
}
$currline
}
}
$ScaledImage.Dispose()
$RawImage.Dispose()
}
function WriteOutputLine {
param($textLine);
# Create a string with leftPadding number of spaces called leftPaddingString
$leftPaddingString = " " * $leftPadding;
if ($OutputKind -eq "console") {
$textLine -split "`n" | ForEach-Object {
"$leftPaddingString$_";
};
}
else {
$textLine -split "`n" | ForEach-Object {
# OutputKind is script
# Write asciiText out as a ' string with ' escaped
$escapedString = $_ -replace "'", "''" # Escape single quotes
"'$leftPaddingString$escapedString'"
}
}
}
function GifToAscii {
param(
[string]$ImagePath,
[int]$alphathreshold,
[int]$COLUMNS,
[switch] $ascii,
[single] $frameDelayScale,
[switch] $resetCursorAtEnd
);
Add-Type -AssemblyName 'System.Drawing'
$RawImage = if (Test-Path $ImagePath -PathType Leaf) {
[Drawing.Bitmap]::FromFile((Resolve-Path $ImagePath))
} else {
throw "Unsupported ImagePath type: $ImagePath";
}
$frameDimension = New-Object System.Drawing.Imaging.FrameDimension($RawImage.FrameDimensionsList[0]);
$frameCount = $RawImage.GetFrameCount($frameDimension);
if ($OutputKind -eq "json") {
[int]$ROWS = $RawImage.Height / $RawImage.Width * $COLUMNS / $(if ($ascii) { 2.2 } else { 1 })
# In block-character mode each pair of pixel rows becomes one character row
[int]$charRows = if ($ascii) { $ROWS } else { [Math]::Ceiling($ROWS / 2.0) }
$frameDelays = for ($i = 0; $i -lt $frameCount; $i++) {
[void]$RawImage.SelectActiveFrame($frameDimension, $i)
$propertyItem = $RawImage.GetPropertyItem(0x5100)
$rawDelay = ($propertyItem.Value[0] + $propertyItem.Value[1] * 256) / 100
if ($rawDelay -le 0) { $rawDelay = 0.01 }
[float]($rawDelay * $frameDelayScale)
}
$jsonData = [ordered]@{
name = [System.IO.Path]::GetFileNameWithoutExtension($ImagePath)
frameCount = $frameCount
widthInPixels = $RawImage.Width
heightInPixels = $RawImage.Height
widthInCharacters = [ordered]@{
withoutPadding = $COLUMNS
withPadding = $COLUMNS + $leftPadding
}
heightInCharacters = [ordered]@{
withoutPadding = $charRows
withPadding = $charRows + $headerHeight
}
ascii = $ascii.IsPresent
alphaThreshold = $alphathreshold
frameDelayScale = $frameDelayScale
}
$outputPath = [System.IO.Path]::ChangeExtension((Resolve-Path $ImagePath).Path, "json")
$jsonData | ConvertTo-Json | Set-Content -Path $outputPath -Encoding UTF8
return;
}
# Save cursor position at start of writing image
# "$e[s";
# Hide the cursor
WriteOutputLine "$e[?25l";
for ($frameIdx = 0; $frameIdx -lt $frameCount * $repeatAnimationCount; $frameIdx++) {
# Restore cursor position before writing image
# "$e[u";
$i = $frameIdx % $frameCount;
for ($headerLineIdx = 0; $headerLineIdx -lt $headerHeight; $headerLineIdx++) {
WriteOutputLine "";
}
[void]$RawImage.SelectActiveFrame($frameDimension, $i);
$propertyItem = $RawImage.GetPropertyItem(0x5100);
$delay = ($propertyItem.Value[0] + $propertyItem.Value[1] * 256) / 100;
# $delay = [System.BitConverter]::ToUInt16($RawImage.GetPropertyItem(0x5100).Value, $i * 4) / 1000;
if ($delay -le 0) {
$delay = 0.01;
}
$delay = $delay * $frameDelayScale;
$clone = $RawImage.Clone();
$asciiText = GifBitmapToAscii -RawImage $clone -alphathreshold $alphathreshold -COLUMNS $COLUMNS -ascii:$ascii;
WriteOutputLine ($leftPaddingString + $asciiText);
$heightInLines = $asciiText.length + 1 + $headerHeight;
# Get the pause time for the current animation frame
# Pause for the delay time
if ($OutputKind -eq "console") {
Start-Sleep -Seconds $delay;
} else {
# Script output so we print out the command to sleep
"Start-Sleep -Seconds $delay;"
}
if ($i -lt $frameCount - 1 -or $resetCursorAtEnd) {
# Move back to the start of the previous frame to overwrite it.
WriteOutputLine "$e[${heightInLines}F$e[0G";
}
}
# Save cursor position at end of writing image so we don't overwrite it anymore
# "$e[s";
# Display the cursor
WriteOutputLine "$e[?25h";
}
GifToAscii -ImagePath $ImagePath -alphathreshold $alphathreshold -COLUMNS $COLUMNS -ascii:$ascii -resetCursorAtEnd:$resetCursorAtEnd -frameDelayScale:$frameDelayScale;