Setting pixel color of 8-bit grayscale image using pointer
I have this code:
Code:
QImage grayImage
= image.
convertToFormat(QImage::Format_Grayscale8);
int size = grayImage.width() * grayImage.height();
QRgb *data = new QRgb[size];
memmove(data, grayImage.constBits(), size * sizeof(QRgb));
QRgb *ptr = data;
QRgb *end = ptr + size;
for (; ptr < end; ++ptr) {
int gray = qGray(*ptr);
}
delete[] data;
It is based on this: https://stackoverflow.com/a/40740985/8257882
How can I set the color of a pixel using that pointer?
In addition, using qGray() and loading a "bigger" image seem to crash this.
This works:
Code:
int width = image.width();
int height = image.height();
for (int y = 0; y < height; ++y) {
for (int x = 0; x < width; ++x) {
image.setPixel(x, y, qRgba(0, 0, 0, 255));
}
}
But it is slow when compared to explicitly manipulating the image data.
Re: Setting pixel color of 8-bit grayscale image using pointer
First of all, I find it sus that grayImage is 8 bit, but the QRgb array is 32 bit. The memmove should either segfault or overwrite the data array with something random.
What format is the original QImage in? Is there a need to convert to grayscale? Yes, you should be able to just use setPixel on it.
Why are you so sure that using setPixel() is slower than manipulating the image through pointers? I doubt it hardly makes a difference.
If it's so time critical, let's see see some time measurements. Otherwise it would be much better to use the provided interface to manipulate the data.