3

I am implementing a function which gets a input pixel array containing a bitmap, rotate the bitmap by 50 degrees and writes the result into an output pixel array. The output pixel array is shown on screen. I am using a rotation matrix to transform every pixel of the input to the output.

The function which rotates the pixels is implemented the following way:

void rotatePixel(int x, int y, float *new_x, float *new_y)
{
    float f_x, f_y;
    UWORD i;
    f_x = (float)x;
    f_y = (float)y;

    i = currentDegree / DEGREE_RESOLUTION - 1;
    *new_x = (f_x * cosLookup[i] - f_y * sinLookup[i]);
    *new_y = (f_x * sinLookup[i] + f_y * cosLookup[i]);
}

The function is invoked for every pixel of the input array. cosLookup and sinLookup are lookup tables to speed up calculation. The result for a red square looks like this:

enter image description here

So, the rotation in general is working but there are lots of free spots where no pixel was written to. To find out whats going wrong, I adjusted my program to mark pixels with green and blue colors, if a pixel is projected at coordinates where already a pixel resides. The result looks like this:

enter image description here

I assume this is because of:

  • the result of my rotation matrix are pixel coordinates in float format
  • the target coordinates in my output array are integers
  • Simply applying round() leads to the problem from above: Pixels are written on the same x/y coordinate two or more times, while other coordinates in my output array remain empty

My question is: How can I resolve this issue? I think tools like gimp, Photoshop etc. rotate objects without this problem. I thought about an internal upscaling to be capable of writing the resulting pixels more precisely to their destination. Or maybe applying a filter which smooths the result after transformation?

3
  • 1
    The input to your function needs to be the new_x and the new_y, and the output needs to be a color value for that position. The corresponding x and y in the original image will not be integers. The simplest thing to do is just round x and y and pick the color at that coordinate. The next level of quality is to perform a linear interpolation of the colors around that x and y coordinate. Beyond that are a variety of filters to blend the surrounding colors. May 4, 2022 at 22:15
  • 3
    In other words, the code is for (y=0; y<outputRows; y++) for (x=0; x<outputCols; x++) outputImage[y][x]=pickColor(y,x,...); where the ... provides access to the original image pixel data, the original image size, and a transformation matrix that specifies the rotation, translation, and scaling of the image. May 4, 2022 at 23:08
  • awesome, works much better this way :) May 8, 2022 at 10:00

1 Answer 1

4

What if you invert the rotation and calculate backwards

new_x, new_x -> old_x, old_y

That would resolve the rounding errors.

for new_pixel_coordinates:
    old_pixel_coordinates = inverse_rotate(new_pixel_coordinates)
    color = lookup_color(old_bitmap, old_pixel_coordinates)
    color_new_picture(new_bitmap, new_pixel_coordinates, color)

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.