Spritesheet Separator/Combiner

Spritesheet Separator/Combiner Version: 1.0
By: ForeverZer0

Introduction

This is a very basic script I wrote to help myself split up one of them big icon sheets into individual files. After admiring its niftiness for a second, I realized someone else may get some use out of it as well, so here it is. I have also included the ability to combine multiple images into a single one.

Features
  • Extremely simple to use.
  • Splits large icon/sprite sheets up into uniform images, then saves them as individual files in a matter of seconds.
  • Can seperate any image into pieces of any size.
  • Can combine separate files into a single sheet.
  • Lightweight and fast

Screenshots

Content Hidden Spoiler
Turns this...
Image

...into this...
Image

...and vice-versa.


Script

  1. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  2. # Spritesheet Seperator/Combiner
  3. # Author: ForeverZer0
  4. # Date: 5.14.2011
  5. # Version: 1.0
  6. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  7. #
  8. # Introduction:
  9. #   This is a very basic script I wrote to help myself split up one of them big
  10. #   icon sheets into individual files. After admiring its niftiness for a sec, I
  11. #   realized someone else may get some use out of it as well, so here it is.
  12. #
  13. # Features:
  14. #   - Extremely simple to use.
  15. #   - Splits large icon/sprite sheets up into uniform images, then saves them as
  16. #     individual files in a matter of seconds.
  17. #   - Can seperate any image into pieces of any size.
  18. #   - Combines many images of any size into one single file
  19. #   - Lightweight and fast
  20. #
  21. # Instructions:
  22. #   - Place script in new project or temporarily in an existing game anywhere
  23. #     before "Main"
  24. #   - Make any needed changes to the few configurations at the top of the script
  25. #   - Run the game
  26. #
  27. # Credits:
  28. #   - ForeverZer0, for the script
  29. #
  30. # Author's Notes:
  31. #   - Report bugs/issues at http://www.chaos-project.com
  32. #
  33. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  34. #                          BEGIN CONFIGURATION
  35. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  36.  
  37. MODE = 1
  38. # Select the mode to run in.
  39. #   0 = Split Image
  40. #   1 = Combine Images
  41.  
  42. FOLDER_NAME = 'Images'
  43. # The folder name that the images will be placed into, or the folder where
  44. # the images are contained that will be combined.
  45.  
  46. #---------------------------------
  47. # Image Splitting
  48. #---------------------------------
  49.  
  50. SOURCE_FILE = 'test.png'
  51. # The name of the source file that will be split. Place in game directory.
  52.  
  53. SPLIT_X = 24
  54. SPLIT_Y = 24
  55. # The width/height in pixels to split the image file into on each axis.
  56.  
  57. BASE_NAME = 'icon'
  58. # The base name used for the output files.
  59.  
  60. NAMING_TYPE = 1
  61. # Define the naming convention applied to the base name.
  62. # 0 = Numerically. (icon 1, icon 2, icon 3, etc, etc)
  63. # 1 = Coordinates. (icon 1x2, icon 3x4, icon 3x5, etc. etc)
  64.  
  65. #---------------------------------
  66. # Image Combining
  67. #---------------------------------
  68.  
  69. IMAGES_WIDE = 12
  70. # The number of images that will be placed per row
  71.  
  72. FILENAME = 'combined'
  73. # The output filename
  74.  
  75. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  76. #                           END CONFIGURATION
  77. #=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
  78.  
  79. module Zlib
  80.   class Png_File < GzipWriter
  81. #-------------------------------------------------------------------------------
  82.     def make_png(bitmap_Fx,mode)
  83.       @mode = mode
  84.       @bitmap_Fx = bitmap_Fx
  85.       self.write(make_header)
  86.       self.write(make_ihdr)
  87.       self.write(make_idat)
  88.       self.write(make_iend)
  89.     end
  90. #-------------------------------------------------------------------------------
  91.     def make_header
  92.       return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
  93.     end
  94. #-------------------------------------------------------------------------------
  95.     def make_ihdr
  96.       ih_size = [13].pack("N")
  97.       ih_sign = "IHDR"
  98.       ih_width = [@bitmap_Fx.width].pack("N")
  99.       ih_height = [@bitmap_Fx.height].pack("N")
  100.       ih_bit_depth = [8].pack("C")
  101.       ih_color_type = [6].pack("C")
  102.       ih_compression_method = [0].pack("C")
  103.       ih_filter_method = [0].pack("C")
  104.       ih_interlace_method = [0].pack("C")
  105.       string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
  106.                ih_compression_method + ih_filter_method + ih_interlace_method
  107.       ih_crc = [Zlib.crc32(string)].pack("N")
  108.       return ih_size + string + ih_crc
  109.     end
  110. #-------------------------------------------------------------------------------
  111.     def make_idat
  112.       header = "\x49\x44\x41\x54"
  113.       data = make_bitmap_data
  114.       data = Zlib::Deflate.deflate(data, 8)
  115.       crc = [Zlib.crc32(header + data)].pack("N")
  116.       size = [data.length].pack("N")
  117.       return size + header + data + crc
  118.     end
  119. #-------------------------------------------------------------------------------
  120.     def make_bitmap_data1
  121.       w = @bitmap_Fx.width
  122.       h = @bitmap_Fx.height
  123.       data = []
  124.       for y in 0...h
  125.         data.push(0)
  126.         for x in 0...w
  127.           color = @bitmap_Fx.get_pixel(x, y)
  128.           red = color.red
  129.           green = color.green
  130.           blue = color.blue
  131.           alpha = color.alpha
  132.           data.push(red)
  133.           data.push(green)
  134.           data.push(blue)
  135.           data.push(alpha)
  136.         end
  137.       end
  138.       return data.pack("C*")
  139.     end
  140. #-------------------------------------------------------------------------------
  141.     def make_bitmap_data
  142.       gz = Zlib::GzipWriter.open('hoge.gz')
  143.       t_Fx = 0
  144.       w = @bitmap_Fx.width
  145.       h = @bitmap_Fx.height
  146.       data = []
  147.       for y in 0...h
  148.         data.push(0)
  149.         for x in 0...w
  150.           t_Fx += 1
  151.           if t_Fx % 10000 == 0
  152.             Graphics.update
  153.           end
  154.           if t_Fx % 100000 == 0
  155.             s = data.pack("C*")
  156.             gz.write(s)
  157.             data.clear
  158.           end
  159.           color = @bitmap_Fx.get_pixel(x, y)
  160.           red = color.red
  161.           green = color.green
  162.           blue = color.blue
  163.           alpha = color.alpha
  164.           data.push(red)
  165.           data.push(green)
  166.           data.push(blue)
  167.           data.push(alpha)
  168.         end
  169.       end
  170.       s = data.pack("C*")
  171.       gz.write(s)
  172.       gz.close    
  173.       data.clear
  174.       gz = Zlib::GzipReader.open('hoge.gz')
  175.       data = gz.read
  176.       gz.close
  177.       File.delete('hoge.gz')
  178.       return data
  179.     end
  180. #-------------------------------------------------------------------------------
  181.     def make_iend
  182.       ie_size = [0].pack("N")
  183.       ie_sign = "IEND"
  184.       ie_crc = [Zlib.crc32(ie_sign)].pack("N")
  185.       return ie_size + ie_sign + ie_crc
  186.     end
  187.   end
  188. end
  189.  
  190. #===============================================================================
  191. # ** Bitmap
  192. #===============================================================================
  193.  
  194. class Bitmap
  195.  
  196.   def make_png(name="like", path="",mode=0)
  197.     make_dir(path) if path != ""
  198.     Zlib::Png_File.open("temp.gz") {|gz| gz.make_png(self, mode) }
  199.     Zlib::GzipReader.open("temp.gz") {|gz| $read = gz.read }
  200.     f = File.open(path + name + ".png","wb")
  201.     f.write($read)
  202.     f.close
  203.     File.delete('temp.gz')
  204.     end
  205. #-------------------------------------------------------------------------------
  206.   def make_dir(path)
  207.     dir = path.split("/")
  208.     dir.each_index {|i|
  209.       unless dir == "."
  210.         add_dir = dir[0..i].join("/")
  211.         begin
  212.           Dir.mkdir(add_dir)
  213.         rescue
  214.         end
  215.       end
  216.     }
  217.   end
  218. end
  219.  
  220. #===============================================================================
  221. # Processing
  222. #===============================================================================
  223.  
  224. time = Time.now
  225.  
  226. if MODE == 0
  227.   bitmap = Bitmap.new(SOURCE_FILE)
  228.   count = 0
  229.   (0...(bitmap.width / SPLIT_X)).each {|x| (0...(bitmap.height / SPLIT_Y)).each {|y|
  230.     count += 1
  231.     icon = Bitmap.new(SPLIT_X, SPLIT_Y)
  232.     rect = Rect.new(x * SPLIT_X, y * SPLIT_Y, SPLIT_X, SPLIT_Y)
  233.     icon.blt(0, 0, bitmap, rect)
  234.     name = (BASE_NAME + (NAMING_TYPE == 0 ? " #{count}" : " #{x+1}x#{y+1}"))
  235.     icon.make_png(name, FOLDER_NAME + '/')
  236.     if count % 80 == 0
  237.       Graphics.update
  238.     end
  239.   }}
  240.  
  241. elsif MODE == 1
  242.   
  243.   images = Dir.entries(FOLDER_NAME) - ['.', '..']
  244.   if images.empty?
  245.     print("No image files found in #{FOLDERNAME} directory.")
  246.     exit
  247.   end
  248.   images.collect! {|filename| Bitmap.new("#{FOLDER_NAME}/#{filename}") }
  249.   w, h, num = images[0].width, images[0].height, images.size
  250.   canvas = Bitmap.new(w * IMAGES_WIDE, (num.to_f / IMAGES_WIDE).ceil * h)
  251.   images.each_with_index {|image, i|
  252.     x, y = (i % IMAGES_WIDE) * w, (i / IMAGES_WIDE) * h
  253.     canvas.blt(x, y, image, Rect.new(0, 0, w, h))
  254.     if (i % 80) == 0
  255.       Graphics.update
  256.     end
  257.   }
  258.   canvas.make_png(FILENAME, '')
  259. end
  260.  
  261. print ("Process Complete!\n" +
  262.       "#{count} images converted in #{Time.now.to_f - time.to_f} seconds.")
  263. exit
Expand to see the code.


Instructions

Place script anywhere above main, place source file in game directory, make any needed adjustments to the configuration, then run the game.

Author's Notes

Please report any bugs/issues so that they can be resolved. Enjoy!

Terms and Conditions

Creative Commons - Attribution-NonCommercial-ShareAlike 3.0 Unported
Content Hidden Spoiler
You are free:

to Share - to copy, distribute and transmit the work
to Remix - to adapt the work

Under the following conditions:

Attribution. You must attribute the work in the manner specified by the author or licensor (but not in any way that suggests that they endorse you or your use of the work).

Noncommercial. You may not use this work for commercial purposes.

Share alike. If you alter, transform, or build upon this work, you may distribute the resulting work only under the same or similar license to this one.

- For any reuse or distribution, you must make clear to others the license terms of this work. The best way to do this is with a link to this web page.

- Any of the above conditions can be waived if you get permission from the copyright holder.

- Nothing in this license impairs or restricts the author's moral rights.
The Zlib:Gzip file (it said "not in gzip format") error?

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=74941