Results 1 to 9 of 9

Thread: Save greyscale image

  1. #1
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Save greyscale image

    Hi all,
    I'm triyng to save a image buffer only grey into an image.
    When I save to a file a get an error saying QVector<T> out of range.
    Is there something I'm missing?

    Thanks in advance for any answer

    Qt Code:
    1. QImage tmp(width, height, QImage::Format_Indexed8);
    2. memcpy(tmp.bits(), pFrame->data[0], width * height);
    3. QString filename = QString("C:\\testscene\\frame%1.jpg").arg(durationText);
    4. tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    May 2006
    Posts
    788
    Thanks
    49
    Thanked 48 Times in 46 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Save greyscale image

    Try this here:

    Qt Code:
    1. extern inline QImage GreyScale( QImage income )
    2. {
    3.  
    4. QImage base = income.convertToFormat(QImage::Format_RGB32);
    5. for (int y = 0; y < base.height(); ++y) {
    6. for (int x = 0; x < base.width(); ++x) {
    7. int pixel = base.pixel(x, y);
    8. int gray = qGray(pixel);
    9. int alpha = qAlpha(pixel);
    10. base.setPixel(x, y, qRgba(gray, gray, gray, alpha));
    11. }
    12. }
    13.  
    14. return base;
    15. }
    To copy to clipboard, switch view to plain text mode 

    Or Sepia convert: ( htmledit/src/interface.cpp )
    http://www.google.com/codesearch?hl=...le+%2BColorize

  3. The following user says thank you to patrik08 for this useful post:

    yogeshgokul (12th August 2008)

  4. #3
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    6,264
    Thanks
    36
    Thanked 1,519 Times in 1,389 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows Symbian S60 Maemo/MeeGo

    Default Re: Save greyscale image

    One can also (ab)use QIcon by adding a pixmap in normal mode and requesting a pixmap in disabled mode.
    J-P Nurmi

  5. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Save greyscale image

    I think the problem comes from the fact that you didn't define a colour map for the indexed pixmap. Indexed images contain not the colour values in their pixel data but indexes of colours in a lookup table. This table has to be filled with real colours. Otherwise if your pixel data contains colour values, you probably should use real-colour image format such as QImage::Format_RGB32. Of course you'll have to provide 32b long pixel values.

  6. #5
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save greyscale image

    Thanks for the answers.
    I tried this

    Qt Code:
    1. for (int i=0; i<=255; i++)
    2. {
    3. colorTable.append(qRgb(i, i, i));
    4. }
    5.  
    6. QImage tmp(width, height, QImage::Format_Indexed8);
    7.  
    8. tmp.setColorTable(colorTable);
    9.  
    10. memcpy(tmp.bits(), pFrame->data[0], width * height);
    11. QString filename = QString("C:\\testscene\\frame%1.png").arg(durationText);
    12.  
    13. tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
    To copy to clipboard, switch view to plain text mode 

    Now there's no error but the resulting png is all black.
    Any idea?
    thanks again

  7. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: Save greyscale image

    What values does pFrame->data[0] contain? They should be from within range 0-255.

  8. #7
    Join Date
    Jan 2006
    Location
    Sta. Eugènia de Berga (Vic - Barcelona - Spain)
    Posts
    869
    Thanks
    70
    Thanked 59 Times in 57 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: Save greyscale image

    Hi,

    Changing
    Qt Code:
    1. memcpy(tmp.bits(),pFrame->data[0], width * height);
    To copy to clipboard, switch view to plain text mode 

    to
    Qt Code:
    1. memcpy(tmp.bits(),pFrame->data, width * height);
    To copy to clipboard, switch view to plain text mode 

    is getting you different result? How "pFrame" is defined?
    Òscar Llarch i Galán

  9. #8
    Join Date
    Jan 2006
    Posts
    122
    Thanks
    16
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Save greyscale image

    I "fixed" it in this way:

    Qt Code:
    1. QImage tmp(width, height, QImage::Format_RGB888);
    2.  
    3. uchar* curs = tmp.bits();
    4. int cont = 0;
    5.  
    6. for(int i=0; i < width * height; i++)
    7. {
    8. curs[cont++] = pFrame->data[0][i];
    9. curs[cont++] = pFrame->data[0][i];
    10. curs[cont++] = pFrame->data[0][i];
    11. }
    12.  
    13. //memcpy(tmp.bits(), pFrame->data[0], width * height);
    14.  
    15. tmp.scaledToHeight(120, Qt::SmoothTransformation).save(filename);
    To copy to clipboard, switch view to plain text mode 

  10. #9
    Join Date
    Dec 2007
    Posts
    628
    Thanks
    3
    Thanked 89 Times in 87 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: Save greyscale image

    @ patrik08
    Good compact function, working fine. I used this function in my app. Thank you.

Similar Threads

  1. is it possible to save a webpage as an image?
    By billconan in forum Qt Programming
    Replies: 2
    Last Post: 8th July 2008, 01:08
  2. save table as an image
    By ulmly in forum Qt Programming
    Replies: 4
    Last Post: 26th June 2008, 08:12
  3. Finding marks on scanned image for alignment
    By caduel in forum Qt Programming
    Replies: 1
    Last Post: 23rd September 2007, 02:10
  4. Painter greyscale, image quality paint-brush!
    By patrik08 in forum Qt Programming
    Replies: 4
    Last Post: 22nd March 2007, 20:32
  5. how to save somtihing drawed on QPaiter to an image?
    By cxl2253 in forum Qt Programming
    Replies: 3
    Last Post: 15th December 2006, 09:34

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.