Thread: Rotate 8x8 bit image

  1. #1
    Registered User
    Join Date
    Jan 2013
    Posts
    106

    Rotate 8x8 bit image

    Hi Guys
    I’m looking to bitwise rotate an 8 byte array by 90 degrees clockwise, so this:
    Code:
    unsigned char shape[8] = {
    0b00011000,
    0b00111100,
    0b01111110,
    0b11111111,
    0b00000000,
    0b00000000,
    0b00000000,
    0b00000000;}
    would become this...
    Code:
    0b00001000,
    0b00001100,
    0b00001110,
    0b00001111,
    0b00001111,
    0b00001110,
    0b00001100,
    0b00001000;
    It seems like it would be easy to rotate 180 degrees by simply reading from one array to another, indexing the bytes in reverse order,
    but I can’t seem to wrap my head around doing it just 90 degrees.
    Any ideas?
    Cheers, Art.

  2. #2
    Programming Wraith GReaper's Avatar
    Join Date
    Apr 2009
    Location
    Greece
    Posts
    2,738
    The simplest method is to start counting from bottom to top and from left to right. Anti-clockwise would be top to bottom and right to left.
    Devoted my life to programming...

  3. #3
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    It might be easier to see the pattern/algorithm for rotation if you use a different, array and (for now) don't think about the bit manipulation. Here's a 4 x 5:
    Code:
    a b c d e
    f g h i j
    k l m n o
    p q r s t
    Rotating 90 degrees clockwise gives
    Code:
    p k f a
    q l g b
    r m h c
    s n i d
    t o j e
    So, you can see that for populating the new array, the top row (moving left to right) becomes the right column (moving top to bottom), the second row becomes the second-from-right column, etc. Assuming N rows and M columns in the original, you will then have M rows and N columns in the new array. Now, you need to find the pattern of mapping old coordinates to new. For example
    Code:
    a (0, 0) -> (0, 3)
    b (0, 1) -> (1, 3)
    c (0, 2) -> (2, 3)
    d (0, 3) -> (3, 3)
    e (0, 4) -> (4, 3)
    ...
    l (2, 1) -> (1, 1)
    m (2, 2) -> (2, 1)
    n (2, 3) -> (3, 1)
    ...
    q (3, 1) -> (1, 0)
    r (3, 2) -> (2, 0)
    s (3, 3) -> (3, 0)
    t (3, 4) -> (4, 0)
    Where coordinates are (old_row, old_col) -> (new_row, new_col)

    If we say that the original matrix was N = 4 rows and M = 5 columns, the new matrix will be M rows (5) and N columns (4).

    new_row is pretty easy, it just tracks the old_col. new_col is a bit trickier. It sort of tracks the old_row, but backwards. I'll let you figure that one out. You should express any formulae in terms of N and M instead of magic numbers like 4 or 5. You could apply similar logic for determining counter-clockwise rotations, or even 180 degree rotations.

    The logic for doing this with bit manipulation is a bit trickier if you try to work it all into the loop, but you could create a function for storing a value in a particular bit location, the you just calculate new_row and new_col, and use your function to set the right bit:
    Code:
    value = get_bit(old_matrix, old_row, old_col);
    new_row = old_col;
    new_col = // you fill in this part
    set_bit(new_matrix, new_row, new_col, value);
    Hope that makes sense and helps you out.

  4. #4
    Registered User
    Join Date
    Jan 2013
    Posts
    106
    Thanks
    I had procedure worked out, but couldn’t get it rolled into a loop.
    I was wasting time anding the bytes with different bit masks.

    Simply look at a vertical row, say all bit 7 for example,
    write that into the first byte, shift all of the source bits to the let & repeat till done

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 01-18-2016, 07:24 PM
  2. c# rotate image
    By wert in forum C# Programming
    Replies: 4
    Last Post: 03-21-2010, 02:07 PM
  3. Replies: 4
    Last Post: 03-13-2010, 05:10 AM
  4. Do I really need to rotate?
    By JimJoyce in forum Game Programming
    Replies: 13
    Last Post: 02-24-2005, 04:04 PM
  5. How to rotate
    By cfrost in forum Windows Programming
    Replies: 5
    Last Post: 07-15-2004, 10:08 AM

Tags for this Thread