SDWebImageDecoder.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272
  1. /*
  2. * This file is part of the SDWebImage package.
  3. * (c) Olivier Poitrey <rs@dailymotion.com>
  4. * (c) james <https://github.com/mystcolor>
  5. *
  6. * For the full copyright and license information, please view the LICENSE
  7. * file that was distributed with this source code.
  8. */
  9. #import "SDWebImageDecoder.h"
  10. @implementation UIImage (ForceDecode)
  11. #if SD_UIKIT || SD_WATCH
  12. static const size_t kBytesPerPixel = 4;
  13. static const size_t kBitsPerComponent = 8;
  14. + (nullable UIImage *)decodedImageWithImage:(nullable UIImage *)image {
  15. if (![UIImage shouldDecodeImage:image]) {
  16. return image;
  17. }
  18. // autorelease the bitmap context and all vars to help system to free memory when there are memory warning.
  19. // on iOS7, do not forget to call [[SDImageCache sharedImageCache] clearMemory];
  20. @autoreleasepool{
  21. CGImageRef imageRef = image.CGImage;
  22. CGColorSpaceRef colorspaceRef = [UIImage colorSpaceForImageRef:imageRef];
  23. size_t width = CGImageGetWidth(imageRef);
  24. size_t height = CGImageGetHeight(imageRef);
  25. size_t bytesPerRow = kBytesPerPixel * width;
  26. // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
  27. // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
  28. // to create bitmap graphics contexts without alpha info.
  29. CGContextRef context = CGBitmapContextCreate(NULL,
  30. width,
  31. height,
  32. kBitsPerComponent,
  33. bytesPerRow,
  34. colorspaceRef,
  35. kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
  36. if (context == NULL) {
  37. return image;
  38. }
  39. // Draw the image into the context and retrieve the new bitmap image without alpha
  40. CGContextDrawImage(context, CGRectMake(0, 0, width, height), imageRef);
  41. CGImageRef imageRefWithoutAlpha = CGBitmapContextCreateImage(context);
  42. UIImage *imageWithoutAlpha = [UIImage imageWithCGImage:imageRefWithoutAlpha
  43. scale:image.scale
  44. orientation:image.imageOrientation];
  45. CGContextRelease(context);
  46. CGImageRelease(imageRefWithoutAlpha);
  47. return imageWithoutAlpha;
  48. }
  49. }
  50. /*
  51. * Defines the maximum size in MB of the decoded image when the flag `SDWebImageScaleDownLargeImages` is set
  52. * Suggested value for iPad1 and iPhone 3GS: 60.
  53. * Suggested value for iPad2 and iPhone 4: 120.
  54. * Suggested value for iPhone 3G and iPod 2 and earlier devices: 30.
  55. */
  56. static const CGFloat kDestImageSizeMB = 60.0f;
  57. /*
  58. * Defines the maximum size in MB of a tile used to decode image when the flag `SDWebImageScaleDownLargeImages` is set
  59. * Suggested value for iPad1 and iPhone 3GS: 20.
  60. * Suggested value for iPad2 and iPhone 4: 40.
  61. * Suggested value for iPhone 3G and iPod 2 and earlier devices: 10.
  62. */
  63. static const CGFloat kSourceImageTileSizeMB = 20.0f;
  64. static const CGFloat kBytesPerMB = 1024.0f * 1024.0f;
  65. static const CGFloat kPixelsPerMB = kBytesPerMB / kBytesPerPixel;
  66. static const CGFloat kDestTotalPixels = kDestImageSizeMB * kPixelsPerMB;
  67. static const CGFloat kTileTotalPixels = kSourceImageTileSizeMB * kPixelsPerMB;
  68. static const CGFloat kDestSeemOverlap = 2.0f; // the numbers of pixels to overlap the seems where tiles meet.
  69. + (nullable UIImage *)decodedAndScaledDownImageWithImage:(nullable UIImage *)image {
  70. if (![UIImage shouldDecodeImage:image]) {
  71. return image;
  72. }
  73. if (![UIImage shouldScaleDownImage:image]) {
  74. return [UIImage decodedImageWithImage:image];
  75. }
  76. CGContextRef destContext;
  77. // autorelease the bitmap context and all vars to help system to free memory when there are memory warning.
  78. // on iOS7, do not forget to call [[SDImageCache sharedImageCache] clearMemory];
  79. @autoreleasepool {
  80. CGImageRef sourceImageRef = image.CGImage;
  81. CGSize sourceResolution = CGSizeZero;
  82. sourceResolution.width = CGImageGetWidth(sourceImageRef);
  83. sourceResolution.height = CGImageGetHeight(sourceImageRef);
  84. float sourceTotalPixels = sourceResolution.width * sourceResolution.height;
  85. // Determine the scale ratio to apply to the input image
  86. // that results in an output image of the defined size.
  87. // see kDestImageSizeMB, and how it relates to destTotalPixels.
  88. float imageScale = kDestTotalPixels / sourceTotalPixels;
  89. CGSize destResolution = CGSizeZero;
  90. destResolution.width = (int)(sourceResolution.width*imageScale);
  91. destResolution.height = (int)(sourceResolution.height*imageScale);
  92. // current color space
  93. CGColorSpaceRef colorspaceRef = [UIImage colorSpaceForImageRef:sourceImageRef];
  94. size_t bytesPerRow = kBytesPerPixel * destResolution.width;
  95. // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
  96. // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
  97. // to create bitmap graphics contexts without alpha info.
  98. destContext = CGBitmapContextCreate(NULL,
  99. destResolution.width,
  100. destResolution.height,
  101. kBitsPerComponent,
  102. bytesPerRow,
  103. colorspaceRef,
  104. kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
  105. if (destContext == NULL) {
  106. return image;
  107. }
  108. CGContextSetInterpolationQuality(destContext, kCGInterpolationHigh);
  109. // Now define the size of the rectangle to be used for the
  110. // incremental blits from the input image to the output image.
  111. // we use a source tile width equal to the width of the source
  112. // image due to the way that iOS retrieves image data from disk.
  113. // iOS must decode an image from disk in full width 'bands', even
  114. // if current graphics context is clipped to a subrect within that
  115. // band. Therefore we fully utilize all of the pixel data that results
  116. // from a decoding opertion by achnoring our tile size to the full
  117. // width of the input image.
  118. CGRect sourceTile = CGRectZero;
  119. sourceTile.size.width = sourceResolution.width;
  120. // The source tile height is dynamic. Since we specified the size
  121. // of the source tile in MB, see how many rows of pixels high it
  122. // can be given the input image width.
  123. sourceTile.size.height = (int)(kTileTotalPixels / sourceTile.size.width );
  124. sourceTile.origin.x = 0.0f;
  125. // The output tile is the same proportions as the input tile, but
  126. // scaled to image scale.
  127. CGRect destTile;
  128. destTile.size.width = destResolution.width;
  129. destTile.size.height = sourceTile.size.height * imageScale;
  130. destTile.origin.x = 0.0f;
  131. // The source seem overlap is proportionate to the destination seem overlap.
  132. // this is the amount of pixels to overlap each tile as we assemble the ouput image.
  133. float sourceSeemOverlap = (int)((kDestSeemOverlap/destResolution.height)*sourceResolution.height);
  134. CGImageRef sourceTileImageRef;
  135. // calculate the number of read/write operations required to assemble the
  136. // output image.
  137. int iterations = (int)( sourceResolution.height / sourceTile.size.height );
  138. // If tile height doesn't divide the image height evenly, add another iteration
  139. // to account for the remaining pixels.
  140. int remainder = (int)sourceResolution.height % (int)sourceTile.size.height;
  141. if(remainder) {
  142. iterations++;
  143. }
  144. // Add seem overlaps to the tiles, but save the original tile height for y coordinate calculations.
  145. float sourceTileHeightMinusOverlap = sourceTile.size.height;
  146. sourceTile.size.height += sourceSeemOverlap;
  147. destTile.size.height += kDestSeemOverlap;
  148. for( int y = 0; y < iterations; ++y ) {
  149. @autoreleasepool {
  150. sourceTile.origin.y = y * sourceTileHeightMinusOverlap + sourceSeemOverlap;
  151. destTile.origin.y = destResolution.height - (( y + 1 ) * sourceTileHeightMinusOverlap * imageScale + kDestSeemOverlap);
  152. sourceTileImageRef = CGImageCreateWithImageInRect( sourceImageRef, sourceTile );
  153. if( y == iterations - 1 && remainder ) {
  154. float dify = destTile.size.height;
  155. destTile.size.height = CGImageGetHeight( sourceTileImageRef ) * imageScale;
  156. dify -= destTile.size.height;
  157. destTile.origin.y += dify;
  158. }
  159. CGContextDrawImage( destContext, destTile, sourceTileImageRef );
  160. CGImageRelease( sourceTileImageRef );
  161. }
  162. }
  163. CGImageRef destImageRef = CGBitmapContextCreateImage(destContext);
  164. CGContextRelease(destContext);
  165. if (destImageRef == NULL) {
  166. return image;
  167. }
  168. UIImage *destImage = [UIImage imageWithCGImage:destImageRef scale:image.scale orientation:image.imageOrientation];
  169. CGImageRelease(destImageRef);
  170. if (destImage == nil) {
  171. return image;
  172. }
  173. return destImage;
  174. }
  175. }
  176. + (BOOL)shouldDecodeImage:(nullable UIImage *)image {
  177. // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error
  178. if (image == nil) {
  179. return NO;
  180. }
  181. // do not decode animated images
  182. if (image.images != nil) {
  183. return NO;
  184. }
  185. CGImageRef imageRef = image.CGImage;
  186. CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
  187. BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
  188. alpha == kCGImageAlphaLast ||
  189. alpha == kCGImageAlphaPremultipliedFirst ||
  190. alpha == kCGImageAlphaPremultipliedLast);
  191. // do not decode images with alpha
  192. if (anyAlpha) {
  193. return NO;
  194. }
  195. return YES;
  196. }
  197. + (BOOL)shouldScaleDownImage:(nonnull UIImage *)image {
  198. BOOL shouldScaleDown = YES;
  199. CGImageRef sourceImageRef = image.CGImage;
  200. CGSize sourceResolution = CGSizeZero;
  201. sourceResolution.width = CGImageGetWidth(sourceImageRef);
  202. sourceResolution.height = CGImageGetHeight(sourceImageRef);
  203. float sourceTotalPixels = sourceResolution.width * sourceResolution.height;
  204. float imageScale = kDestTotalPixels / sourceTotalPixels;
  205. if (imageScale < 1) {
  206. shouldScaleDown = YES;
  207. } else {
  208. shouldScaleDown = NO;
  209. }
  210. return shouldScaleDown;
  211. }
  212. + (CGColorSpaceRef)colorSpaceForImageRef:(CGImageRef)imageRef {
  213. // current
  214. CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
  215. CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
  216. BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
  217. imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
  218. imageColorSpaceModel == kCGColorSpaceModelCMYK ||
  219. imageColorSpaceModel == kCGColorSpaceModelIndexed);
  220. if (unsupportedColorSpace) {
  221. colorspaceRef = CGColorSpaceCreateDeviceRGB();
  222. CFAutorelease(colorspaceRef);
  223. }
  224. return colorspaceRef;
  225. }
  226. #elif SD_MAC
  227. + (nullable UIImage *)decodedImageWithImage:(nullable UIImage *)image {
  228. return image;
  229. }
  230. + (nullable UIImage *)decodedAndScaledDownImageWithImage:(nullable UIImage *)image {
  231. return image;
  232. }
  233. #endif
  234. @end