# Clipping Sprite Sheets with Variable Placement

I was looking at some sprite sheets at sprite sheet database. I noticed that some of the sprite sheets had several different objects placed together in one file. Some of the sprites, such as movement animations were placed next to each other so that they were of the same height. However, sometimes next to them are larger sprite objects that take up a different amount of space.

These sprite sheets have no apparent information such as cell width and height. It seems like you would have to clip them through trial and error. Is there some sort of algorithm to figure out how to clip them without external sprite grid information?

http://sdb.drshnaps.com/display.php?object=7604

• "Is there some sort of algorithm to figure out how to clip them without external sprite grid information?" Why not just make some external sprite grid information? It would probably take a lot more time to solve programmatically than just having some meta data that you author by hand. – Tetrad Nov 25 '10 at 6:47

Almost all sprite sheets ripped from old games that you will find online contain images that have been rebuilt from the original, smaller (usually 8x8 or 16x16 pixels) tiles. Below is an image of a tile editor without the color paletted adjusted to illustrate what i'm saying.

So, to use those graphics in your game, you should have the sprite origins and sizes stored in some way, for performance's sake. But, if you still want to get the sizes programatically, do something along this pseudocode:

var current_line = 0
var current_column = 0

while (IsEmptyLine(lines[current_line]))
{
current_line++
}

var top_line = current_line

while (!IsEmptyLine(lines[current_line]))
{
current_line++
}

var bottom_line = current_line

while (IsEmptyColumn(columns[current_column]))
{
current_column++
}

var left_column = current_column

while (!IsEmptyColumn(columns[current_column]))
{
current_column++
}

var right_column = current_column

// rectangle(x, y, width, height)
rectangle bounds = new rectangle(left_column, top_line, right_column - left_column, bottom_line - top_line)


IsEmptyLine/Column determines if a line or column is entirely made of fully transparent pixels. Also keep in mind that this is a generic code, and won't work for special cases, like non-vertical sprite tiling.

You will most likely have to either (a) create a mapping of the sprite sheet or (b) rearrange the sprites based on your application's mapping convention using some sort of graphical editing application (such as Adobe Photoshop or GIMP); most modern graphical editing applications support adding guidelines and rulers which make both (a) and (b) much easier to do.