Ribs
Ribs

Reputation: 1505

AS3 - make a new bitmapData with non integar width and height

Hey guys hoping someone can help here. I am cropping a bitmap image into tiles using the following function:

function crop( _x:Number, _y:Number, _width:Number, _height:Number, callingScope:MovieClip, displayObject:DisplayObject = null, pixelSnapping:Boolean = false):Bitmap
{
    var cropArea:Rectangle = new Rectangle( 0, 0, _width, _height );
    var croppedBitmap:Bitmap;

    if(pixelSnapping == true)
    {
        croppedBitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.ALWAYS, true );
    }
    else
    {
        croppedBitmap = new Bitmap( new BitmapData( _width, _height ), PixelSnapping.NEVER, true );
    }

    croppedBitmap.bitmapData.draw( (displayObject!=null) ? displayObject : callingScope.stage, new Matrix(1, 0, 0, 1, -_x, -_y) , null, null, cropArea, true );

    return croppedBitmap;
}

The width and height being passed in is a non integer value, for instance 18.75. When the new BitmapData is created it always rounds down the value to an integer, since the arguments for BitmapData are typed as such. In the case of my needs here, the width and height will not likely ever be integers. Is there a way to create these bitmap pieces of another image at the exact width and height I need or can a new bitmapData only be created with integer values for height and width?

Thanks for any insight.

EDIT: I realize you can't have a fraction of a pixel, but... What I am trying to achieve is dividing an image into tiles. I want the amount of tiles to be variable, say 4 rows by 4 columns, or 6 rows by 8 columns. the division of an image into X number of parts results in widths and heights in most cases to be non integar values like 18.75 for example. The goal is to divide an image up into tiles, and have that image appear, assembled seamlessly, above the source image, where I would then manipulate the individual tiles for various purposes (puzzle game, tiled animation to new scene, etc). I need the image, when assembled from all the tile pieces, to be an exact copy of the original image, with no lines between tiles, or white edges anywhere, and I need this to happen with non integer widths and heights for the bitmapData pieces that comprise the tiles. Does that makes sense? O_o

Upvotes: 0

Views: 749

Answers (2)

Ilya Denisov
Ilya Denisov

Reputation: 878

If you need to create tiles with preferred size you can use something like that:

private function CreateTilesBySize( bigImage:Bitmap, preferredTileWidth:int, preferredTileHeight:int ):Tiles
{
  var result:Tiles = new Tiles();
  var y:int = 0;
  while ( y < bigImage.height ) {
    result.NewRow();
    const tileHeight:int = Math.min( preferredTileHeight, bigImage.height - y );
    var x:int = 0;
    while ( x < bigImage.width ) {
      const tileWidth:int = Math.min( preferredTileWidth, bigImage.width - x );
      const tile:Bitmap = Crop( x, y, tileWidth, tileHeight, bigImage );
      result.AddTile( tile );
      x += tileWidth;
    }
    y += tileHeight;
  }
  return result;
}

If you need to create exact amount of rows and calls. Then you can use:

private function CreateTilesByNum( bigImage:Bitmap, cols:int, rows:int ):Tiles
{
  const preferredTileWidth:int = Math.ceil( bigImage.width / cols );
  const preferredTileHeight:int = Math.ceil( bigImage.height / rows );
  return CreateTilesBySize( bigImage, preferredTileWidth, preferredTileHeight );
}

But remember that tiles will have diferent sizes (last tiles in a row and last tiles in a column)

Upvotes: 0

Creynders
Creynders

Reputation: 4583

A BitmapData can only be created with integer values for the dimensions.

And to be honest, I'm trying to think what a BitmapData object with floating number values for dimensions would be like, but my brain starts to scream in pain: DOES NOT MAKE SENSE!

My brain's a bit melodramatic sometimes.

-- EDIT -- Answer to your edited question:

2 options:

1/ Just copy the original full bitmap data as many times as you have tiles and then use masks on bitmap objects to show the appropriate parts of the tiles

2/make sure no fractional widths or heights are generated by the slicing. For instance if you got an image of width 213 you want to split in 2 rows and 2 columns, then set the width of the left tiles to 106 and the width of the right tiles to 107

Maybe there are other options, but those two seem to me to be the easiest with most chance of success.

Upvotes: 1

Related Questions