将单图位图转换为8位整数的1D C数组
为了澄清标题,我需要将其转换
"00011111111111110000111111111111
00101111111111110000111111111111
00111111111111110000111111111111
01001111111111110000111111111111
01011111111111110000111111111111
01101111111111110000111111111111
01111111111111110000111111111111
10001111111111110000111111111111"
为
0b00011111,
0b00101111,
0b00111111,
0b01001111,
0b01011111,
0b01101111,
0b01111111,
0b10001111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
...
每个8x8二进制块的二进制块,以所示格式转换为C数组。在我运行的实际数据中,位图尺寸为128、48,(宽度,高度),因此您不能假设位图高8个。 (否则我不会问这个,这是很重要的)
这是我当前的代码:
# input_str is a (very long) string of binary representation of a bitmap of height 48, width 128.
# I have removed it here as it is irrelevant other than its dimensions.
# I am aware that this code is a mess, but this is just a quick script that I'm only using to
# automate this task once, as I would rather not sit at my desk for 6 hours sorting out 8x8 blocks
# of binary from a very long string.
input_str = ""
pass_0 = ""
output_str = ""
IMG_WIDTH = 128
IMG_HEIGHT = 48
BLOCK_SIZE = 8
# Split the bitmap into lines on each 128th index
for i in range(len(input_str)):
pass_0 += input_str[i]
if (i % IMG_WIDTH == 0):
pass_0 += "\n"
# Interate through the lines and get an X block index and a Y block index
blockIndexX = 0
blockIndexY = 0
y = 0
for line in iter(pass_0.splitlines()):
if (y % BLOCK_SIZE == 0):
blockIndexY += 1
for x in range(len(line)):
if (x % BLOCK_SIZE == 0):
blockIndexX += 1
blockIndex = (blockIndexX * 6) + blockIndexY
# And I have no idea how to continue here.
y += 1
blockIndexX = 0
print(output_str)
To clarify the title, I need to convert, say,
"00011111111111110000111111111111
00101111111111110000111111111111
00111111111111110000111111111111
01001111111111110000111111111111
01011111111111110000111111111111
01101111111111110000111111111111
01111111111111110000111111111111
10001111111111110000111111111111"
into
0b00011111,
0b00101111,
0b00111111,
0b01001111,
0b01011111,
0b01101111,
0b01111111,
0b10001111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
0b11111111,
...
Each 8x8 block of binary is converted into a C array in the format shown. In the actual data I'm operating on, the bitmap dimensions are 128, 48, (width, height), so you can't assume that the bitmap is 8 high. (otherwise I wouldn't be asking this and it would be trivial to do)
This is my current code:
# input_str is a (very long) string of binary representation of a bitmap of height 48, width 128.
# I have removed it here as it is irrelevant other than its dimensions.
# I am aware that this code is a mess, but this is just a quick script that I'm only using to
# automate this task once, as I would rather not sit at my desk for 6 hours sorting out 8x8 blocks
# of binary from a very long string.
input_str = ""
pass_0 = ""
output_str = ""
IMG_WIDTH = 128
IMG_HEIGHT = 48
BLOCK_SIZE = 8
# Split the bitmap into lines on each 128th index
for i in range(len(input_str)):
pass_0 += input_str[i]
if (i % IMG_WIDTH == 0):
pass_0 += "\n"
# Interate through the lines and get an X block index and a Y block index
blockIndexX = 0
blockIndexY = 0
y = 0
for line in iter(pass_0.splitlines()):
if (y % BLOCK_SIZE == 0):
blockIndexY += 1
for x in range(len(line)):
if (x % BLOCK_SIZE == 0):
blockIndexX += 1
blockIndex = (blockIndexX * 6) + blockIndexY
# And I have no idea how to continue here.
y += 1
blockIndexX = 0
print(output_str)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我设法弄清楚了一种(非常骇人听闻的)方式,那就是将我的输入位图重新格式化为8x768图像,“伸出” 8x8块,使其(某种程度上)琐碎地将其解析到8x8块中。以这种格式,这种(非常Janky and Hacky)代码有效:
现在,我觉得我应该这么说:如果您是一个遇到这个人试图做我同样的事情的人,请不要使用这一点代码。只需使用一个用于循环和列表,而不是这种笨拙的黑客入侵。
I managed to figure out a (very hacky) way of doing it, which is to reformat my input bitmap into a 8x768 image, "stretching out" the 8x8 blocks, making it (somewhat) trivial to parse into the 8x8 blocks. In that format, this bit of (very janky and hacky) code works:
Now, I feel that I should say this: If you're a person coming across this trying to do the same thing I am, please do not use this bit of code. Just use a for loop and a list, not this janky hacked together mess.