Authored by DreamPiggy
Committed by GitHub

Merge pull request #2272 from dreampiggy/fix_force_decode_alpha

Fix that force decode not works for alpha-channel images.
@@ -236,7 +236,12 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -236,7 +236,12 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
236 @autoreleasepool{ 236 @autoreleasepool{
237 237
238 CGImageRef imageRef = image.CGImage; 238 CGImageRef imageRef = image.CGImage;
239 - CGColorSpaceRef colorspaceRef = [[self class] colorSpaceForImageRef:imageRef]; 239 + // device color space
  240 + CGColorSpaceRef colorspaceRef = SDCGColorSpaceGetDeviceRGB();
  241 + BOOL hasAlpha = SDCGImageRefContainsAlpha(imageRef);
  242 + // iOS display alpha info (BRGA8888/BGRX8888)
  243 + CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;
  244 + bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;
240 245
241 size_t width = CGImageGetWidth(imageRef); 246 size_t width = CGImageGetWidth(imageRef);
242 size_t height = CGImageGetHeight(imageRef); 247 size_t height = CGImageGetHeight(imageRef);
@@ -250,7 +255,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -250,7 +255,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
250 kBitsPerComponent, 255 kBitsPerComponent,
251 0, 256 0,
252 colorspaceRef, 257 colorspaceRef,
253 - kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast); 258 + bitmapInfo);
254 if (context == NULL) { 259 if (context == NULL) {
255 return image; 260 return image;
256 } 261 }
@@ -294,8 +299,12 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -294,8 +299,12 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
294 destResolution.width = (int)(sourceResolution.width*imageScale); 299 destResolution.width = (int)(sourceResolution.width*imageScale);
295 destResolution.height = (int)(sourceResolution.height*imageScale); 300 destResolution.height = (int)(sourceResolution.height*imageScale);
296 301
297 - // current color space  
298 - CGColorSpaceRef colorspaceRef = [[self class] colorSpaceForImageRef:sourceImageRef]; 302 + // device color space
  303 + CGColorSpaceRef colorspaceRef = SDCGColorSpaceGetDeviceRGB();
  304 + BOOL hasAlpha = SDCGImageRefContainsAlpha(sourceImageRef);
  305 + // iOS display alpha info (BGRA8888/BGRX8888)
  306 + CGBitmapInfo bitmapInfo = kCGBitmapByteOrder32Host;
  307 + bitmapInfo |= hasAlpha ? kCGImageAlphaPremultipliedFirst : kCGImageAlphaNoneSkipFirst;
299 308
300 // kCGImageAlphaNone is not supported in CGBitmapContextCreate. 309 // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
301 // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast 310 // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
@@ -306,7 +315,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -306,7 +315,7 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
306 kBitsPerComponent, 315 kBitsPerComponent,
307 0, 316 0,
308 colorspaceRef, 317 colorspaceRef,
309 - kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast); 318 + bitmapInfo);
310 319
311 if (destContext == NULL) { 320 if (destContext == NULL) {
312 return image; 321 return image;
@@ -453,14 +462,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -453,14 +462,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
453 return NO; 462 return NO;
454 } 463 }
455 464
456 - CGImageRef imageRef = image.CGImage;  
457 -  
458 - BOOL hasAlpha = SDCGImageRefContainsAlpha(imageRef);  
459 - // do not decode images with alpha  
460 - if (hasAlpha) {  
461 - return NO;  
462 - }  
463 -  
464 return YES; 465 return YES;
465 } 466 }
466 467
@@ -558,21 +559,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over @@ -558,21 +559,6 @@ static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to over
558 559
559 return shouldScaleDown; 560 return shouldScaleDown;
560 } 561 }
561 -  
562 -+ (CGColorSpaceRef)colorSpaceForImageRef:(CGImageRef)imageRef {  
563 - // current  
564 - CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));  
565 - CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);  
566 -  
567 - BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||  
568 - imageColorSpaceModel == kCGColorSpaceModelMonochrome ||  
569 - imageColorSpaceModel == kCGColorSpaceModelCMYK ||  
570 - imageColorSpaceModel == kCGColorSpaceModelIndexed);  
571 - if (unsupportedColorSpace) {  
572 - colorspaceRef = SDCGColorSpaceGetDeviceRGB();  
573 - }  
574 - return colorspaceRef;  
575 -}  
576 #endif 562 #endif
577 563
578 @end 564 @end
@@ -43,12 +43,12 @@ @@ -43,12 +43,12 @@
43 expect(decodedImage).to.equal(animatedImage); 43 expect(decodedImage).to.equal(animatedImage);
44 } 44 }
45 45
46 -- (void)test04ThatDecodedImageWithImageDoesNotDecodeImagesWithAlpha { 46 +- (void)test04ThatDecodedImageWithImageWorksWithAlphaImages {
47 NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"]; 47 NSString * testImagePath = [[NSBundle bundleForClass:[self class]] pathForResource:@"TestImage" ofType:@"png"];
48 UIImage *image = [UIImage imageWithContentsOfFile:testImagePath]; 48 UIImage *image = [UIImage imageWithContentsOfFile:testImagePath];
49 UIImage *decodedImage = [UIImage decodedImageWithImage:image]; 49 UIImage *decodedImage = [UIImage decodedImageWithImage:image];
50 expect(decodedImage).toNot.beNil(); 50 expect(decodedImage).toNot.beNil();
51 - expect(decodedImage).to.equal(image); 51 + expect(decodedImage).toNot.equal(image);
52 } 52 }
53 53
54 - (void)test05ThatDecodedImageWithImageWorksEvenWithMonochromeImage { 54 - (void)test05ThatDecodedImageWithImageWorksEvenWithMonochromeImage {