Changing the hue of a QImage / QPixmap

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

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

Nothing in Qt Docs goes that far into image manipulation by simply changing alpha and colors. Should I look into the open source library (the end result will most likely be independently sold software)? If so, are there any recommendations? Or is there a secret feature hidden in Qt docs that can be executed without the need for external libraries / crazy algorithms?


source to share

2 answers

Possible course of action:

  • Load the image as a QImage
  • Do QImage QImage::convertToFormat(QImage::Format_Indexed8)

    to get indexed image with color table
  • Get color table values ​​using QRgb QImage::color ( int i ) const

  • Manipulate colors with QColor ( QRgb color )

    and other QColor methods
  • Change color table with void QImage::setColor ( int index, QRgb colorValue )



You have several options, none of which is a built-in Qt solution, unfortunately.

  • Use OpenMP or another concurrency library to take advantage of SSE / SSE2.
  • Using the GPU via OpenGL, DirectX or various GPGPU programming methods.
  • (the solution I chose) Use OpenCL to take advantage of both CPU and GPU compatibility without any "fun" of shader programming.
  • Create a pool of thread workers and ask each of them to process a piece of the image.

My application does a lot of image filtering, and I was honestly blown away by the performance boost it should have had after I ported the OpenCL filters.

In 1936x2592, the luma modification filter ran for 175ms in native C ++ code, iterating through each pixel in 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 the overhead was nothing compared to the performance gain of OpenCL.

We wish you the best of luck with your adventures!



All Articles