[Qt-interest] Fast displaying of 8bit images

Jiri Horky jiri.horky at gmail.com
Tue Mar 31 21:08:10 CEST 2009


Hi all,

I have a big uchar array, where my image (2448x2048) is stored as 1 byte
(gray degree) per 1 pixel.  Pixels start from left corner, going
line-by-line to right bottom corner of image ( it is PGM image, but
without header). What I need to do is simple - just display the image,
but as fast as possible. From what I've found so far, I tried to convert
my image into QImage class and then using fromImage function loading it
into QPixmap, as in following code:
(please excuse using fprintf in C++, but time measuring was copied from
another project :-)

    QVector<QRgb> colortable;
    QRgb rgb;
    for ( int i = 0; i < 256; i++ ) {
        rgb = qRgb(i,i,i); // grey
        colortable.append(rgb);
    }

    g_timer_start(timer);  // start of measuring

    QImage * image = new QImage (img, 2448, 2048, QImage::Format_Indexed8);
    image->setColorTable(colortable);
    *pixmap = pixmap->fromImage(*image, Qt::ThresholdDither);

    g_timer_stop(timer);
     fprintf(stderr, "Via QImage it takes %lfs\n",
g_timer_elapsed(timer, NULL));

My measurement shows that it takes about 0.135 ms to convert from uchar
array to Pixmap.

I am wondering if there isn't any faster method, because manual of
fromImage function says :

In case of monochrome and 8-bit images, the image is first converted to
a 32-bit pixmap and then filled with the colors in the color table. If
this is too expensive an operation, you can use QBitmap::fromImage
<qbitmap.html#fromImage>() instead.

But as manual also says, QBitmap is suitable only for 1-bit images....



My next question is: would be possible to prepare QPixmap in another
thread, if I allocate it in GUI thread and make sure it wont be
manipulated from another thread when displayed?


I am testing it on Linux, in case it matters.

Thank you for any ideas!

Jiri Horky



More information about the Qt-interest-old mailing list