Search code examples
c++qtqt5qimage

Set pixel value of 16 bit grayscale QImage


I have an 16 bit image of width ("imagewidth") and height ("imageheight"). The data is currently stored in an unsigned short int array of length ("imagewidth"*"imageheight")

I want to create an 16 bit grayscale QImage (using Qt 5.14) from my dataset which is called "data".

This is the code that I am using:

QImage image = Qimage(imagewidth,imageheight,QImage::Format_Grayscale16);

for(int i=0;i<imagewidth;i++)
{
   for(int j=0;j<imageheight;j++)
   {

    uint pixelval = data[i+j*imagewidth];
    QRgb color = qRgb(pixelval, pixelval, pixelval);
    image.setPixel(i,j, color);

   }
}

The code is working and I am getting an image but I only get values in increments of 255... So 0, 255, ...

How I can set the actual pixel value for each pixel from 0 to 65535 ?


Solution

  • @Scheff, thanks for the input ! I am now using the following code:

    Qimage = QImage(imagewidth,imageheight,QImage::Format_Grayscale16);
    
    
        for (int j = 0; j < imageheight; ++j)
        {
           quint16 *dst =  reinterpret_cast<quint16*>(Qimage.bits() + j * Qimage.bytesPerLine());
    
           for (int i = 0; i < imagewidth; ++i)
           {
    
                unsigned short pixelval =  static_cast<unsigned short>(image[i + j * imagewidth]);
    
                dst[i] = pixelval;
    
           }
    
        }