Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Shifting the hue of a QImage / QPixmap

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?

like image 780
giraffee Avatar asked Mar 30 '12 02:03

giraffee


2 Answers

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 )
like image 149
Chris Browet Avatar answered Oct 23 '22 13:10

Chris Browet


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!

like image 2
PhotoMonkee Avatar answered Oct 23 '22 14:10

PhotoMonkee