A
A
alexey_prokopyev2014-03-21 10:45:08
Search Engine Optimization
alexey_prokopyev, 2014-03-21 10:45:08

How to optimize the algorithm for rotating an image by an angle that is a multiple of 90 degrees?

There is an image in the form of a w x h data array. The algorithm should rotate the image by 90, -90 and 180 degrees.
For such angles, the sines and cosines degenerate into a simple algorithm, for example, for 90 degrees:

new_w = h;
new_h = w;
for (int i = 0; i < h; i++)
{
    for (int j = 0; j < w; j++)
    {
        dest[j * new_w + new_w - i - 1] = source[i * w + j];
    }
}

The speed of such copying is 4-5 times less than the speed of copying without rotation. The reason, most likely, is in inconsistent access to memory when writing to dest: the recording goes over elements spaced from each other by the size of the image line.
Who knows ways to speed up random access to memory?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
I
Ilya Popov, 2014-03-23
@encyclopedist

This problem, although not completely identical, is similar to the matrix transposition problem, and the same optimization methods are applicable to it. To improve memory access, you can split into blocks.
Check out this answer on StackOverflow, especially the two methods at the very end:
stackoverflow.com/a/16743203
You can also check out this wikipedia article: In-place matrix transposition

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question