author | nemo |
Mon, 27 Dec 2010 12:51:08 -0500 | |
branch | 0.9.15 |
changeset 4723 | 29ab0d49c3e6 |
parent 4478 | 05029b4d8490 |
child 4976 | 088d40d8aba2 |
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 { |
|
3778 | 57 |
if (secondImage == nil) { |
58 |
DLog(@"Warning, secondImage == nil"); |
|
59 |
return self; |
|
60 |
} |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
61 |
CGFloat screenScale = getScreenScale(); |
4478 | 62 |
int w = self.size.width * screenScale; |
63 |
int h = self.size.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
|
64 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
65 |
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
|
66 |
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
|
67 |
return self; |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
68 |
} |
3778 | 69 |
|
3573 | 70 |
// 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
|
71 |
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
|
72 |
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
|
73 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
74 |
// 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
|
75 |
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
|
76 |
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
|
77 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
78 |
// 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
|
79 |
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
|
80 |
|
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
81 |
// Create a new UIImage object |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
82 |
UIImage *resultImage; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
83 |
if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
84 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
85 |
else |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
86 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 87 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
88 |
// 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
|
89 |
CGColorSpaceRelease(colorSpace); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
90 |
CGContextRelease(context); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
91 |
CFRelease(imageRef); |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
92 |
|
3547 | 93 |
return resultImage; |
94 |
} |
|
95 |
||
96 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect { |
|
97 |
// load image from path |
|
98 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path]; |
|
3697 | 99 |
|
3547 | 100 |
if (nil != image) { |
101 |
// get its CGImage representation with a give size |
|
3621 | 102 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect); |
3697 | 103 |
|
3547 | 104 |
// clean memory |
105 |
[image release]; |
|
3697 | 106 |
|
3547 | 107 |
// create a UIImage from the CGImage (memory must be allocated already) |
3621 | 108 |
UIImage *sprite = [self initWithCGImage:cgImage]; |
3697 | 109 |
|
3547 | 110 |
// clean memory |
3621 | 111 |
CGImageRelease(cgImage); |
3547 | 112 |
|
113 |
// return resulting image |
|
114 |
return sprite; |
|
115 |
} else { |
|
116 |
DLog(@"error - image == nil"); |
|
117 |
return nil; |
|
118 |
} |
|
119 |
} |
|
120 |
||
3621 | 121 |
-(UIImage *)cutAt:(CGRect) rect { |
122 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect); |
|
3697 | 123 |
|
3621 | 124 |
UIImage *res = [UIImage imageWithCGImage:cgImage]; |
125 |
CGImageRelease(cgImage); |
|
3697 | 126 |
|
3621 | 127 |
return res; |
128 |
} |
|
129 |
||
3547 | 130 |
-(UIImage *)convertToGrayScale { |
3573 | 131 |
// Create image rectangle with current image width/height |
132 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
|
3697 | 133 |
|
3573 | 134 |
// Grayscale color space |
135 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
|
3697 | 136 |
|
3573 | 137 |
// Create bitmap content with current image size and grayscale colorspace |
138 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
|
3697 | 139 |
|
3573 | 140 |
// Draw image into current context, with specified rectangle |
141 |
// using previously defined context (with grayscale colorspace) |
|
142 |
CGContextDrawImage(context, imageRect, [self CGImage]); |
|
3697 | 143 |
|
3573 | 144 |
// Create bitmap image info from pixel data in current context |
145 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 146 |
|
147 |
// Create a new UIImage object |
|
3573 | 148 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 149 |
|
3573 | 150 |
// Release colorspace, context and bitmap information |
3978 | 151 |
CFRelease(imageRef); |
3573 | 152 |
CGContextRelease(context); |
3978 | 153 |
CGColorSpaceRelease(colorSpace); |
3697 | 154 |
|
3573 | 155 |
// Return the new grayscale image |
156 |
return newImage; |
|
3547 | 157 |
} |
158 |
||
159 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
160 |
-(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
3573 | 161 |
// prepare the reference image |
162 |
CGImageRef maskRef = [maskImage CGImage]; |
|
3697 | 163 |
|
3573 | 164 |
// create the mask using parameters of the mask reference |
3547 | 165 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
166 |
CGImageGetHeight(maskRef), |
|
167 |
CGImageGetBitsPerComponent(maskRef), |
|
168 |
CGImageGetBitsPerPixel(maskRef), |
|
169 |
CGImageGetBytesPerRow(maskRef), |
|
170 |
CGImageGetDataProvider(maskRef), NULL, false); |
|
3697 | 171 |
|
3573 | 172 |
// create an image in the current context |
3547 | 173 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
174 |
CGImageRelease(mask); |
|
3697 | 175 |
|
3547 | 176 |
UIImage* retImage = [UIImage imageWithCGImage:masked]; |
177 |
CGImageRelease(masked); |
|
3697 | 178 |
|
3547 | 179 |
return retImage; |
180 |
} |
|
181 |
||
182 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
3573 | 183 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
184 |
CGFloat fw, fh; |
|
3547 | 185 |
if (ovalWidth == 0 || ovalHeight == 0) { |
186 |
CGContextAddRect(context, rect); |
|
187 |
return; |
|
188 |
} |
|
189 |
CGContextSaveGState(context); |
|
190 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
191 |
CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
192 |
fw = CGRectGetWidth (rect) / ovalWidth; |
|
193 |
fh = CGRectGetHeight (rect) / ovalHeight; |
|
194 |
CGContextMoveToPoint(context, fw, fh/2); |
|
195 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
196 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
197 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
198 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
199 |
CGContextClosePath(context); |
|
200 |
CGContextRestoreGState(context); |
|
201 |
} |
|
202 |
||
3697 | 203 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh { |
3573 | 204 |
CGFloat cornerWidth = sizewh.width; |
205 |
CGFloat cornerHeight = sizewh.height; |
|
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
206 |
CGFloat screenScale = getScreenScale(); |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
207 |
CGFloat w = self.size.width * screenScale; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
208 |
CGFloat h = self.size.height * screenScale; |
3697 | 209 |
|
3547 | 210 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
211 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 212 |
|
3547 | 213 |
CGContextBeginPath(context); |
214 |
CGRect rect = CGRectMake(0, 0, w, h); |
|
215 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
216 |
CGContextClosePath(context); |
|
217 |
CGContextClip(context); |
|
3697 | 218 |
|
3573 | 219 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
3697 | 220 |
|
3547 | 221 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
222 |
CGContextRelease(context); |
|
223 |
CGColorSpaceRelease(colorSpace); |
|
3697 | 224 |
|
4461
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
225 |
UIImage *newImage; |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
226 |
if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
227 |
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
|
228 |
else |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
229 |
newImage = [UIImage imageWithCGImage:imageMasked]; |
2f4f5d649bcd
add a simple check to prevent loading sounds when device is muted
koda
parents:
4446
diff
changeset
|
230 |
|
3547 | 231 |
CGImageRelease(imageMasked); |
3697 | 232 |
|
3547 | 233 |
return newImage; |
234 |
} |
|
235 |
||
3903 | 236 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/ |
237 |
-(UIImage *)convertToNegative { |
|
238 |
UIGraphicsBeginImageContext(self.size); |
|
239 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); |
|
240 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; |
|
241 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); |
|
242 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); |
|
243 |
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
|
244 |
// create an image from the current contex (not thread safe) |
3903 | 245 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); |
246 |
UIGraphicsEndImageContext(); |
|
247 |
return result; |
|
248 |
} |
|
249 |
||
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
250 |
+(UIImage *)whiteImage:(CGSize) ofSize { |
4356 | 251 |
CGFloat w = ofSize.width; |
252 |
CGFloat h = ofSize.height; |
|
4478 | 253 |
DLog(@"w: %f, h: %f", w, h); |
3910
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
254 |
|
4356 | 255 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
256 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
257 |
||
258 |
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
|
259 |
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
|
260 |
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
|
261 |
|
4356 | 262 |
CGImageRef image = CGBitmapContextCreateImage(context); |
263 |
CGContextRelease(context); |
|
264 |
CGColorSpaceRelease(colorSpace); |
|
265 |
||
266 |
UIImage *bkgImg = [UIImage imageWithCGImage:image]; |
|
267 |
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
|
268 |
return bkgImg; |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
269 |
} |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
270 |
|
3547 | 271 |
@end |