Results 1 to 4 of 4

Thread: read file in b/w mode

  1. #1
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default read file in b/w mode

    Hello all!

    I need to load an image file (which is true color) in grayscale format. So, I need to convert the file during loading somehow. I read that there are flags for that. I am not sure (even after reading the assistant) how to do it.
    How close am I ?

    Qt Code:
    1. myImage.load(filename,Qt::MonoOnly);
    To copy to clipboard, switch view to plain text mode 

    thanks

  2. #2
    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: read file in b/w mode

    Faar faaaar away

    Load the image as usual and then either use QImage::convertToFormat() or iterate over its pixels and change their value to gray using qGray() and QImage::setPixel().

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

    eric (11th December 2007)

  4. #3
    Join Date
    Nov 2007
    Posts
    57
    Thanks
    36
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: read file in b/w mode

    OK you got me a bit closer, thanks. But I am still confused about what the "Format" really means and what the syntax is.

    These don't work:

    Qt Code:
    1. //myFile is a QPixmap
    2.  
    3. myFile.convertToFormat(3, Qt::MonoOnly);
    4.  
    5. //or
    6.  
    7. myFile.convertToFormat(Format_Indexed8, Qt::MonoOnly);
    To copy to clipboard, switch view to plain text mode 

    Compiler says: 'class QPixmap has no member named convertToFormat'

  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: read file in b/w mode

    The compiler is right. You should be using QImage.

    The format is the way image data is stored internally - I doubt converting to indexed format will make you image gray - you'd have to change its palette to do that afterwards. MonoOnly would probably make you image black and white instead of grayscale. So you either need to convert to an indexed mode and then readjust the colour map (palette) or iterate over pixels and convert each of them to gray.

Similar Threads

  1. How to open a file in Read Write mode
    By merry in forum Qt Programming
    Replies: 13
    Last Post: 16th November 2007, 14:40
  2. How to read text only as it is from file
    By thomasjoy in forum Qt Programming
    Replies: 3
    Last Post: 9th August 2007, 08:47
  3. qt-3.3.8 fail in scratchbox
    By nass in forum Installation and Deployment
    Replies: 0
    Last Post: 25th May 2007, 15:21
  4. How to read line from file
    By Krishnacins in forum Newbie
    Replies: 10
    Last Post: 1st June 2006, 23:14
  5. Replies: 13
    Last Post: 1st June 2006, 14:01

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.