Convert png to TGA-16

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
jasonwpalmer
Posts: 4
Joined: 2014-01-01T10:47:32-07:00
Authentication code: 6789

Convert png to TGA-16

Post by jasonwpalmer »

I am trying to convert a PNG 8888 to TGA 5551 format. I am a novice with Image Magick and do not know if this is currently possible. I understand that TGA is outdated and used very little today. Can somebody please point me in the right direction?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert png to TGA-16

Post by fmw42 »

I know little about TGA format, but a conversion of 32-bit (8/8/8/8) png to tga format (via PNG8) shows the result as 8/8/8/1

convert rose: -fuzz 25% -transparent red -channel a -blur 0x1 +channel -depth 8 rose_tmp.png

Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 8-bit


convert rose_tmp.png PNG8:- | convert - rose_tmp.tga

Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit
alpha: 1-bit


For some reason, the following does not work to convert to 8-bit total color

convert rose_tmp.png -type palettealpha rose_tmp2.tga

Channel depth:
red: 8-bit
green: 8-bit
blue: 8-bit


It ends up 24-bit color and no alpha.


I do not know if it is possible to get 5:5:5:1 as output.
jasonwpalmer
Posts: 4
Joined: 2014-01-01T10:47:32-07:00
Authentication code: 6789

Re: Convert png to TGA-16

Post by jasonwpalmer »

Thanks for the attempt.

I have learned that whenever you ask Image Magick for a tga file - it only produces a tga with 8 bits per channel as you just experienced. I think the answer is to extract the channels, truncate the 8-bit rgb values to 5-bit rgb values and then add a bitmask alpha layer based on the alpha in the png. Which incidentally is stored with no translucent pixels, meaning every value should be 0 or 255 for the alpha in the source image file. Then I imagine Image Magick would need to know how to write the header - of which I already know the values I want to insert. But, like I said - I am not experienced enough with Image Magick to know which commands to call or how to go about this.
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert png to TGA-16

Post by fmw42 »

-fx (http://www.imagemagick.org/script/fx.php) can do bit shifting. Howerver, IM may identify that as 8/5-bits or something like that. I do not think IM has any way to write headers. But I am not a software engineer to know enough about this. You can probably also convert to 15 bits (5:5:5) by division or multiplications. Binarizing the alpha channel is easy, just use -threshold 50%.

In other words, take the png, convert to 32-bit color (8/8/8/8), shift the bits on the RGB or zero out the lowest 3 bits in each, then threshold the alpha channel, then convert as I did to tga. It will be reported as 8-bit total color (but really only the 5 most significant bits are being used) and the alpha channel will be 1-bit (0 or 255). I do not know if this is acceptable for your purposes or not. If you need to read a proper 15-bit color and 1-bit alpha tga, then this may not suffice.

You may want to look for other conversion tools that can deal with that format tga.
User avatar
glennrp
Posts: 1147
Joined: 2006-04-01T08:16:32-07:00
Location: Maryland 39.26.30N 76.16.01W

Re: Convert png to TGA-16

Post by glennrp »

Looking at the source code (coders/tga.c), it appears to have the ability to
read 5-bit-per-channel RGB images, but can only write 8-bit-per-channel.
So converting from PNG can't give you a TGA-16 with 5 bits per color sample.
jasonwpalmer
Posts: 4
Joined: 2014-01-01T10:47:32-07:00
Authentication code: 6789

Re: Convert png to TGA-16

Post by jasonwpalmer »

I have also looked at the source code and this is why I know that tga.c cannot be used to write the tga, however - the header on a tga-16 is only 18 bytes. So in theory, I believe the bit-shifting could be done with a proper fx: command. The next problem would be writing the header - I thought I remembered seeing somewhere that individual header values can be written with Image Magick, but can an entire header be inserted by Image Magick if I feed it the correct values? Is this possible?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: Convert png to TGA-16

Post by fmw42 »

I know of no function in IM that can modify or create headers. But I am not a software engineer. However, you might look into EXIFTOOL. I believe it can modify meta data (in the header). So if you can modify the pixel data in IM in a tga format and them write it out. You may be able to use Exiftool to modify the data to correspond.
jasonwpalmer
Posts: 4
Joined: 2014-01-01T10:47:32-07:00
Authentication code: 6789

Re: Convert png to TGA-16

Post by jasonwpalmer »

I appreciate your help.

I have been able to write the TGA Image files by Implementing a codec with JAI. I was hoping to be able to just use Image Magick as JAI (Java Advanced Imaging) hasn't changed or been developed for over 7 years now.

Wishful thinking I suppose.

Thanks again for your time.
Post Reply