Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] Fastest method of chopping up an image?

[SOLVED] Fastest method of chopping up an image?

Scheduled Pinned Locked Moved General and Desktop
4 Posts 2 Posters 3.6k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gyrovorbis
    wrote on last edited by
    #1

    So once again, I'm working on a level editor for a 2D tile-based game. The tiles are 32x32 pixels large and are all stored within a single 512x512 image (for a total of 256 tiles).

    We have devised a method of splitting up the tiles that involves creating a QImageReader for the PNG file, extracting chunks of QImages, then converting those QImages to QPixMaps for rendering later... While it isn't SUPER slow, it is still too unresponsive to not have some form of a loadbar for the user.

    Is there any faster method of doing this? I'm not sure how QImageReader works, but I'm guessing there could be a chance that it's literally reopening and closing the file for each fetch. Is there a way to grab the WHOLE IMAGE then read subsets of that QImage into other QImages?

    Thanks for your time and help! Source code attached.
    [code]unsigned spriteAmount = (sheetWidth/spriteWidth)*(sheetHeight/spriteHeight);
    sprite._frames = new QPixmap[spriteAmount];

    unsigned int curRow = 0;
    unsigned int curColumn = 0;
    

    unsigned int rowSize = sheetWidth/spriteWidth;

    for(; clipRect.y() < sheetHeight; clipRect.translate (0,(clipRect.y() + (int)spriteHeight <= sheetHeight)? spriteHeight : sheetHeight-clipRect.y())) {
    for(clipRect.moveTo(0,clipRect.y()); clipRect.x() < sheetWidth; clipRect.translate((clipRect.x() + (int)spriteWidth <= sheetWidth)? spriteWidth : sheetWidth-clipRect.x(),0)) {
    reader.setFileName(QString(sprite._attrib->getTexPath()));
    reader.setClipRect(clipRect);
    QImage image = reader.read();

            if(image.isNull()) {
                QString error = reader.errorString();
                Debug::Log(Debug::CRITICAL, "TileManager::DivideSpriteSheet(): There was an error reading the image! Error message: " + error);
    //sprite._spriteAmount = 0;
                return false;
            }
    

    if(curRow*rowSize+curColumn >= spriteAmount) return false;

    sprite._frames[curRow*rowSize+curColumn] = QPixmap::fromImage(image);

            ++curColumn;
        }
        curColumn = 0;
        ++curRow;
    }[/code]
    
    1 Reply Last reply
    0
    • A Offline
      A Offline
      andre
      wrote on last edited by
      #2

      Did you try using QImage::copy()? That is a way to get a part of a QImage as a new QImage.

      1 Reply Last reply
      0
      • G Offline
        G Offline
        gyrovorbis
        wrote on last edited by
        #3

        Holy CRAP! Using QImage::copy rather than QImageReader just dropped the loadtime from 5.218 seconds to 51 ms. THANK YOU!!!

        1 Reply Last reply
        0
        • A Offline
          A Offline
          andre
          wrote on last edited by
          #4

          You're welcome :-)
          I'm glad your issue is solved.

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved