Render bitmap: What's wrong with this code?

I want to render a movieclip into a bitmap, and then chop the bitmap up into pieces (a grid). Ultimately I want this to result in an explosion but right now I’m working through the code that creates the grid of bitmaps.

The test movieclip I created is a square with some straight lines and swirly colors, in order to make it really obvious whether I’m rendering the bitmaps accurately.

I’m working just in the Flash IDE, and my movieclip (mc) is in the Library.

Here is my code, and my problem is below that:


var mc = new MC();

var mcBitmapData:BitmapData = new BitmapData(mc.width, mc.height, true, 0xFFFFFF);
var mcBitmap:Bitmap = new Bitmap(mcBitmapData);
addChild(mcBitmap);
mcBitmapData.draw(mc);
mcBitmap.x = mcBitmap.y = 250; //show original

var cols:uint = 4;
var rows:uint = 4;

for (var c:uint = 0; c < cols; c++) {
	for (var r:uint = 0; r < rows; r++) {
		var W:Number = mcBitmap.width/cols;
		var H:Number = mcBitmap.height/rows;
		var bitmapByteArray:ByteArray = mcBitmap.bitmapData.getPixels(new Rectangle(c * W, r * H, W, H));
		bitmapByteArray.position = 0;
		
		var bitmapFragment:BitmapData = new BitmapData(W, H); // width, height
		bitmapFragment.setPixels(new Rectangle(0, 0, W, H), bitmapByteArray);
		
		var newBitmap:Bitmap = new Bitmap(bitmapFragment);
		addChild(newBitmap);
		newBitmap.x = c*mcBitmap.width/cols;
		newBitmap.y = r*mcBitmap.height/rows;
	}
}

This works nicely as is, but when I start playing around with different column and row numbers (cols, rows), things get weird.

cols/rows values of 1/1, 2/2, 3/3, 4/4, 6/6, 2/4, 3/4, 3/6 work fine. (Others probably too.) But combinations like 5/5, 7/7, and 10/10 result in odd behavior. Sometimes some columns of bitmaps look fine, while others appear to be turned at an angle or sheared or some such. Sometimes they all look properly aligned but there is a small gap between them.

Just for grins I changed the type of cols and rows from uint to Number. That didn’t help.

What’s going on?