RGBA 5551 raw binary

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
andrey
Posts: 10
Joined: 2010-03-01T09:39:57-07:00
Authentication code: 8675308

RGBA 5551 raw binary

Post by andrey »

I have a bunch of PNG files with each pixel either completely transparent or completely opaque. I want to convert them to raw binaries (containing only pixel data - no headers, no other stuff) so each pixel is represented by two bytes containing R, G, B values 5 bit per channel and single bit alpha value - 16 bits per pixel. Alpha bit should be least significant bit of the two-byte word, followed by B, then G and R in most significant bits:
Image
All I managed to do at this moment is either export only R, G and B 5 bit values (without alpha):

Code: Select all

convert -depth 5 accept.png bgr:accept.bin
or export R, G, B and A but A is also 5 bit (with similar command line). And both produce totally unusable files because they are exported as stream of bits without padding to byte borders: pixels start and end inside bytes.
Is there any way to achieve what I want with ImageMagick?
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: RGBA 5551 raw binary

Post by anthony »

You can use stream to reformat the 556 RGB data to 8bit RGB data.
The option wanted is -map with -storage-type but I am not certain of the exact formating to use, and the reference manual is not very clear.
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
andrey
Posts: 10
Joined: 2010-03-01T09:39:57-07:00
Authentication code: 8675308

Re: RGBA 5551 raw binary

Post by andrey »

In case there is some misunderstanding: the source are 32bpp PNGs (24bpp color + 8 bit alpha, but alpha is either 0 or 255 for each pixel). I want 16bpp raw files (so in each two bytes three 5-bit color components and 1-bit alpha is packed as on image in first post), so the output file size is exactly image_height * image_width * 2.

Code: Select all

stream -map rgb -storage-type short 111.png output.bin
produces files with completely strange contents: it uses two bytes per component, while documentation states that -storage-type defines pixel storage type, and specifying -depth does not change anything while -storage-type is there. And if I remove -storage-type and add -depth:

Code: Select all

stream -map rgb -depth 5 111.png output.bin
I still get 8 bit per channel file.

And aside from that I have completely no idea how to specify alpha depth separately from color depth.
User avatar
anthony
Posts: 8883
Joined: 2004-05-31T19:27:03-07:00
Authentication code: 8675308
Location: Brisbane, Australia

Re: RGBA 5551 raw binary

Post by anthony »

Oh, you want to pack to a 5551 format, not unpack it.

Hmmm you don't say how big the image is. I'll assume 512

This reads in a raw RGBA data image (8 bits per channel)
then adjusts the values in each channel to the right bit range (assuming the IM is at least a Q16 version not Q8)
separates the four channels and adds them together (just into the red channel)
then saves just the resulting red channel as raw 16 bit integers.

Code: Select all

convert -size 512x512 -depth 8 image.rgba \
            -channel R +level 0,31 -evaluate multiply 2048 +channel \
            -channel G +level 0,31 -evaluate multiply 64 +channel \
            -channel B +level 0,31 -evaluate multiply 2 +channel \
            -channel A +level 0,1 +channel \
            -channel RGBA -separate +channel 
            -channel R -evaluate-sequence add  +channel \
            -depth 16   R:image
Anthony Thyssen -- Webmaster for ImageMagick Example Pages
https://imagemagick.org/Usage/
andrey
Posts: 10
Joined: 2010-03-01T09:39:57-07:00
Authentication code: 8675308

Re: RGBA 5551 raw binary

Post by andrey »

Whoa! Nice. It works. I haven't event thought in that direction. Thank you very much.
Post Reply