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