如何在C++中把宽度*高度的RGB565值的1d数组变成图像。

问题描述 投票:0回答:1

我有一个16位整数的1d数组,代表RGB565像素。在我的理解中,这意味着。

最重要的5位代表红色,接下来的6位代表绿色,最不重要的5位代表蓝色。

数组的大小是宽度*高度,它们是已知值。

如何把这个变成一个可以查看的文件?

文件格式并不重要,只要是我可以查看的东西就可以了!

我知道Magick++.h,但我不确定它是否能做到这一点。我也愿意接受命令行工具的建议。

c++ file rgb
1个回答
1
投票

这里有几个方法。我使用了一个随机样本图像,我发现从 此处.


首先,使用 图片魔术:

magick -size 720x480 RGB565:reference.rgb565 -auto-level result.jpg

第二,使用 ffmpeg:

ffmpeg -vcodec rawvideo -f rawvideo -pix_fmt rgb565 -s 720x480 -i reference.rgb565 -f image2 -vcodec png image.png

第三种方法是: Perl实际上包括在所有的 macOS 版本。

#!/usr/bin/perl -w
use autodie;

$num_args = $#ARGV + 1;
if ($num_args != 3) {
    printf "Usage: RGB565.pl width height RB565file > result.ppm\n";
    exit;
}

$w=$ARGV[0];  # width
$h=$ARGV[1];  # height
$f=$ARGV[2];  # filename

# Open file as raw
open my $fh, '<:raw', $f;

# Output PPM header https://en.wikipedia.org/wiki/Netpbm#PPM_example
print "P6\n$w $h\n255\n";

# Read w*h pixels
for(my $p=0; $p<($w * $h); $p++){
    # Read 2 bytes of RGB565
    read $fh, my $RGB565, 2;
    my ($b2, $b1) = unpack 'cc', $RGB565;
    # Write 3 bytes of RGB888
    my $r = $b1 & 0xF8;
    my $g = (($b1 & 0x07)<<5) | (($b2 & 0xE0)>>3);
    my $b = ($b2 & 0x1F) << 3;
    printf "%c%c%c", $r, $g, $b;
}

结果是一样的。

enter image description here

关键词:: 图像处理, RGB565, ImageMagick, ffmpeg, RGB565转PNG, RGB565转JPEG.

© www.soinside.com 2019 - 2024. All rights reserved.