24位bmp到RGB565文件转换 [英] 24 bit bmp to RGB565 file conversion
问题描述
我想将24位bmp文件转换为RGB565格式以写入串行TFT彩色显示器.
I want to convert 24 bit bmp files to RGB565 format to write to a serial TFT colour display.
24位bmp的大小将始终为320x240像素,因为我的TFT显示屏为320x240
The size of the 24bit bmp will always be 320x240 pixels as my TFT display is 320x240
有人有这样做的经验吗?可能是C/C ++,Shell,Python,Java Script等...
Has anyone any experience of doing this? It could be C/C++, Shell, Python, Java Script and so on...
推荐答案
我将使用 NetPBM (体积更小,重量更轻)或 ImageMagick (体积更大的安装) )将BMP
转换为易于解析的格式,然后使用 Perl 将其转换为RGB565格式.
I would use either NetPBM (much smaller and lighter-weight) or ImageMagick (much bigger installation) to convert the BMP
into a format that is simple to parse and then use Perl to convert that to RGB565 format.
我假设您打算将RGB565数据写入帧缓冲区,所以您将执行以下操作:
I assume you are planning to write the RGB565 data to a frame buffer, so you would do something like:
./bmp2rgb565 image.bmp > /dev/fb1
因此,将以下内容另存为bmp2rgb565
:
So, save the following as bmp2rgb565
:
#!/bin/bash
################################################################################
# bmp2rgb565
# Mark Setchell
################################################################################
if [ $# -ne 1 ]; then
echo Usage: $0 image.bmp
exit 1
fi
file=$1
# Use NetPBM's "bmptopnm" to convert BMP to PNM for easy reading
# You could use ImageMagick: convert "$file" PNM: | perl ...
bmptopnm "$file" 2> /dev/null |
perl -e '
my $debug=0; # Change to 1 for debugging
# Discard first 3 lines of PNM header:
# P3
# 320 240
# 255
my $line=<STDIN>; $line=<STDIN>; $line=<STDIN>;
# Read file, 3 RGB bytes at a time
{
local $/ = \3;
while(my $pixel=<STDIN>){
# Extract 8-bit R,G and B from pixel
my ($r,$g,$b)=unpack("CCC",$pixel);
printf("R/G/B: %d/%d/%d\n",$r,$g,$b) if $debug;
# Convert to RGB565
my $r5=$r>>3;
my $g6=$g>>2;
my $b5=$b>>3;
my $rgb565 = ($r5<<11) | ($g6<<5) | $b5;
# Convert to little-endian 16-bit (VAX order) and write 2 bytes
my $v=pack("v",$rgb565);
syswrite(STDOUT,$v,2);
}
}
'
我没有方便测试的帧缓冲区,但是应该很接近.
I don't have a frame buffer handy to test, but it should be pretty close.
请注意,您可以从以下代码开始使代码更健壮:
Note that you could make the code more robust by starting off with:
convert "$file" -depth 8 -resize 320x240\! PNM: | perl ...
这将确保图像始终与帧缓冲区大小匹配,并且它是8位而不是16位.如果BMP
图像是上下颠倒还是从正面到背面,则可能还需要-flip
或-flop
.
which would make sure the image always matches the framebuffer size, and that it is 8-bit and not 16-bit. You may also want a -flip
or -flop
in there if the BMP
image is upside-down or back-to-front.
请注意,如果使用 ImageMagick convert
,该代码将适用于GIF,TIFF,JPEG,PNG和大约150种其他格式以及BMP.
Note that if you use ImageMagick convert
, the code will work for GIFs, TIFFs, JPEGs, PNGs and around 150 other formats as well as BMP.
请注意,如果要测试代码,可以使用 ImageMagick 生成黑色图像,如下所示:
Note that if you want to test the code, you can generate a black image with ImageMagick like this:
convert -size 320x240 xc:black BMP3:black.bmp
,然后在运行时在输出中寻找一串零:
and then look for a bunch of zeroes in the output if you run:
./bmp2rgb565 black.bmp | xxd -g2
同样,您可以生成白色图像并查找一堆ff
:
Likewise, you can generate a white image and look for a bunch of ff
s:
convert -size 320x240 xc:red BMP3:white.bmp
以红色,绿色和蓝色依次类推:
And so on with red, green and blue:
convert -size 320x240 xc:red BMP3:red.bmp
convert -size 320x240 xc:lime BMP3:green.bmp
convert -size 320x240 xc:blue BMP3:blue.bmp
# Or make a cyan-magenta gradient image
convert -size 320x240 gradient:cyan-magenta cyan-magenta-gradient.bmp
示例:
./RGB565 red.bmp | xxd -g2 | more
00000000: 00f8 00f8 00f8 00f8 00f8 00f8 00f8 00f8 ................
00000010: 00f8 00f8 00f8 00f8 00f8 00f8 00f8 00f8 ................
示例:
./RGB565 blue.bmp | xxd -g2 | more
00000000: 1f00 1f00 1f00 1f00 1f00 1f00 1f00 1f00 ................
00000010: 1f00 1f00 1f00 1f00 1f00 1f00 1f00 1f00 ................
关键字::RGB565,rgb565,帧缓冲区,帧缓冲区,打包,解包,Perl,BMP,PGM,图像,Raspberry Pi,RASPI
Keywords: RGB565, rgb565, framebuffer, frame-buffer, pack, unpack, Perl, BMP, PGM, image, Raspberry Pi, RASPI
这篇关于24位bmp到RGB565文件转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!