Results 1 to 2 of 2

Thread: Setting pixel color of 8-bit grayscale image using pointer

  1. #1
    Join Date
    Nov 2020
    Posts
    1
    Qt products
    Qt5
    Platforms
    Windows

    Default Setting pixel color of 8-bit grayscale image using pointer

    I have this code:
    Qt Code:
    1. QImage grayImage = image.convertToFormat(QImage::Format_Grayscale8);
    2. int size = grayImage.width() * grayImage.height();
    3. QRgb *data = new QRgb[size];
    4. memmove(data, grayImage.constBits(), size * sizeof(QRgb));
    5.  
    6. QRgb *ptr = data;
    7. QRgb *end = ptr + size;
    8. for (; ptr < end; ++ptr) {
    9. int gray = qGray(*ptr);
    10. }
    11.  
    12. delete[] data;
    To copy to clipboard, switch view to plain text mode 
    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:
    Qt Code:
    1. int width = image.width();
    2. int height = image.height();
    3. for (int y = 0; y < height; ++y) {
    4. for (int x = 0; x < width; ++x) {
    5. image.setPixel(x, y, qRgba(0, 0, 0, 255));
    6. }
    7. }
    To copy to clipboard, switch view to plain text mode 
    But it is slow when compared to explicitly manipulating the image data.

  2. #2
    Join Date
    Jan 2009
    Location
    Germany
    Posts
    369
    Thanks
    87
    Thanked 13 Times in 13 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default 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.

Similar Threads

  1. Replies: 5
    Last Post: 15th May 2020, 02:29
  2. Replies: 0
    Last Post: 25th March 2020, 13:11
  3. Grayscale image Color Conversion
    By 2lights in forum Newbie
    Replies: 6
    Last Post: 10th February 2014, 07:32
  4. Trouble with color table on Grayscale image
    By xxxollixxx in forum Qt Programming
    Replies: 0
    Last Post: 3rd December 2013, 09:36
  5. Replies: 2
    Last Post: 17th February 2012, 00:10

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.