File ImageMagick-bmp3-opacity.patch of Package ImageMagick.openSUSE_13.2_Update

Index: coders/bmp.c
===================================================================
--- coders/bmp.c.orig	2015-11-09 12:22:54.697816951 +0100
+++ coders/bmp.c	2015-11-09 13:29:58.137445707 +0100
@@ -869,8 +869,7 @@
     image->rows=(size_t) MagickAbsoluteValue(bmp_info.height);
     image->depth=bmp_info.bits_per_pixel <= 8 ? bmp_info.bits_per_pixel : 8;
     image->matte=((bmp_info.alpha_mask != 0) &&
-      (bmp_info.compression == BI_BITFIELDS)) || 
-       (bmp_info.bits_per_pixel == 32) ? MagickTrue : MagickFalse;
+      (bmp_info.compression == BI_BITFIELDS)) ? MagickTrue : MagickFalse;
     if (bmp_info.bits_per_pixel < 16)
       {
         size_t
@@ -976,6 +975,32 @@
     */
     if (bmp_info.compression == BI_RGB)
       {
+        /*
+          We should ignore the alpha value in BMP3 files but there have been
+          reports about 32 bit files with alpha. We do a quick check to see if
+          the alpha channel contains a value that is not zero (default value).
+          If we find a non zero value we asume the program that wrote the file
+          wants to use the alpha channel.
+        */
+        if ((image->matte != MagickTrue) && (bmp_info.size == 40) &&
+            (bmp_info.bits_per_pixel == 32))
+          {
+            bytes_per_line=4*(image->columns);
+            for (y=(ssize_t) image->rows-1; y >= 0; y--)
+            {
+              p=pixels+(image->rows-y-1)*bytes_per_line;
+              for (x=0; x < (ssize_t) image->columns; x++)
+              {
+                if (*(p+3) != 0)
+                  {
+                    image->matte=MagickTrue;
+                    y=-1;
+                    break;
+                  }
+                p+=4;
+              }
+            }
+          }
         bmp_info.alpha_mask=image->matte != MagickFalse ? 0xff000000U : 0U;
         bmp_info.red_mask=0x00ff0000U;
         bmp_info.green_mask=0x0000ff00U;
@@ -1196,15 +1221,18 @@
               blue|=((blue & 0xe000) >> 5);
             if (quantum_bits.blue <= 8)
               blue|=((blue & 0xff00) >> 8);
-            opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
-            if (quantum_bits.opacity <= 8)
-              opacity|=((opacity & 0xff00) >> 8);
             SetPixelRed(q,ScaleShortToQuantum((unsigned short) red));
             SetPixelGreen(q,ScaleShortToQuantum((unsigned short) green));
             SetPixelBlue(q,ScaleShortToQuantum((unsigned short) blue));
             SetPixelOpacity(q,OpaqueOpacity);
             if (image->matte != MagickFalse)
-              SetPixelAlpha(q,ScaleShortToQuantum((unsigned short) opacity));
+              {
+                opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
+                if (quantum_bits.opacity <= 8)
+                  opacity|=((opacity & 0xff00) >> 8);
+                SetPixelAlpha(q,ScaleShortToQuantum(
+                  (unsigned short) opacity));
+              }
             q++;
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
@@ -1290,15 +1318,18 @@
             blue=((pixel & bmp_info.blue_mask) << shift.blue) >> 16;
             if (quantum_bits.blue == 8)
               blue|=(blue >> 8);
-            opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
-            if (quantum_bits.opacity == 8)
-              opacity|=(opacity >> 8);
             SetPixelRed(q,ScaleShortToQuantum((unsigned short) red));
             SetPixelGreen(q,ScaleShortToQuantum((unsigned short) green));
             SetPixelBlue(q,ScaleShortToQuantum((unsigned short) blue));
             SetPixelOpacity(q,OpaqueOpacity);
             if (image->matte != MagickFalse)
-              SetPixelAlpha(q,ScaleShortToQuantum((unsigned short) opacity));
+              {
+                opacity=((pixel & bmp_info.alpha_mask) << shift.opacity) >> 16;
+                if (quantum_bits.opacity == 8)
+                  opacity|=(opacity >> 8);
+                SetPixelAlpha(q,ScaleShortToQuantum(
+                  (unsigned short) opacity));
+              }
             q++;
           }
           if (SyncAuthenticPixels(image,exception) == MagickFalse)
openSUSE Build Service is sponsored by