Results 1 to 13 of 13

Thread: Loading a custom image into a QPixmap

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    28
    Thanked 976 Times in 912 Posts

    Default Re: Loading a custom image into a QPixmap

    Quote Originally Posted by KShots
    They look like they're rotated to me...
    AFAIR .bmp numbers rows bottom to top, while .tga --- the other way around. That's why those images are flipped.

  2. #2
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    24
    Thanked 2 Times in 2 Posts

    Wink Re: Loading a custom image into a QPixmap

    Ah, I think I see what's going on, then... though I'm still confused why OGL handles it correctly.

    Yeah, it looks like you're right, it's not rotated 90 degrees counter-clockwise, it's upside down. It's really hard to tell with the size of the icons, but I think you're right. It also seems that the red and the blue bytes are swapped.

    I'll probably have to have a function that goes in and adjusts that inside a QImage in the case of targa files. I'm not entirely sure what writing a QImageIOPlugin gives me if I'm reading the data from memory rather than a file (with the header stripped out).

    I'm still not sure what's going on with the BMP stuff above, either... that corruption really bothers me.

    EDIT: Fixing the targa was... well... easy enough. I had to do an image = image.rgbSwapped(); and an image = image.mirrored() to get it to show up correctly... but it did. I guess that'll have to do for the targas.

    Still mystified with the .bmp corruption

    EDIT2: I think I've figured out what's going on with the bitmaps... by definition, as bitmaps, they're 24-bits per pixel. QImage only seems to take 32-bits per pixel (unless I'm missing something important). Therefore, when QImage reads in a pixel, it's expecting 0xFFRRGGBB... and I'm giving it 0xRRGGBB... so the 1st pixel is 0xRRGGBBRR, then 2nd pixel is 0xGGBBRRGG, and so on. No wonder it looks corrupted. Any ideas on how to force QImage to read in 24-bits rather than 32?
    Last edited by KShots; 20th July 2006 at 05:02.
    Life without passion is death in disguise

  3. #3
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 14 Times in 12 Posts

    Default Re: Loading a custom image into a QPixmap

    You are looking for QImage::Format_ARGB32 to read in 0xAARRGGBB. However, it won't work for targa since targa is 0xBBGGRRAA (AA = alpha-channel) - if I remember correctly (should be, wrote my own targa reader two days ago... but left it at work).

    If you are loading targa images keep in mind that the targa header is 18 bytes so if you read it as a struct the sizeof(tgaheaderstruct) will still be 20 (unless #pragma pack(1) was specified). That reading 20 instead of 18 bytes also explains wrong colours as the pointer onto the datastream starts with the wrong color code.

  4. #4
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    24
    Thanked 2 Times in 2 Posts

    Default Re: Loading a custom image into a QPixmap

    Quote Originally Posted by Methedrine
    You are looking for QImage::Format_ARGB32 to read in 0xAARRGGBB. However, it won't work for targa since targa is 0xBBGGRRAA (AA = alpha-channel) - if I remember correctly (should be, wrote my own targa reader two days ago... but left it at work).
    Yeah, I got my targas working a while ago, I used the functions mentioned in my last post to correct it - my problem now is with my bitmaps
    Quote Originally Posted by Methedrine
    If you are loading targa images keep in mind that the targa header is 18 bytes so if you read it as a struct the sizeof(tgaheaderstruct) will still be 20 (unless #pragma pack(1) was specified). That reading 20 instead of 18 bytes also explains wrong colours as the pointer onto the datastream starts with the wrong color code.
    Hmm... that explains why I recently had to rebuild my texture loader recently - it just suddenly refused to load the header correctly. I ended up having to read it in variable by variable to get it to read the right sized chunk.

    Anyways... the targa's been behaving well... any ideas on the bitmaps?
    Life without passion is death in disguise

  5. #5
    Join Date
    Feb 2006
    Location
    USA
    Posts
    142
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows Android
    Thanks
    24
    Thanked 2 Times in 2 Posts

    Default Re: Loading a custom image into a QPixmap

    Alright... I think I solved my problem(s). It appears Qt does not support loading in bitmaps from raw data - I had to convert my (already loaded) bitmap into a 32-bit image through opengl, then pass it over to Qt, like so:
    Qt Code:
    1. //I start with a 24-bit bitmap raw-loaded into OpenGL
    2. unsigned char * pixelData = new unsigned char * [4 * texture->GetWidth() * texture->GetHeight()];
    3. glGetTexImage(GL_TEXTURE_2D, 0, GL_RGBA, GL_UNSIGNED_BYTE, pixelData);
    4. //Now I have a 32-bit texture loaded into memory...
    5. QImage image((uchar *) pixelData, texture->GetWidth(), texture->GetHeight(), QImage::Format_ARGB32);
    6. delete [] pixelData;
    7. image = image.rgbSwapped();
    8. image = image.mirrored();
    9. QPixmap pixmap(QPixmap::fromImage(image));
    10. return QIcon(pixmap.scaled(60, 60, Qt::KeepAspectRatio, Qt::SmoothTransformation));
    To copy to clipboard, switch view to plain text mode 
    On the one hand, I'm a little disappointed that Qt has no way of handling raw 24-bit images... but on the other hand, I found that I didn't need to store my textures in memory if I can retrieve them from OGL easily
    Life without passion is death in disguise

  6. #6
    Join Date
    May 2006
    Location
    Germany
    Posts
    108
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows
    Thanks
    2
    Thanked 14 Times in 12 Posts

    Default Re: Loading a custom image into a QPixmap

    I think I'd have used QFile to read the targa into a QByteArray (simply add the missing byte for a rgb32!) which in turn I'd then use to construct a QImage which would then be assigned to a QPixmap.

    But then again if you are writing something based upon OpenGL you're probably doing it correctly anyway.

Similar Threads

  1. QPixmap and HBITMAP
    By ToddAtWSU in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 16:24
  2. Fast image drawing/scaling in Qt 3.3
    By eriwik in forum Qt Programming
    Replies: 1
    Last Post: 21st June 2006, 10:45
  3. How and when to repaint a widget ?
    By yellowmat in forum Newbie
    Replies: 7
    Last Post: 3rd April 2006, 16:36
  4. Question about updating an image on screen
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 24th February 2006, 19:01
  5. image for a custom menu Item.......
    By Naveen in forum Qt Programming
    Replies: 2
    Last Post: 23rd February 2006, 09:28

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
  •  
Qt is a trademark of The Qt Company.