Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Alpha channel not written to bmp by QImage::save?
QtWS25 Last Chance

Alpha channel not written to bmp by QImage::save?

Scheduled Pinned Locked Moved Solved General and Desktop
qimagebmp
9 Posts 4 Posters 4.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    shwoods
    wrote on 18 Jan 2018, 01:11 last edited by
    #1

    Hello --

    Simple question, I hope. If I am using a QImage format that includes alpha, is QImage capable of writing it to a bmp with the alpha?

    For instance, given the following program:

    #include <QGuiApplication>
    #include <QDebug>
    #include <QImage>
    #include <QPixelFormat>

    int main(int argc, char *argv[])
    {
    QGuiApplication a(argc, argv);

    QImage internal(50,50, QImage::Format_ARGB32);
    
    bool internal_has_alpha = internal.hasAlphaChannel();
    
    internal.save("Saved.bmp");
    
    QImage saved_bitmap("Saved.bmp", "BMP");
    
    bool saved_has_alpha = saved_bitmap.hasAlphaChannel();
    
    qDebug() << "internal alpha = " << internal_has_alpha;
    qDebug() << "saved alpha = " << saved_has_alpha;
    
    return 0;
    

    }

    The output is:

    internal alpha = true
    saved alpha = false

    Am I doing something wrong on save?

    Thanks for any info you can give.

    J R 2 Replies Last reply 18 Jan 2018, 07:04
    0
    • S shwoods
      18 Jan 2018, 01:11

      Hello --

      Simple question, I hope. If I am using a QImage format that includes alpha, is QImage capable of writing it to a bmp with the alpha?

      For instance, given the following program:

      #include <QGuiApplication>
      #include <QDebug>
      #include <QImage>
      #include <QPixelFormat>

      int main(int argc, char *argv[])
      {
      QGuiApplication a(argc, argv);

      QImage internal(50,50, QImage::Format_ARGB32);
      
      bool internal_has_alpha = internal.hasAlphaChannel();
      
      internal.save("Saved.bmp");
      
      QImage saved_bitmap("Saved.bmp", "BMP");
      
      bool saved_has_alpha = saved_bitmap.hasAlphaChannel();
      
      qDebug() << "internal alpha = " << internal_has_alpha;
      qDebug() << "saved alpha = " << saved_has_alpha;
      
      return 0;
      

      }

      The output is:

      internal alpha = true
      saved alpha = false

      Am I doing something wrong on save?

      Thanks for any info you can give.

      J Offline
      J Offline
      J.Hilk
      Moderators
      wrote on 18 Jan 2018, 07:04 last edited by
      #2

      @shwoods
      hi,
      I'm not sure, what the inner workings of QImage::save are, however alphachannel support seams not to be supported for bmp-saves. But, if you change your format from bmp to png, it should work.


      Be aware of the Qt Code of Conduct, when posting : https://forum.qt.io/topic/113070/qt-code-of-conduct


      Q: What's that?
      A: It's blue light.
      Q: What does it do?
      A: It turns blue.

      1 Reply Last reply
      2
      • S shwoods
        18 Jan 2018, 01:11

        Hello --

        Simple question, I hope. If I am using a QImage format that includes alpha, is QImage capable of writing it to a bmp with the alpha?

        For instance, given the following program:

        #include <QGuiApplication>
        #include <QDebug>
        #include <QImage>
        #include <QPixelFormat>

        int main(int argc, char *argv[])
        {
        QGuiApplication a(argc, argv);

        QImage internal(50,50, QImage::Format_ARGB32);
        
        bool internal_has_alpha = internal.hasAlphaChannel();
        
        internal.save("Saved.bmp");
        
        QImage saved_bitmap("Saved.bmp", "BMP");
        
        bool saved_has_alpha = saved_bitmap.hasAlphaChannel();
        
        qDebug() << "internal alpha = " << internal_has_alpha;
        qDebug() << "saved alpha = " << saved_has_alpha;
        
        return 0;
        

        }

        The output is:

        internal alpha = true
        saved alpha = false

        Am I doing something wrong on save?

        Thanks for any info you can give.

        R Offline
        R Offline
        raven-worx
        Moderators
        wrote on 18 Jan 2018, 07:44 last edited by
        #3

        @shwoods
        as @J.Hilk said, the BMP image format has per definition no alpha channel support.
        You need to save to a format which supports alpha channel (PNG, TIFF, ...)

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        S 1 Reply Last reply 18 Jan 2018, 12:17
        0
        • R raven-worx
          18 Jan 2018, 07:44

          @shwoods
          as @J.Hilk said, the BMP image format has per definition no alpha channel support.
          You need to save to a format which supports alpha channel (PNG, TIFF, ...)

          S Offline
          S Offline
          shwoods
          wrote on 18 Jan 2018, 12:17 last edited by
          #4

          @raven-worx

          I believe what you say is true -- that earlier bmp headers by definition did not support the alpha channel, but as I understand it, newer BMP headers (BITMAPV4HEADER and later) support alpha channel. Is it safe to say that QImage uses the older headers, which do not support alpha, and just convert it to RGB on save?

          You are correct that png works fine -- I just need the later header on a bmp for the system I am working with. I can work around this, I am just curious as to whether I am just missing the command to generate the later headers.

          1 Reply Last reply
          0
          • SGaistS Offline
            SGaistS Offline
            SGaist
            Lifetime Qt Champion
            wrote on 18 Jan 2018, 15:14 last edited by
            #5

            Hi and welcome to devnet,

            AFAIK, you didn't miss anything. You can take a look at the qbmphandler.cpp for the details of bitmap creation. If you'd like to, you can consider adding the support for alpha channel there.

            Interested in AI ? www.idiap.ch
            Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

            R 1 Reply Last reply 18 Jan 2018, 15:23
            0
            • SGaistS SGaist
              18 Jan 2018, 15:14

              Hi and welcome to devnet,

              AFAIK, you didn't miss anything. You can take a look at the qbmphandler.cpp for the details of bitmap creation. If you'd like to, you can consider adding the support for alpha channel there.

              R Offline
              R Offline
              raven-worx
              Moderators
              wrote on 18 Jan 2018, 15:23 last edited by raven-worx
              #6

              @SGaist
              if i see correctly, the code for reading supports the BMP alpha channel, where as only the code for writing does not.
              Maybe for a reason? idk

              --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
              If you have a question please use the forum so others can benefit from the solution in the future

              S 1 Reply Last reply 29 Jan 2018, 13:28
              1
              • S Offline
                S Offline
                shwoods
                wrote on 18 Jan 2018, 15:30 last edited by
                #7

                Thank you both! I will take a look at the code and see what it would take to change it.

                1 Reply Last reply
                0
                • R raven-worx
                  18 Jan 2018, 15:23

                  @SGaist
                  if i see correctly, the code for reading supports the BMP alpha channel, where as only the code for writing does not.
                  Maybe for a reason? idk

                  S Offline
                  S Offline
                  shwoods
                  wrote on 29 Jan 2018, 13:28 last edited by
                  #8

                  @raven-worx

                  I was checking the code today and had the same question as you -- was there a particular reason alpha was left off on writing? Did you ever get feedback on this?

                  R 1 Reply Last reply 29 Jan 2018, 13:49
                  0
                  • S shwoods
                    29 Jan 2018, 13:28

                    @raven-worx

                    I was checking the code today and had the same question as you -- was there a particular reason alpha was left off on writing? Did you ever get feedback on this?

                    R Offline
                    R Offline
                    raven-worx
                    Moderators
                    wrote on 29 Jan 2018, 13:49 last edited by
                    #9

                    @shwoods said in Alpha channel not written to bmp by QImage::save?:

                    Did you ever get feedback on this?

                    no, since i never asked anyone :)
                    You can ask on the Qt mailing-list if you like.

                    --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                    If you have a question please use the forum so others can benefit from the solution in the future

                    1 Reply Last reply
                    0

                    • Login

                    • Login or register to search.
                    • First post
                      Last post
                    0
                    • Categories
                    • Recent
                    • Tags
                    • Popular
                    • Users
                    • Groups
                    • Search
                    • Get Qt Extensions
                    • Unsolved