Page 1 of 1

cannot produce Targa(tga) image with transparent background

Posted: 2011-03-16T01:16:44-07:00
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?

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

Posted: 2011-03-16T07:26:40-07:00
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).

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

Posted: 2011-03-16T08:39:31-07:00
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/

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

Posted: 2011-03-16T09:25:49-07:00
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.

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

Posted: 2011-03-16T10:13:23-07:00
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?

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

Posted: 2011-03-16T10:19:44-07:00
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.

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

Posted: 2011-03-16T11:22:54-07:00
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.