Page 1 of 2 12 LastLast
Results 1 to 20 of 21

Thread: Probem Tiling an image with QPixmap

  1. #1
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Probem Tiling an image with QPixmap

    Hey guys,

    I'm trying to cut an QPixmap image into 32x32 QPixmap tiles using the following function:
    Qt Code:
    1. bool copyTileContainerFromImageFile( QString &fileString, QVector<Tile*> *sourceVector )
    2. {
    3. if(fileString.isEmpty())
    4. return false;
    5.  
    6. QRect rect(0,0,32,32);
    7. QPixmap originalImage(fileString);
    8. //goal = amount of possible tiles in this image.
    9. int goal = (originalImage.width() * originalImage.height()) / 1024; //1024 Pixels fits in one Tile
    10. sourceVector->reserve(goal); //reserves the amount of images in the vector.
    11. for(int i = 0;i != goal;i++) {
    12. sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
    13.  
    14. if(rect.x() + 32 >= originalImage.width()) {
    15. rect.setY(rect.y() + 32);
    16. rect.setX(0);
    17. }else
    18. rect.setX(rect.x() + 32);
    19.  
    20. }
    21. return true;
    22. }
    To copy to clipboard, switch view to plain text mode 

    Am I just not seeing the woods for the trees and the function works incorrect or is it not possible to cut a QPixmap with the copy function?
    Last edited by LuckyBlade; 12th January 2011 at 15:30.

  2. #2
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    and the function works incorrect
    What is the result you expect, and what do you get?

    You have some small errors:
    Qt Code:
    1. for(int i = 0;i != goal;i++) {
    2. sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
    3.  
    4. if(rect.x() + 32 >= (originalImage.width() - 32)) { //otherwise go beyond the width - it would be better if you do that in the upper for: for(x=0; x<originalImage.width()-32; x+=32)
    5. rect.setY(rect.y() + 32); //here the same thing, rect.y() < originalImage.height() - 32
    6. rect.setX(0);
    7. }else
    8. rect.setX(rect.x() + 32);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  3. #3
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    Quote Originally Posted by high_flyer View Post
    What is the result you expect, and what do you get?
    The result I expect should look like this:


    The result I actually get looks like this:


    The idea behind
    Qt Code:
    1. if(rect.x() + 32 >= originalImage.width())
    To copy to clipboard, switch view to plain text mode 
    is to check if the rect would go beyond the images width if a next tile gets cut out on the x asis.If it fails the x coordinate of the rect gets reset to 0 and the y coordinate gets increased by 32.

  4. #4
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    is to check if the rect would go beyond the images width if a next tile gets cut out on the x asis.
    I am well aware of that.
    But it allows rect.x() to be larger than originalImage.width()-32, which will cut a a rect which is beyond originalWidth().
    So for any width which is not a multiple of 32, this will not work right.
    You really should do something like:
    Qt Code:
    1. for(int y=0; y<originalImage.height() - iRectHeight; y+=iRectHeight){
    2. for(int x=0; x<originalImage.width() -iRectWidth; x+=iRectWidth){
    3. rect.setTopLeft(QPos(x,y);
    4. sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
    5. }
    6. }
    To copy to clipboard, switch view to plain text mode 

    But is just as side note.

    Regarding the result you are getting, I'd say it has more to do with how you compose your result image than the way you cut it.
    Can you show how you are composing your result image?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  5. #5
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    Ah okay that's a good point. Thanks for the hint.
    I only tested the tiling with images of the size 128x128 or 512x512.

    Here's the paintEvent() function:
    Qt Code:
    1. for(int i = 0;i != curTileset->getTileContainer()->size();i++)
    2. painter.drawPixmap(*curTileset->getTileContainer()->at(i)->getPosition(),*curTileset->getTileContainer()->at(i)->getTileImage());
    To copy to clipboard, switch view to plain text mode 

    getPosition returns a pointer to the rect (the rect is the rect you set as argument when creating a new Tile object)from the tile and getTileImage returns a pointer to the pixmap.
    Last edited by LuckyBlade; 13th January 2011 at 11:21.

  6. #6
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    Can you show getPosition() and getTileImage()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  7. #7
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    Qt Code:
    1. QPixmap * Tile::getTileImage()
    2. {
    3. return &tileImage;
    4. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QRect *Tile::getPosition()
    2. {
    3. return &position;
    4. }
    To copy to clipboard, switch view to plain text mode 

    and the constructor:

    Qt Code:
    1. Tile::Tile(QRect rect,int id,QPixmap tileImage)
    2. {
    3. this->position = rect;
    4. this->tilesetID = id;
    5. this->tileImage = tileImage;
    6. }
    To copy to clipboard, switch view to plain text mode 
    Last edited by LuckyBlade; 13th January 2011 at 11:33.

  8. #8
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    Your for loop statement is not good:
    Qt Code:
    1. for(int i = 0;i </*!=*/ curTileset->getTileContainer()->size();i++)
    To copy to clipboard, switch view to plain text mode 
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  9. #9
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    As far as I know that's the common way to iterate through vector containers / containers in general.
    I read it in the book "C++ Primer" and "QT 4.6 - Gui Entwicklung mit C++" (a german book)

  10. #10
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    Sorry I was thinking about something else.
    Your version is ok, but dangerous, since it will also allow i> maxVal.
    With '<' you are on the safe side.

    I read it in the book "C++ Primer" and "QT 4.6 - Gui Entwicklung mit C++" (a german book)
    Did they really use != for this case, or did they use an iterator and compared it to <container>.end() (with !=)?
    Its not the same case!

    But true, this is not the problem here.

    Can you show your full paintEvent()? the problem must be somewhere there...
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  11. #11
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    I'm not sure right now if they used != only for iteration with iterators in "C++ Primer", but I'm quite sure that they did it in "QT 4.6 - Gui Entwicklung" without iterators.

    But anyway, here's the whole paintEvent:
    Qt Code:
    1. void K15_TilesetEditWidget::paintEvent(QPaintEvent *pe)
    2. {
    3. QPainter painter(this);
    4. if(curTileset != NULL) {
    5. //1.Tileset gets drawn.
    6. for(int i = 0;i != curTileset->getTileContainer()->size();i++)
    7. painter.drawPixmap(*curTileset->getTileContainer()->at(i)->getPosition(),*curTileset->getTileContainer()->at(i)->getTileImage());
    8.  
    9. //2.Collision gets drawn (if collisionMode is true)
    10. if(this->mode == selectCollision)
    11. for(int i = 0;i != curTileset->getTileContainer()->size();i++) {
    12. //Attention!Multiline Code
    13. painter.drawPixmap*(curTileset->getTileContainer()->at(i)->getPosition(),
    14. *curTileset->getTileContainer()->at(i)->getCollisionType()->getPixmap());
    15. }
    16.  
    17. //3.TileID gets drawn (if true)
    18. if(this->tileIDsVisible)
    19. for(int i = 0;i != curTileset->getTileContainer()->size();i++)
    20. //Attention!Multiline Code
    21. painter.drawText(curTileset->getTileContainer()->at(i)->getPosition()->x() + 12,
    22. curTileset->getTileContainer()->at(i)->getPosition()->y() + 12,
    23. QString().setNum(curTileset->getTileContainer()->at(i)->getTilesetID()));
    24.  
    25. //4.Grid gets drawn (if true)
    26. if(this->gridVisible) {
    27. for(int i = 0;i <= curTileset->getImage()->height();i += 32)
    28. painter.drawLine(0,i,curTileset->getImage()->width(),i);
    29.  
    30. for(int j = 0;j <= curTileset->getImage()->width();j += 32)
    31. painter.drawLine(j,0,j,curTileset->getImage()->height());
    32. }
    33. }
    34. }
    To copy to clipboard, switch view to plain text mode 

    Thanks btw for your help so far.

  12. #12
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    I'm not sure right now if they used != only for iteration with iterators in "C++ Primer", but I'm quite sure that they did it in "QT 4.6 - Gui Entwicklung" without iterators.
    Bad!

    Well, you are over writting your pixmap that you drew in the first for loop.
    Comment all the sections except the first one, and see if it gets written correctly.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  13. #13
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    The pixmap won't get over written completly.
    The pixmap that get's drawn in the second loop is just a little image that is nearly complete transparent (I'm working on a leveleditor for a 2D game and the image that gets drawn in the second loop is a little image that shows if the current tile is passable by the player or not.).

    I commented anyway but that didn't change anything :-/

    EDIT:
    Whats interesting though, is that also the little "collsionimages" gets drawn incorrect.
    I pretent that the error must be in the function where the original image gets tiled.
    I'll add an image in a few minutes.
    Last edited by LuckyBlade; 13th January 2011 at 13:15.

  14. #14
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    Ok, leave it commented out, so that we can easily find the problem.
    In the screenshot you posted, there are 4 pixmaps - you get all 4 even with only one for loop in your paintEvent()?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  15. #15
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap


    Thats what I talked about in the last post.If you look at the "collisionimages" you'll see that they're also on the wrong position.

    EDIT:
    Yeah I get 4 images if I leave it commented.The result is the same.

  16. #16
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    I am sorry, how your answer is answering my question?
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  17. #17
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    Sorry.
    The result is the same as shown above in the image I posted.

  18. #18
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    This means that there is more code influencing your vector tiles, since more is drawn then is filled in the code you posted so far.
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

  19. The following user says thank you to high_flyer for this useful post:

    LuckyBlade (13th January 2011)

  20. #19
    Join Date
    Dec 2010
    Posts
    14
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: Probem Tiling an image with QPixmap

    Holy maccaroni!
    I fixed it.

    I changed
    Qt Code:
    1. for(int i = 0;i != goal;i++) {
    2. sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
    3.  
    4. if(rect.x() + 32 >= originalImage.width()) {
    5. rect.setY(rect.y() + 32);
    6. rect.setX(0);
    7. }else
    8. rect.setX(rect.x() + 32);
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 

    to

    Qt Code:
    1. for(int i = 0;i != goal;i++) {
    2. sourceVector->append(new Tile(rect,i,originalImage.copy(rect)));
    3.  
    4. if(rect.x() + 32 >= originalImage.width()) {
    5. rect.moveTop(32 + rect.y());
    6. rect.moveLeft(0);
    7. }else
    8. rect.moveLeft(32 + rect.x());
    9.  
    10. }
    To copy to clipboard, switch view to plain text mode 
    Everything works fine!

  21. #20
    Join Date
    Jan 2006
    Location
    Munich, Germany
    Posts
    4,714
    Thanks
    21
    Thanked 418 Times in 411 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: Probem Tiling an image with QPixmap

    If you had used my code from post 4 you'd had that solved since I was using setTopLeft().
    ==========================signature=============== ==================
    S.O.L.I.D principles (use them!):
    https://en.wikipedia.org/wiki/SOLID_...iented_design)

    Do you write clean code? - if you are TDD'ing then maybe, if not, your not writing clean code.

Similar Threads

  1. OpenGL Image Tiling Interpolation Problem
    By Halcom in forum Qt Programming
    Replies: 1
    Last Post: 22nd December 2010, 13:41
  2. using QPixmap to transparent image
    By sophister in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2010, 13:06
  3. QImage Non-Standard Format Probem
    By photo_tom in forum Qt Programming
    Replies: 3
    Last Post: 19th February 2010, 08:06
  4. Getting image file from QPixmap
    By di_zou in forum Qt Programming
    Replies: 2
    Last Post: 5th October 2009, 16:20
  5. add(draw) an icon(or image) to a QPixmap?
    By ascii in forum Qt Programming
    Replies: 4
    Last Post: 20th November 2008, 12:44

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.