旋转由字节数组表示的位图 [英] Rotate a bitmap represented by an array of bytes

查看:95
本文介绍了旋转由字节数组表示的位图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在AVR中,我使用八个字节的数组存储在8x8 LED矩阵上显示的图片.图片需要不时旋转.因此,给定图片定义为:

In an AVR, I'm using an array of eight bytes to store a picture displayed on an 8x8 LED matrix. The picture needs to be rotated from time to time. So, given the picture defined as:

uint8_t rows[8] = {
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b11111111
};

我想逆时针旋转"以获得为:

I want to "rotate" this anticlockwise to get as:

uint8_t rows2[8] = {
    0b11111111,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001,
    0b00000001
};

或者按顺时针方向执行此操作,:

Or this if done clockwise, :

uint8_t rows3[8] = {
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b10000000,
    0b11111111
};

我如何在C语言中做到这一点?

How do I do this in straight C?

推荐答案

某些按位操作可以解决问题.

Some bitwise operations can do the trick.

#include <inttypes.h>

int main(){

  uint8_t rows[8] = {
      0b11111111,
      0b00000001,
      0b00000001,
      0b00111111,
      0b00000001,
      0b00000001,
      0b00000001,
      0b11111111
  };


  uint8_t rows2[8] = {0, 0, 0, 0, 0, 0, 0, 0};
  uint8_t rows3[8] = {0, 0, 0, 0, 0, 0, 0, 0};

  int i, j;
  // rotate clockwise
  for(i=0; i<8; ++i){
    for(j=0; j<8; ++j){
      rows3[i] = (  ( (rows[j] & (1 << (7-i) ) ) >> (7-i) ) << j ) | rows3[i];
    }
  }

  // rotate anti-clockwise
  for(i=0; i<8; ++i){
    for(j=0; j<8; ++j){
      rows2[i] = (  ( (rows[j] & (1 << i ) ) >> i ) << (7-j) ) | rows2[i];
    }
  }
}

在顺时针方向上,使用(rows[j] & (1 << (7-i) ) ) >> (7-i)获取第j个原始字节的第(7-i)个位,然后将其移至第j个位置.通过对字节本身执行或"(|)来收集所有位,因此将数组初始化为0非常重要. 逆时针情况类似,更改了索引. 我用另一封信对其进行了测试,可以让您确定旋转是否正常工作.如果您需要进一步的解释,请问.

In the clockwise case, you get each (7-i)-th bit of the j-th original byte with (rows[j] & (1 << (7-i) ) ) >> (7-i) and then shift it to the j-th position. You collect all the bits by doing an "or" (|) with the byte itself, so it is very important to initialize the array with 0s. The anti-clockwise case is analogous, changing the indexing. I used another letter to test it, that let you know for sure if the rotation is working properly. If you need further explanation, please just ask.

如果要查看结果,则在此SO问题中使用该函数:

If you want to look the result, I'm using the function in this SO question: Is there a printf converter to print in binary format?

这篇关于旋转由字节数组表示的位图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆