0

hello I want to display grayscale image using qt. my problem is when i have image i get pixel with

QRgb pixel=imagef->pixel(i,j);
int color= qGray(pixelfft);

then i apply some modifications then I return the pixel result to image which I want to display grayscale. I do that and this work with color images in my source

imagef->setPixel(i,j, qRgb(x,x,x));

but when i choose white and black image then i do my treatment and when i display it that dosen't work only with this

imagef->setPixel(i,j,qGray(qRgb(x,x,x)));

can i find how to display grayscale image with any type of image source? please help me.

rabeb
  • 19
  • 2

1 Answers1

0

It seems your grayscale image has a different format that RGB or ARGB. Note that the signature of setPixel is

void QImage::setPixel(int x, int y, uint index_or_rgb)

in RGB or ARGB, the value will be a ARGB value, in mono or indexed it will be a color index.

If your image is already grayscale I to see fail why you should process it. Thus just do

switch(image.format())
{
   case Qimage::Indexed:
        //drawing in indexed is not possible
        image = make_gray_indexed_processing(image);
        break;
   case QImage::Format_RGB32:
   case QImage::Format_ARGB32:
      image = make_gray_rgb_processing(image);
      break;

   case QImage::Format_Mono :
   case QImage::Format_Mono_LSB :
      //do nothing
      break;
   default:
      //out of scope of answer
}
UmNyobe
  • 22,539
  • 9
  • 61
  • 90
  • `QImage::pixel()` and `setPixel()` are really slow. Get/set pixel values from raw pixel buffer if possible. See [`QImage::bits()`](https://doc.qt.io/qt-5/qimage.html#bits) and [`QImage::scanLine()`](https://doc.qt.io/qt-5/qimage.html#scanLine). Example: http://stackoverflow.com/a/2095127/4149835 and http://stackoverflow.com/a/6102833/4149835 – Vladimir Bershov Feb 09 '16 at 12:19
  • 1
    @VladimirBershov [I am well aware of it](http://stackoverflow.com/a/27952197/1122645). But he is not even there. – UmNyobe Feb 09 '16 at 13:06