16bit TGA with transparency
Posted: 2015-04-28T09:31:38-07:00
Hi!
I'm doing some embedded (arduino + color LCD) projects and was looking for a file format that has transparency and low bit count. Arduinos have little processing power and little memory, so jpeg and png decoding was ruled out. I wanted to use BMP files but imagemagick does not support 16bpp with transparency. I chose TGA as my format of choice.
I saw that since 6.9.0, 16 bit TGA files were supported. However, I had a lot of problems with transparency.
First, the TGA writing was wrong about "and-ing" 80 to the 16 bits to do alpha channel. It should have been 0x80. (The MSB of the byte)
But then struck another problem. Not a lot of viewer are able to parse 16 bits TGA with transparency properly. The only one working about right is gimp. But the files were still incorrectly displayed. Based on a specification sheet of TGA ( http://www.fileformat.info/format/tga/egff.htm ), and the fact that with 32bpp files, a fully transparent pixel is one with 0 value in the attribute byte, I extrapoled that the attribute bit would be the same in 16bpp files and 1 for a fully opaque pixel. So, the current implementation was reversed.
That where the < midpoint change to > midpoint comes from.
Also, the header also did not specified the number of attribute bits when using 16bpp files, as it did when using 32bpp.
So I added a little logic to put a 1 in the "Image Descriptor Byte".
Lastly, when inputing the resulting file back into IM, the transparency was inverted.
This is the reasong the ternary operation being inverted.
With all this, the 16bpp files created with IM can now succesfully be read with gimp and a file created with it can succesfully be converted back to an other format with IM.
The patch is here:
http://pastebin.com/suC6vV6W
Thanks
Patrick
References:
http://www.fileformat.info/format/tga/egff.htm
http://www.paulbourke.net/dataformats/tga/
https://rt.cpan.org/Public/Bug/Display.html?id=32926
https://bugzilla.gnome.org/show_bug.cgi?id=114913
I'm doing some embedded (arduino + color LCD) projects and was looking for a file format that has transparency and low bit count. Arduinos have little processing power and little memory, so jpeg and png decoding was ruled out. I wanted to use BMP files but imagemagick does not support 16bpp with transparency. I chose TGA as my format of choice.
I saw that since 6.9.0, 16 bit TGA files were supported. However, I had a lot of problems with transparency.
First, the TGA writing was wrong about "and-ing" 80 to the 16 bits to do alpha channel. It should have been 0x80. (The MSB of the byte)
But then struck another problem. Not a lot of viewer are able to parse 16 bits TGA with transparency properly. The only one working about right is gimp. But the files were still incorrectly displayed. Based on a specification sheet of TGA ( http://www.fileformat.info/format/tga/egff.htm ), and the fact that with 32bpp files, a fully transparent pixel is one with 0 value in the attribute byte, I extrapoled that the attribute bit would be the same in 16bpp files and 1 for a fully opaque pixel. So, the current implementation was reversed.
That where the < midpoint change to > midpoint comes from.
Also, the header also did not specified the number of attribute bits when using 16bpp files, as it did when using 32bpp.
So I added a little logic to put a 1 in the "Image Descriptor Byte".
Lastly, when inputing the resulting file back into IM, the transparency was inverted.
This is the reasong the ternary operation being inverted.
With all this, the 16bpp files created with IM can now succesfully be read with gimp and a file created with it can succesfully be converted back to an other format with IM.
The patch is here:
http://pastebin.com/suC6vV6W
Thanks
Patrick
References:
http://www.fileformat.info/format/tga/egff.htm
http://www.paulbourke.net/dataformats/tga/
https://rt.cpan.org/Public/Bug/Display.html?id=32926
https://bugzilla.gnome.org/show_bug.cgi?id=114913