SDWebImageDecoder.m 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  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. // Allocate enough pixel data to hold the output image.
  96. void* destBitmapData = malloc( bytesPerRow * destResolution.height );
  97. if (destBitmapData == NULL) {
  98. return image;
  99. }
  100. // kCGImageAlphaNone is not supported in CGBitmapContextCreate.
  101. // Since the original image here has no alpha info, use kCGImageAlphaNoneSkipLast
  102. // to create bitmap graphics contexts without alpha info.
  103. destContext = CGBitmapContextCreate(destBitmapData,
  104. destResolution.width,
  105. destResolution.height,
  106. kBitsPerComponent,
  107. bytesPerRow,
  108. colorspaceRef,
  109. kCGBitmapByteOrderDefault|kCGImageAlphaNoneSkipLast);
  110. if (destContext == NULL) {
  111. free(destBitmapData);
  112. return image;
  113. }
  114. CGContextSetInterpolationQuality(destContext, kCGInterpolationHigh);
  115. // Now define the size of the rectangle to be used for the
  116. // incremental blits from the input image to the output image.
  117. // we use a source tile width equal to the width of the source
  118. // image due to the way that iOS retrieves image data from disk.
  119. // iOS must decode an image from disk in full width 'bands', even
  120. // if current graphics context is clipped to a subrect within that
  121. // band. Therefore we fully utilize all of the pixel data that results
  122. // from a decoding opertion by achnoring our tile size to the full
  123. // width of the input image.
  124. CGRect sourceTile = CGRectZero;
  125. sourceTile.size.width = sourceResolution.width;
  126. // The source tile height is dynamic. Since we specified the size
  127. // of the source tile in MB, see how many rows of pixels high it
  128. // can be given the input image width.
  129. sourceTile.size.height = (int)(kTileTotalPixels / sourceTile.size.width );
  130. sourceTile.origin.x = 0.0f;
  131. // The output tile is the same proportions as the input tile, but
  132. // scaled to image scale.
  133. CGRect destTile;
  134. destTile.size.width = destResolution.width;
  135. destTile.size.height = sourceTile.size.height * imageScale;
  136. destTile.origin.x = 0.0f;
  137. // The source seem overlap is proportionate to the destination seem overlap.
  138. // this is the amount of pixels to overlap each tile as we assemble the ouput image.
  139. float sourceSeemOverlap = (int)((kDestSeemOverlap/destResolution.height)*sourceResolution.height);
  140. CGImageRef sourceTileImageRef;
  141. // calculate the number of read/write operations required to assemble the
  142. // output image.
  143. int iterations = (int)( sourceResolution.height / sourceTile.size.height );
  144. // If tile height doesn't divide the image height evenly, add another iteration
  145. // to account for the remaining pixels.
  146. int remainder = (int)sourceResolution.height % (int)sourceTile.size.height;
  147. if(remainder) {
  148. iterations++;
  149. }
  150. // Add seem overlaps to the tiles, but save the original tile height for y coordinate calculations.
  151. float sourceTileHeightMinusOverlap = sourceTile.size.height;
  152. sourceTile.size.height += sourceSeemOverlap;
  153. destTile.size.height += kDestSeemOverlap;
  154. for( int y = 0; y < iterations; ++y ) {
  155. @autoreleasepool {
  156. sourceTile.origin.y = y * sourceTileHeightMinusOverlap + sourceSeemOverlap;
  157. destTile.origin.y = destResolution.height - (( y + 1 ) * sourceTileHeightMinusOverlap * imageScale + kDestSeemOverlap);
  158. sourceTileImageRef = CGImageCreateWithImageInRect( sourceImageRef, sourceTile );
  159. if( y == iterations - 1 && remainder ) {
  160. float dify = destTile.size.height;
  161. destTile.size.height = CGImageGetHeight( sourceTileImageRef ) * imageScale;
  162. dify -= destTile.size.height;
  163. destTile.origin.y += dify;
  164. }
  165. CGContextDrawImage( destContext, destTile, sourceTileImageRef );
  166. CGImageRelease( sourceTileImageRef );
  167. }
  168. }
  169. CGImageRef destImageRef = CGBitmapContextCreateImage(destContext);
  170. CGContextRelease(destContext);
  171. if (destImageRef == NULL) {
  172. return image;
  173. }
  174. UIImage *destImage = [UIImage imageWithCGImage:destImageRef scale:image.scale orientation:image.imageOrientation];
  175. CGImageRelease(destImageRef);
  176. if (destImage == nil) {
  177. return image;
  178. }
  179. return destImage;
  180. }
  181. }
  182. + (BOOL)shouldDecodeImage:(nullable UIImage *)image {
  183. // Prevent "CGBitmapContextCreateImage: invalid context 0x0" error
  184. if (image == nil) {
  185. return NO;
  186. }
  187. // do not decode animated images
  188. if (image.images != nil) {
  189. return NO;
  190. }
  191. CGImageRef imageRef = image.CGImage;
  192. CGImageAlphaInfo alpha = CGImageGetAlphaInfo(imageRef);
  193. BOOL anyAlpha = (alpha == kCGImageAlphaFirst ||
  194. alpha == kCGImageAlphaLast ||
  195. alpha == kCGImageAlphaPremultipliedFirst ||
  196. alpha == kCGImageAlphaPremultipliedLast);
  197. // do not decode images with alpha
  198. if (anyAlpha) {
  199. return NO;
  200. }
  201. return YES;
  202. }
  203. + (BOOL)shouldScaleDownImage:(nonnull UIImage *)image {
  204. BOOL shouldScaleDown = YES;
  205. CGImageRef sourceImageRef = image.CGImage;
  206. CGSize sourceResolution = CGSizeZero;
  207. sourceResolution.width = CGImageGetWidth(sourceImageRef);
  208. sourceResolution.height = CGImageGetHeight(sourceImageRef);
  209. float sourceTotalPixels = sourceResolution.width * sourceResolution.height;
  210. float imageScale = kDestTotalPixels / sourceTotalPixels;
  211. if (imageScale < 1) {
  212. shouldScaleDown = YES;
  213. } else {
  214. shouldScaleDown = NO;
  215. }
  216. return shouldScaleDown;
  217. }
  218. + (CGColorSpaceRef)colorSpaceForImageRef:(CGImageRef)imageRef {
  219. // current
  220. CGColorSpaceModel imageColorSpaceModel = CGColorSpaceGetModel(CGImageGetColorSpace(imageRef));
  221. CGColorSpaceRef colorspaceRef = CGImageGetColorSpace(imageRef);
  222. BOOL unsupportedColorSpace = (imageColorSpaceModel == kCGColorSpaceModelUnknown ||
  223. imageColorSpaceModel == kCGColorSpaceModelMonochrome ||
  224. imageColorSpaceModel == kCGColorSpaceModelCMYK ||
  225. imageColorSpaceModel == kCGColorSpaceModelIndexed);
  226. if (unsupportedColorSpace) {
  227. colorspaceRef = CGColorSpaceCreateDeviceRGB();
  228. CFAutorelease(colorspaceRef);
  229. }
  230. return colorspaceRef;
  231. }
  232. #elif SD_MAC
  233. + (nullable UIImage *)decodedImageWithImage:(nullable UIImage *)image {
  234. return image;
  235. }
  236. + (nullable UIImage *)decodedAndScaledDownImageWithImage:(nullable UIImage *)image {
  237. return image;
  238. }
  239. #endif
  240. @end