bitmap rotation
rotate image
graphic editing
90 degree rotation
image processing

how to rotate a bitmap 90 degrees

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Rotating a bitmap 90 degrees means remapping every source pixel into a new destination coordinate system. The key detail is that a 90-degree rotation swaps width and height, so the rotated image needs a different output shape from the original.

Understand the Coordinate Mapping

Assume the original bitmap has:

  • width w
  • height h
  • coordinates (x, y) where x grows to the right and y grows downward

For a clockwise rotation, a source pixel at (x, y) moves to:

  • new x = h - 1 - y
  • new y = x

That means the rotated image has:

  • new width h
  • new height w

The transformation matters more than the image format itself.

A Simple Python Example

Here is a pure Python version using a 2D list to show the logic clearly:

python
1def rotate_90_clockwise(bitmap):
2    height = len(bitmap)
3    width = len(bitmap[0])
4    rotated = [[0] * height for _ in range(width)]
5
6    for y in range(height):
7        for x in range(width):
8            rotated[x][height - 1 - y] = bitmap[y][x]
9
10    return rotated
11
12
13bitmap = [
14    [1, 2, 3],
15    [4, 5, 6],
16]
17
18print(rotate_90_clockwise(bitmap))

Output:

python
[[4, 1], [5, 2], [6, 3]]

This is the same operation image libraries perform, just shown in a form that makes the coordinate mapping explicit.

Counterclockwise Rotation Uses a Different Mapping

For a 90-degree counterclockwise rotation, the mapping changes to:

  • new x = y
  • new y = w - 1 - x

A Python version:

python
1def rotate_90_counterclockwise(bitmap):
2    height = len(bitmap)
3    width = len(bitmap[0])
4    rotated = [[0] * height for _ in range(width)]
5
6    for y in range(height):
7        for x in range(width):
8            rotated[width - 1 - x][y] = bitmap[y][x]
9
10    return rotated

If you mix up clockwise and counterclockwise formulas, the output will still look rotated, just in the wrong direction.

Using an Image Library Is Usually Better

In real applications, you normally should not rotate raw bitmap buffers manually unless you need a custom low-level pipeline. Libraries already handle pixel format details, stride, metadata, and performance.

For example, with Pillow:

python
1from PIL import Image
2
3image = Image.open("input.png")
4rotated = image.transpose(Image.Transpose.ROTATE_90)
5rotated.save("output.png")

This is shorter, safer, and easier to maintain than manual buffer math.

Performance and Memory Considerations

A 90-degree rotation usually requires allocating a new image buffer because the dimensions change. In-place rotation is only straightforward for square matrices and even then is more relevant to algorithm exercises than production image code.

For large bitmaps, the main concerns are:

  • memory for the destination image
  • cache efficiency in pixel traversal
  • preserving the correct pixel format

The algorithm itself is linear in the number of pixels, which is optimal for a full-image transform.

Bitmap Format Details Matter in Low-Level Code

When developers say "bitmap," they often mean more than a simple grid of pixels. Real bitmap formats may include:

  • row padding
  • channel order such as BGR versus RGB
  • alpha channel data
  • top-down or bottom-up row storage

If you are manipulating raw BMP bytes directly, the rotation logic must account for those details too. If you are using a decoded image object, the library usually handles them for you.

Common Use Cases

Rotating by 90 degrees appears often in:

  • camera or photo orientation fixes
  • UI asset transformation
  • document scanning apps
  • game sprites and textures

The general algorithm is the same even though the surrounding application code differs.

Common Pitfalls

  • Forgetting that width and height swap after a 90-degree rotation.
  • Using the clockwise formula when the intended rotation is counterclockwise.
  • Trying to rotate raw bitmap bytes without accounting for row padding or channel layout.
  • Reimplementing low-level pixel rotation when an image library already provides a correct method.
  • Assuming in-place rotation is simple for non-square images.

Summary

  • A 90-degree bitmap rotation is a coordinate remapping problem.
  • Clockwise and counterclockwise rotations use different formulas.
  • The output image dimensions swap width and height.
  • For real applications, image libraries are usually safer than manual buffer code.
  • Manual rotation is still useful for understanding the underlying pixel transformation.

Course illustration
Course illustration

All Rights Reserved.