author | koda |
Wed, 08 Dec 2010 14:47:52 +0100 | |
changeset 4476 | 4bf74e158f44 |
parent 4461 | 2f4f5d649bcd |
child 4478 | 05029b4d8490 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
3 |
* Copyright (c) 2009-2010 Vittorio Giovara <vittorio.giovara@gmail.com> |
|
4 |
* |
|
5 |
* This program is free software; you can redistribute it and/or modify |
|
6 |
* it under the terms of the GNU General Public License as published by |
|
7 |
* the Free Software Foundation; version 2 of the License |
|
8 |
* |
|
9 |
* This program is distributed in the hope that it will be useful, |
|
10 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|
11 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|
12 |
* GNU General Public License for more details. |
|
13 |
* |
|
14 |
* You should have received a copy of the GNU General Public License |
|
15 |
* along with this program; if not, write to the Free Software |
|
16 |
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
|
17 |
* |
|
18 |
* File created on 08/04/2010. |
|
19 |
*/ |
|
20 |
||
3547 | 21 |
|
22 |
#import "UIImageExtra.h" |
|
23 |
||
24 |
||
25 |
@implementation UIImage (extra) |
|
3697 | 26 |
|
4446 | 27 |
CGFloat getScreenScale(void) { |
28 |
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) |
|
29 |
return [UIScreen mainScreen].scale; |
|
30 |
else |
|
31 |
return 1.0f; |
|
32 |
} |
|
33 |
||
3547 | 34 |
-(UIImage *)scaleToSize:(CGSize) size { |
3573 | 35 |
DLog(@"warning - this is a very expensive operation, you should avoid using it"); |
3697 | 36 |
|
3573 | 37 |
// Create a bitmap graphics context; this will also set it as the current context |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
38 |
if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)]) |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
39 |
UIGraphicsBeginImageContextWithOptions(size, NO, getScreenScale()); |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
40 |
else |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
41 |
UIGraphicsBeginImageContext(size); |
3697 | 42 |
|
3573 | 43 |
// Draw the scaled image in the current context |
44 |
[self drawInRect:CGRectMake(0, 0, size.width, size.height)]; |
|
3697 | 45 |
|
3573 | 46 |
// Create a new image from current context |
47 |
UIImage* scaledImage = UIGraphicsGetImageFromCurrentImageContext(); |
|
3697 | 48 |
|
3573 | 49 |
// Pop the current context from the stack |
50 |
UIGraphicsEndImageContext(); |
|
3697 | 51 |
|
3573 | 52 |
// Return our new scaled image (autoreleased) |
53 |
return scaledImage; |
|
3547 | 54 |
} |
55 |
||
56 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint { |
|
57 |
// create a contex of size of the background image |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
58 |
return [self mergeWith:secondImage atPoint:secondImagePoint ofSize:self.size]; |
3547 | 59 |
} |
60 |
||
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
61 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint ofSize:(CGSize) resultingSize { |
3778 | 62 |
if (secondImage == nil) { |
63 |
DLog(@"Warning, secondImage == nil"); |
|
64 |
return self; |
|
65 |
} |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
66 |
CGFloat screenScale = getScreenScale(); |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
67 |
int w = resultingSize.width * screenScale; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
68 |
int h = resultingSize.height * screenScale; |
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
69 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
70 |
if (w == 0 || h == 0) { |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
71 |
DLog(@"Can have 0 dimesions"); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
72 |
return self; |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
73 |
} |
3778 | 74 |
|
3573 | 75 |
// Create a bitmap graphics context; this will also set it as the current context |
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
76 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
77 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
78 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
79 |
// draw the two images in the current context |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
80 |
CGContextDrawImage(context, CGRectMake(0, 0, self.size.width*screenScale, self.size.height*screenScale), [self CGImage]); |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
81 |
CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]); |
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
82 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
83 |
// Create bitmap image info from pixel data in current context |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
84 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
85 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
86 |
// Create a new UIImage object |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
87 |
UIImage *resultImage; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
88 |
if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
89 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
90 |
else |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
91 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 92 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
93 |
// Release colorspace, context and bitmap information |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
94 |
CGColorSpaceRelease(colorSpace); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
95 |
CGContextRelease(context); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
96 |
CFRelease(imageRef); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
97 |
|
3547 | 98 |
return resultImage; |
99 |
} |
|
100 |
||
101 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect { |
|
102 |
// load image from path |
|
103 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path]; |
|
3697 | 104 |
|
3547 | 105 |
if (nil != image) { |
106 |
// get its CGImage representation with a give size |
|
3621 | 107 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect); |
3697 | 108 |
|
3547 | 109 |
// clean memory |
110 |
[image release]; |
|
3697 | 111 |
|
3547 | 112 |
// create a UIImage from the CGImage (memory must be allocated already) |
3621 | 113 |
UIImage *sprite = [self initWithCGImage:cgImage]; |
3697 | 114 |
|
3547 | 115 |
// clean memory |
3621 | 116 |
CGImageRelease(cgImage); |
3547 | 117 |
|
118 |
// return resulting image |
|
119 |
return sprite; |
|
120 |
} else { |
|
121 |
DLog(@"error - image == nil"); |
|
122 |
return nil; |
|
123 |
} |
|
124 |
} |
|
125 |
||
3621 | 126 |
-(UIImage *)cutAt:(CGRect) rect { |
127 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect); |
|
3697 | 128 |
|
3621 | 129 |
UIImage *res = [UIImage imageWithCGImage:cgImage]; |
130 |
CGImageRelease(cgImage); |
|
3697 | 131 |
|
3621 | 132 |
return res; |
133 |
} |
|
134 |
||
3547 | 135 |
-(UIImage *)convertToGrayScale { |
3573 | 136 |
// Create image rectangle with current image width/height |
137 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
|
3697 | 138 |
|
3573 | 139 |
// Grayscale color space |
140 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
|
3697 | 141 |
|
3573 | 142 |
// Create bitmap content with current image size and grayscale colorspace |
143 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
|
3697 | 144 |
|
3573 | 145 |
// Draw image into current context, with specified rectangle |
146 |
// using previously defined context (with grayscale colorspace) |
|
147 |
CGContextDrawImage(context, imageRect, [self CGImage]); |
|
3697 | 148 |
|
3573 | 149 |
// Create bitmap image info from pixel data in current context |
150 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 151 |
|
152 |
// Create a new UIImage object |
|
3573 | 153 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 154 |
|
3573 | 155 |
// Release colorspace, context and bitmap information |
3978 | 156 |
CFRelease(imageRef); |
3573 | 157 |
CGContextRelease(context); |
3978 | 158 |
CGColorSpaceRelease(colorSpace); |
3697 | 159 |
|
3573 | 160 |
// Return the new grayscale image |
161 |
return newImage; |
|
3547 | 162 |
} |
163 |
||
164 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
165 |
-(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
3573 | 166 |
// prepare the reference image |
167 |
CGImageRef maskRef = [maskImage CGImage]; |
|
3697 | 168 |
|
3573 | 169 |
// create the mask using parameters of the mask reference |
3547 | 170 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
171 |
CGImageGetHeight(maskRef), |
|
172 |
CGImageGetBitsPerComponent(maskRef), |
|
173 |
CGImageGetBitsPerPixel(maskRef), |
|
174 |
CGImageGetBytesPerRow(maskRef), |
|
175 |
CGImageGetDataProvider(maskRef), NULL, false); |
|
3697 | 176 |
|
3573 | 177 |
// create an image in the current context |
3547 | 178 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
179 |
CGImageRelease(mask); |
|
3697 | 180 |
|
3547 | 181 |
UIImage* retImage = [UIImage imageWithCGImage:masked]; |
182 |
CGImageRelease(masked); |
|
3697 | 183 |
|
3547 | 184 |
return retImage; |
185 |
} |
|
186 |
||
187 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
3573 | 188 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
189 |
CGFloat fw, fh; |
|
3547 | 190 |
if (ovalWidth == 0 || ovalHeight == 0) { |
191 |
CGContextAddRect(context, rect); |
|
192 |
return; |
|
193 |
} |
|
194 |
CGContextSaveGState(context); |
|
195 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
196 |
CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
197 |
fw = CGRectGetWidth (rect) / ovalWidth; |
|
198 |
fh = CGRectGetHeight (rect) / ovalHeight; |
|
199 |
CGContextMoveToPoint(context, fw, fh/2); |
|
200 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
201 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
202 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
203 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
204 |
CGContextClosePath(context); |
|
205 |
CGContextRestoreGState(context); |
|
206 |
} |
|
207 |
||
3697 | 208 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
3573 | 209 |
CGFloat cornerWidth = sizewh.width; |
210 |
CGFloat cornerHeight = sizewh.height; |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
211 |
CGFloat screenScale = getScreenScale(); |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
212 |
CGFloat w = self.size.width * screenScale; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
213 |
CGFloat h = self.size.height * screenScale; |
3697 | 214 |
|
3547 | 215 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
216 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 217 |
|
3547 | 218 |
CGContextBeginPath(context); |
219 |
CGRect rect = CGRectMake(0, 0, w, h); |
|
220 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
221 |
CGContextClosePath(context); |
|
222 |
CGContextClip(context); |
|
3697 | 223 |
|
3573 | 224 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
3697 | 225 |
|
3547 | 226 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
227 |
CGContextRelease(context); |
|
228 |
CGColorSpaceRelease(colorSpace); |
|
3697 | 229 |
|
4461
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
230 |
UIImage *newImage; |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
231 |
if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
232 |
newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp]; |
4461
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
233 |
else |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
234 |
newImage = [UIImage imageWithCGImage:imageMasked]; |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
235 |
|
3547 | 236 |
CGImageRelease(imageMasked); |
3697 | 237 |
|
3547 | 238 |
return newImage; |
239 |
} |
|
240 |
||
3903 | 241 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/ |
242 |
-(UIImage *)convertToNegative { |
|
243 |
UIGraphicsBeginImageContext(self.size); |
|
244 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); |
|
245 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; |
|
246 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); |
|
247 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); |
|
248 |
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height)); |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
249 |
// create an image from the current contex (not thread safe) |
3903 | 250 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); |
251 |
UIGraphicsEndImageContext(); |
|
252 |
return result; |
|
253 |
} |
|
254 |
||
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
255 |
+(UIImage *)whiteImage:(CGSize) ofSize { |
4356 | 256 |
CGFloat w = ofSize.width; |
257 |
CGFloat h = ofSize.height; |
|
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
258 |
|
4356 | 259 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
260 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
261 |
||
262 |
CGContextBeginPath(context); |
|
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
263 |
CGContextSetRGBFillColor(context, 1.0, 1.0, 1.0, 1.0); |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
264 |
CGContextFillRect(context,CGRectMake(0,0,ofSize.width,ofSize.height)); |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
265 |
|
4356 | 266 |
CGImageRef image = CGBitmapContextCreateImage(context); |
267 |
CGContextRelease(context); |
|
268 |
CGColorSpaceRelease(colorSpace); |
|
269 |
||
270 |
UIImage *bkgImg = [UIImage imageWithCGImage:image]; |
|
271 |
CGImageRelease(image); |
|
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
272 |
return bkgImg; |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
273 |
} |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
274 |
|
3547 | 275 |
@end |