1
0

iconsmooth_inv.py 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python3
  2. # Copyright (c) 2022 Space Wizards Federation
  3. #
  4. # Permission is hereby granted, free of charge, to any person obtaining a copy
  5. # of this software and associated documentation files (the "Software"), to deal
  6. # in the Software without restriction, including without limitation the rights
  7. # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  8. # copies of the Software, and to permit persons to whom the Software is
  9. # furnished to do so, subject to the following conditions:
  10. #
  11. # The above copyright notice and this permission notice shall be included in all
  12. # copies or substantial portions of the Software.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  19. # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  20. # SOFTWARE.
  21. import PIL
  22. import PIL.Image
  23. import sys
  24. import iconsmooth_lib
  25. if len(sys.argv) != 5:
  26. print("iconsmooth_inv.py INPREFIX METRICS <" + iconsmooth_lib.all_conv + "> out.png")
  27. print("INPREFIX is something like, say, " + iconsmooth_lib.explain_prefix)
  28. print(iconsmooth_lib.explain_mm)
  29. raise Exception("see printed help")
  30. # Input detail configuration
  31. input_prefix = sys.argv[1]
  32. metric_mode = sys.argv[2]
  33. conversion_mode = iconsmooth_lib.conversion_modes[sys.argv[3]]
  34. output_name = sys.argv[4]
  35. tile_w, tile_h, subtile_w, subtile_h, remtile_w, remtile_h = iconsmooth_lib.parse_metric_mode(metric_mode)
  36. # Source loading
  37. tiles = []
  38. for j in range(8):
  39. src_img = PIL.Image.open(input_prefix + str(j) + ".png")
  40. tile_a = PIL.Image.new("RGBA", (remtile_w, remtile_h))
  41. tile_a.paste(src_img, (-subtile_w, -subtile_h))
  42. tile_b = PIL.Image.new("RGBA", (subtile_w, subtile_h))
  43. tile_b.paste(src_img, (-tile_w, 0))
  44. tile_c = PIL.Image.new("RGBA", (remtile_w, subtile_h))
  45. tile_c.paste(src_img, (-subtile_w, -tile_h))
  46. tile_d = PIL.Image.new("RGBA", (subtile_w, remtile_h))
  47. tile_d.paste(src_img, (-tile_w, -(tile_h + subtile_h)))
  48. tiles.append([tile_a, tile_b, tile_c, tile_d])
  49. # Prepare finale
  50. output_tw = conversion_mode.tw
  51. output_th = conversion_mode.th
  52. full_finale = PIL.Image.new("RGBA", (tile_w * output_tw, tile_h * output_th))
  53. # State table to be inverted
  54. out_states = conversion_mode.states
  55. # Directions to subtile offsets
  56. subtile_ofx = [1, 0, 1, 0]
  57. subtile_ofy = [1, 0, 0, 1]
  58. for i in [7, 6, 5, 4, 3, 2, 1, 0]:
  59. for j in range(4):
  60. target_tile = out_states[i][j]
  61. if target_tile != -1:
  62. target_stx = (target_tile % output_tw) * tile_w
  63. target_sty = (target_tile // output_tw) * tile_h
  64. target_stx += subtile_ofx[j] * subtile_w
  65. target_sty += subtile_ofy[j] * subtile_h
  66. full_finale.paste(tiles[i][j], (target_stx, target_sty))
  67. # Done!
  68. full_finale.save(output_name)