Results 1 to 3 of 3

Thread: Exception (crash) when accessing QImage pixel

  1. #1
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Exception (crash) when accessing QImage pixel

    I'm still learning Qt, so as an exercise (and a tool for an upcoming project) I'm building a simple histogram widget. It's throwing an exception as it's parsing the image data, and I have no idea what could be causing it. I'm using 4.6.1 in XP Pro SP3.
    Qt Code:
    1. void Histogrammer::analyzeImage(QImage *original)
    2. {
    3. // histogramData is a member variable -- an int array of size 256
    4. initializeHistogramData(); // initializes the array to zeroes
    5.  
    6. for(int row = 0; row < original->height(); row++)
    7. {
    8. for(int col = 0; col < original->width(); col++)
    9. {
    10. histogramData[(QColor(original->pixel(row, col)).red() + QColor(original->pixel(row, col)).green() + QColor(original->pixel(row, col)).blue()) / 3]++;
    11. }
    12. }
    13. }
    To copy to clipboard, switch view to plain text mode 
    I put the widget into a MainWindow application, and compile it (WidgetTester.exe). This works. ...for the first 10 pixels.

    My image is 30px wide by 10px tall, and is a standard 24-bit Windows bitmap (I load my QImage using the "BMP" format as well). I put debug outputs into the code, and can confirm that it detects the dimensions correctly. I also put debug outputs in to detect the row/column it was currently working on, and can confirm it is scanning the first row, from left to right.

    When it attempts to read the pixel at (0, 10), it throws:
    Qt Code:
    1. An unhandled win32 exception occurred in WidgetTester.exe [3304]
    To copy to clipboard, switch view to plain text mode 
    If I open the debugger (VS2008), I see:
    Qt Code:
    1. Unhandled exception at 0x00000001 in WidgetTester.exe: 0xC0000005: Access violation reading location 0x00000001.
    To copy to clipboard, switch view to plain text mode 
    I've parsed images in this way before, so I don't know why it would be complaining now. I have all the same rights to the image file that I'm using, as all of the previous images I've used.

    Since this isn't the first time I've read pixels from a QImage, and trying a different image had the same effect, I figured it must be the array. I tried different ways of incrementing the histogramData entry (object += 1, and object = object + 1), but it still throws the same errors. It can't be going outside the bounds of the array, because--as a bitmap--the image's pixels have a maximum value of (255, 255, 255), which (obviously) averages to 255.

    Could it be a problem with my runtime? Should I try re-installing Qt? Could it be a problem with threading (which I'm not messing with right now)? Can you tell I'm grasping at straws?

    P.S. On Linux, the app just crashes, saying the application finished unexpectedly with a return code of 0.

  2. #2
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: Exception (crash) when accessing QImage pixel

    You have to swap row and col! And using QImage::bits() is faster than using two for loops and QImage::pixel().

  3. The following user says thank you to Lykurg for this useful post:

    TheJim01 (19th February 2010)

  4. #3
    Join Date
    Feb 2010
    Location
    Pennsylvania, USA
    Posts
    36
    Thanks
    9
    Thanked 3 Times in 1 Post
    Qt products
    Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Exception (crash) when accessing QImage pixel

    Oh boy, I feel dumb. Can you tell it has been a long week?

    Thank you for the tip on QImage::bits. I didn't see that one, and it will definitely make parsing the data easier.

Similar Threads

  1. Shifting a QImage up by 1 pixel row
    By MSUdom5 in forum Qt Programming
    Replies: 2
    Last Post: 7th May 2010, 10:25
  2. Multithreaded per pixel operations on QImage
    By N¤X in forum Qt Programming
    Replies: 8
    Last Post: 14th September 2009, 12:29
  3. Accessing alpha channel of QImage directly
    By spraff in forum Qt Programming
    Replies: 3
    Last Post: 19th March 2009, 16:24
  4. QImage-Greyscale-8-bits-pixel
    By Ivan Labrador in forum Newbie
    Replies: 5
    Last Post: 3rd August 2008, 00:08
  5. QPixmap pixel by pixel ?
    By tommy in forum Qt Programming
    Replies: 19
    Last Post: 3rd December 2007, 22:52

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.