January 3, 2012

gyrovorbis gyrovorbis
Lab Rat
1 posts

[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 32×32 pixels large and are all stored within a single 512×512 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.

  1. unsigned spriteAmount = (sheetWidth/spriteWidth)*(sheetHeight/spriteHeight);
  2.  sprite._frames = new QPixmap[spriteAmount];
  3.  
  4.     unsigned int curRow = 0;
  5.     unsigned int curColumn = 0;
  6.  unsigned int rowSize = sheetWidth/spriteWidth;
  7.  
  8.  for(; clipRect.y() < sheetHeight; clipRect.translate (0,(clipRect.y() + (int)spriteHeight <= sheetHeight)? spriteHeight : sheetHeight-clipRect.y())) {
  9.   for(clipRect.moveTo(0,clipRect.y()); clipRect.x() < sheetWidth; clipRect.translate((clipRect.x() + (int)spriteWidth <= sheetWidth)? spriteWidth : sheetWidth-clipRect.x(),0)) {
  10.    reader.setFileName(QString(sprite._attrib->getTexPath()));
  11.             reader.setClipRect(clipRect);
  12.             QImage image = reader.read();
  13.  
  14.             if(image.isNull()) {
  15.                 QString error = reader.errorString();
  16.                 Debug::Log(Debug::CRITICAL, "TileManager::DivideSpriteSheet(): There was an error reading the image! Error message: " + error);
  17.     //sprite._spriteAmount = 0;
  18.                 return false;
  19.             }
  20.  
  21.    if(curRow*rowSize+curColumn >= spriteAmount) return false;
  22.  
  23.    sprite._frames[curRow*rowSize+curColumn] = QPixmap::fromImage(image);
  24.  
  25.             ++curColumn;
  26.         }
  27.         curColumn = 0;
  28.         ++curRow;
  29.     }

3 replies

January 3, 2012

Andre Andre
Robot Herder
6422 posts

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

January 6, 2012

gyrovorbis gyrovorbis
Lab Rat
1 posts

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

January 6, 2012

Andre Andre
Robot Herder
6422 posts

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

 
  ‹‹ QGraphicsScene population SLOW on Windows but not any other platform?      Draging a ListWidget item’s icon to a label? ››

You must log in to post a reply. Not a member yet? Register here!