cannot produce Targa(tga) image with transparent background

Questions and postings pertaining to the development of ImageMagick, feature enhancements, and ImageMagick internals. ImageMagick source code and algorithms are discussed here. Usage questions which are too arcane for the normal user list should also be posted here.
Post Reply
academy25
Posts: 4
Joined: 2011-03-16T01:02:16-07:00
Authentication code: 8675308

cannot produce Targa(tga) image with transparent background

Post by academy25 »

Hi all, I have a QImage (QT's image class) with transparent background. I use the following code to convert it to a Magick image.

Code: Select all

Magick::Image* TgaFileType::toImage(QImage& qimage)
{

	Magick::Image *newImage ;
	double scale = 1 / 255.0;
	Magick::PixelPacket *pixels;
	Magick::ColorRGB mgc;

	newImage = new Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::ColorRGB(0.5, 0.2, 0.3));

	newImage->modifyImage();

	for (int y = 0; y < qimage.height(); y++) {
		pixels = newImage->getPixels(0, y, newImage->columns(), 1);
		for (int x = 0; x < qimage.width(); x++) {
			
			QRgb pix = qimage.pixel(x, y);

			mgc.red( scale*qRed(pix) );
			mgc.green( scale*qGreen(pix)  );
			mgc.blue( scale*qBlue(pix) );
			mgc.alpha( scale*qAlpha(pix) );

			*pixels++ = mgc;
		}
		newImage->syncPixels();
	}

	return newImage;
}
But when I write the resulting Magick image to file, the background all seems black. However, when I save the original QImage as png via

Code: Select all

qimage.save(dstFilename)
, the transparency is preserved.

Code: Select all

Magick::Image* image = toImage(*imgOriginal);
image->write(dstFilename);
The more weird thing is, even if I set the alpha directly to zero, via

Code: Select all

mgc.alpha( 0 );
and save the Magick image, it does the same thing resulting with a fully black background image.
How can I set alpha values to produce a transparent Targa image?
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: cannot produce Targa(tga) image with transparent backgro

Post by magick »

Call newImage->matte(true) to tell ImageMagick you want to respect the matte channel, otherwise the alpha values are ignored. Also make sure that there is not a problem with the TGA image format. Save to PNG with newImage->write("image.png"). Otherwise your code should work (we tried a mock example of your code and returned the correct results with ImageMagick 6.6.8-4).
academy25
Posts: 4
Joined: 2011-03-16T01:02:16-07:00
Authentication code: 8675308

Re: cannot produce Targa(tga) image with transparent backgro

Post by academy25 »

hello, I have modified the above toImage function as follows,

Code: Select all

Magick::Image* TgaFileType::toImage(QImage& qimage)
{

	Magick::Image *newImage ;
	double scale = 1 / 255.0;
	Magick::PixelPacket *pixels;
	Magick::ColorRGB mgc;

	newImage = new Magick::Image(Magick::Geometry(qimage.width(), qimage.height()), Magick::ColorRGB(0.5, 0.2, 0.3));

	newImage->modifyImage();

	for (int y = 0; y < qimage.height(); y++) {
		pixels = newImage->getPixels(0, y, newImage->columns(), 1);
		for (int x = 0; x < qimage.width(); x++) {
			
			QRgb pix = qimage.pixel(x, y);

			mgc.red( scale*qRed(pix) );
			mgc.green( scale*qGreen(pix)  );
			mgc.blue( scale*qBlue(pix) );
			mgc.alpha( scale*qAlpha(pix) );

			*pixels++ = mgc;
		}
		newImage->syncPixels();
	}

	newImage->matte(true);

	newImage->magick("PNG");
	newImage->write(&blob);
	newImage->read(blob);

	newImage->write("C:\\tgapng.png");

	return newImage;
}
but again the tga output and png output has a black background with no transparency.
The following is the input png image with a transparent background(you can save and open it via photoshop or any software that you can check its transparent part) ,
http://img820.imageshack.us/i/transparanlena.png/
And following is the png result written by the above code
http://img828.imageshack.us/i/tgapng.png/
User avatar
magick
Site Admin
Posts: 11064
Joined: 2003-05-31T11:32:55-07:00

Re: cannot produce Targa(tga) image with transparent backgro

Post by magick »

A couple of things to try. Move the matte() method before the loop. Its possible your QImage pixels are not correct. Instead set
  • mgc.red( scale*qRed(pix) );
to
  • mgc.red( scale*127.0 );
and do that for green, blue, and alpha. See if you get a semitransparent gray image.
academy25
Posts: 4
Joined: 2011-03-16T01:02:16-07:00
Authentication code: 8675308

Re: cannot produce Targa(tga) image with transparent backgro

Post by academy25 »

It works fine for png, I got a semi-transparent image. But it fails for tga, there is no transparency, I just got a gray image.
Did you succeed for the tga case? Would you please try to obtain a semi-transparent tga image with the same code?
User avatar
fmw42
Posts: 25562
Joined: 2007-07-02T17:14:51-07:00
Authentication code: 1152
Location: Sunnyvale, California, USA

Re: cannot produce Targa(tga) image with transparent backgro

Post by fmw42 »

This works for me in CLI

convert logo: -transparent white logot.png
convert logot.png logot.tga

The colors are slightly different but the transparency is there.
academy25
Posts: 4
Joined: 2011-03-16T01:02:16-07:00
Authentication code: 8675308

Re: [SOLVED] cannot produce transparent Targa(tga) image

Post by academy25 »

The problem is solved, didn't know that the opacity values are inverted in ImageMagick: 0 for fully opaque and 1.0 for fully transparent !!
So I subtracted the alpha value that belongs to QImage from 255, before scaling the result by 1/255.

Code: Select all

mgc.alpha( scale*(255-qAlpha(pix)) );
Setting the matte to 'true' before the loop, and considering the inverted alpha values solved the problem.
Post Reply