How to properly load binary data and display it?


I just have a silly question. I got a screenshot of my android phone made through ADB, a command line tool. The tool have 2 options, it takes a screenshot and saves it in png format or raw binary. I choose to save it as binary data since the conversion from raw data over png is too compute-expensive to calculate (It takes twice as much to generate a PNG with phone’s resources).

Anyway, I tried the following in a gmic-cli compilation I made a couple of years ago (2.0.0).

gmic -i screen.raw,int,720,1520,1,3

And I can get gmic to succesfully display a picture with the right dimensions but no color information, besides everything looking blue and white. I know all the color information is in the raw data since whenever a filter is applied tonality is affected.

I’m trying this because I want to “see” my phone’s screen (it’s damaged). So I made a loop that takes pictures and save them as raw data to later pull them through ADB and a sshfs setup. I got the data but I don’t know how to properly handle it.

Basically I don’t know how to set values to represent RGB from binary data.
How can I properly setup the input data to see the actual screenshot?
Any help is appreciated!

screen.raw (4.2 MB)

I get something with:

$ gmic screen.raw,uchar rows 16,100% r 4,720,1520,1,-1 permute yzcx

There is a 16 bytes header I’ve removed.
And the raw data is actually in RGBA, not RGB.

EDIT: My first answer was wrong (not in color), I’ve edited it**.

1 Like


Now I need to check the manual to try to understand the magic you did!

Thank you very much. :slight_smile:

I need to add that G’MIC is wonderful, thank you very much to make it available to the whole world!

1 Like

There’s actually not much magic :slight_smile: :

Your .raw data is organized as follow:

  • a 16 bytes header (I don’t know what’s inside, but probably the image dimension at least).
  • a sequence of 720x1520 32bits integers, coding the colors, in an interleaved way: RGBARGBARGBA…

As there is a header, we cannot use the input options when we input the .raw file directly. I just specify uchar to isolate read it byte by byte.

Then, rows 16,100% allows me to discard the header.
The resize after actually creates a 4x720x1520x1 image from the remaining raw data, and I use permute to “de-interleave” the image data.

1 Like

Thank you for providing me details about what’s going on.

Sorry to bother you, but I got a couple of questions:

  1. I’m confused about this parameter:
-r 4,720,1520,1,-1

According to the manual, resize takes 7 options, being:

Which in this case we care only about 6 of them, since we are working with a single image.
I assume the boundary condition is the one not present in the line above, since Interpolation=4 does not require to set a value to _boundary_condition
But it shouldn’t mean that we have to specify a value (zero) like so?:


Or an empty space?


Also, why spectrum is set to -1 and not 4 (RGBA)?

  1. Regarding the permutation.
    What’s the analysis on the binary data to know which values to permute?

Thank you very much!

Another question, sorry.
How raw binary data is piped into gmic?

Doing, does shows a picture:

cat image.png | gmic -.png

But by doing this, shows errors (doesn’t found the data/file):

adb shell "screencap" | gmic -.raw,uchar ...

Which essentially is the same as doing:

cat screen.raw | gmic -.raw,uchar ...

Not in this case, I use the version of resize that takes as argument:

Note that an argument starting with an underscore can be actually omitted (it has a default value).
In this case, I want width=4, height=720, depth=1520 and spectrum=1, because I know this will end up to a 720x1520x1x4 image after the use of command permute.

For the other questions : a .raw file contains no information about the image dimensions, so G’MIC wouldn’t know when to stop reading stdin.
If you specify the image dimensions, then it is OK (at least with the latest version 2.9.6) :

$ gmic sp lena,16 o -.raw | gmic -.raw,uchar,16,16,1,3
[gmic]-0./ Start G'MIC interpreter.[gmic]-0./ Start G'MIC interpreter.
[gmic]-0./ Input raw file '-.raw' with type 'uchar' at position 0
[gmic]-1./ Input sample image 'lena' (1 image 16x16x1x3).
[gmic]-1./ Output image [0] as raw file '-.raw', with pixel type 'auto' (1 image 16x16x1x3).
[gmic]-1./ End G'MIC interpreter.
 (1 image 16x16x1x3).
[gmic]-1./ Display image [0] = '-.raw'.
[0] = '-.raw':
  size = (16,16,1,3) [3072 b of floats].
  data = (226,225,173,191,206,209,207,206,205,196,208,225,(...),98,74,78,93,102,104,135,164,90,88,83,81).
  min = 24, max = 240, mean = 128.258, std = 52.1119, coords_min = (15,1,0,1), coords_max = (15,10,0,0).
[gmic]-1./ End G'MIC interpreter.
1 Like

Thank you, setting up dimensions worked.

This explains a lot.

Once again, thank you very much!
I really appreciated.

1 Like