lugia19

SideStoryUpscaler source code

Jan 4th, 2020
203
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 23.06 KB | None | 0 0
  1. Imports System.Drawing
  2. Imports System.IO
  3. Imports System.Threading
  4.  
  5.  
  6. Module Module1
  7. Dim Logs As StreamWriter = My.Computer.FileSystem.OpenTextFileWriter(Environment.CurrentDirectory & "\SSU-log.txt", True, System.Text.Encoding.UTF8)
  8.  
  9. Sub Main()
  10. Logs.AutoFlush = True
  11. Dim choice, ScaleFraction As String
  12. Dim ScaleFactor As Double
  13. Dim Failsafe As Boolean
  14. Dim cudnn As Boolean = False
  15. Dim mode As String
  16. Logs.WriteLine(vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & DateTime.Now.ToShortDateString & " - " & Now.TimeOfDay.ToString & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf & vbCrLf)
  17. Console.WriteLine("Waifu2x (which is used to upscale the images) requires the Microsoft Visual C++ 2015 Redistributable Update 3")
  18. Console.WriteLine("If you don't have it installed (or if you don't know) just run the vc.exe file that's in this same folder.")
  19. Console.WriteLine("Press Enter to continue.")
  20. Console.ReadLine()
  21. Do
  22. Console.WriteLine("Please enter the number corresponding to your resolution then press Enter to confirm.")
  23. Console.WriteLine("1) 1080p")
  24. Console.WriteLine("2) 720p")
  25. choice = Console.ReadLine()
  26. If choice = "1" Then
  27. ScaleFactor = 1.8
  28. ScaleFraction = "18/10"
  29. Console.Clear()
  30. Exit Do
  31. ElseIf choice = "2" Then
  32. ScaleFactor = 1.5
  33. ScaleFraction = "18/10"
  34. Console.Clear()
  35. Exit Do
  36. End If
  37. Console.Clear()
  38. Loop
  39.  
  40. Do
  41. Console.WriteLine("Use GPU acceleration? (Note: Requires an Nvidia GPU and CUDA, run cuda.exe to install it)")
  42. Console.WriteLine("1) Yes")
  43. Console.WriteLine("2) No")
  44. choice = Console.ReadLine()
  45. If choice = "1" Then
  46. Failsafe = False
  47. Console.Clear()
  48. Exit Do
  49. ElseIf choice = "2" Then
  50. Failsafe = True
  51. Console.Clear()
  52. Exit Do
  53. End If
  54. Console.Clear()
  55. Loop
  56.  
  57. If Command() <> "" Then
  58. If Command().Contains("/cudnn") Then
  59. cudnn = True
  60. Console.WriteLine("CUDNN acceleration enabled.")
  61. End If
  62. End If
  63.  
  64. If cudnn Then
  65. mode = "cudnn"
  66. ElseIf Failsafe Then
  67. mode = "cpu"
  68. Else
  69. mode = "gpu"
  70. End If
  71.  
  72. Dim di As New DirectoryInfo(Environment.CurrentDirectory)
  73. Dim count As Integer = 0
  74. For Each subfolder As DirectoryInfo In di.GetDirectories("*", SearchOption.AllDirectories)
  75. count = 0
  76. Dim fiArr As FileInfo() = subfolder.GetFiles()
  77. If Not (File.Exists(subfolder.FullName & "\upscaled")) And Not (subfolder.FullName.Contains("Magick")) Then
  78. Dim backgrounds As New List(Of String)
  79.  
  80. If File.Exists(subfolder.FullName & "\story.ini") Then
  81. Dim text() As String = File.ReadAllLines(subfolder.FullName & "\story.ini")
  82. For i = 0 To text.Length - 1
  83. text(i) = text(i).Replace(".bmp", ".png").Replace(".jpg", ".png").Replace(".jpeg", ".png").Replace("�@", " ")
  84.  
  85. If text(i).Contains("bg,") And text(i).Contains(".") Then
  86. backgrounds.Add(text(i).Substring(text(i).IndexOf(",") + 1, text(i).IndexOf(".", text(i).IndexOf(",") + 1) - text(i).IndexOf(",") - 1))
  87. End If
  88.  
  89. If text(i).Contains("haikei") And text(i).Contains(".") Then
  90. backgrounds.Add(text(i).Substring(text(i).IndexOf("""") + 1, text(i).IndexOf(".") - text(i).IndexOf("""") - 1))
  91. End If
  92. Try
  93. If text(i).Substring(0, 4) = "name" Then
  94. If text(i).Chars(text(i).Length - 2) = " " Then
  95. text(i) = text(i).Substring(0, text(i).LastIndexOf(" ")) & """"
  96. End If
  97. End If
  98. Catch ex As Exception
  99.  
  100. End Try
  101.  
  102. Next
  103.  
  104. backgrounds = backgrounds.Distinct.ToList()
  105.  
  106. File.Delete(subfolder.FullName & "\story.ini")
  107. File.WriteAllLines(subfolder.FullName & "\story.ini", text)
  108. End If
  109.  
  110.  
  111. For Each fri In fiArr
  112. If fri.IsReadOnly Then
  113. fri.IsReadOnly = False
  114. End If
  115. count = count + 1
  116. If backgrounds.Contains(fri.Name.Substring(0, fri.Name.Length - 4)) Then
  117. RunCommandH("""" & Environment.CurrentDirectory & "\waifu2x\waifu2x-caffe-cui.exe""", "-p " & mode & " -s " & ScaleFactor & " --Model_dir """ & Environment.CurrentDirectory & "\waifu2x\models\anime_style_art_rgb"" -i """ & fri.FullName & """ -o """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  118. If fri.Extension <> ".png" Then
  119. fri.Delete()
  120. End If
  121. Else
  122. If MaskCheck(fri) Then
  123. SplitAndMerge(fri)
  124. RunCommandH("""" & Environment.CurrentDirectory & "\waifu2x\waifu2x-caffe-cui.exe""", "-p " & mode & " -s " & ScaleFactor & " --Model_dir """ & Environment.CurrentDirectory & "\waifu2x\models\anime_style_art_rgb"" -i """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""" & " -o """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  125. If fri.Extension <> ".png" Then
  126. fri.Delete()
  127. End If
  128.  
  129.  
  130. ElseIf HasTransparency(fri) Then
  131. RunCommandH("""" & Environment.CurrentDirectory & "\waifu2x\waifu2x-caffe-cui.exe""", "-p " & mode & " -s " & ScaleFactor & " --Model_dir """ & Environment.CurrentDirectory & "\waifu2x\models\anime_style_art_rgb"" -i """ & fri.FullName & """ -o """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  132. If fri.Extension <> ".png" Then
  133. fri.Delete()
  134. End If
  135.  
  136. ElseIf BGCheck(fri).Bool Then
  137. Dim bmp As New Bitmap(fri.FullName)
  138. Dim x, y As Double
  139. y = 0
  140. If BGCheck(fri).side = "right" Then
  141. x = bmp.Width - 1
  142. ElseIf BGCheck(fri).side = "left" Then
  143. x = 0
  144. End If
  145. Dim color As String = GetRGB(fri.FullName, x, y)
  146. bmp.Dispose()
  147. RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert """ & fri.FullName & """ -fuzz 0% -transparent """ & color & """ """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  148.  
  149. fri = New FileInfo(fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png")
  150. RunCommandH("""" & Environment.CurrentDirectory & "\waifu2x\waifu2x-caffe-cui.exe""", "-p " & mode & " -s " & ScaleFactor & " --Model_dir """ & Environment.CurrentDirectory & "\waifu2x\models\anime_style_art_rgb"" -i """ & fri.FullName & """ -o """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  151. If fri.Extension <> ".png" Then
  152. fri.Delete()
  153. End If
  154. ElseIf fri.Extension = ".png" Or fri.Extension = ".jpg" Or fri.Extension = ".jpeg" Or fri.Extension = ".bmp" Then
  155. Dim answer As Integer = 0
  156. Do Until answer = 1 Or answer = 2 Or answer = 3
  157. Console.Clear()
  158. Console.WriteLine("Does the image have (write the number corresponding to your answer):")
  159. Console.WriteLine("1) A right side that functions as a mask (green except for where the character/CG is)")
  160. Console.WriteLine("2) Some color it uses to distinguish the background from the character (if it seemingly has no background, it's probably using white as the color)")
  161. Console.WriteLine("3) Neither, it's a background or a full screen CG/image (it should have the background included in the image)")
  162. Console.WriteLine("You can find examples of all of these under the ""Sample"" folder.")
  163. Console.WriteLine("Note: The whitespace surrounding the image is not a mask or anything of the sort, it's simply empty space. You can find an example of that in the folder as well.")
  164. If Process.GetProcessesByName("IMDisplay").Length < 1 Then
  165. RunCommandNW("""" & Environment.CurrentDirectory & "\ImageMagick\IMDisplay.exe""", """" & fri.FullName & """")
  166. End If
  167.  
  168.  
  169. Try
  170. answer = Console.ReadLine()
  171. Catch ex As Exception
  172.  
  173. End Try
  174. Loop
  175. Console.Clear()
  176.  
  177. Try
  178. For Each p As Process In Process.GetProcessesByName("IMDisplay")
  179. p.Kill()
  180. Next
  181. Catch ex As Exception
  182.  
  183. End Try
  184.  
  185.  
  186. If answer = 1 Then
  187. SplitAndMerge(fri)
  188. If fri.Extension <> ".png" Then
  189. fri.Delete()
  190. End If
  191. fri = New FileInfo(fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png")
  192. ElseIf answer = 2 Then
  193. answer = 0
  194. Do
  195. Console.Clear()
  196. Console.WriteLine("Is the background color present in:")
  197. Console.WriteLine("1) The upper left corner")
  198. Console.WriteLine("2) The upper right corner")
  199. Console.WriteLine("If it's in both just choose one, it doesn't matter.")
  200. Console.WriteLine("If it's in neither, come tell me in the NG+ discord because it means I don't actually get how it works.")
  201. If Process.GetProcessesByName("IMDisplay").Length < 1 Then
  202. RunCommandNW("""" & Environment.CurrentDirectory & "\ImageMagick\IMDisplay.exe""", """" & fri.FullName & """")
  203. End If
  204.  
  205. Try
  206. answer = Console.ReadLine
  207.  
  208. Catch ex As Exception
  209.  
  210. End Try
  211. Loop Until answer = 1 Or answer = 2
  212. Console.Clear()
  213.  
  214. Try
  215. For Each p As Process In Process.GetProcessesByName("IMDisplay")
  216. p.Kill()
  217. Next
  218. Catch ex As Exception
  219.  
  220. End Try
  221.  
  222.  
  223. Dim bmp As New Bitmap(fri.FullName)
  224. Dim x, y As Double
  225. y = 0
  226. If answer = 1 Then
  227. x = 0
  228. ElseIf answer = 2 Then
  229. x = bmp.Width - 1
  230. End If
  231. Dim color As String = GetRGB(fri.FullName, x, y)
  232. bmp.Dispose()
  233. RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert """ & fri.FullName & """ -fuzz 0% -transparent """ & color & """ """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  234. fri.Delete()
  235. fri = New FileInfo(fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png")
  236. End If
  237. RunCommandH("""" & Environment.CurrentDirectory & "\waifu2x\waifu2x-caffe-cui.exe""", "-p " & mode & " -s " & ScaleFactor & " --Model_dir """ & Environment.CurrentDirectory & "\waifu2x\models\anime_style_art_rgb"" -i """ & fri.FullName & """ -o """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  238.  
  239. If fri.Extension <> ".png" Then
  240. fri.Delete()
  241. End If
  242. End If
  243. End If
  244. Console.WriteLine("File " & count + 1 & " out of " & fiArr.GetLength(0) + 1 & " completed")
  245. Next fri
  246. File.WriteAllText(subfolder.FullName & "\upscaled", "Already upscaled. Ignore this folder.")
  247. Console.WriteLine("Folder " & subfolder.Name & " completed")
  248. End If
  249. Next
  250. If File.Exists(Environment.CurrentDirectory & "\disclaimershown.ini") Then
  251. File.Delete(Environment.CurrentDirectory & "\disclaimershown.ini")
  252. End If
  253.  
  254. File.WriteAllText(Environment.CurrentDirectory & "\disclaimershown.ini", "[disclaimer]" & vbCrLf & "shown=""true""")
  255. Logs.Close()
  256. End Sub
  257.  
  258.  
  259. Function BGCheck(fri As FileInfo) As CustomType
  260. Dim result As New CustomType
  261. If fri.Extension <> ".png" And fri.Extension <> ".jpg" And fri.Extension <> ".jpeg" And fri.Extension <> ".bmp" Then
  262. result.Bool = False
  263. result.side = ""
  264. Return result
  265. End If
  266. Dim bmp As New Bitmap(fri.FullName)
  267.  
  268. Dim c(4) As String
  269. c(0) = GetRGB(fri.FullName, 0, 0)
  270. c(1) = GetRGB(fri.FullName, 0, bmp.Height - 1)
  271. c(2) = GetRGB(fri.FullName, bmp.Width - 1, bmp.Height - 1)
  272. c(3) = GetRGB(fri.FullName, bmp.Width - 1, 0)
  273. Dim counter As Integer = 0
  274.  
  275. For i = 0 To 4
  276. counter = 0
  277. For j = 0 To 4
  278. If i = j Then
  279. j = j + 1
  280. Else
  281. If c(i) = c(j) Then
  282. counter = counter + 1
  283. If counter = 2 Then
  284.  
  285. result.Bool = True
  286. If c(i) = c(0) Then
  287. result.side = "left"
  288. ElseIf c(i) = c(3) Then
  289. result.side = "right"
  290. End If
  291. bmp.Dispose()
  292. Return result
  293. End If
  294. End If
  295. End If
  296. Next
  297. Next
  298. bmp.Dispose()
  299. result.Bool = False
  300. result.side = ""
  301. Return result
  302.  
  303. End Function
  304.  
  305. Class CustomType
  306. Public Bool As Boolean
  307. Public side As String
  308. End Class
  309.  
  310. Function MaskCheck(fri As FileInfo) As Boolean
  311.  
  312. If fri.Extension <> ".bmp" Then
  313. Return False
  314. End If
  315.  
  316. If fri.FullName.Contains("black.bmp") Or fri.FullName.Contains("Marco1") Then
  317. Return True
  318. End If
  319. Dim bmp As New Bitmap(fri.FullName)
  320. Dim FileNameOnly As String = fri.FullName.Substring(0, fri.FullName.Length - 4)
  321. Dim Corner1, Corner2, Corner3, Corner4 As Boolean
  322.  
  323. If Getcolor(fri.FullName, "Red", 0, 0) = 0 And Getcolor(fri.FullName, "Green", 0, 0) = 255 And Getcolor(fri.FullName, "Blue", 0, 0) = 0 Then
  324. Corner1 = True
  325. Else
  326. Corner1 = False
  327. End If
  328.  
  329. If Getcolor(fri.FullName, "Red", 0, bmp.Height - 1) = 0 And Getcolor(fri.FullName, "Green", 0, bmp.Height - 1) = 255 And Getcolor(fri.FullName, "Blue", 0, bmp.Height - 1) = 0 Then
  330. Corner2 = True
  331. Else
  332. Corner2 = False
  333. End If
  334.  
  335. If Getcolor(fri.FullName, "Red", bmp.Width / 2 - 1, 0) = 0 And Getcolor(fri.FullName, "Green", bmp.Width / 2 - 1, 0) = 255 And Getcolor(fri.FullName, "Blue", bmp.Width / 2 - 1, 0) = 0 Then
  336. Corner3 = True
  337. Else
  338. Corner3 = False
  339. End If
  340.  
  341. If Getcolor(fri.FullName, "Red", bmp.Width / 2 - 1, bmp.Height - 1) = 0 And Getcolor(fri.FullName, "Green", bmp.Width / 2 - 1, bmp.Height - 1) = 255 And Getcolor(fri.FullName, "Blue", bmp.Width / 2 - 1, bmp.Height - 1) = 0 Then
  342. Corner4 = True
  343. Else
  344. Corner4 = False
  345. End If
  346.  
  347. bmp.Dispose()
  348. If Corner1 Or Corner2 Or Corner3 Or Corner4 Then
  349. Return True
  350. Else
  351. Return False
  352. End If
  353.  
  354. End Function
  355.  
  356. Function HasTransparency(fri As FileInfo) As Boolean
  357. If fri.Extension <> ".png" And fri.Extension <> ".bmp" Then
  358. Return False
  359. End If
  360. Dim output As String = RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert """ & fri.FullName & """ -verbose info:")
  361. Try
  362. output = output.Substring(output.IndexOf("Channel statistics:"), output.IndexOf("Image statistics:") - output.IndexOf("Channel statistics:"))
  363. Catch ex As Exception
  364. Return False
  365. End Try
  366.  
  367.  
  368. If output.Contains("Alpha") Then
  369. output = output.Substring(output.IndexOf("Alpha"), output.LastIndexOf("kurtosis") - output.IndexOf("Alpha"))
  370.  
  371. Dim sd As Double
  372. Try
  373. sd = Convert.ToDouble(output.Substring(output.IndexOf("standard deviation:") + 19, output.LastIndexOf("(") - output.IndexOf("standard deviation:") - 19))
  374. Catch ex As Exception
  375. Try
  376. Dim min, max, mean As Double
  377. min = Convert.ToDouble(output.Substring(output.IndexOf("min:") + 4, output.IndexOf("(", output.IndexOf("min:")) - output.IndexOf("min:") - 4))
  378. max = Convert.ToDouble(output.Substring(output.IndexOf("max:") + 4, output.IndexOf("(", output.IndexOf("min:")) - output.IndexOf("max:") - 4))
  379. mean = Convert.ToDouble(output.Substring(output.IndexOf("mean:") + 5, output.IndexOf("(", output.IndexOf("mean:")) - output.IndexOf("mean:") - 5))
  380. If min = max And max = mean Then
  381. Return False
  382. Else
  383. Return True
  384. End If
  385. Catch ex2 As Exception
  386. Return False
  387. End Try
  388. End Try
  389.  
  390. If sd <> 0 Then
  391. Return True
  392. Else
  393. Return False
  394. End If
  395. Else
  396. Return False
  397. End If
  398.  
  399. End Function
  400.  
  401.  
  402.  
  403. Sub SplitAndMerge(fri As FileInfo)
  404. Dim filenameonly As String = fri.FullName.Substring(0, fri.FullName.Length - 4)
  405. RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert -crop 50%x100% """ & fri.FullName & """ """ & filenameonly & "_%d.png""")
  406. RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert """ & filenameonly & "_0.png"" ( """ & filenameonly & "_1.png"" -colorspace gray -threshold 0 -alpha off -negate ) -compose copy-opacity -composite """ & fri.FullName.Substring(0, fri.FullName.Length - 4) & ".png""")
  407.  
  408. File.Delete(filenameonly & "_1.png")
  409. File.Delete(filenameonly & "_0.png")
  410. If fri.Extension <> ".png" Then
  411. File.Delete(fri.FullName)
  412. End If
  413. End Sub
  414.  
  415. Function Getcolor(File As String, Color As String, X As Double, Y As Double) As Int32
  416. Select Case Color
  417. Case "Red"
  418. Color = "r"
  419. Case "Green"
  420. Color = "g"
  421. Case "Blue"
  422. Color = "b"
  423. End Select
  424. Return Convert.ToInt32(RunCommandH("""" & Environment.CurrentDirectory & "\ImageMagick\magick.exe""", "convert """ & File & """[1x1+" & X & "+" & Y & "] -format ""%[fx: floor(255 * u." & Color & ")]"" info:"))
  425. End Function
  426.  
  427. Function GetRGB(File As String, X As Double, Y As Double) As String
  428. Dim r, g, b As Integer
  429. r = Getcolor(File, "Red", X, Y)
  430. g = Getcolor(File, "Green", X, Y)
  431. b = Getcolor(File, "Blue", X, Y)
  432. Return "rgb(" & r & "," & g & "," & b & ")"
  433. End Function
  434.  
  435. Function RunCommandH(Command As String, Arguments As String) As String
  436. 'Console.WriteLine(Command)
  437. Logs.WriteLine(Command & Arguments)
  438. 'Console.ReadLine()
  439. Dim oProcess As New Process()
  440. Dim oStartInfo As New ProcessStartInfo(Command, Arguments)
  441. oStartInfo.UseShellExecute = False
  442. oStartInfo.RedirectStandardOutput = True
  443. oProcess.StartInfo = oStartInfo
  444. Try
  445. oProcess.Start()
  446.  
  447. Catch ex As Exception
  448. File.WriteAllText(Environment.CurrentDirectory & "crashreport.txt", ex.ToString)
  449. Console.WriteLine("Something went wrong.")
  450. Console.WriteLine("Please message me on discord (you can find me on the NG+ server, check ecstasywastaken.blogspot.com for the link) and send me the crashreport.txt file.")
  451. Console.ReadLine()
  452. End Try
  453.  
  454.  
  455.  
  456. Dim sOutput As String
  457. Using oStreamReader As System.IO.StreamReader = oProcess.StandardOutput
  458. sOutput = oStreamReader.ReadToEnd()
  459. End Using
  460. If Not Arguments.Contains("verbose") Or Arguments.Contains("[1x1") Then
  461. Logs.Writeline(sOutput)
  462. 'Console.WriteLine(sOutput)
  463. End If
  464.  
  465.  
  466. oProcess.WaitForExit()
  467. Return sOutput
  468. oProcess.WaitForExit()
  469.  
  470. oProcess.Dispose()
  471.  
  472.  
  473.  
  474. End Function
  475.  
  476. Sub RunCommandNW(Command As String, Arguments As String)
  477. 'Console.WriteLine(Command)
  478. 'Console.ReadLine()
  479. Dim oProcess As New Process()
  480. Dim oStartInfo As New ProcessStartInfo(Command, Arguments)
  481. oStartInfo.UseShellExecute = False
  482. oStartInfo.RedirectStandardOutput = True
  483. oProcess.StartInfo = oStartInfo
  484. oProcess.Start()
  485.  
  486. oProcess.Dispose()
  487. End Sub
  488.  
  489. End Module
Add Comment
Please, Sign In to add comment