title: Strange color behaviour while merging two images with transparency
body: <p>This is a question that has already been asked, but I'm encountering an issue that I couldn't find anywhere and that I don't understand. </p>
  
  <p>My goal is simple: draw a background image and then draw upon it an image with transparency. It works great at the beginning, when the top image has a weak transparency, but as soon as the top image is almost transparent, I get weird color on the result image.</p>
  
  <p>Here is the original image:
  <a href="http://img406.imageshack.us/img406/7452/bidou.jpg" rel="nofollow noreferrer">original image http://img406.imageshack.us/img406/7452/bidou.jpg</a></p>
  
  <p>Here is the image I make progressively transparent:
  <a href="http://img515.imageshack.us/img515/4464/loltest31.png" rel="nofollow noreferrer">mask http://img515.imageshack.us/img515/4464/loltest31.png</a> (I resize it when drawing; I've tried to make it the original size, in case my issue would have come from interpolation or something like that, but it did not fix the issue...)</p>
  
  <p>And now the mask with some transparency:
  <a href="http://img19.imageshack.us/img19/3791/loltest4.png" rel="nofollow noreferrer">mask step 4 http://img19.imageshack.us/img19/3791/loltest4.png</a></p>
  
  <p>And the result:
  <a href="https://i.stack.imgur.com/wiIMn.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/wiIMn.jpg" alt="result step 4"></a>
  </p>
  
  <p>And a last result, when the transparency on the mask (I never change the color data, only the alpha value) is almost complete (alpha=7 (scale from 0 to 255)):
  <a href="https://i.stack.imgur.com/BWSyH.jpg" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/BWSyH.jpg" alt="result step 1"></a>
  </p>
  
  <p>As you can see, some strange color appears on the result, like the red dots on top of the hats, and the transparency doesn't seem to be at the same level everywhere (although it is, because I check it by saving the mask). What is more, some zones of the result image are entirely transparent, even if I never modify the original image and always draw it first.</p>
  
  <p>When the mask is entirely transparent, the result is the same as the original image, but as soon as the alpha is not 0, it makes those strange colored pixels.</p>
  
  <p>I have tried different blend modes, without success.</p>
  
  <p>Here is one of the way I do it in the code:</p>
  
  <pre><code>    UIImage *bottomImage = self.originalImage;
      UIImage *image       = [self getUIImage];
  
      CGSize newSize = mySize;
      UIGraphicsBeginImageContext( newSize );
  
      // Use existing opacity as is
      [bottomImage drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  
      // Apply supplied opacity if applicable
      [image drawInRect:CGRectMake(0,0,newSize.width,newSize.height)];
  
      resultImage = UIGraphicsGetImageFromCurrentImageContext();
  
      UIGraphicsEndImageContext();
  </code></pre>
  
  <p>And one other way (which is the same, finally):</p>
  
  <pre><code>CGSize mySize = self.originalImage.size;
  UIGraphicsBeginImageContextWithOptions(mySize, YES, 0);
  CGContextRef globalContext = UIGraphicsGetCurrentContext();
  
  CGContextSaveGState(globalContext);
  
  CGContextSetBlendMode(globalContext, kCGBlendModeNormal);
  CGContextDrawImage(globalContext, CGRectMake(0, 0, mySize.width, mySize.height), self.originalImage.CGImage);
  CGContextDrawImage(globalContext, CGRectMake(0, 0, mySize.width, mySize.height), [self getUIImage].CGImage);
  
  CGImageRef   resultRef = CGBitmapContextCreateImage(globalContext);
  UIImage*     resultImage = [UIImage imageWithCGImage:resultRef];
  
  CGContextRestoreGState(globalContext);
  UIGraphicsEndImageContext();
  </code></pre>
  
  <p>Anyone, any idea? I can't think of anything I haven't tried...</p>
  
  <p>Edit: A result with a simple 4*2 image:</p>
  
  <pre><code> Empty:
  0: 0; 0; 0; 16
  1: 0; 0; 0; 16
  2: 0; 0; 0; 0
  3: 0; 0; 0; 0
  4: 0; 0; 0; 0
  5: 0; 0; 0; 0
  6: 0; 0; 0; 0
  7: 0; 0; 0; 0
  
   Bottom drawn:
  0: 0; 0; 0; 255
  1: 128; 0; 0; 255
  2: 128; 128; 0; 255
  3: 0; 128; 0; 255
  4: 0; 128; 128; 255
  5: 0; 0; 128; 255
  6: 128; 0; 128; 255
  7: 128; 128; 128; 255
  
   foreground:
  0: 0; 0; 0; 7
  1: 128; 0; 0; 7
  2: 128; 128; 0; 7
  3: 0; 128; 0; 7
  4: 0; 128; 128; 7
  5: 0; 0; 128; 7
  6: 128; 0; 128; 7
  7: 128; 128; 128; 7
  
   front drawn:
  0: 0; 0; 0; 255
  1: 0; 128; 128; 192
  2: 128; 128; 0; 0
  3: 0; 128; 0; 255
  4: 0; 128; 128; 255
  5: 0; 0; 128; 255
  6: 128; 0; 128; 0
  </code></pre>