Advertisement
Guest User

Glev Load Screen Multithreaded

a guest
Mar 19th, 2018
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.30 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. function GlevConvertmultithread() {
  81. #Gets all files that match Stuff (.*) a period (\.) image extensions (jpg|jpeg|...)
  82. #Grave accent (`) just lets you space things across multiple lines so it's easier to read. if you get really long commands
  83. $imgs = Get-ChildItem -Path "C:\Users\USERNAME\Desktop\IMGs\static" | Where-Object {$_.Name -match ".*\.(jpg|jpeg|jif|jiff|gif|png|tif|tiff)"} | `
  84. Select BaseName,FullName,Extension
  85. foreach ($file in $imgs) {
  86. Start-Job -ScriptBlock {param($fullfilepath, $fileextension)
  87. #Start of Script Block
  88. #harcoding some stuff that's needed
  89. $script:maxw=3840
  90. $ref = "C:\users\USERNAME\desktop\zzGlev\Data\Tools\GlevLoadScreenMaker\reference\loadscreenref_"
  91. $outfolder = "C:\Users\USERNAME\Desktop\zzOutput"
  92. $identloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\identifyx64.exe"
  93. $convertloc = "C:\Users\USERNAME\Desktop\zzGlev\data\Tools\GlevLoadScreenMaker\reference\ImageMagickPortable\convertx64.exe"
  94.  
  95. Write-Host "Starting Job $jobnum"
  96. #Instead of building your own random filenames, you could use GUIDs. Almost impossible to get duplicates, and just one line
  97. #and built in to windows through .Net libraries
  98. $guidName = [guid]::NewGuid().ToString()
  99. $newName = $guidname + $fileextension
  100.  
  101. #Note that if you are testing this in ISE, Start-Process is bugged and doesn't actually return the output to console
  102. #The below doesn't work in ISE, but would work if just running as a script
  103. #$output = Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$($file.FullName)`"" -NoNewWindow -Wait
  104.  
  105. Start-Process -FilePath $identloc -ArgumentList "-format %wx%h `"$fullfilepath`"" -NoNewWindow -Wait -RedirectStandardOutput "$env:temp\$guidName.txt"
  106.  
  107. $output = Get-Content "$env:temp\$guidName.txt"
  108. Remove-Item "$env:temp\$guidName.txt"
  109.  
  110. #No real changes here
  111. $srcimgsize = $output.Split('x')
  112. [int]$imgw = $srcimgsize[0]
  113. [int]$imgh = $srcimgsize[1]
  114. $offstw = 0
  115. $offsth = 82 #WAS 75, but 82 seems perfect
  116. $rezw = 960
  117. $rezh = 540
  118. while ($imgw -gt $rezw -Or $imgh -gt $rezh){
  119. if($rezw -eq $script:maxw ){
  120. break
  121. }
  122. $rezw = $rezw * 2
  123. $rezh = $rezh * 2
  124. if ($offstw -eq 0){
  125. $offstw = $offstw +1
  126. }
  127. else{
  128. $offstw = $offstw * 2
  129. }
  130. $offsth = $offsth * 2
  131. }
  132. [string]$rez = [string]$rezw + 'x' + [string]$rezh
  133. [string]$offset ='+'+[string]$offstw+'-'+[string]$offsth
  134. #write-host 'Resizing to :'$rez'...'
  135. #Double quote strings will auto-expand variables, you don't need to add them, unless they aren't strings, or they are objects
  136. $pngref = "$ref$rez.png"
  137.  
  138. #Save our temporary jpg to the actual temp directory, so if we fail to delete it, Windows will eventually clean it up
  139. #Using $($variable.Property) allows you to expand object properties inside of a double quoted string. Looks kind of ugly, but if you
  140. #have multiple strings in a string, can make it easier to just be one line.
  141. #Also, when passing anything file related, assuming there will be spaces, so make sure you escape the inner quotes, so when you pass the
  142. #filepath + file to imagemagick, it's surrounded by double quotes (single quotes won't work)
  143. Start-Process -FilePath $convertloc -ArgumentList "-resize $rez `"$fullfilepath`" `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  144.  
  145. #write-host 'Creating reference image...'
  146. Start-Process -FilePath $convertloc -ArgumentList "`"$pngref`" `"$env:temp\$guidName.jpg`" -gravity center -geometry $offset -compose over -composite `"$env:temp\$guidName.jpg`"" -NoNewWindow -Wait
  147.  
  148. #write-host 'Converting to DDS...'
  149. #Define the compression type once, change if you have compression, then use that string
  150. #Rather than call two different functions in an if statement taht do basically the same thing
  151. $compression = "dds:compression=none"
  152. if ($compress) {
  153. $compression = "dds:compression=a8r8g8b8"
  154. }
  155. 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
  156. Remove-Item "$env:temp\$guidName.jpg"
  157.  
  158. #End of Job ScriptBLock
  159. Write-Host "Completing Job $jobnum"
  160. } -ArgumentList $file.Fullname,$file.Extension
  161. }
  162.  
  163. #Job Monitoring
  164. #Note: This is also bugged in ISE, only really works if you just run the powershell script.
  165. While ($(Get-Job -State Running).count -gt 0) {
  166. $ComputersStillRunning = ""
  167. 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)
  168. Start-Sleep -Seconds 3
  169. }
  170. Write-Progress -Activity "Waiting for Jobs to finish" -Status "0 images remaining" -Completed
  171.  
  172. #When done, kill all jobs, just in case
  173. Get-Job | Remove-Job -Force
  174. }
  175.  
  176. Measure-Command {GlevConvertSequential}
  177. #converting 39 images, random sizes, took 210-230 seconds processing sequentially
  178.  
  179. Measure-Command {GlevConvertmultithread}
  180. #converting 39 (of the same) images, took 40-120 seconds processing using jobs, huge differences were probably due to other background stuff
  181.  
  182. #Your mileage may vary
  183.  
  184. #In case you run the script itself for testing, you won't lose the results
  185. pause
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement