Results 1 to 11 of 11

Thread: QImage::loadFromData() behaviour

  1. #1
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default QImage::loadFromData() behaviour

    Hello,
    below a piece of code, which consists in loading image data in false color mode (or at least this is what I expected to do!) and retrieving them immediatly. I have deleted additionnal code, which includes a color table construction, to be attached to the image.
    I obviously make a mistake somewhere or more likely I have not understood the way QImage interprets the raw data. I have not found any response to this specific problem in reading the various posts on loadFromData method, including http://www.qtcentre.org/forum/f-qt-p...t=loadFromData so this is the reason I create a new topic.
    Thanks in advance for your help.

    Code:

    Qt Code:
    1. this->Print("size is (%u,%u)", size[0], size[1]);
    2. uchar *data;
    3. if ((data = (uchar *)malloc(size[0]*size[1]*sizeof(uchar))) == NULL) {
    4. this->Error("memory allocation error (%d)", size[0]*size[1]*sizeof(uchar));
    5. return;
    6. }
    7. QImage *image = new QImage(QSize(size[0], size[1]), QImage::Format_Indexed8);
    8. for (int j=0 ; j<size[1] ; j++)
    9. for (int i=0 ; i<size[0] ; i++)
    10. data[j*size[0]+i] = i==0 && j==0 ? 255 : 0;
    11. for (int j=0 ; j<size[1] ; j++) {
    12. for (int i=0 ; i<size[0] ; i++) {
    13. this->Print(" %u\r", data[j*size[0]+i]);
    14. }
    15. this->Print("\n");
    16. }
    17. image->loadFromData(data, field.size());
    18. for (int j=0 ; j<size[1] ; j++) {
    19. for (int i=0 ; i<size[0] ; i++) {
    20. QRgb rgb = image->pixel(i, j);
    21. this->Print(" %u\r", image->bits()[j*size[0]+i]);
    22. }
    23. this->Print("\n");
    24. }
    To copy to clipboard, switch view to plain text mode 

    Output:

    Qt Code:
    1. wlFieldImage: size is (4,4)
    2. wlFieldImage: Original values:
    3. wlFieldImage: 255 0 0 0
    4. wlFieldImage: 0 0 0 0
    5. wlFieldImage: 0 0 0 0
    6. wlFieldImage: 0 0 0 0
    7. wlFieldImage: Extracted values:
    8. wlFieldImage: 40 59 19 8
    9. wlFieldImage: 0 0 0 0
    10. wlFieldImage: 216 103 12 8
    11. wlFieldImage: 184 193 9 8
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage::loadFromData() behaviour

    Hi,

    Maybe you have a problem setting the data using integers. "data" is defined as "uchar*" and you are indexing it using "ints".
    The first pixel must be "data", second pixel "data+1 (byte)", and so on. One integer is 32 bytes so you are jumping to a different position.

    Is this your problem? I had been using QImage like you one year ago and I made some similar error.
    Òscar Llarch i Galán

  3. #3
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    Quote Originally Posted by ^NyAw^ View Post
    Hi,

    Maybe you have a problem setting the data using integers. "data" is defined as "uchar*" and you are indexing it using "ints".
    The first pixel must be "data", second pixel "data+1 (byte)", and so on. One integer is 32 bytes so you are jumping to a different position.

    Is this your problem? I had been using QImage like you one year ago and I made some similar error.
    indexing cannot be done but with ints!
    Your error was probably more likely something like this:
    int *p = data;
    for (...)
    *p++ = some value;
    or int *data then loadFromImage(data, ....)
    which is of course a mistake.
    But nothing wrong in adressing the data in my code...unfortunately because my error remains

  4. #4
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: QImage::loadFromData() behaviour

    Hi,

    But nothing wrong in adressing the data in my code...unfortunately because my error remains
    So nothing is wrong but it doesn't works. What are you expecting the code do?
    Òscar Llarch i Galán

  5. #5
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    Quote Originally Posted by ^NyAw^ View Post
    Hi,



    So nothing is wrong but it doesn't works. What are you expecting the code do?
    I am expecting to get the same data I entered before as I extract them using QImage::bits()!

  6. #6
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    Quote Originally Posted by ^NyAw^ View Post
    Hi,



    So nothing is wrong but it doesn't works. What are you expecting the code do?
    I am expecting to get the same data I entered before as I extract them using QImage::bits()!
    Actually I build a pixmap from this image and obviously the pixmap is wrong!
    That is the reason I checked which data I get from the image and discovered data have been altered during their storage in QImage. So probably this is a normal behaviour but at least:
    - I would like to understand this behaviour,
    - I would like to know how to create a false color image from a list of color table indexes.

  7. #7
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage::loadFromData() behaviour

    Hi,
    loadFromData method isn't meant to read image from raw pixels matrix, but to read binary data in a specific format, i.e. when you read contents of *.png file and pass it to loadFromData method.

    You shall create an empty image with given size & use setPixel to fill it.

    If you really want to generate data outside the image, see PPM format - it's matrix of pixel values (with color depth and dimensions at the beginning) but in textual format.
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  8. #8
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    Ok, thanks for these explainations, I will use setPixel() instead.
    Regards.

  9. #9
    Join Date
    Nov 2007
    Posts
    9
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    In many situations it is quite slow to loop through the image using setPixel. What I do is to use a constructor that accepts raw data, I cite:

    ###
    QImage::QImage ( uchar * data, int width, int height, Format format )

    Constructs an image with the given width, height and format, that uses an existing memory buffer, data. The width and height must be specified in pixels, data must be 32-bit aligned, and each scanline of data in the image must also be 32-bit aligned.
    ### ref: http://doc.trolltech.com/4.2/qimage.html#QImage-4

  10. #10
    Join Date
    Sep 2007
    Location
    Szczecin, Poland
    Posts
    153
    Thanks
    7
    Thanked 11 Times in 8 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QImage::loadFromData() behaviour

    Yeah,
    anarsynd is right: constructors data is a raw data, it's not the same as data in loadFromData which is binary image coded to some format, so this is what you need.
    See GrEEn (Graphics Effects Environment)
    http://sourceforge.net/project/platf...roup_id=232746
    a qt-based plugins oriented MDI image processing application(contains also qt plugins like styles & imageformats).

  11. #11
    Join Date
    Mar 2006
    Posts
    142
    Thanks
    8
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: QImage::loadFromData() behaviour

    Quote Originally Posted by mchara View Post
    Yeah,
    anarsynd is right: constructors data is a raw data, it's not the same as data in loadFromData which is binary image coded to some format, so this is what you need.
    not exactly because I have to assign new content to the image after it has been constructed, in a slot, and I cannot create a new image because I would have to take care of the image state, which would be complicated, so setPixel() is ok for me, although not so efficient.

Similar Threads

  1. Disable default tab behaviour for a QGraphicsItem
    By nmather in forum Qt Programming
    Replies: 3
    Last Post: 13th December 2017, 10:30
  2. QAbstractItemView selection behaviour issue
    By Caius Aérobus in forum Qt Programming
    Replies: 4
    Last Post: 1st May 2007, 16:33
  3. very strange behaviour
    By regix in forum Qt Programming
    Replies: 23
    Last Post: 20th July 2006, 17:38
  4. Different behaviour on different distributions
    By Kumula in forum Qt Programming
    Replies: 17
    Last Post: 6th March 2006, 23:58
  5. [Qt 4.1] Strange behaviour with QTableView
    By fane in forum Qt Programming
    Replies: 1
    Last Post: 23rd January 2006, 06:17

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.