How to save a QImage with transparency? - c++

I want to create a PNG image with transparency.
QImage image(dim, dim, QImage;;Format_ARGB32);
And I set the
qRgba(qRed, qGreen, qBlue, alpha)
But the alpha channel does not modify the transparency/opacity of the color, instead modify the intensity color (from white to color).
Thus, how can I set the transparency of the QImage (which I save on file)?
I have to use QImage, no other stuff.

How do you expect to see the transparency? Ordinary image viewers like photo viewers in Windows and Linux are painting the image on a white canvas. That's why the transparent pixels are being blended with white color. Look at the pictures below :
White background with no transparency on the circle:
The same circle with 50% opacity on white background:
The circle with transparency on a grid background:
That is why advanced image editors like Photoshop, are using such canvas as their default background.

Related

How to set a "plain" or "flat" background color for a QTableWidgetItem

I have a QTableWidget. I want to have alternating background colors for the rows, but I can't use QTableWidget::setAlternatingRowColors because I need one color for two rows and the other one for the next two and so on (see the below image).
Thus, when I add a QTableWidgetItem, I set the according background color manually by QTableWidgetItem::setBackground().
But I don't get a "flat" or "plain" background by this, but a gradient and rounded corners:
I would like to have the background color all over the cells, without further "decoration". How can I get rid of this?
When you select a single color as the background (QBrush constructed from a QColor), the style engine tries to render a styled background, which in your case draws this gradient with border.
You can trick the style engine by using a QBrush constructed from a QImage, so the render engine draws exactly that image and nothing more. In your case, use an image with a single pixel, namely the color you want as the background. For this, construct a 1x1-sized QImage and set the color of the pixel using fill, then use that image as a brush:
// Create the image (using the default image format)
QImage img(QSize(1, 1), QImage::Format_ARGB32_Premultiplied);
// Set the color, here light gray:
img.fill(QColor(224, 224, 224));
// Apply the image as the background for the item:
item->setBackground(QBrush(img));

Is there a way to make bmps transparent?

(Using WinApi) Is there a way to:
Make transparent pixels?
Somehow instead of using transparency just have the image dynamically get the background colors and textures, and fill certain Colors with those textures, For Example: If I had a video game sprite and the background color of it was white, could I somehow get those white pixels and fill them with the background colors/textures?
If you create a 32-bit bitmap, 24 bits of each pixel are used for RGB values and the extra 8 bits are used for an alpha channel. Just set the alpha to 0 for full transparency.
When creating a bitmap that uses 24-bit or smaller pixels, the transparent color is usually indicated by the pixel in the lower-left corner of the bitmap.
Either way, creating a transparent bitmap is only half the equation. Creating a transparent bitmap itself is easy, but you then have to render the bitmap in a transparent manner. The Win32 API has TransparentBlt() and AlphaBlend() functions for that purpose, and there are plenty of online turorials and blogs that explain how to use them.

Plotting a graph over a QPixmap using QPainter

I need to plot a real time graph keeping the background an image. Let's say I am plotting the graph with a red color.
But the logic I use for plotting is drawing small lines using painter.drawline, where painter is a pixmap painter. Then I am constantly drawing this pixmap using a painter to parent widget on a timer timeout() event. So it appears the updated Image (which is the graph) gets plotted in the Gui.
The pixmap is black in background and plotting pen color is red, So when it reaches the end of screen width, I have to start drawing the graph from position 0, but from the second cycle onwards I have to fill the previously drawn pixels (not the entire pixmap, just a rect of width few pixels and height, height of pixmap) with black color.
But now I need to keep a background image, and draw a graph over it. So the fillling with black color doesn't do, as I have to keep a background an image. So how can I plot a graph over an image like aforementioned?
Is there any way to blend two images such that the pixels only having a particular color (red) in source image is blended/replaced with corresponding pixels in destination image?
Keep previously drawed Ys in memory than fill exactly the same pixels by corresponding background. It's faster and much more reliable.

Qt: QImage always saves transparent color as black

How do I save a file with transparency to a JPEG file without Qt making the transparent color black?
I know JPEG doesn't support alpha, and the black is probably just a default "0" value for alpha, but black is a horrible default color.
It seems like this should be a simple operation, but all of the mask and alpha functions I've tried are ignored when saving as JPEG.
For example:
image->load("someFile.png"); // Has transparent background or alpha channel
image->save("somefile.jpg", "JPG"); // Transparent color is black
I've tried filling the image with white before saving as a JPEG, converting the image to ARGB32 (with 8-bit alpha channel) before saving, and even tried ridiculously slow stuff like:
QImage image2 = image1->convertToFormat(QImage::Format_ARGB32);
image2.setAlphaChannel(image1->alphaChannel());
image2.save(fileURI, "JPG", this->jpgQuality; // Still black!
See: http://67.207.149.83/qt_black_transparent.png for a visual.
I'd try something like this (i.e., load the image, create another image of the same size, paint the background, paint the image):
QImage image1("someFile.png");
QImage image2(image1.size());
image2.fill(QColor(Qt::white).rgb());
QPainter painter(&image2);
painter.drawImage(0, 0, image1);
image2.save("somefile.jpg", "JPG");
Jpeg doesn't support transparency
True if you want to use Alpha Chanel (Transparent) you should save the imge in *.png *.bmp formats

BlendFunc for textured fonts with changing background

I am attempting to use Textured fonts as so that I can display text in my openGL scene. However I am having trouble finding glBlendFunc values that will work.
The background that the text will be placed on is a grayscale image but will change throughout the execution. Because the background changes the text could possibly be on top of any color from black to white.
The best values I have found are glBlendFunc(Gl.GL_SRC_COLOR, Gl.GL_ONE_MINUS_SRC_ALPHA). This will make the black box surrounding the character disappear but the character itself will fade as the background goes towards white.
Please help!
Do you want the text to invert based on the background color? white text over black background, black text on white? I think you can achieve an invert via blendfunc.
Alternatively you can use a font texture which has a "border" built into it to help set the character apart from the background. Imagine a white font w/ a smooth alpha blended black "glow". The font will look good against almost all colors.