burnedWood
burnedWood

Reputation: 93

How to turn a Width*Height sized 1d array of RGB565 values into an image in C++

I have a 1d array of 16 bit integers that represent RGB565 pixels. In my understanding this means:

The 5 most significant bits represent red. The 6 next bits represent green. The 5 least significant bits represent blue.

the size of the array is width * height and they are known values.

How do I turn this into a file that I can view?

The file format doesn't matter, as long as it's something I can view!

I am aware of Magick++.h but I'm not sure it can do that. I am also open to command line tools for suggestions.

Upvotes: 0

Views: 876

Answers (1)

Mark Setchell
Mark Setchell

Reputation: 207465

Here are a couple of methods. I used a random sample image I found from here.


First, using ImageMagick:

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

Second, using ffmpeg:

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

The third method uses Perl, which is actually included in all macOS versions:

#!/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;
}

The result is the same:

enter image description here

Keywords: Image processing, RGB565, ImageMagick, ffmpeg, convert RGB565 to PNG, convert RGB565 to JPEG, prime.

Upvotes: 1

Related Questions