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
|
|
48 |
return [self mergeWith:secondImage atPoint:secondImagePoint atSize:self.size];
|
|
49 |
}
|
|
50 |
|
|
51 |
-(UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint) secondImagePoint atSize:(CGSize) resultingSize {
|
3778
|
52 |
if (secondImage == nil) {
|
|
53 |
DLog(@"Warning, secondImage == nil");
|
|
54 |
return self;
|
|
55 |
}
|
|
56 |
|
3573
|
57 |
// Create a bitmap graphics context; this will also set it as the current context
|
3547
|
58 |
UIGraphicsBeginImageContext(resultingSize);
|
3697
|
59 |
|
3573
|
60 |
// draw the background image in the current context
|
3547
|
61 |
[self drawAtPoint:CGPointMake(0,0)];
|
3697
|
62 |
|
3573
|
63 |
// draw the image on top of the first image (because the context is the same)
|
3547
|
64 |
[secondImage drawAtPoint:secondImagePoint];
|
3697
|
65 |
|
3547
|
66 |
// create an image from the current contex (not thread safe)
|
|
67 |
UIImage *resultImage = UIGraphicsGetImageFromCurrentImageContext();
|
3697
|
68 |
|
3547
|
69 |
// free drawing contex
|
|
70 |
UIGraphicsEndImageContext();
|
3697
|
71 |
|
3547
|
72 |
// return the resulting autoreleased image
|
|
73 |
return resultImage;
|
|
74 |
}
|
|
75 |
|
|
76 |
-(id) initWithContentsOfFile:(NSString *)path andCutAt:(CGRect) rect {
|
|
77 |
// load image from path
|
|
78 |
UIImage *image = [[UIImage alloc] initWithContentsOfFile: path];
|
3697
|
79 |
|
3547
|
80 |
if (nil != image) {
|
|
81 |
// get its CGImage representation with a give size
|
3621
|
82 |
CGImageRef cgImage = CGImageCreateWithImageInRect([image CGImage], rect);
|
3697
|
83 |
|
3547
|
84 |
// clean memory
|
|
85 |
[image release];
|
3697
|
86 |
|
3547
|
87 |
// create a UIImage from the CGImage (memory must be allocated already)
|
3621
|
88 |
UIImage *sprite = [self initWithCGImage:cgImage];
|
3697
|
89 |
|
3547
|
90 |
// clean memory
|
3621
|
91 |
CGImageRelease(cgImage);
|
3547
|
92 |
|
|
93 |
// return resulting image
|
|
94 |
return sprite;
|
|
95 |
} else {
|
|
96 |
DLog(@"error - image == nil");
|
|
97 |
return nil;
|
|
98 |
}
|
|
99 |
}
|
|
100 |
|
3621
|
101 |
-(UIImage *)cutAt:(CGRect) rect {
|
|
102 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect);
|
3697
|
103 |
|
3621
|
104 |
UIImage *res = [UIImage imageWithCGImage:cgImage];
|
|
105 |
CGImageRelease(cgImage);
|
3697
|
106 |
|
3621
|
107 |
return res;
|
|
108 |
}
|
|
109 |
|
3547
|
110 |
-(UIImage *)convertToGrayScale {
|
3573
|
111 |
// Create image rectangle with current image width/height
|
|
112 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height);
|
3697
|
113 |
|
3573
|
114 |
// Grayscale color space
|
|
115 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray();
|
3697
|
116 |
|
3573
|
117 |
// Create bitmap content with current image size and grayscale colorspace
|
|
118 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone);
|
3697
|
119 |
|
3573
|
120 |
// Draw image into current context, with specified rectangle
|
|
121 |
// using previously defined context (with grayscale colorspace)
|
|
122 |
CGContextDrawImage(context, imageRect, [self CGImage]);
|
3697
|
123 |
|
3573
|
124 |
// Create bitmap image info from pixel data in current context
|
|
125 |
CGImageRef imageRef = CGBitmapContextCreateImage(context);
|
3697
|
126 |
|
|
127 |
// Create a new UIImage object
|
3573
|
128 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef];
|
3697
|
129 |
|
3573
|
130 |
// Release colorspace, context and bitmap information
|
|
131 |
CGColorSpaceRelease(colorSpace);
|
|
132 |
CGContextRelease(context);
|
|
133 |
CFRelease(imageRef);
|
3697
|
134 |
|
3573
|
135 |
// Return the new grayscale image
|
|
136 |
return newImage;
|
3547
|
137 |
}
|
|
138 |
|
|
139 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda
|
|
140 |
-(UIImage*) maskImageWith:(UIImage *)maskImage {
|
3573
|
141 |
// prepare the reference image
|
|
142 |
CGImageRef maskRef = [maskImage CGImage];
|
3697
|
143 |
|
3573
|
144 |
// create the mask using parameters of the mask reference
|
3547
|
145 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef),
|
|
146 |
CGImageGetHeight(maskRef),
|
|
147 |
CGImageGetBitsPerComponent(maskRef),
|
|
148 |
CGImageGetBitsPerPixel(maskRef),
|
|
149 |
CGImageGetBytesPerRow(maskRef),
|
|
150 |
CGImageGetDataProvider(maskRef), NULL, false);
|
3697
|
151 |
|
3573
|
152 |
// create an image in the current context
|
3547
|
153 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask);
|
|
154 |
CGImageRelease(mask);
|
3697
|
155 |
|
3547
|
156 |
UIImage* retImage = [UIImage imageWithCGImage:masked];
|
|
157 |
CGImageRelease(masked);
|
3697
|
158 |
|
3547
|
159 |
return retImage;
|
|
160 |
}
|
|
161 |
|
|
162 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda
|
3573
|
163 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) {
|
|
164 |
CGFloat fw, fh;
|
3547
|
165 |
if (ovalWidth == 0 || ovalHeight == 0) {
|
|
166 |
CGContextAddRect(context, rect);
|
|
167 |
return;
|
|
168 |
}
|
|
169 |
CGContextSaveGState(context);
|
|
170 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect));
|
|
171 |
CGContextScaleCTM (context, ovalWidth, ovalHeight);
|
|
172 |
fw = CGRectGetWidth (rect) / ovalWidth;
|
|
173 |
fh = CGRectGetHeight (rect) / ovalHeight;
|
|
174 |
CGContextMoveToPoint(context, fw, fh/2);
|
|
175 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1);
|
|
176 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1);
|
|
177 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1);
|
|
178 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1);
|
|
179 |
CGContextClosePath(context);
|
|
180 |
CGContextRestoreGState(context);
|
|
181 |
}
|
|
182 |
|
3697
|
183 |
-(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
|
3573
|
184 |
CGFloat cornerWidth = sizewh.width;
|
|
185 |
CGFloat cornerHeight = sizewh.height;
|
|
186 |
CGFloat w = self.size.width;
|
|
187 |
CGFloat h = self.size.height;
|
3697
|
188 |
|
3547
|
189 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
|
|
190 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst);
|
3697
|
191 |
|
3547
|
192 |
CGContextBeginPath(context);
|
|
193 |
CGRect rect = CGRectMake(0, 0, w, h);
|
|
194 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight);
|
|
195 |
CGContextClosePath(context);
|
|
196 |
CGContextClip(context);
|
3697
|
197 |
|
3573
|
198 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]);
|
3697
|
199 |
|
3547
|
200 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context);
|
|
201 |
CGContextRelease(context);
|
|
202 |
CGColorSpaceRelease(colorSpace);
|
3697
|
203 |
|
3573
|
204 |
UIImage *newImage = [UIImage imageWithCGImage:imageMasked];
|
3547
|
205 |
CGImageRelease(imageMasked);
|
3697
|
206 |
|
3547
|
207 |
return newImage;
|
|
208 |
}
|
|
209 |
|
3903
|
210 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/
|
|
211 |
-(UIImage *)convertToNegative {
|
|
212 |
UIGraphicsBeginImageContext(self.size);
|
|
213 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy);
|
|
214 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)];
|
|
215 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference);
|
|
216 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor);
|
|
217 |
CGContextFillRect(UIGraphicsGetCurrentContext(), CGRectMake(0, 0, self.size.width, self.size.height));
|
|
218 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext();
|
|
219 |
UIGraphicsEndImageContext();
|
|
220 |
return result;
|
|
221 |
}
|
|
222 |
|
3547
|
223 |
@end
|