views:

204

answers:

1

I want bind QImage to the MMF file to manipulate the image without the cost of memory directly on the disc. Unfortunately, my code creates a copy in memory.

 QFile file("Boston City Flow.jpg");
 if(!file.open(QIODevice::ReadOnly))
   QMessageBox::information(this, "Error", "Error");

 qint64 size = file.size();
 unsigned char *mmf = file.map(0, size);

 QImage image;
 image.loadFromData(mmf, size, NULL);

My program needs to handle very large images.

+1  A: 

Try with declaring mmf const:

const unsigned char* mmf = file.map(0, size);

and then have a look at the QImage ctors, especially

QImage( const uchar*, int width, int height, Format )

QImage::QImage ( const uchar * data, int width, int height, Format format )

The docs say:

"The buffer must remain valid throughout the life of the QImage and all copies that have not been modified or otherwise detached from the original buffer. The image does not delete the buffer at destruction. [...] Unlike the similar QImage constructor that takes a non-const data buffer, this version will never alter the contents of the buffer. For example, calling QImage::bits() will return a deep copy of the image, rather than the buffer passed to the constructor. This allows for the efficiency of constructing a QImage from raw data, without the possibility of the raw data being changed."

Note that the non-const uchar* version copies the right away, so make sure to pass a const uchar*. Also note that calling non-const methods of QImage will copy the data.

Frank