Skip to content

Instantly share code, notes, and snippets.

@robesris
Created May 11, 2010 23:14
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save robesris/398015 to your computer and use it in GitHub Desktop.
Save robesris/398015 to your computer and use it in GitHub Desktop.
module PixenToGameQuery
require 'RMagick'
include Magick
FPR = 12 # Frames per row in a sprite sheet generated by Pixen
COLPAD = 2 # Pix between each column of images on the sprite sheet
ROWPAD = 2 # Pix between each row of images on the sprite sheet
LEFTPAD = 4 # Extra pix on left of sprite sheet
RIGHTPAD = 20 # Extra pix on right of sprite sheet
BOTTOMPAD = 2 # Extra pix on bottom of sprite sheet
def PixenToGameQuery.convert_sprite(image_path, width, height, frames, new_image_path)
original = ImageList.new(image_path)
img = Image.new((width + COLPAD) * frames, height)
sprite_rows = (frames.to_f / FPR).ceil
0.upto(sprite_rows - 1) do |rownum|
rowframes = (rownum == sprite_rows - 1) ? frames % FPR : FPR
row_width = rowframes * (width + COLPAD)
pix = original.get_pixels(LEFTPAD,
ROWPAD + (height + ROWPAD) * rownum,
row_width,
height)
img.store_pixels(rownum * FPR * (width + COLPAD),
0,
row_width,
height,
pix)
end
img.write(new_image_path)
end
end
@robesris
Copy link
Author

Minor changes to encapsulate the code in a Module and change filename to match module name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment