[Qt-interest] Memory mapping QImage

Samuel Rødal samuel.rodal at nokia.com
Tue Apr 13 13:24:24 CEST 2010


ext Kustaa Nyholm wrote:
> Hi,
> 
> currently I'm copying the contents of a QImage to a memory mapped file
> with something like this:
> 
> 
>    char* mmap = (char*) mmap(0, .....
>    for (int i=0; i<mImage.height(); ++i) {
>        uchar* p=;  
>        memcpy(
>            mmap +i*mImage.bytesPerLine(),
>            mImage.scanLine(i) ,
>            mImage.bytesPerLine());
>        }
> 
> this works and is reasonably fast for my purpose.
> 
> However it seems slightly sub-optimal because of the
> copying. Ideally I could just pass the memory mapped
> file pointer to QImage to use as the pixel store
> and no copying would be involved.
> 
> But I can't find a public method for that.
> 
> Is there some way to accomplish this?

QImage has constructors that allow you to wrap any existing memory 
buffer: http://doc.trolltech.com/4.6/qimage.html#QImage-6

> I'm using the QImage as a paint device in a lighthouse
> 
> QPaintDevice *JEmbedWindowSurface::paintDevice()
> {
>     return &mImage;
> }
> 
> so it does not 'have to' be a QImage, but that
> was the best I could find.
> 
> I could probably eliminate the for loop and
> get away with a slightly faster:
> 
> 
>        memcpy(
>            mmap,
>            mImage.scanLine(0) ,
>            mImage.bytesPerLine()*mImage.height());
> 
> but that would violate the scanLine() contract, what?

That should be fine if bytesPerLine() * 8 == width() * depth(), i.e. 
there's no padding. Instead of scanLine(0) you can use bits().

--
Samuel




More information about the Qt-interest-old mailing list