Raw Bytes to Image conversion...

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
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Raw Bytes to Image conversion...

Post by frandallfarmer »

I posted this to stackoverflow before I discovered ImageMagick...
http://stackoverflow.com/questions/4238 ... age-format

"How do I convert a file with up to 320k of bytes into an image with with 1, 2, or 3 bytes (any will do) of color?
I'm wanting to show off how small my 1980's era application is by representing 100% of the 320k of data as a bitmap image printed onto my project card.
I know the data won't look like anything but noise, but that's OK. I just want to be able to say: "That's it - those pixels uniquely represent every byte in this application!"
It'd be cool if there was a "2D barcode" app that does this, but after an hour of searching I couldn't find one that had the capacity and was implemented (plenty of papers.)"

Can ImageMagick do this for me? Stream of bytes -> 8 bit color image. I'm sure I'll have to give a image size that can hold the data, but what switches/formats can I use with ImageMagick to do this?

Thank you so much. If you're on SO, please give your answer there so I can vote it up and choose it as best answer. :-)
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Raw Bytes to Image conversion...

Post by fmw42 »

try

Code: Select all

convert image -threshold XX% image_1byte.gif

Code: Select all

convert image -monochrome image_1byte.gif

Code: Select all

convert image +dither -colors 2 -depth 2 image_2byte.gif
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Raw Bytes to Image conversion...

Post by snibgo »

I think the OP has 320,000 bytes of any old data, and wants to make this into an 8-bit/channel, 3-channel image.

First, decide what image size you want. You need (width * height) = 320000/3. For example, approx 326x326 pixels. This would need a filesize of 318,828 pixels. Suppose the file is called in.bin. Then:

Code: Select all

convert -size 326x326 -depth 8 RGB:in.bin out.png
snibgo's IM pages: im.snibgo.com
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

Wow! That's a quick reply, but I'm confused by those examples.
Are you assuming the input bytes are coming on STDIN?

Lets say my source file is called input.bin and the image will be output.gif - how would those change?

Thanks so much for the help...
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

Ugh. Only saw the code. Sorry.
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Raw Bytes to Image conversion...

Post by snibgo »

Okay, do you understand now? The main difficulty is deciding on the image size. If you have 3*N bytes, the image could be Nx1 or 1xN pixels. If N is a prime number, those are the only possibilities. (But you might pad extra bytes to the file.)
snibgo's IM pages: im.snibgo.com
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

Getting factors for my file size is easy.
The only problem I'm having now is this:

convert -size 194x92 -depth 8 RGB:caribe1.d64 out.png
convert.im6: unexpected end-of-file `caribe1.d64': No such file or directory @ error/rgb.c/ReadRGBImage/235.

I know the file is there, because bash filled it in for me auto-complete. :-)
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

It's 17484 bytes - we're talking 1 byte per pixel, right? (aka 256 color)
snibgo
Posts: 12159
Joined: 2010-01-23T23:01:33-07:00
Authentication code: 1151
Location: England, UK

Re: Raw Bytes to Image conversion...

Post by snibgo »

See my first post. 194*92*3 = 53544.
snibgo's IM pages: im.snibgo.com
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

This is working for me. Thank you very much!

convert -size 194x92 RGB:caribe1.d64 out.tiff
frandallfarmer
Posts: 7
Joined: 2017-02-22T11:33:34-07:00
Authentication code: 1151

Re: Raw Bytes to Image conversion...

Post by frandallfarmer »

Image

That is two sides of a Commodore 64 disk image that holds Lucasfilm's Habitat/Club Caribe, the first MMO/Virtual World.

I'm leading an open source effort to bring it back to life @ http://neohabitat.org http://github.com/frandallfarmer/neohabitat

Thanks for the help!
Post Reply