[SOLVED] Fastest method of chopping up an image?



  • 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]


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



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



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


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.