project_files/HedgewarsMobile/Classes/UIImageExtra.m
changeset 6078 8c0cc07731e5
parent 5503 d8632f589008
child 6080 ce02ddfe8aa1
--- a/project_files/HedgewarsMobile/Classes/UIImageExtra.m	Fri Sep 30 22:33:28 2011 -0400
+++ b/project_files/HedgewarsMobile/Classes/UIImageExtra.m	Sun Oct 02 00:57:04 2011 +0200
@@ -24,21 +24,11 @@
 
 @implementation UIImage (extra)
 
-CGFloat getScreenScale(void) {
-    float scale = 1.0f;
-    if ([[UIScreen mainScreen] respondsToSelector:@selector(scale)])
-        scale = [[UIScreen mainScreen] scale];
-    return scale;
-}
-
 -(UIImage *)scaleToSize:(CGSize) size {
     DLog(@"warning - this is a very expensive operation, you should avoid using it");
 
     // Create a bitmap graphics context; this will also set it as the current context
-    if (UIGraphicsBeginImageContextWithOptions != NULL)
-        UIGraphicsBeginImageContextWithOptions(size, NO, getScreenScale());
-    else
-        UIGraphicsBeginImageContext(size);
+    UIGraphicsBeginImageContextWithOptions(size, NO, [[UIScreen mainScreen] scale]);
 
     // Draw the scaled image in the current context
     [self drawInRect:CGRectMake(0, 0, size.width, size.height)];
@@ -58,7 +48,7 @@
         DLog(@"Warning, secondImage == nil");
         return self;
     }
-    CGFloat screenScale = getScreenScale();
+    CGFloat screenScale = [[UIScreen mainScreen] scale];
     int w = self.size.width * screenScale;
     int h = self.size.height * screenScale;
     
@@ -79,11 +69,7 @@
     CGImageRef imageRef = CGBitmapContextCreateImage(context);
     
     // Create a new UIImage object
-    UIImage *resultImage;
-    if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
-        resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
-    else
-        resultImage = [UIImage imageWithCGImage:imageRef];
+    UIImage *resultImage = [UIImage imageWithCGImage:imageRef scale:screenScale orientation:UIImageOrientationUp];
 
     // Release colorspace, context and bitmap information
     CGColorSpaceRelease(colorSpace);
@@ -203,7 +189,7 @@
 -(UIImage *)makeRoundCornersOfSize:(CGSize) sizewh {
     CGFloat cornerWidth = sizewh.width;
     CGFloat cornerHeight = sizewh.height;
-    CGFloat screenScale = getScreenScale();
+    CGFloat screenScale = [[UIScreen mainScreen] scale];
     CGFloat w = self.size.width * screenScale;
     CGFloat h = self.size.height * screenScale;
 
@@ -222,11 +208,7 @@
     CGContextRelease(context);
     CGColorSpaceRelease(colorSpace);
 
-    UIImage *newImage;
-    if ([self respondsToSelector:@selector(imageWithCGImage:scale:orientation:)])
-        newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
-    else
-        newImage = [UIImage imageWithCGImage:imageMasked];
+    UIImage *newImage = [UIImage imageWithCGImage:imageMasked scale:screenScale orientation:UIImageOrientationUp];
 
     CGImageRelease(imageMasked);
 
@@ -268,4 +250,48 @@
     return bkgImg;
 }
 
+// this routine checks for the PNG size without loading it in memory
+// https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
++(CGSize) imageSizeFromMetadataOf:(NSString *)aFileName {
+    // File Name to C String.
+    const char *fileName = [aFileName UTF8String];
+    // source file
+    FILE *infile = fopen(fileName, "rb");
+    if (infile == NULL) {
+        DLog(@"Can't open the file: %@", aFileName);
+        return CGSizeZero;
+    }
+
+    // Bytes Buffer.
+    unsigned char buffer[30];
+    // Grab Only First Bytes.
+    fread(buffer, 1, 30, infile);
+    // Close File.
+    fclose(infile);
+
+    // PNG Signature.
+    unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
+
+    // Compare File signature.
+    if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
+        DLog(@"The file (%@) is not a PNG file", aFileName);
+        return CGSizeZero;
+    }
+
+    // Calc Sizes. Isolate only four bytes of each size (width, height).
+    int width[4];
+    int height[4];
+    for (int d = 16; d < (16 + 4); d++) {
+        width[d-16] = buffer[d];
+        height[d-16] = buffer[d+4];
+    }
+
+    // Convert bytes to Long (Integer)
+    long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
+    long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
+
+    // Return Size.
+    return CGSizeMake(resultWidth,resultHeight);
+}
+
 @end