Skip to content

Instantly share code, notes, and snippets.

@martinwells
Created March 13, 2014 23:24
Show Gist options
  • Save martinwells/9539270 to your computer and use it in GitHub Desktop.
Save martinwells/9539270 to your computer and use it in GitHub Desktop.
Cut up an image into chunks
var bitmapData = Assets.getBitmapData(bitmapPath);
_fullWidth = bitmapData.width;
_fullHeight = bitmapData.height;
_chunkSize = chunkSize;
_lastChunkRect = new Rectangle();
_currentChunkRect = new Rectangle();
// it's different! remove the old cached chunk data
FileUtils.rmdir(_cachePath);
FileUtils.mkdir(_cachePath);
// generate bitmap files
var chunksAcross = Math.floor(bitmapData.width/chunkSize);
var chunksDown = Math.floor(bitmapData.height/chunkSize);
if (chunksAcross <= 1 && chunksDown <= 1) return; // todo handle this case better
var chunk = new BitmapData(chunkSize, chunkSize);
for (y in 0...chunksDown)
{
for (x in 0...chunksAcross)
{
// save this chunk of the bitmap as a separate chunkfile
var sourceRect = new Rectangle(x * chunkSize, y * chunkSize, chunkSize, chunkSize);
var destRect = new Rectangle(0, 0, chunkSize, chunkSize);
chunk.copyPixels(bitmapData, sourceRect, new Point(0,0));
var pngChunkData = chunk.encode("png");
sys.io.File.saveBytes(chunkFilename(x, y), pngChunkData);
trace('generating: ' + _cachePath + '/' + chunkFilename(x, y) + ' (' + chunk.width + 'x' + chunk.height + ')');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment