问题
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?
回答1:
A possible course of action:
- Load your image as a QImage
- Do a
QImage QImage::convertToFormat(QImage::Format_Indexed8)
to get a indexed image with a color table - Get color table values with
QRgb QImage::color ( int i ) const
- Manipulate the colors with
QColor ( QRgb color )
and the other QColor methods - Alter the color
table with
void QImage::setColor ( int index, QRgb colorValue )
回答2:
You've got a few options, none of which are built-in Qt solutions, unfortunately.
- Use OpenMP or some other concurrency library to take advantage of SSE/SSE2.
- Use the GPU via OpenGL, DirectX or various GPGPU programming techniques.
- (the solution I chose) Use OpenCL to take advantage of both CPU and GPU concurency without all the "fun" of shader programming.
- 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!
来源:https://stackoverflow.com/questions/9936021/shifting-the-hue-of-a-qimage-qpixmap