On 2016-06-27 07:23,
tlal...@gmail.com wrote:
> currently I am attempting to write an application that generates a preview
> of decoded JPEG2000 images in Qt. After decoding the image I use the
> following code to create a QImage:
>
> QImage image(width, height, QImage::Format_RGB32); // create image
>
> for (int x = 0; x < width; x++){
> for (int y = 0; y < height; y++){
>
> QRgb argb = qRgba(
> psImage->comps[0].data[x * y], //red
> psImage->comps[1].data[x * y], //green
> psImage->comps[2].data[x * y], //blue
> 255); //alpha
>
> image.setPixel(QPoint(x,y), argb);
> }
> }
>
> The creation of the QImage currently takes around 200ms on my average
> notebook for the 1920x540 image. Also the image content is useless (see
> attachment).
Well... the way you are computing the pixel index looks fishy. I think
you mean something like 'y*width + x'¹. Also, using the QPoint overload
of setPixel may be less efficient than passing x and y separately.
Also, using y for the inner iteration is likely causing massive cache
inefficiency; try switching your inner and outer loops.
(¹ I often hoist the computation of the scanline offset out of the inner
loop. Even better, though, use QImage::scanLine outside the inner loop,
and in the inner loop, write to the data directly rather than using
QImage::setPixel. Don't forget the cast to QRgb* — see the Qt docs. All
of this also assumes that you switch the loops...)
Try this (not tested, may contain typos):
for (int y = 0; y < height; ++y)
{
auto const ky = y * width;
auto const l = reinterpret_cast<QRgb*>(image.scanLine(y));
for (int x = 0; x < width; ++x)
{
auto const k = ky + x;
l[x] = qRgb(psImage->comps[0].data[k],
psImage->comps[1].data[k],
psImage->comps[2].data[k]);
}
}
(qRgb(r,g,b) is equivalent to qRgba(r,g,b,255)...)
--
Matthew