PageRenderTime 26ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/GraphicsUtilities.m

http://networkpx.googlecode.com/
Objective C | 340 lines | 242 code | 49 blank | 49 comment | 26 complexity | 558bd734e2d476549b37e6571314dac8 MD5 | raw file
Possible License(s): GPL-3.0
  1. /*
  2. GraphicsUtilities.m ... Convenient functions for manipulating bitmaps
  3. Copyright (c) 2009, KennyTM~
  4. All rights reserved.
  5. Redistribution and use in source and binary forms, with or without modification,
  6. are permitted provided that the following conditions are met:
  7. * Redistributions of source code must retain the above copyright notice, this
  8. list of conditions and the following disclaimer.
  9. * Redistributions in binary form must reproduce the above copyright notice,
  10. this list of conditions and the following disclaimer in the documentation
  11. and/or other materials provided with the distribution.
  12. * Neither the name of the KennyTM~ nor the names of its contributors may be
  13. used to endorse or promote products derived from this software without
  14. specific prior written permission.
  15. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  16. ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  17. WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  18. DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  19. ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  20. (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  21. LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  22. ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  23. (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  24. SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  25. */
  26. #include <math.h>
  27. #include <GraphicsUtilities.h>
  28. #import <UIKit/UIImage.h>
  29. CGImageRef GUImageCreateByConcatSubimages (CGImageRef img,
  30. CGRect subrect1,
  31. CGRect subrect2,
  32. bool tileVertically) {
  33. CGRect imgRect = CGRectZero;
  34. if (tileVertically)
  35. imgRect.size = CGSizeMake(subrect1.size.width, subrect1.size.height + subrect2.size.height);
  36. else
  37. imgRect.size = CGSizeMake(subrect1.size.width + subrect2.size.width, subrect1.size.height);
  38. CGImageRef subimg1 = CGImageCreateWithImageInRect(img, subrect1);
  39. CGImageRef subimg2 = CGImageCreateWithImageInRect(img, subrect2);
  40. GUCreateContext(c, imgRect.size.width, imgRect.size.height);
  41. if (tileVertically) {
  42. CGContextDrawImage(c, CGRectMake(0, 0, subrect2.size.width, subrect2.size.height), subimg2);
  43. CGContextDrawImage(c, CGRectMake(0, subrect2.size.height, subrect1.size.width, subrect1.size.height), subimg1);
  44. } else {
  45. CGContextDrawImage(c, CGRectMake(0, 0, subrect1.size.width, subrect1.size.height), subimg1);
  46. CGContextDrawImage(c, CGRectMake(subrect1.size.width, 0, subrect2.size.width, subrect2.size.height), subimg2);
  47. }
  48. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  49. CGImageRelease(subimg1);
  50. CGImageRelease(subimg2);
  51. CGContextRelease(c);
  52. return finalImg;
  53. }
  54. CGImageRef GUImageCreateByComposition (CGImageRef foreground,
  55. CGImageRef background,
  56. CGRect fgRect,
  57. CGRect bgSubRect) {
  58. GUCreateContext(c, bgSubRect.size.width, bgSubRect.size.height);
  59. CGImageRef subimage = NULL;
  60. if (background != NULL) {
  61. if (bgSubRect.size.width == CGImageGetWidth(background) && bgSubRect.size.height == CGImageGetHeight(background))
  62. subimage = CGImageRetain(background);
  63. else
  64. subimage = CGImageCreateWithImageInRect(background, bgSubRect);
  65. CGContextDrawImage(c, CGRectMake(0, 0, bgSubRect.size.width, bgSubRect.size.height), subimage);
  66. }
  67. CGContextDrawImage(c, fgRect, foreground);
  68. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  69. CGImageRelease(subimage);
  70. CGContextRelease(c);
  71. return finalImg;
  72. }
  73. CGImageRef GUImageCreateWithPatching(CGImageRef img, CGRect src, CGRect target) {
  74. CGRect imgRect = CGRectMake(0, 0, CGImageGetWidth(img), CGImageGetHeight(img));
  75. GUCreateContext(c, imgRect.size.width, imgRect.size.height);
  76. CGContextDrawImage(c, imgRect, img);
  77. CGImageRef subImg = CGImageCreateWithImageInRect(img, src);
  78. CGContextSetBlendMode(c, kCGBlendModeCopy);
  79. CGContextDrawImage(c, target, subImg);
  80. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  81. CGImageRelease(subImg);
  82. CGContextRelease(c);
  83. return finalImg;
  84. }
  85. CGImageRef GUImageCreateWithMask(CGImageRef src, CGImageRef mask) {
  86. GUCreateContextWithImageAuto(src);
  87. CGContextClipToMask(c, imgRect, mask);
  88. CGContextDrawImage(c, imgRect, src);
  89. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  90. CGContextRelease(c);
  91. return finalImg;
  92. }
  93. CGImageRef GUImageCreateWithCappedMask(CGImageRef src, CGImageRef mask, GUCaps caps) {
  94. GUCreateContextWithImageAuto(src);
  95. GUDrawImageWithCaps(c, imgRect, mask, caps);
  96. CGContextSetBlendMode(c, kCGBlendModeSourceIn);
  97. CGContextDrawImage(c, imgRect, src);
  98. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  99. CGContextRelease(c);
  100. return finalImg;
  101. }
  102. CGImageRef GUImageCreateWithCaps(CGImageRef img, CGRect rect, GUCaps caps) {
  103. GUCreateContext(c, rect.size.width, rect.size.height);
  104. GUDrawImageWithCaps(c, rect, img, caps);
  105. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  106. CGContextRelease(c);
  107. return finalImg;
  108. }
  109. void GUDrawImageWithCaps(CGContextRef c, CGRect rect, CGImageRef img, GUCaps caps) {
  110. // assert: img.width > caps.left+caps.right && img.height > caps.top+caps.bottom.
  111. size_t imgWidth = CGImageGetWidth(img), imgHeight = CGImageGetHeight(img);
  112. bool matchX = (rect.size.width == imgWidth), matchY = (rect.size.height == imgHeight);
  113. if (matchX && matchY) {
  114. // the caps are bigger than the image: just draw the image.
  115. CGContextDrawImage(c, rect, img);
  116. } else if (matchX) {
  117. // create a 3-part image vertically.
  118. CGImageRef top = CGImageCreateWithImageInRect(img,
  119. CGRectMake(0, 0,
  120. imgWidth, caps.top));
  121. CGImageRef middle = CGImageCreateWithImageInRect(img,
  122. CGRectMake(0, caps.top,
  123. imgWidth, imgHeight-caps.top-caps.bottom));
  124. CGImageRef bottom = CGImageCreateWithImageInRect(img,
  125. CGRectMake(0, imgHeight-caps.bottom,
  126. imgWidth, caps.bottom));
  127. CGContextDrawImage(c, CGRectMake(rect.origin.x, rect.origin.y+rect.size.height-caps.top,
  128. rect.size.width, caps.top), top);
  129. CGContextDrawImage(c, CGRectMake(rect.origin.x, rect.origin.y+caps.bottom,
  130. rect.size.width, rect.size.height-caps.top-caps.bottom), middle);
  131. CGContextDrawImage(c, CGRectMake(rect.origin.x, rect.origin.y,
  132. rect.size.width, caps.bottom), bottom);
  133. CGImageRelease(top);
  134. CGImageRelease(middle);
  135. CGImageRelease(bottom);
  136. } else if (matchY) {
  137. // create a 3-part image horizontally.
  138. CGImageRef left = CGImageCreateWithImageInRect(img, CGRectMake(0, 0,
  139. caps.left, imgHeight));
  140. CGImageRef middle = CGImageCreateWithImageInRect(img, CGRectMake(caps.left, 0,
  141. imgWidth-caps.left-caps.right, imgHeight));
  142. CGImageRef right = CGImageCreateWithImageInRect(img, CGRectMake(imgWidth-caps.right, 0,
  143. caps.right, imgHeight));
  144. CGContextDrawImage(c, CGRectMake(rect.origin.x, rect.origin.y,
  145. caps.left, rect.size.height), left);
  146. CGContextDrawImage(c, CGRectMake(rect.origin.x+caps.left, rect.origin.y,
  147. rect.size.width-caps.left-caps.right, rect.size.height), middle);
  148. CGContextDrawImage(c, CGRectMake(rect.origin.x+rect.size.width-caps.right, rect.origin.y,
  149. caps.right, rect.size.height), right);
  150. CGImageRelease(left);
  151. CGImageRelease(middle);
  152. CGImageRelease(right);
  153. } else {
  154. // create a 9-part image.
  155. /*
  156. +---+---+---+ ^
  157. | Q | W | E | | w3
  158. +---+---+---+ x
  159. | A | S | D | | w2
  160. +---+---+---+ x
  161. | Z | X | C | | w1
  162. +---+---+---+ v
  163. <-> <-> <->
  164. w1 w2 w3
  165. */
  166. CGFloat w1 = caps.left, w2 = imgWidth-caps.left-caps.right, w3 = caps.right;
  167. CGFloat h1 = caps.top, h2 = imgHeight-caps.top-caps.bottom, h3 = caps.bottom;
  168. CGFloat x1 = 0, x2 = w1, x3 = w1+w2;
  169. CGFloat y1 = 0, y2 = h1, y3 = h1+h2;
  170. CGImageRef Q = CGImageCreateWithImageInRect(img, CGRectMake(x1, y1, w1, h1));
  171. CGImageRef W = CGImageCreateWithImageInRect(img, CGRectMake(x2, y1, w2, h1));
  172. CGImageRef E = CGImageCreateWithImageInRect(img, CGRectMake(x3, y1, w3, h1));
  173. CGImageRef A = CGImageCreateWithImageInRect(img, CGRectMake(x1, y2, w1, h2));
  174. CGImageRef S = CGImageCreateWithImageInRect(img, CGRectMake(x2, y2, w2, h2));
  175. CGImageRef D = CGImageCreateWithImageInRect(img, CGRectMake(x3, y2, w3, h2));
  176. CGImageRef Z = CGImageCreateWithImageInRect(img, CGRectMake(x1, y3, w1, h3));
  177. CGImageRef X = CGImageCreateWithImageInRect(img, CGRectMake(x2, y3, w2, h3));
  178. CGImageRef C = CGImageCreateWithImageInRect(img, CGRectMake(x3, y3, w3, h3));
  179. w2 = rect.size.width-w1-w3;
  180. h2 = rect.size.height-h1-h3;
  181. x1 = rect.origin.x;
  182. x2 = rect.origin.x+w1;
  183. x3 = x2+w2;
  184. y3 = rect.origin.y;
  185. y2 = rect.origin.y+h3;
  186. y1 = y2+h2;
  187. CGContextDrawImage(c, CGRectMake(x1, y1, w1, h1), Q);
  188. CGContextDrawImage(c, CGRectMake(x2, y1, w2, h1), W);
  189. CGContextDrawImage(c, CGRectMake(x3, y1, w3, h1), E);
  190. CGContextDrawImage(c, CGRectMake(x1, y2, w1, h2), A);
  191. CGContextDrawImage(c, CGRectMake(x2, y2, w2, h2), S);
  192. CGContextDrawImage(c, CGRectMake(x3, y2, w3, h2), D);
  193. CGContextDrawImage(c, CGRectMake(x1, y3, w1, h3), Z);
  194. CGContextDrawImage(c, CGRectMake(x2, y3, w2, h3), X);
  195. CGContextDrawImage(c, CGRectMake(x3, y3, w3, h3), C);
  196. CGImageRelease(Q);
  197. CGImageRelease(W);
  198. CGImageRelease(E);
  199. CGImageRelease(A);
  200. CGImageRelease(S);
  201. CGImageRelease(D);
  202. CGImageRelease(Z);
  203. CGImageRelease(X);
  204. CGImageRelease(C);
  205. }
  206. }
  207. float GUAverageLuminance (CGImageRef img) {
  208. float retval = NAN;
  209. if (img == NULL)
  210. return retval;
  211. size_t height = CGImageGetHeight(img);
  212. size_t width = CGImageGetWidth(img);
  213. size_t area = width*height;
  214. struct{unsigned char r,g,b,a;}* data = calloc(area, 4);
  215. CGColorSpaceRef rgbSpace = CGColorSpaceCreateDeviceRGB();
  216. CGContextRef c = CGBitmapContextCreate(data, width, height, 8, 4*width, rgbSpace, kCGImageAlphaPremultipliedLast|kCGBitmapByteOrder32Big);
  217. CGColorSpaceRelease(rgbSpace);
  218. CGContextDrawImage(c, CGRectMake(0, 0, width, height), img);
  219. UInt32 A = 0, R = 0, G = 0, B = 0;
  220. for (int i = 0; i < area; ++ i) {
  221. R += data[i].r;
  222. G += data[i].g;
  223. B += data[i].b;
  224. A += data[i].a;
  225. }
  226. if (A != 0) {
  227. retval = GULuminance(R,G,B)/A;
  228. }
  229. CGContextRelease(c);
  230. free(data);
  231. return retval;
  232. }
  233. CGImageRef GUImageCreateWithPNG(const char* filename) {
  234. CGDataProviderRef data = CGDataProviderCreateWithFilename(filename);
  235. if (data == NULL)
  236. return NULL;
  237. else {
  238. CGImageRef retimg = CGImageCreateWithPNGDataProvider(data, NULL, false, kCGRenderingIntentDefault);
  239. CGDataProviderRelease(data);
  240. return retimg;
  241. }
  242. }
  243. UIImage* GUCreateUIImageAndRelease(CGImageRef img) {
  244. UIImage* retval = [UIImage imageWithCGImage:img];
  245. CGImageRelease(img);
  246. return retval;
  247. }
  248. CGImageRef GUImageCreateByReducingBrightness(CGImageRef img, CGFloat reductionRatio) {
  249. GUCreateContextWithImageAuto(img);
  250. CGContextDrawImage(c, imgRect, img);
  251. CGContextSetBlendMode(c, kCGBlendModeDestinationIn);
  252. CGContextSetAlpha(c, reductionRatio);
  253. CGContextFillRect(c, imgRect);
  254. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  255. CGContextRelease(c);
  256. return finalImg;
  257. }
  258. CGPathRef GUPathCreateRoundRect(CGRect rect, CGFloat radius) {
  259. CGFloat right = rect.origin.x + rect.size.width, top = rect.origin.y + rect.size.height;
  260. CGPoint centers[4];
  261. centers[0] = CGPointMake(rect.origin.x + radius, top - radius);
  262. centers[1] = CGPointMake(right - radius, top - radius);
  263. centers[2] = CGPointMake(right - radius, rect.origin.y + radius);
  264. centers[3] = CGPointMake(rect.origin.x + radius, rect.origin.y + radius);
  265. CGMutablePathRef path = CGPathCreateMutable();
  266. CGPathAddArc(path, NULL, centers[0].x, centers[0].y, radius, M_PI, M_PI/2, true);
  267. CGPathAddArc(path, NULL, centers[1].x, centers[1].y, radius, M_PI/2, 0, true);
  268. CGPathAddArc(path, NULL, centers[2].x, centers[2].y, radius, 0, -M_PI/2, true);
  269. CGPathAddArc(path, NULL, centers[3].x, centers[3].y, radius, -M_PI/2, -M_PI, true);
  270. CGPathCloseSubpath(path);
  271. return path;
  272. }
  273. CGImageRef GUImageCreateByClippingToRoundRect(CGImageRef img, CGRect rect, CGFloat radius) {
  274. GUCreateContextWithImageAuto(img);
  275. CGPathRef roundRect = GUPathCreateRoundRect(rect, radius);
  276. CGContextAddPath(c, roundRect);
  277. CGContextClip(c);
  278. CGContextDrawImage(c, imgRect, img);
  279. CGImageRef finalImg = CGBitmapContextCreateImage(c);
  280. CGContextRelease(c);
  281. CGPathRelease(roundRect);
  282. return finalImg;
  283. }