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. Monochrome QImage transformation - void area looks black

Monochrome QImage transformation - void area looks black

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 2 Posters 1.9k Views 1 Watching
  • 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.
  • K Offline
    K Offline
    KBA_
    wrote on last edited by
    #1

    Hi there,

    I want to rotate a monochrome image of Type QImage using QImage::transformed method.
    After applying rotation, the background color of the void area is always black.

    The Image before Rotation lookes like this: 0_1510848324138_original.gif

    After rotation, of 45° clockwise, it lookes like this: 0_1510848336862_rotated.gif

    But it should look like this: 0_1510848350525_rotated_correct.png

    My question is how can I make sure that the void area color is white?
    Thanks in advance!

    Here is my sample code:

    #include <QImage>
    #include <QPainter>
    
    int main()
    {
       // size of square Image
       const int nSize = 100;
    
       // create monochrome Image
       QImage img = QImage(nSize, nSize, QImage::QImage::Format_Mono);
    
       QPainter pnt;
       pnt.begin(&img);
    
       // fill background with white
       pnt.fillRect(0, 0, nSize, nSize, Qt::color0);
       pnt.setPen(QPen(Qt::color1));
       // draw a rectangle
       pnt.drawRect(0, 0, nSize-1, nSize-1);
       pnt.end();
    
       img.save("original.bmp");
    
       // rotate image
       QMatrix m;
       m.rotate(45);
       img = img.transformed(m);
       img.save("rotated.bmp");
    
       return 0;
    }
    
    1 Reply Last reply
    0
    • VRoninV Offline
      VRoninV Offline
      VRonin
      wrote on last edited by
      #2

      Top of my head: call img.invertPixels() both before and after the transformation

      P.S.
      QMatrix is deprecated, use QTransform instead

      "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
      ~Napoleon Bonaparte

      On a crusade to banish setIndexWidget() from the holy land of Qt

      1 Reply Last reply
      2
      • K Offline
        K Offline
        KBA_
        wrote on last edited by
        #3

        First of all, thank you for your reply!

        I already tried your approach before, but my application is very time critical and I want to avoid unnecessary computation time! Is there a way to tell the transformation process which color to take for the void area pixels?

        By the way, the approach doesn't work, it produces the following:

        • 1_original.bmp
          0_1510911143081_1_original.jpg

        • 2_original_inverted.bmp
          0_1510911152525_2_original_inverted.jpg

        • 3_rotated_inverted.bmp
          0_1510911160790_3_rotated_inverted.jpg

        • 4_rotated_normal.bmp:
          0_1510911166687_4_rotated_normal.jpg

        Using this code fragment:

        #include <QImage>
        #include <QPainter>
        
        int main()
        {
           // size of square Image
           const int nSize = 100;
        
           // create monochrome Image
           QImage img = QImage(nSize, nSize, QImage::QImage::Format_Mono);
        
           QPainter pnt;
           pnt.begin(&img);
        
           // fill background with white
           pnt.fillRect(0, 0, nSize, nSize, Qt::color0);
           pnt.setPen(QPen(Qt::color1));
           // draw a rectangle
           pnt.drawRect(0, 0, nSize-1, nSize-1);
           pnt.end();
        
           img.save("1_original.bmp");
           img.invertPixels();
           img.save("2_original_inverted.bmp");
        
           // rotate image
           QMatrix m;
           m.rotate(45);
           img = img.transformed(m);
           img.save("3_rotated_inverted.bmp");
        
           img.invertPixels();
           img.save("4_rotated_normal.bmp");
        
           return 0;
        }
        
        1 Reply Last reply
        0
        • VRoninV Offline
          VRoninV Offline
          VRonin
          wrote on last edited by
          #4

          img = img.transformed(m); will convert the format() to QImage::Format_ARGB32_Premultiplied (you can check by adding qDebug() << img.format(); before img.save("3_rotated_inverted.bmp");) so you have to convert it back to Mono

          #include <QImage>
          #include <QPainter>
          int main()
          {
             // size of square Image
             const int nSize = 100;
          
             // create monochrome Image
             QImage img = QImage(nSize, nSize, QImage::Format_Mono);
          
             QPainter pnt;
             pnt.begin(&img);
          
             // fill background with white
             pnt.fillRect(0, 0, nSize, nSize, Qt::color0);
             pnt.setPen(QPen(Qt::color1));
             // draw a rectangle
             pnt.drawRect(0, 0, nSize-1, nSize-1);
             pnt.end();
          
             img.save("1_original.bmp");
             img.invertPixels();
             img.save("2_original_inverted.bmp");
          
             // rotate image
             QTransform m;
             m.rotate(45);
             img = img.transformed(m);
             img.save("3_rotated_inverted.bmp");
             img=img.convertToFormat(QImage::Format_Mono);
             img.invertPixels();
             img.save("4_rotated_normal.bmp");
          
             return 0;
          }
          

          I had a quick look at the sources and I couldn't see any way of setting the "background" colour nor to prevent the conversion to ARGB32.

          "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
          ~Napoleon Bonaparte

          On a crusade to banish setIndexWidget() from the holy land of Qt

          1 Reply Last reply
          1

          • Login

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