cannot produce Targa(tga) image with transparent background
Posted: 2011-03-16T01:16:44-07:00
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.
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 , the transparency is preserved.
The more weird thing is, even if I set the alpha directly to zero, via 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?
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;
}
Code: Select all
qimage.save(dstFilename)
Code: Select all
Magick::Image* image = toImage(*imgOriginal);
image->write(dstFilename);
Code: Select all
mgc.alpha( 0 );
How can I set alpha values to produce a transparent Targa image?