3

I'm trying to write a function to rotate an image matrix using the loop-tiling technique. However, I'm running into some issues with getting it to work properly.

EDIT: Here's my updated code that works, but only when n is a multiple of the block size. How would I go about handling varying matrix sizes? Right now, I'm just using square blocks, and it works very well for those square blocks. How would I go about changing this to use rectangular blocks based on the size of the array I'm given. Specifically, if I'm given an n x n array, how do I choose the rectangular block dimensions to split it up into?

  //Block size to tune
  int block = 20;
  int i1, j1, k1,  i, j, k;

  for(i1 = 0; i1 < n; i1 += block) {
    for(j1 = 0; j1< n; j1 += block) {
            for(i = i1; i < i1 + block; i++){
                for(j = j1; j < j1 + block; j++){
                    dest[getInd(j, i, n)] = src[getInd(i, n - 1 - j, n)]; 

                }
            }
        }
    }

}

3
  • 2
    well... where is the problem? what results are you getting? Commented Oct 6, 2010 at 20:32
  • Added image of results with original matrix on top, incorrectly rotated on bottom. Commented Oct 6, 2010 at 20:40
  • Just as a thought, could the fact that I'm using square blocks (block x block) instead of rectangular blocks solve my problem? If so, how could I implement this? Commented Oct 6, 2010 at 21:00

1 Answer 1

1

The first two for loops look wrong:

  for(i1 = 0; i1 < n/block; i1 += block) {
    for(j1 = 0; j1< n/block; j1 += block) {

should probably be:

  for(i1 = 0; i1 < n; i1 += block) {
    for(j1 = 0; j1 < n; j1 += block) {

When that's corrected though you'll probably just need to step through the code in your debugger to work out what else needs fixing.

Sign up to request clarification or add additional context in comments.

2 Comments

I made this change, and got rid of the k loop, instead just using n as the stride of my row (Which i'm not sure about). Doing that and changing the block size makes my code run, but only for certain matrices. For example, my code only works correctly if the size of the matrix is a multiple of my block size. How would I go handling this?
I suspect that you may just have to accept the limitation that n needs to be an integer multiple of block. You can always pad your image to meet this constraint if it is some odd (prime) size.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.