-
-
Save cajun-code/3978252 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment