author | raptor <buckyballreaction@gmail.com> |
Fri, 16 Aug 2019 10:59:50 -0600 | |
changeset 15338 | c4d68ab7aecb |
parent 12877 | 00215a7ec5f5 |
permissions | -rw-r--r-- |
3829 | 1 |
/* |
2 |
* Hedgewars-iOS, a Hedgewars port for iOS devices |
|
6700 | 3 |
* Copyright (c) 2009-2012 Vittorio Giovara <vittorio.giovara@gmail.com> |
3829 | 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 |
|
10108
c68cf030eded
update FSF address. note: two sdl include files (by Sam Lantinga) still have the old FSF address in their copyright - but I ain't gonna touch their copyright headers
sheepluva
parents:
6832
diff
changeset
|
16 |
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
3829 | 17 |
*/ |
18 |
||
3547 | 19 |
|
20 |
#import "UIImageExtra.h" |
|
21 |
||
22 |
||
23 |
@implementation UIImage (extra) |
|
3697 | 24 |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
25 |
- (UIImage *)scaleToSize:(CGSize)size { |
6209 | 26 |
// Create a bitmap graphics context; this will also set it as the current context |
27 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
28 |
CGContextRef context = CGBitmapContextCreate(NULL, size.width, size.height, 8, 4 * size.width, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 29 |
|
6209 | 30 |
// draw the image inside the context |
31 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
|
32 |
CGContextDrawImage(context, CGRectMake(0, 0, size.width*screenScale, size.height*screenScale), self.CGImage); |
|
3697 | 33 |
|
6209 | 34 |
// Create bitmap image info from pixel data in current context |
35 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 36 |
|
6209 | 37 |
// Create a new UIImage object |
38 |
UIImage *resultImage; |
|
39 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
40 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
41 |
else |
|
42 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
|
3697 | 43 |
|
6209 | 44 |
// Release colorspace, context and bitmap information |
45 |
CGColorSpaceRelease(colorSpace); |
|
46 |
CGContextRelease(context); |
|
47 |
CFRelease(imageRef); |
|
3697 | 48 |
|
6209 | 49 |
return resultImage; |
3547 | 50 |
} |
51 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
52 |
- (UIImage *)mergeWith:(UIImage *)secondImage atPoint:(CGPoint)secondImagePoint { |
3778 | 53 |
if (secondImage == nil) { |
54 |
DLog(@"Warning, secondImage == nil"); |
|
55 |
return self; |
|
56 |
} |
|
6209 | 57 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
4478 | 58 |
int w = self.size.width * screenScale; |
59 |
int h = self.size.height * screenScale; |
|
6080
ce02ddfe8aa1
on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents:
6078
diff
changeset
|
60 |
int yOffset = self.size.height - secondImage.size.height + secondImagePoint.y; |
ce02ddfe8aa1
on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents:
6078
diff
changeset
|
61 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
62 |
if (w == 0 || h == 0) { |
6080
ce02ddfe8aa1
on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents:
6078
diff
changeset
|
63 |
DLog(@"Cannot have 0 dimesions"); |
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
64 |
return self; |
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
65 |
} |
6209 | 66 |
|
3573 | 67 |
// 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
|
68 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
6080
ce02ddfe8aa1
on ipad2 and above animate the little hogs on game config and fix the hat display imaging
koda
parents:
6078
diff
changeset
|
69 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h+yOffset, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
6209 | 70 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
71 |
// 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
|
72 |
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
|
73 |
CGContextDrawImage(context, CGRectMake(secondImagePoint.x*screenScale, secondImagePoint.y*screenScale, secondImage.size.width*screenScale, secondImage.size.height*screenScale), [secondImage CGImage]); |
6209 | 74 |
|
3948
24daa33a3114
some rethinking of initial menu presentation and initial orientation (also merging images should be threadsafe now)
koda
parents:
3910
diff
changeset
|
75 |
// 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
|
76 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
6209 | 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 |
// Create a new UIImage object |
6209 | 79 |
UIImage *resultImage; |
80 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
81 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
82 |
else |
|
83 |
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); |
6209 | 89 |
|
3547 | 90 |
return resultImage; |
91 |
} |
|
92 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
93 |
- (id)initWithContentsOfFile:(NSString *)path andCutAt:(CGRect)rect { |
3547 | 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 |
3697 | 102 |
|
3547 | 103 |
// create a UIImage from the CGImage (memory must be allocated already) |
3621 | 104 |
UIImage *sprite = [self initWithCGImage:cgImage]; |
3697 | 105 |
|
3547 | 106 |
// clean memory |
3621 | 107 |
CGImageRelease(cgImage); |
3547 | 108 |
|
109 |
// return resulting image |
|
110 |
return sprite; |
|
111 |
} else { |
|
112 |
DLog(@"error - image == nil"); |
|
113 |
return nil; |
|
114 |
} |
|
115 |
} |
|
116 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
117 |
- (UIImage *)cutAt:(CGRect)rect { |
3621 | 118 |
CGImageRef cgImage = CGImageCreateWithImageInRect([self CGImage], rect); |
3697 | 119 |
|
3621 | 120 |
UIImage *res = [UIImage imageWithCGImage:cgImage]; |
121 |
CGImageRelease(cgImage); |
|
3697 | 122 |
|
3621 | 123 |
return res; |
124 |
} |
|
125 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
126 |
- (UIImage *)convertToGrayScale { |
3573 | 127 |
// Create image rectangle with current image width/height |
128 |
CGRect imageRect = CGRectMake(0, 0, self.size.width, self.size.height); |
|
3697 | 129 |
|
3573 | 130 |
// Grayscale color space |
131 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceGray(); |
|
3697 | 132 |
|
3573 | 133 |
// Create bitmap content with current image size and grayscale colorspace |
134 |
CGContextRef context = CGBitmapContextCreate(nil, self.size.width, self.size.height, 8, 0, colorSpace, kCGImageAlphaNone); |
|
3697 | 135 |
|
3573 | 136 |
// Draw image into current context, with specified rectangle |
137 |
// using previously defined context (with grayscale colorspace) |
|
138 |
CGContextDrawImage(context, imageRect, [self CGImage]); |
|
3697 | 139 |
|
3573 | 140 |
// Create bitmap image info from pixel data in current context |
141 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
3697 | 142 |
|
143 |
// Create a new UIImage object |
|
3573 | 144 |
UIImage *newImage = [UIImage imageWithCGImage:imageRef]; |
3697 | 145 |
|
3573 | 146 |
// Release colorspace, context and bitmap information |
3978 | 147 |
CFRelease(imageRef); |
3573 | 148 |
CGContextRelease(context); |
3978 | 149 |
CGColorSpaceRelease(colorSpace); |
3697 | 150 |
|
3573 | 151 |
// Return the new grayscale image |
152 |
return newImage; |
|
3547 | 153 |
} |
154 |
||
155 |
// by http://iphonedevelopertips.com/cocoa/how-to-mask-an-image.html turned into a category by koda |
|
156 |
-(UIImage*) maskImageWith:(UIImage *)maskImage { |
|
3573 | 157 |
// prepare the reference image |
158 |
CGImageRef maskRef = [maskImage CGImage]; |
|
3697 | 159 |
|
3573 | 160 |
// create the mask using parameters of the mask reference |
3547 | 161 |
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(maskRef), |
162 |
CGImageGetHeight(maskRef), |
|
163 |
CGImageGetBitsPerComponent(maskRef), |
|
164 |
CGImageGetBitsPerPixel(maskRef), |
|
165 |
CGImageGetBytesPerRow(maskRef), |
|
166 |
CGImageGetDataProvider(maskRef), NULL, false); |
|
3697 | 167 |
|
3573 | 168 |
// create an image in the current context |
3547 | 169 |
CGImageRef masked = CGImageCreateWithMask([self CGImage], mask); |
170 |
CGImageRelease(mask); |
|
3697 | 171 |
|
3547 | 172 |
UIImage* retImage = [UIImage imageWithCGImage:masked]; |
173 |
CGImageRelease(masked); |
|
3697 | 174 |
|
3547 | 175 |
return retImage; |
176 |
} |
|
177 |
||
178 |
// by http://blog.sallarp.com/iphone-uiimage-round-corners/ turned into a category by koda |
|
3573 | 179 |
void addRoundedRectToPath(CGContextRef context, CGRect rect, CGFloat ovalWidth, CGFloat ovalHeight) { |
180 |
CGFloat fw, fh; |
|
3547 | 181 |
if (ovalWidth == 0 || ovalHeight == 0) { |
182 |
CGContextAddRect(context, rect); |
|
183 |
return; |
|
184 |
} |
|
185 |
CGContextSaveGState(context); |
|
186 |
CGContextTranslateCTM (context, CGRectGetMinX(rect), CGRectGetMinY(rect)); |
|
187 |
CGContextScaleCTM (context, ovalWidth, ovalHeight); |
|
188 |
fw = CGRectGetWidth (rect) / ovalWidth; |
|
189 |
fh = CGRectGetHeight (rect) / ovalHeight; |
|
190 |
CGContextMoveToPoint(context, fw, fh/2); |
|
191 |
CGContextAddArcToPoint(context, fw, fh, fw/2, fh, 1); |
|
192 |
CGContextAddArcToPoint(context, 0, fh, 0, fh/2, 1); |
|
193 |
CGContextAddArcToPoint(context, 0, 0, fw/2, 0, 1); |
|
194 |
CGContextAddArcToPoint(context, fw, 0, fw, fh/2, 1); |
|
195 |
CGContextClosePath(context); |
|
196 |
CGContextRestoreGState(context); |
|
197 |
} |
|
198 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
199 |
- (UIImage *)makeRoundCornersOfSize:(CGSize)sizewh { |
3573 | 200 |
CGFloat cornerWidth = sizewh.width; |
201 |
CGFloat cornerHeight = sizewh.height; |
|
6209 | 202 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
4476
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
203 |
CGFloat w = self.size.width * screenScale; |
4bf74e158f44
team selection completely refactored, now has animation and more performance
koda
parents:
4461
diff
changeset
|
204 |
CGFloat h = self.size.height * screenScale; |
3697 | 205 |
|
3547 | 206 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
207 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
3697 | 208 |
|
3547 | 209 |
CGContextBeginPath(context); |
210 |
CGRect rect = CGRectMake(0, 0, w, h); |
|
211 |
addRoundedRectToPath(context, rect, cornerWidth, cornerHeight); |
|
212 |
CGContextClosePath(context); |
|
213 |
CGContextClip(context); |
|
3697 | 214 |
|
3573 | 215 |
CGContextDrawImage(context, CGRectMake(0, 0, w, h), [self CGImage]); |
3697 | 216 |
|
3547 | 217 |
CGImageRef imageMasked = CGBitmapContextCreateImage(context); |
218 |
CGContextRelease(context); |
|
219 |
CGColorSpaceRelease(colorSpace); |
|
3697 | 220 |
|
6209 | 221 |
UIImage *resultImage; |
222 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
223 |
resultImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp]; |
|
224 |
else |
|
225 |
resultImage = [UIImage imageWithCGImage:imageMasked]; |
|
3547 | 226 |
CGImageRelease(imageMasked); |
3697 | 227 |
|
6209 | 228 |
return resultImage; |
3547 | 229 |
} |
230 |
||
3903 | 231 |
// by http://www.sixtemia.com/journal/2010/06/23/uiimage-negative-color-effect/ |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
232 |
- (UIImage *)convertToNegative { |
3903 | 233 |
UIGraphicsBeginImageContext(self.size); |
234 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeCopy); |
|
235 |
[self drawInRect:CGRectMake(0, 0, self.size.width, self.size.height)]; |
|
236 |
CGContextSetBlendMode(UIGraphicsGetCurrentContext(), kCGBlendModeDifference); |
|
237 |
CGContextSetFillColorWithColor(UIGraphicsGetCurrentContext(),[UIColor whiteColor].CGColor); |
|
238 |
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
|
239 |
// create an image from the current contex (not thread safe) |
3903 | 240 |
UIImage *result = UIGraphicsGetImageFromCurrentImageContext(); |
241 |
UIGraphicsEndImageContext(); |
|
242 |
return result; |
|
243 |
} |
|
244 |
||
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
245 |
+ (UIImage *)whiteImage:(CGSize)ofSize { |
4356 | 246 |
CGFloat w = ofSize.width; |
247 |
CGFloat h = ofSize.height; |
|
4478 | 248 |
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
|
249 |
|
4356 | 250 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
251 |
CGContextRef context = CGBitmapContextCreate(NULL, w, h, 8, 4 * w, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
252 |
||
253 |
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
|
254 |
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
|
255 |
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
|
256 |
|
4356 | 257 |
CGImageRef image = CGBitmapContextCreateImage(context); |
258 |
CGContextRelease(context); |
|
259 |
CGColorSpaceRelease(colorSpace); |
|
260 |
||
261 |
UIImage *bkgImg = [UIImage imageWithCGImage:image]; |
|
262 |
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
|
263 |
return bkgImg; |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
264 |
} |
dd47efbdec46
move all preview drawing code into its own class (for a simplified and more readable MapConfigViewController)
koda
parents:
3903
diff
changeset
|
265 |
|
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
266 |
+ (UIImage *)drawHogsRepeated:(NSInteger)manyTimes { |
6209 | 267 |
NSString *imgString = [[NSString alloc] initWithFormat:@"%@/hedgehog.png",[[NSBundle mainBundle] resourcePath]]; |
268 |
UIImage *hogSprite = [[UIImage alloc] initWithContentsOfFile:imgString]; |
|
269 |
CGFloat screenScale = [[UIScreen mainScreen] safeScale]; |
|
270 |
int w = hogSprite.size.width * screenScale; |
|
271 |
int h = hogSprite.size.height * screenScale; |
|
272 |
CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
|
273 |
CGContextRef context = CGBitmapContextCreate(NULL, w * 3, h, 8, 4 * w * 3, colorSpace, kCGImageAlphaPremultipliedFirst); |
|
274 |
||
275 |
// draw the two images in the current context |
|
276 |
for (int i = 0; i < manyTimes; i++) |
|
277 |
CGContextDrawImage(context, CGRectMake(i*8*screenScale, 0, w, h), [hogSprite CGImage]); |
|
278 |
||
279 |
// Create bitmap image info from pixel data in current context |
|
280 |
CGImageRef imageRef = CGBitmapContextCreateImage(context); |
|
281 |
||
282 |
// Create a new UIImage object |
|
283 |
UIImage *resultImage; |
|
284 |
if ([UIImage respondsToSelector:@selector(imageWithCGImage:scale:orientation:)]) |
|
285 |
resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp]; |
|
286 |
else |
|
287 |
resultImage = [UIImage imageWithCGImage:imageRef]; |
|
288 |
||
289 |
// Release colorspace, context and bitmap information |
|
290 |
CGColorSpaceRelease(colorSpace); |
|
291 |
CGContextRelease(context); |
|
292 |
CFRelease(imageRef); |
|
293 |
||
294 |
return resultImage; |
|
295 |
} |
|
296 |
||
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
297 |
// this routine checks for the PNG size without loading it in memory |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
298 |
// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m |
12877
00215a7ec5f5
- BIG CHANGE: Convert iOS project to use ARC
antonc27 <antonc27@mail.ru>
parents:
10108
diff
changeset
|
299 |
+ (CGSize)imageSizeFromMetadataOf:(NSString *)aFileName { |
6078
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
300 |
// File Name to C String. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
301 |
const char *fileName = [aFileName UTF8String]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
302 |
// source file |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
303 |
FILE *infile = fopen(fileName, "rb"); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
304 |
if (infile == NULL) { |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
305 |
DLog(@"Can't open the file: %@", aFileName); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
306 |
return CGSizeZero; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
307 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
308 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
309 |
// Bytes Buffer. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
310 |
unsigned char buffer[30]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
311 |
// Grab Only First Bytes. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
312 |
fread(buffer, 1, 30, infile); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
313 |
// Close File. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
314 |
fclose(infile); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
315 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
316 |
// PNG Signature. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
317 |
unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10}; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
318 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
319 |
// Compare File signature. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
320 |
if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) { |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
321 |
DLog(@"The file (%@) is not a PNG file", aFileName); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
322 |
return CGSizeZero; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
323 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
324 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
325 |
// Calc Sizes. Isolate only four bytes of each size (width, height). |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
326 |
int width[4]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
327 |
int height[4]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
328 |
for (int d = 16; d < (16 + 4); d++) { |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
329 |
width[d-16] = buffer[d]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
330 |
height[d-16] = buffer[d+4]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
331 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
332 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
333 |
// Convert bytes to Long (Integer) |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
334 |
long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
335 |
long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3]; |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
336 |
|
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
337 |
// Return Size. |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
338 |
return CGSizeMake(resultWidth,resultHeight); |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
339 |
} |
8c0cc07731e5
headers cleanup, converted some function-only sources into proper class method files, more use of OOP power, removed some 'respondsToSelector' calls, moved defines into their own header, more use of objc categories
koda
parents:
5503
diff
changeset
|
340 |
|
3547 | 341 |
@end |