[Qt-interest] Help with threaded tile-image viewer

Cole, Derek dcole at integrity-apps.com
Thu Jun 23 19:33:21 CEST 2011


Hello,

I am trying to create a threaded tile image viewer that will load tiles as they are displayed in the viewer.  I am using Linux.
It appears that I am not able to use QImageReader to read from various parts of the same file at the same time.  Here is kind of what I have set up:



RasterTile::RasterTile(QImageReader *reader, int nBlocksX, int nBlocksY, int xoffset, int yoffset, int nXBlockSize, int nYBlockSize)
    : Tile(nBlocksX, nBlocksY, xoffset, yoffset, nXBlockSize, nYBlockSize)
{
        this->reader = reader;
        connect(&watcher,SIGNAL(finished()),this,SLOT(updateSceneSlot()));
}


void RasterTile::paint(QPainter *painter, const QStyleOptionGraphicsItem *option,QWidget *widget)
{
    if(image.isNull())
    {
        TilePainter=painter;
        TileOption=option;
        TileWidget=widget;
        future = QtConcurrent::run(this, &RasterTile::LoadTilePixmap);
        watcher.setFuture(future);

    }else
    {
        QRectF imageRect = image.rect();
        painter->drawImage(imageRect, image);
    }

}

QImage RasterTile::LoadTilePixmap()
{
    qDebug("Loading tileX %i: " ,tilePosX);
    qDebug("Loading tileY %i: ", tilePosY);
    QImage img(nBlockXSize, nBlockYSize, QImage::Format_RGB32);

    QRect rect(tilePosX*nBlockXSize, tilePosY*nBlockYSize, nBlockXSize, nBlockYSize);

    reader->setClipRect(rect);
    reader->read(&img);
    return img;

}

And here is the slot, in Tile, that gets called when the watcher is finished:

void Tile::updateSceneSlot()
{
    image = future.result();
    prepareGeometryChange();
}


where image is the QImage the resides in the Tile.

So basically Tile is subclassed by RasterTile, and Tile itself is a subclass of QGraphicsItem. I made the paint method of Tile virtual, so that I can overload it with whatever kind of sub-tiles I come up with.

In this instance, I want to use QImageReader to read from a particular QRect within the image, which is based on the parameters of the tile. I think I have something going wrong in my code, because I am getting something like this:

improper call to JPEG library in state 205

or

improper call to JPEG library in state 203

or

Corrupt JPEG data: bad Huffman code

>From the files I am trying to read. I am not sure on that last one whether my code has corrupted the file or not. It appears to me that the  files are still readable in other viewers.

Is there something non-thread safe about my code, other than possibly that multiple threads are trying to access the same file? Is there a way to test for that and try to avoid it, if that is the problem?

Thanks

Derek


-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.qt-project.org/pipermail/qt-interest-old/attachments/20110623/4780b56f/attachment.html 


More information about the Qt-interest-old mailing list