Skip to content
This repository

Fix "Alpha on JPEG with no alpha" when decoding #299

Merged
merged 1 commit into from about 1 year ago

2 participants

Martín Conte Mac Donell Olivier Poitrey
Martín Conte Mac Donell

Fix for revert: 63178bd

Martín Conte Mac Donell Reflejo referenced this pull request from a commit February 07, 2013
Olivier Poitrey Revert "Fixed CGBitmapContextCreate warnings for invalid alpha proper…
…ties"

This reverts commit 12196f4.
It adds alpha on JPEG with no alpha.
63178bd
Olivier Poitrey rs merged commit 5f53560 into from February 16, 2013
Olivier Poitrey rs closed this February 16, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Feb 13, 2013
Martín Conte Mac Donell [BUG] Fix invalid alpha on JPEG files de19c24
This page is out of date. Refresh to see the latest.

Showing 1 changed file with 35 additions and 2 deletions. Show diff stats Hide diff stats

  1. 37  SDWebImage/SDWebImageDecoder.m
37  SDWebImage/SDWebImageDecoder.m
@@ -18,8 +18,41 @@ + (UIImage *)decodedImageWithImage:(UIImage *)image
18 18
     CGSize imageSize = CGSizeMake(CGImageGetWidth(imageRef), CGImageGetHeight(imageRef));
19 19
     CGRect imageRect = (CGRect){.origin = CGPointZero, .size = imageSize};
20 20
 
21  
-    CGColorSpaceRef colorSpace = CGImageGetColorSpace(imageRef);
22  
-    CGContextRef context = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, CGImageGetBitsPerComponent(imageRef), CGImageGetBytesPerRow(imageRef), colorSpace, CGImageGetBitmapInfo(imageRef));
  21
+    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
  22
+    CGBitmapInfo bitmapInfo = CGImageGetBitmapInfo(imageRef);
  23
+
  24
+    int infoMask = (bitmapInfo & kCGBitmapAlphaInfoMask);
  25
+    BOOL anyNonAlpha = (infoMask == kCGImageAlphaNone ||
  26
+                        infoMask == kCGImageAlphaNoneSkipFirst ||
  27
+                        infoMask == kCGImageAlphaNoneSkipLast);
  28
+
  29
+    // CGBitmapContextCreate doesn't support kCGImageAlphaNone with RGB.
  30
+    // https://developer.apple.com/library/mac/#qa/qa1037/_index.html
  31
+    if (infoMask == kCGImageAlphaNone && CGColorSpaceGetNumberOfComponents(colorSpace) > 1)
  32
+    {
  33
+        // Unset the old alpha info.
  34
+        bitmapInfo &= ~kCGBitmapAlphaInfoMask;
  35
+       
  36
+        // Set noneSkipFirst.
  37
+        bitmapInfo |= kCGImageAlphaNoneSkipFirst;
  38
+    }
  39
+    // Some PNGs tell us they have alpha but only 3 components. Odd.
  40
+    else if (!anyNonAlpha && CGColorSpaceGetNumberOfComponents(colorSpace) == 3)
  41
+    {
  42
+        // Unset the old alpha info.
  43
+        bitmapInfo &= ~kCGBitmapAlphaInfoMask;
  44
+        bitmapInfo |= kCGImageAlphaPremultipliedFirst;
  45
+    }
  46
+
  47
+    // It calculates the bytes-per-row based on the bitsPerComponent and width arguments.
  48
+    CGContextRef context = CGBitmapContextCreate(NULL,
  49
+                                                 imageSize.width,
  50
+                                                 imageSize.height,
  51
+                                                 CGImageGetBitsPerComponent(imageRef),
  52
+                                                 0,
  53
+                                                 colorSpace,
  54
+                                                 bitmapInfo);
  55
+    CGColorSpaceRelease(colorSpace);
23 56
 
24 57
     // If failed, return undecompressed image
25 58
     if (!context) return image;
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.