Page 1 of 1
RGBA 5551 raw binary
Posted: 2011-11-01T07:36:43-07:00
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:
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?
Re: RGBA 5551 raw binary
Posted: 2011-11-01T19:42:32-07:00
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.
Re: RGBA 5551 raw binary
Posted: 2011-11-02T01:45:06-07:00
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.
Re: RGBA 5551 raw binary
Posted: 2011-11-02T20:06:32-07:00
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
Re: RGBA 5551 raw binary
Posted: 2011-11-03T01:17:21-07:00
by andrey
Whoa! Nice. It works. I haven't event thought in that direction. Thank you very much.