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