How to view raw binary data as an image with given width and height?

Does there exist a program for Linux that can display raw binary data?

Each byte in my binary files represent a pixel, so it would be very useful if something like this exists where I could say

program_name --input=dat001.bin --width=200 --height=100

and it would display the pixels.

I wonder if gnuplot, can be used for this...?


Solution 1:

To see the "raw binary data", I would use the hex dump command hd or hexdump

$ hd -C a.txt
00000000  61 0a 61 61 0a 61 61 61  0a 61 61 61 61 0a 61 61  |a.aa.aaa.aaaa.aa|
00000010  61 61 61 0a 62 62 62 0a  62 62 62 62 0a 62 62 62  |aaa.bbb.bbbb.bbb|
00000020  62 62 0a 3c 62 65 67 69  6e 3e 0a 61 61 61 61 61  |bb.<begin>.aaaaa|
00000030  61 0a 61 61 61 61 61 61  61 0a 61 61 61 61 61 61  |a.aaaaaaa.aaaaaa|
00000040  61 61 0a                                          |aa.|
00000043

I don't know of any image format that consists of unstructured bytes - is the data 8-bit RGB values? If the file contains 30000 bytes is that RGB for 100x100 pixels or RGB for 50x200 pixels or RGB for 200x50 pixels or something else? Is there a palette? You have to know something about the organisation of the data!

To view it as an image I would use the NetPBM utilities or maybe ImageMagick to convert it to a form understood by an image viewer

If the above can't do the job I'd investigate writing a small Perl script

Solution 2:

Okay, gnuplot can do it.

http://gnuplot.sourceforge.net/demo_4.4/image.html

Solution 3:

convert from ImageMagick

E.g., an 8-bit 2x3 grayscale:

printf '\x00\xFF\x88\xFF\x00\xFF' > in.bin

Then:

convert -depth 8 -size 3x2+0 gray:in.bin out.png

Command explanation:

  • -depth 8: each color has 8 bits
  • -size 2x3+0: 2x3 image. +0 means starting at offset 0 in the file. If there are metadata headers, you can skip them with the offset.
  • gray:in.bin: the input file is in.bin, and the format is gray, as defined at http://www.imagemagick.org/script/formats.php This weird notation is used because ImageMagick usually determines the format from the extension, but here there is no extension.

How to view such tiny outputs like this example

The problem now is how to view the such a tiny 3x2 output accurately. A direct eog:

eog out.png

is not very good because the image is too small, and if you zoom in a lot eog uses a display algorithm that mixes up pixels continuously, which is better for most pictures, but not for such a tiny image.

One good possibility is to run:

convert out.png -scale 300x out2.png

or directly in one go with:

convert -depth 8 -size 3x2+0 gray:in.bin -scale 300x out.png

-scale is needed instead of -resize, since -resize mixes pixel colors continuously up much like eog by default.

Output:

enter image description here

Anther option is to view it in Gimp:

gimp out.png

Image editors such as Gimp must show every single pixel separately if you zoom in enough.

RGB example

printf '\xFF\x00\x00\x00\xFF\x00\x00\x00\xFF' > in.bin
convert -depth 8 -size 3x1+0 rgb:in.bin out.png

or with the -scale to make it more viewable:

convert -depth 8 -size 3x1+0 rgb:in.bin -scale 300x out.png

enter image description here

Related:

  • https://askubuntu.com/questions/147554/what-software-can-display-raw-bitmaps-on-linux

Tested on Ubuntu 16.04, ImageMagick 6.8.9.

Solution 4:

If not wanting to view it directly but rather convert it the convert utility can do this:

To read from stdin, assuming 320 x 200 pixels, 8 bit gray, header of 0, saving to pic.png in PNG format.

convert -depth 8 -size 320x200+0 gray:- pic.png