سؤال

I suppose this is more of a graphics manipulation question in general, but I'd like to accomplish this in Qt (c++). If I have an image - let's just say a circle on a transparent background - of a light gray color, is there any built-in functionality in Qt to shift the hue / saturation to color it?

I suppose I could go pixel by pixel, and modifying the rgb mathematically - add x to r, g, and b, to get a desired color, but there must be a better way than modifying every single pixel.

Nothing in the Qt Docs goes this far into image manipulation, just altering alpha and colors. Should I look into an open source library (the end result will likely be an independently sold software)? If so, are there any recommendations? Or is there a secret function hidden in the Qt docs that can accomplish without the need of outside libraries / crazy algorithms?

هل كانت مفيدة؟

المحلول

A possible course of action:

  1. Load your image as a QImage
  2. Do a QImage QImage::convertToFormat(QImage::Format_Indexed8) to get a indexed image with a color table
  3. Get color table values with QRgb QImage::color ( int i ) const
  4. Manipulate the colors with QColor ( QRgb color ) and the other QColor methods
  5. Alter the color table with void QImage::setColor ( int index, QRgb colorValue )

نصائح أخرى

You've got a few options, none of which are built-in Qt solutions, unfortunately.

  1. Use OpenMP or some other concurrency library to take advantage of SSE/SSE2.
  2. Use the GPU via OpenGL, DirectX or various GPGPU programming techniques.
  3. (the solution I chose) Use OpenCL to take advantage of both CPU and GPU concurency without all the "fun" of shader programming.
  4. Spawn off a pool of thread workers and have each of them handle a chunk of the image.

My application does lots of image filtering and I was honestly shocked at the performance gain that was to be had after I ported filters to OpenCL.

At 1936×2592 a brightness modification filter was running in 175ms in native C++ code iterating through each pixel within a QImage.

After porting to OpenCL that went down to 15ms. Of course, the data had to be pulled out of the QImage and reinserted but that overhead was nothing compared to the OpenCL performance gains.

Best of luck on your coding adventures!

In this case it sounds like you might be able to get away with using a QGraphicsColorizeEffect.

Instead of shifting the hue, this method will set it. Although it seems QGraphicsEffects can only be applied to QWidgets and QGraphicsItems, which can be inconvenient.


Another option - in this case - would be to use a QPainter's CompositionMode.

Just use QImage::pixelColor and QImage::setPixelColor (available since Qt 5.6).

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top