Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to divide a bitmap into parts that are bitmaps too

I need some info on the possible methods for dividing a bitmap in smaller pieces.

More importantly I would need some options to judge. I have checked many posts and I am still not entirely convinced about what to do:

  1. cut the portion of bitmap
  2. How do I cut out the middle area of the bitmap?

These two posts are some good options I found, but I cant calculate the CPU and RAM cost of each method, or maybe I should not bother with this calculation at all. Nonetheless if I am about to do something, why not do it the best way from the start.

I would be grateful to get some tips and links on bitmap compression so maybe I get better performance combining the two methods.

like image 669
Rubin Basha Avatar asked Nov 01 '13 13:11

Rubin Basha


1 Answers

This function allows you to split a bitmap into and number of rows and columns.

Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 1); Would create a vertically split bitmap stored in a two dimensional array. 2 columns 1 row

Example Bitmap[][] bitmaps = splitBitmap(bmp, 2, 2); Would split a bitmap into four bitmaps stored in a two dimensional array. 2 columns 2 rows

public Bitmap[][] splitBitmap(Bitmap bitmap, int xCount, int yCount) {
    // Allocate a two dimensional array to hold the individual images.
    Bitmap[][] bitmaps = new Bitmap[xCount][yCount];
    int width, height;
    // Divide the original bitmap width by the desired vertical column count
    width = bitmap.getWidth() / xCount;
    // Divide the original bitmap height by the desired horizontal row count
    height = bitmap.getHeight() / yCount;
    // Loop the array and create bitmaps for each coordinate
    for(int x = 0; x < xCount; ++x) {
        for(int y = 0; y < yCount; ++y) {
            // Create the sliced bitmap
            bitmaps[x][y] = Bitmap.createBitmap(bitmap, x * width, y * height, width, height);
        }
    }
    // Return the array
    return bitmaps;     
}
like image 80
K.Rodgers Avatar answered Sep 20 '22 13:09

K.Rodgers