Jump to content
New account registrations are disabed. This website is now an archive. Read more here.
ForeverZer0

Spritesheet Separator/Combiner

Recommended Posts

Spritesheet Separator/Combiner

Authors: ForeverZer0

Version: 1.0


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. It can also combine images into a single larger image as well.


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 individual files into a single larger one.
  • Lightweight and fast


Screenshots

 

Turns this...

 

icon-sheet.png

 

 

Into this...

 

icons_separated.png

 

 

And vice-versa.


Demo

 

None.


Script

 

Click spoiler for the script.

 

#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# Spritesheet Seperator/Combiner
# Author: ForeverZer0
# Date: 5.14.2011
# Version: 1.0
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#
# 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 sec, I
#   realized someone else may get some use out of it as well, so here it is.
#
# 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.
#   - Combines many images of any size into one single file
#   - Lightweight and fast
#
# Instructions:
#   - Place script in new project or temporarily in an existing game anywhere
#     before "Main"
#   - Make any needed changes to the few configurations at the top of the script
#   - Run the game
#
# Credits:
#   - ForeverZer0, for the script
#
# Author's Notes:
#   - Report bugs/issues at www.chaos-project.com
#
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#                          BEGIN CONFIGURATION
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

MODE = 1
# Select the mode to run in. 
#   0 = Split Image
#   1 = Combine Images

FOLDER_NAME = 'Images'
# The folder name that the images will be placed into, or the folder where 
# the images are contained that will be combined.

#---------------------------------
# Image Splitting
#---------------------------------

SOURCE_FILE = 'test.png'
# The name of the source file that will be split. Place in game directory.

SPLIT_X = 24
SPLIT_Y = 24
# The width/height in pixels to split the image file into on each axis.

BASE_NAME = 'icon'
# The base name used for the output files. 

NAMING_TYPE = 1
# Define the naming convention applied to the base name.
# 0 = Numerically. (icon 1, icon 2, icon 3, etc, etc)
# 1 = Coordinates. (icon 1x2, icon 3x4, icon 3x5, etc. etc)

#---------------------------------
# Image Combining
#---------------------------------

IMAGES_WIDE = 12
# The number of images that will be placed per row

FILENAME = 'combined'
# The output filename

#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
#                           END CONFIGURATION
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=

module Zlib
 class Png_File < GzipWriter
#-------------------------------------------------------------------------------
   def make_png(bitmap_Fx,mode)
     @mode = mode
     @bitmap_Fx = bitmap_Fx
     self.write(make_header)
     self.write(make_ihdr)
     self.write(make_idat)
     self.write(make_iend)
   end
#-------------------------------------------------------------------------------
   def make_header
     return [0x89,0x50,0x4e,0x47,0x0d,0x0a,0x1a,0x0a].pack("C*")
   end
#-------------------------------------------------------------------------------
   def make_ihdr
     ih_size = [13].pack("N")
     ih_sign = "IHDR"
     ih_width = [@bitmap_Fx.width].pack("N")
     ih_height = [@bitmap_Fx.height].pack("N")
     ih_bit_depth = [8].pack("C")
     ih_color_type = [6].pack("C")
     ih_compression_method = [0].pack("C")
     ih_filter_method = [0].pack("C")
     ih_interlace_method = [0].pack("C")
     string = ih_sign + ih_width + ih_height + ih_bit_depth + ih_color_type +
              ih_compression_method + ih_filter_method + ih_interlace_method
     ih_crc = [Zlib.crc32(string)].pack("N")
     return ih_size + string + ih_crc
   end
#-------------------------------------------------------------------------------
   def make_idat
     header = "\x49\x44\x41\x54"
     data = make_bitmap_data
     data = Zlib::Deflate.deflate(data, 8)
     crc = [Zlib.crc32(header + data)].pack("N")
     size = [data.length].pack("N")
     return size + header + data + crc
   end
#-------------------------------------------------------------------------------
   def make_bitmap_data1
     w = @bitmap_Fx.width
     h = @bitmap_Fx.height
     data = []
     for y in 0...h
       data.push(0)
       for x in 0...w
         color = @bitmap_Fx.get_pixel(x, y)
         red = color.red
         green = color.green
         blue = color.blue
         alpha = color.alpha
         data.push(red)
         data.push(green)
         data.push(blue)
         data.push(alpha)
       end
     end
     return data.pack("C*")
   end
#-------------------------------------------------------------------------------
   def make_bitmap_data
     gz = Zlib::GzipWriter.open('hoge.gz')
     t_Fx = 0
     w = @bitmap_Fx.width
     h = @bitmap_Fx.height
     data = []
     for y in 0...h
       data.push(0)
       for x in 0...w
         t_Fx += 1
         if t_Fx % 10000 == 0
           Graphics.update
         end
         if t_Fx % 100000 == 0
           s = data.pack("C*")
           gz.write(s)
           data.clear
         end
         color = @bitmap_Fx.get_pixel(x, y)
         red = color.red
         green = color.green
         blue = color.blue
         alpha = color.alpha
         data.push(red)
         data.push(green)
         data.push(blue)
         data.push(alpha)
       end
     end
     s = data.pack("C*")
     gz.write(s)
     gz.close    
     data.clear
     gz = Zlib::GzipReader.open('hoge.gz')
     data = gz.read
     gz.close
     File.delete('hoge.gz') 
     return data
   end
#-------------------------------------------------------------------------------
   def make_iend
     ie_size = [0].pack("N")
     ie_sign = "IEND"
     ie_crc = [Zlib.crc32(ie_sign)].pack("N")
     return ie_size + ie_sign + ie_crc
   end
 end
end

#===============================================================================
# ** Bitmap
#===============================================================================

class Bitmap

 def make_png(name="like", path="",mode=0)
   make_dir(path) if path != ""
   Zlib::Png_File.open("temp.gz") {|gz| gz.make_png(self, mode) }
   Zlib::GzipReader.open("temp.gz") {|gz| $read = gz.read }
   f = File.open(path + name + ".png","wb")
   f.write($read)
   f.close
   File.delete('temp.gz') 
   end
#-------------------------------------------------------------------------------
 def make_dir(path)
   dir = path.split("/")
   dir.each_index {|i|
     unless dir == "."
       add_dir = dir[0..i].join("/")
       begin
         Dir.mkdir(add_dir)
       rescue
       end
     end
   }
 end
end

#===============================================================================
# Processing
#===============================================================================

time = Time.now

if MODE == 0
 bitmap = Bitmap.new(SOURCE_FILE)
 count = 0
 (0...(bitmap.width / SPLIT_X)).each {|x| (0...(bitmap.height / SPLIT_Y)).each {|y|
   count += 1
   icon = Bitmap.new(SPLIT_X, SPLIT_Y)
   rect = Rect.new(x * SPLIT_X, y * SPLIT_Y, SPLIT_X, SPLIT_Y)
   icon.blt(0, 0, bitmap, rect)
   name = (BASE_NAME + (NAMING_TYPE == 0 ? " #{count}" : " #{x+1}x#{y+1}"))
   icon.make_png(name, FOLDER_NAME + '/')
   if count % 80 == 0
     Graphics.update
   end
 }}

elsif MODE == 1

 images = Dir.entries(FOLDER_NAME) - ['.', '..']
 if images.empty?
   print("No image files found in #{FOLDERNAME} directory.")
   exit
 end
 images.collect! {|filename| Bitmap.new("#{FOLDER_NAME}/#{filename}") }
 w, h, num = images[0].width, images[0].height, images.size
 canvas = Bitmap.new(w * IMAGES_WIDE, (num.to_f / IMAGES_WIDE).ceil * h)
 images.each_with_index {|image, i|
   x, y = (i % IMAGES_WIDE) * w, (i / IMAGES_WIDE) * h
   canvas.blt(x, y, image, Rect.new(0, 0, w, h))
   if (i % 80) == 0
     Graphics.update
   end
 }
 canvas.make_png(FILENAME, '')
end

print ("Process Complete!\n" +
     "#{count} images converted in #{Time.now.to_f - time.to_f} seconds.")
exit

 


Instructions

 

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


Compatibility

 

None.


Credits and Thanks

 

  • ForeverZer0, for the script.


Author's Notes

 

Please report any bugs/issues, I'll be sure to help you resolve them.

Edited by ForeverZer0

Share this post


Link to post
Share on other sites

Very useful. Is there a way you can make it so i can make sprite sheets also?

Altho that might be a little hard.

Share this post


Link to post
Share on other sites

Very useful. Is there a way you can make it so i can make sprite sheets also?

Altho that might be a little hard.

 

I'll see what I can do. It actually won't be too difficult, the basic mechanics of the system are already in place. I could have it so that it loads all the images in a folder, uses the Bitmap#blt function to "stitch" them together to the defined dimensions, and then save the image as a whole. Naturally, it would only work on images of consistent size, though.

 

I'm not sure why I didn't think to do that, but I'll check it out right now and see if I can update here in just a bit.

Share this post


Link to post
Share on other sites

Hey pol, if you are looking for a spritesheet maker as well, I know where to get one.

*cough* http://www.4shared.com/file/126502165/3f5de612/ssm-beta1.html *cough* but it can ONLY put together spritesheets, so with this script and that you can flip between the 2 easily enough

Share this post


Link to post
Share on other sites

Done.

Can now combine smaller files into sheets just as easily.

I renamed the script accordingly, as well.

Share this post


Link to post
Share on other sites

Done.

Can now combine smaller files into sheets just as easily.

I renamed the script accordingly, as well.

Perfect. Thanks :)

Share this post


Link to post
Share on other sites

You can't just make a folder any 'ol where, it is relative to where the program is being ran from. Make an "Images" folder in the same directory as the Game.exe file.

Share this post


Link to post
Share on other sites

Please sign in to comment

You will be able to leave a comment after signing in



Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

×
×
  • Create New...