arduino,c ++的简单位图旋转
Simple bitmap rotation for arduino, c++
我正在尝试对我的应用程序进行妥协,但到目前为止还没有运气(或者更确切地说是知识)。
我有黑白屏幕的位图,它看起来像这样(我使用arduino字节风格,因为它更具可读性)
{
B00111100, B01001000,
B00100100, B01010000,
B00111100, B01110000,
B00100100, B01001000
}
它是字节数组,每个字节代表接下来的8个水平像素。
问题是我必须使用位图,其中每个字节代表 8 个下一个垂直像素,所以就像这样转动它
{
B00000000,
B00000000,
B11110000,
B10100000,
B11110000,
B00000000,
B11110000,
B00100000,
B01100000,
B10010000
}
我试过了,但最后完全不知道该怎么做。
编辑。可能会被误解,所以我在代码中加了括号,现在更清楚了。
这是一个使用纯 C (gcc) 的示例:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint8_t byte;
void print_bin(byte x) {
printf("B");
for (int i = 0; i < 8; i++) {
printf("%s", (x >> (7-i)) % 2 ? "1" : "0");
}
printf("\n");
}
void reverse(byte* in, byte* out, int width, int height) {
int width_bytes = (width + 7) / 8;
int height_bytes = (height + 7) / 8;
// init *out. You can skip the next line if you are sure that *out is clear.
memset (out, 0, width * height_bytes);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (in[(y * width_bytes + x / 8)] & (1 << (7 - x % 8))) {
out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
}
}
}
}
#define WIDTH 13
#define HEIGHT 4
#define IN_SIZE (((WIDTH + 7) / 8) * HEIGHT)
#define OUT_SIZE (((HEIGHT + 7) / 8) * WIDTH)
int main() {
byte in[IN_SIZE] = {
0b00111100, 0b01001000,
0b00100100, 0b01010000,
0b00111100, 0b01110000,
0b00100100, 0b01001000
};
byte* out = calloc(OUT_SIZE, 1);
reverse (in, out, WIDTH, HEIGHT);
for (int i = 0; i < OUT_SIZE; i++) {
print_bin(out[i]);
}
}
这是结果:
B00000000
B00000000
B11110000
B10100000
B10100000
B11110000
B00000000
B00000000
B00000000
B11110000
B00100000
B01100000
B10010000
如果速度有问题,您可以进行以下优化:
void reverse(byte* in, byte* out, int width, int height) {
int width_bytes = (width + 7) / 8;
int height_bytes = (height + 7) / 8;
// init *out. You can skip the next line if you are sure that *out is clear.
memset (out, 0, width * height_bytes);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int t; // optimisation
if ((x % 8) == 0) t = in[(y * width_bytes + x / 8)];
if (t & (1 << (7 - x % 8))) {
out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
}
}
}
}
我正在尝试对我的应用程序进行妥协,但到目前为止还没有运气(或者更确切地说是知识)。
我有黑白屏幕的位图,它看起来像这样(我使用arduino字节风格,因为它更具可读性)
{
B00111100, B01001000,
B00100100, B01010000,
B00111100, B01110000,
B00100100, B01001000
}
它是字节数组,每个字节代表接下来的8个水平像素。 问题是我必须使用位图,其中每个字节代表 8 个下一个垂直像素,所以就像这样转动它
{
B00000000,
B00000000,
B11110000,
B10100000,
B11110000,
B00000000,
B11110000,
B00100000,
B01100000,
B10010000
}
我试过了,但最后完全不知道该怎么做。
编辑。可能会被误解,所以我在代码中加了括号,现在更清楚了。
这是一个使用纯 C (gcc) 的示例:
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef uint8_t byte;
void print_bin(byte x) {
printf("B");
for (int i = 0; i < 8; i++) {
printf("%s", (x >> (7-i)) % 2 ? "1" : "0");
}
printf("\n");
}
void reverse(byte* in, byte* out, int width, int height) {
int width_bytes = (width + 7) / 8;
int height_bytes = (height + 7) / 8;
// init *out. You can skip the next line if you are sure that *out is clear.
memset (out, 0, width * height_bytes);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
if (in[(y * width_bytes + x / 8)] & (1 << (7 - x % 8))) {
out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
}
}
}
}
#define WIDTH 13
#define HEIGHT 4
#define IN_SIZE (((WIDTH + 7) / 8) * HEIGHT)
#define OUT_SIZE (((HEIGHT + 7) / 8) * WIDTH)
int main() {
byte in[IN_SIZE] = {
0b00111100, 0b01001000,
0b00100100, 0b01010000,
0b00111100, 0b01110000,
0b00100100, 0b01001000
};
byte* out = calloc(OUT_SIZE, 1);
reverse (in, out, WIDTH, HEIGHT);
for (int i = 0; i < OUT_SIZE; i++) {
print_bin(out[i]);
}
}
这是结果:
B00000000
B00000000
B11110000
B10100000
B10100000
B11110000
B00000000
B00000000
B00000000
B11110000
B00100000
B01100000
B10010000
如果速度有问题,您可以进行以下优化:
void reverse(byte* in, byte* out, int width, int height) {
int width_bytes = (width + 7) / 8;
int height_bytes = (height + 7) / 8;
// init *out. You can skip the next line if you are sure that *out is clear.
memset (out, 0, width * height_bytes);
for (int y = 0; y < height; y++) {
for (int x = 0; x < width; x++) {
int t; // optimisation
if ((x % 8) == 0) t = in[(y * width_bytes + x / 8)];
if (t & (1 << (7 - x % 8))) {
out[(x * height_bytes + y / 8)] |= (1 << (7 - y % 8));
}
}
}
}