Advertisement
Guest User

Glev Load Screen Multithreaded

a guest
Mar 19th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.21 KB | None | 0 0
  1. function GlevConvertSequential() {
  2. #harcoding some stuff that's needed
  3. $script:maxw=3840
  4. $ref = "C:\users\USERNAME\desktop\zzGlev\Data\Tools\GlevLoadScreenMaker\reference\loadscreenref_"
  5.  
  6. #Gets all files that match Stuff (.*) a period (\.) image extensions (jpg|jpeg|...)
  7. #Grave accent (`) just lets you space things across multiple lines so it's easier to read. if you get really long commands
  8. $imgs = Get-ChildItem -Path "C:\Users\USERNAME\Desktop\IMGs\static" | Where-Object {$_.Name -match ".*\.(jpg|jpeg|jif|jiff|gif|png|tif|tiff)"} | `
  9. Select BaseName,FullName,Extension
  10. $outfolder = "C:\Users\USERNAME\Desktop\zzOutput"
  11. $identloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\identifyx64.exe"
  12. $convertloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\convertx64.exe"
  13.  
  14. foreach ($file in $imgs) {
  15. #Instead of building your own random filenames, you could use GUIDs. Almost impossible to get duplicates, and just one line
  16. #and built in to windows through .Net libraries
  17. $guidName = [guid]::NewGuid().ToString()
  18. $newName = $guidname + $file.Extension
  19.  
  20. #Note that if you are testing this in ISE, Start-Process is bugged and doesn't actually return the output to console
  21. #The below doesn't work in ISE, but would work if just running as a script
  22. #$output = Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait
  23.  
  24. Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait -RedirectStandardOutput "$env:temp\$guidName.txt"
  25.  
  26. $output = Get-Content "$env:temp\$guidName.txt"
  27. Remove-Item "$env:temp\$guidName.txt"
  28.  
  29. #No real changes here
  30. $srcimgsize = $output.Split('x')
  31. [int]$imgw = $srcimgsize[0]
  32. [int]$imgh = $srcimgsize[1]
  33. $offstw = 0
  34. $offsth = 82 #WAS 75, but 82 seems perfect
  35. $rezw = 960
  36. $rezh = 540
  37. while ($imgw -gt $rezw -Or $imgh -gt $rezh){
  38. if($rezw -eq $script:maxw ){
  39. break
  40. }
  41. $rezw = $rezw * 2
  42. $rezh = $rezh * 2
  43. if ($offstw -eq 0){
  44. $offstw = $offstw +1
  45. }
  46. else{
  47. $offstw = $offstw * 2
  48. }
  49. $offsth = $offsth * 2
  50. }
  51. [string]$rez = [string]$rezw + 'x' + [string]$rezh
  52. [string]$offset ='+'+[string]$offstw+'-'+[string]$offsth
  53. write-host 'Resizing to :'$rez'...'
  54. #Double quote strings will auto-expand variables, you don't need to add them, unless they aren't strings, or they are objects
  55. $pngref = "$ref$rez.png"
  56.  
  57. #Save our temporary jpg to the actual temp directory, so if we fail to delete it, Windows will eventually clean it up
  58. #Using $($variable.Property) allows you to expand object properties inside of a double quoted string. Looks kind of ugly, but if you
  59. #have multiple strings in a string, can make it easier to just be one line.
  60. #Also, when passing anything file related, assuming there will be spaces, so make sure you escape the inner quotes, so when you pass the
  61. #filepath + file to imagemagick, it's surrounded by double quotes (single quotes won't work)
  62. Start-Process -FilePath $convertloc -ArgumentList "-resize $rez `"$($file.FullName)`" `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  63.  
  64. write-host 'Creating reference image...'
  65. Start-Process -FilePath $convertloc -ArgumentList "`"$pngref`" `"$env:temp\$guidName.jpg`" -gravity center -geometry $offset -compose over -composite `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  66.  
  67. write-host 'Converting to DDS...'
  68. #Define the compression type once, change if you have compression, then use that string
  69. #Rather than call two different functions in an if statement taht do basically the same thing
  70. $compression = "dds:compression=none"
  71. if ($compress) {
  72. $compression = "dds:compression=a8r8g8b8"
  73. }
  74. Start-Process -FilePath $convertloc -ArgumentList "-format dds -define dds:mipmaps=0 -define $compression -define dds:cluster-fit=true `"$env:temp\$guidName.jpg`" `"$outfolder\$guidName.dds`"" -NoNewWindow -Wait
  75. Remove-Item "$env:temp\$guidName.jpg"
  76. }
  77.  
  78. }
  79.  
  80. #Not mine, from here.
  81. #http://dille.name/blog/2015/09/11/displaying-progress-from-powershell-jobs/
  82. function Show-JobProgress {
  83. [CmdletBinding()]
  84. param(
  85. [Parameter(Mandatory,ValueFromPipeline)]
  86. [ValidateNotNullOrEmpty()]
  87. [System.Management.Automation.Job[]]
  88. $Job
  89. ,
  90. [Parameter()]
  91. [ValidateNotNullOrEmpty()]
  92. [scriptblock]
  93. $FilterScript
  94. )
  95.  
  96. Process {
  97. $Job.ChildJobs | ForEach-Object {
  98. if (-not $_.Progress) {
  99. return
  100. }
  101.  
  102. $LastProgress = $_.Progress
  103. if ($FilterScript) {
  104. $LastProgress = $LastProgress | Where-Object -FilterScript $FilterScript
  105. }
  106.  
  107. $LastProgress | Group-Object -Property Activity,StatusDescription | ForEach-Object {
  108. $_.Group | Select-Object -Last 1
  109.  
  110. } | ForEach-Object {
  111. $ProgressParams = @{}
  112. if ($_.Activity -and $_.Activity -ne $null) { $ProgressParams.Add('Activity', $_.Activity) }
  113. if ($_.StatusDescription -and $_.StatusDescription -ne $null) { $ProgressParams.Add('Status', $_.StatusDescription) }
  114. if ($_.CurrentOperation -and $_.CurrentOperation -ne $null) { $ProgressParams.Add('CurrentOperation', $_.CurrentOperation) }
  115. if ($_.ActivityId -and $_.ActivityId -gt -1) { $ProgressParams.Add('Id', $_.ActivityId) }
  116. if ($_.ParentActivityId -and $_.ParentActivityId -gt -1) { $ProgressParams.Add('ParentId', $_.ParentActivityId) }
  117. if ($_.PercentComplete -and $_.PercentComplete -gt -1) { $ProgressParams.Add('PercentComplete', $_.PercentComplete) }
  118. if ($_.SecondsRemaining -and $_.SecondsRemaining -gt -1) { $ProgressParams.Add('SecondsRemaining', $_.SecondsRemaining) }
  119.  
  120. Write-Progress @ProgressParams
  121. }
  122. }
  123. }
  124. }
  125.  
  126. function GlevConvertmultithread() {
  127. #Gets all files that match Stuff (.*) a period (\.) image extensions (jpg|jpeg|...)
  128. #Grave accent (`) just lets you space things across multiple lines so it's easier to read. if you get really long commands
  129. $imgs = Get-ChildItem -Path "C:\Users\USERNAME\Desktop\IMGs\static" | Where-Object {$_.Name -match ".*\.(jpg|jpeg|jif|jiff|gif|png|tif|tiff)"} | `
  130. Select BaseName,FullName,Extension
  131. foreach ($file in $imgs) {
  132. Start-Job -ScriptBlock {param($fullfilepath, $fileextension)
  133. #Start of Script Block
  134. #harcoding some stuff that's needed
  135. $script:maxw=3840
  136. $ref = "C:\users\USERNAME\desktop\zzGlev\Data\Tools\GlevLoadScreenMaker\reference\loadscreenref_"
  137. $outfolder = "C:\Users\USERNAME\Desktop\zzOutput"
  138. $identloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\identifyx64.exe"
  139. $convertloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\convertx64.exe"
  140.  
  141. Write-Host "Starting Job $jobnum"
  142. #Instead of building your own random filenames, you could use GUIDs. Almost impossible to get duplicates, and just one line
  143. #and built in to windows through .Net libraries
  144. $guidName = [guid]::NewGuid().ToString()
  145. $newName = $guidname + $fileextension
  146.  
  147. #Note that if you are testing this in ISE, Start-Process is bugged and doesn't actually return the output to console
  148. #The below doesn't work in ISE, but would work if just running as a script
  149. #$output = Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait
  150.  
  151. Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$fullfilepath`"" -NoNewWindow -Wait -RedirectStandardOutput "$env:temp\$guidName.txt"
  152.  
  153. $output = Get-Content "$env:temp\$guidName.txt"
  154. Remove-Item "$env:temp\$guidName.txt"
  155.  
  156. #No real changes here
  157. $srcimgsize = $output.Split('x')
  158. [int]$imgw = $srcimgsize[0]
  159. [int]$imgh = $srcimgsize[1]
  160. $offstw = 0
  161. $offsth = 82 #WAS 75, but 82 seems perfect
  162. $rezw = 960
  163. $rezh = 540
  164. while ($imgw -gt $rezw -Or $imgh -gt $rezh){
  165. if($rezw -eq $script:maxw ){
  166. break
  167. }
  168. $rezw = $rezw * 2
  169. $rezh = $rezh * 2
  170. if ($offstw -eq 0){
  171. $offstw = $offstw +1
  172. }
  173. else{
  174. $offstw = $offstw * 2
  175. }
  176. $offsth = $offsth * 2
  177. }
  178. [string]$rez = [string]$rezw + 'x' + [string]$rezh
  179. [string]$offset ='+'+[string]$offstw+'-'+[string]$offsth
  180. #write-host 'Resizing to :'$rez'...'
  181. #Double quote strings will auto-expand variables, you don't need to add them, unless they aren't strings, or they are objects
  182. $pngref = "$ref$rez.png"
  183.  
  184. #Save our temporary jpg to the actual temp directory, so if we fail to delete it, Windows will eventually clean it up
  185. #Using $($variable.Property) allows you to expand object properties inside of a double quoted string. Looks kind of ugly, but if you
  186. #have multiple strings in a string, can make it easier to just be one line.
  187. #Also, when passing anything file related, assuming there will be spaces, so make sure you escape the inner quotes, so when you pass the
  188. #filepath + file to imagemagick, it's surrounded by double quotes (single quotes won't work)
  189. Start-Process -FilePath $convertloc -ArgumentList "-resize $rez `"$fullfilepath`" `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  190.  
  191. #write-host 'Creating reference image...'
  192. Start-Process -FilePath $convertloc -ArgumentList "`"$pngref`" `"$env:temp\$guidName.jpg`" -gravity center -geometry $offset -compose over -composite `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  193.  
  194. #write-host 'Converting to DDS...'
  195. #Define the compression type once, change if you have compression, then use that string
  196. #Rather than call two different functions in an if statement taht do basically the same thing
  197. $compression = "dds:compression=none"
  198. if ($compress) {
  199. $compression = "dds:compression=a8r8g8b8"
  200. }
  201. Start-Process -FilePath $convertloc -ArgumentList "-format dds -define dds:mipmaps=0 -define $compression -define dds:cluster-fit=true `"$env:temp\$guidName.jpg`" `"$outfolder\$guidName.dds`"" -NoNewWindow -Wait
  202. Remove-Item "$env:temp\$guidName.jpg"
  203.  
  204. #End of Job ScriptBLock
  205. Write-Host "Completing Job $jobnum"
  206. } -ArgumentList $file.Fullname,$file.Extension
  207. }
  208.  
  209. #Job Monitoring
  210. While ($(Get-Job -State Running).count -gt 0) {
  211. $ComputersStillRunning = ""
  212. Write-Progress -Activity "Waiting for Jobs to finish" -Status "$($(Get-Job -State Running).count) images remaining" -PercentComplete ($(Get-Job -State Completed).count / $imgs.count * 100)
  213. Start-Sleep -Seconds 3
  214. }
  215. Write-Progress -Activity "Waiting for Jobs to finish" -Status "0 images remaining" -Completed
  216.  
  217. #When done, kill all jobs, just in case
  218. Get-Job | Remove-Job -Force
  219. }
  220.  
  221. Measure-Command {GlevConvertSequential}
  222. #converting 39 images, random sizes, took 210-230 seconds processing sequentially
  223.  
  224. Measure-Command {GlevConvertmultithread}
  225. #converting 39 (of the same) images, took 40-120 seconds processing using jobs, huge differences were probably due to other background stuff
  226.  
  227. #Your mileage may vary
  228.  
  229. #In case you run the script itself for testing, you won't lose the results
  230. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement