[XP][VX] Spritesheet Separator/Combiner

Started by ForeverZer0, April 23, 2011, 09:56:55 pm

Previous topic - Next topic

ForeverZer0

April 23, 2011, 09:56:55 pm Last Edit: May 14, 2011, 03:08:40 pm by Baraka
Spritesheet Separator/Combiner
Authors: ForeverZer0
Version: 1.0
Type: Image Splitter/Combiner
Key Term: Game Utility



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

None.


Demo

None.


Script

Click spoiler for the script.
Spoiler: ShowHide

#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# 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.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

WhiteRose

Excellent! I was looking for something like this a few weeks ago. Great work!

ForeverZer0

Oh yeah, I did see that post. Sorry I forgot about it then, I could have made it for you faster.
The script only took a little bit to write.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Jragyn

Wait, so this essentially rips a VX -styled iconsheet apart into XP -styled icons?
A bright light can either illuminate or blind, but how will you know which until you open your eyes?

ForeverZer0

Yes, that is what I made it for, though you it could also be used to split other images up too, but I imagine it will be most beneficial for them large icon-sheets you see around.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

G_G

No offense, but this could be done in C# a lot faster and more user friendly. In fact I have a program for it I created awhile ago, I'm gonna dig it up when I leave mah g'mas.

ForeverZer0

Of course it could, but this is a simple copy-past of text into a program that the user already has.
I was originally going to write a C# app for it, but then its going to require someone to download .NET if they don't have it and download the program. Not everything is better just because its a little prettier. This is less than 7 kb of text and is ran on an existing platform and it takes less than 3 seconds to split a sheet. If you make a Windows form for it, you're going to end up with a file around 200-300 kb, and at most it is 2 seconds faster, but you have the above issues.

You don't always need some type of GUI to perform minor actions, and I hardly think changing the name of the file in RMXP's script editor is that big of a pain in the ass to warrant it.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

Jragyn

And here all this time I've been using Paint.NET to cut out and paste icons as needed...
Now I can have a bit that does it for me <3

Thanks again to ForeverZero for his rising l33tness.

And though it wouldn't hurt to practice programming something of this nature in C#, it most certainly benefits those who already have RMXP and want to use it without loading an external program.
A bright light can either illuminate or blind, but how will you know which until you open your eyes?

Jragyn

Spoiler: ShowHide


It keeps droppin' this error whenever I use it to cut up an iconsheet :(

I put it in a new project as instructed with the iconsheet in the game-directory...
and when I run it, it pauses a second... and pops that error. Every time.
It also cuts a seemingly random number of icons from the sheet before doing so, most of which just end up being black squares that are... well... not the icons.
A bright light can either illuminate or blind, but how will you know which until you open your eyes?

ForeverZer0

That looks like a problem with Window's UAC. You are likely running as Admin and accessing non-Admin files or vice-versa. That is unavoidable, and there is no "script fix" for it, since the file access is being blocked by Windows. If your not already, run RMXP as Administrator, and make sure that the file/directory it is accessing belong to the Administrator, or just make sure that RMXP is being invoked by the same user that the files belong to.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

ForeverZer0

Double-posts

I updated and renamed the script.
It can now also make spritesheets from multiple files just as easily.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

sasofrass

I am trying to split this huge iconset but all it does is make the file larger and I can't get it working..

Here is the iconset:

https://dl-web.dropbox.com/get/set.png?w=60c350ab

ForeverZer0

You aren't configuring properly from the sounds of it.
On a side note, the link you gave me is not valid. It may be for you, but I can't access directly from your user page.

If you get another link, or better yet show me your config and I'll be happy to help.  ;)
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

sasofrass

June 05, 2011, 10:52:14 am #13 Last Edit: June 05, 2011, 10:55:42 am by sasofrass
So stupid me didn't have it set to mode = 0 :P   but I think the icon set is too big, it says script is hanging.

Here is the iconset:
http://i795.photobucket.com/albums/yy236/pureiiipker/set.png

Here is my config currently:
#=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=+=
# 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 = 0
# 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 = 'set.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 = 0
# 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


EDIT: i keep running it and it is making about 30-50 icons every time. It works

EDIT 2: after it made 333 icons, it now says not in gzip format

ForeverZer0

Your spritesheet isn't using 24x24 icons like what you have defined in the config, there 8x8. I just ran it and got over 2000 icons.

http://dl.dropbox.com/u/20787370/test.zip

Here they are.

I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

sasofrass

June 07, 2011, 11:19:16 pm #15 Last Edit: June 07, 2011, 11:25:09 pm by sasofrass
8x8?

I downloaded your file and they're extremely small and I can't even use them as icons.


Edit:  The icon set I gave you must have shrunk, they should have been 24x24 :\

ForeverZer0

Yes, the file you gave had 8x8 icons. I just split them that way. Either way it worked okay. If you get me the actual sheet, unshrunk, I can do the same.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

sasofrass


ForeverZer0



Converted Icons

The empty icons or any icons that appear as all black are because of the empty space in the sheet. I cropped the left side, but I didn't bother with the empty rows in between.
I am done scripting for RMXP. I will likely not offer support for even my own scripts anymore, but feel free to ask on the forum, there are plenty of other talented scripters that can help you.

sasofrass