convert from random input file to 1 bit bitmap

Questions and postings pertaining to the usage of ImageMagick regardless of the interface. This includes the command-line utilities, as well as the C and C++ APIs. Usage questions are like "How do I use ImageMagick to create drop shadows?".
Post Reply
Dannick
Posts: 6
Joined: 2014-08-07T14:00:58-07:00
Authentication code: 6789
Contact:

convert from random input file to 1 bit bitmap

Post by Dannick »

I want to be able to visualize the bit distribution in a file containing random bytes, like this Image.

At the moment, I do it by converting the data file to ascii, and then to PBM (P1), and then convert it to a bitmap using ppmtobmp.

This is not a pretty way of doing it, but it works :/

How do I do this with convert? I do not know where to begin, since the input file is not an image but just a bunch of random bytes.

I tried "fooling" convert it was a an RGB file, eg: convert -depth 1 -monochrome -size 1920x1080.rgba output.bmp

This will give me a monochrome picture, but it is converted wrong, compared to the ppmtobmp way. It looks like it does not actually use 1 bit per pixel :(

Any starting pointers on how to do this?
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK
Contact:

Re: convert from random input file to 1 bit bitmap

Post by snibgo »

IM can directly read and write P1 files, eg:

Code: Select all

f:\web\im>%IM%convert -size 10x10 gradient: -type bilevel -compress none x.pbm

f:\web\im>type x.pbm
P1
10 10
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 1 0 0 1 1 1 1 1 1 0 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
"-compress none" means write ASCII text.

There is also a .gray format:

Code: Select all

f:\web\im>%IM%convert -size 100x100 gradient: -type bilevel -depth 1 x.gray

f:\web\im>%IM%convert -size 100x100 -depth 1 x.gray x.png
snibgo's IM pages: im.snibgo.com
Dannick
Posts: 6
Joined: 2014-08-07T14:00:58-07:00
Authentication code: 6789
Contact:

Re: convert from random input file to 1 bit bitmap

Post by Dannick »

Thanks, but how do I do this without having to go via the pbm format?
I would like to do: convert [arguments] random.dat output.bmp
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK
Contact:

Re: convert from random input file to 1 bit bitmap

Post by snibgo »

You'll have to experiment, as I can't see your file. Try:

Code: Select all

convert -size 1920x1080 -depth 1 GRAY:random.dat output.bmp
If that doesn't work, try:

Code: Select all

rename random.dat random.gray

convert -size 1920x1080 -depth 1 random.gray output.bmp
snibgo's IM pages: im.snibgo.com
Dannick
Posts: 6
Joined: 2014-08-07T14:00:58-07:00
Authentication code: 6789
Contact:

Re: convert from random input file to 1 bit bitmap

Post by Dannick »

And experimenting I did! Thanks for the GRAY tip. This is how I got it perfected. Trial & error ftw...

Reference bmp (converted from pbm) :

Code: Select all

out1.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.009
Attempt 1:

Code: Select all

$ convert -size 1920x1080 -depth 1 GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 8-bit DirectClass 6.221MB 0.000u 0:00.009
Wrong depth in output bmp :(
Attempt 2:

Code: Select all

$ convert -size 1920x1080 -depth 1 -monochrome GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 8-bit DirectClass 6.221MB 0.000u 0:00.009
Still wrong depth :?
Attempt 3:

Code: Select all

$ convert -size 1920x1080 -depth 1 -monochrome -type bilevel GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 8-bit DirectClass 6.221MB 0.000u 0:00.000
And... still wrong depth :-|
Attempt 4:

Code: Select all

$ convert -size 1920x1080 -depth 1 -monochrome -type bilevel -colors 2 GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.000
Finally! Now, perhaps I don't need all those options... Trimming:

Code: Select all

$ convert -size 1920x1080 -depth 1 -colors 2 GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.000
Great! But... the image is not identical to the reference one :?
At closer inspection, they look like inversed versions of each other. Ah... *one minute googling later since -inverse didn't work*
Attempt 5:

Code: Select all

$ convert -size 1920x1080 -depth 1 -colors 2 -negate GRAY:random.dat out2.bmp ; identify out2.bmp
out2.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.010
Now, the output bmp and the reference bitmap are seemingly identical. The files are not identical though - but that does not matter.

Code: Select all

out1.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.009
out2.bmp BMP 1920x1080 1920x1080+0+0 1-bit PseudoClass 2c 259KB 0.000u 0:00.010
snibgo, thanks for the rapid reply!

BTW, the random.dat is just any data. In this case it is output from /dev/urandom. To get data for a 1-bit 1920x1080 image, I just use dd:

Code: Select all

$ dd if=/dev/urandom bs=$((1920*1080/8)) count=1 of=random.dat
Dannick
Posts: 6
Joined: 2014-08-07T14:00:58-07:00
Authentication code: 6789
Contact:

Re: convert from random input file to 1 bit bitmap

Post by Dannick »

I just confirmed that the reference bmp and the convert:ed bmp are identical:

Code: Select all

$ convert out1.bmp out1.ppm ; convert out2.bmp out2.ppm ; md5sum out?.ppm
58a516ebdaf526423137a2b2a636ff7b  out1.ppm
58a516ebdaf526423137a2b2a636ff7b  out2.ppm
Hence, perfect conversion accomplished!
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK
Contact:

Re: convert from random input file to 1 bit bitmap

Post by snibgo »

Good stuff. You can pipe dd directly into convert:

Code: Select all

dd if=/dev/urandom bs=$((1920*1080/8)) count=1 | convert -size 1920x1080 -depth 1 -negate GRAY:- -colors 2 out3.bmp
(I moved "-colors 2", because this operation should occur after convert reads the input, not before.)

IM contains its own noise generators, of course. See http://www.imagemagick.org/script/comma ... .php#noise
snibgo's IM pages: im.snibgo.com
Dannick
Posts: 6
Joined: 2014-08-07T14:00:58-07:00
Authentication code: 6789
Contact:

Re: convert from random input file to 1 bit bitmap

Post by Dannick »

Yes, the IM noise generator I discovered earlier but for this operation I need a file containing the output from a known random source.

Thanks for the tip though, and very much thanks again for the GRAY tip which I've managed to miss during my IM knowledge hunt.
Post Reply

Who is online

Users browsing this forum: Bing [Bot] and 42 guests