project_files/HedgewarsMobile/Classes/CommodityFunctions.m
changeset 4290 82f1f1d819c0
parent 4281 e033cf015b2c
child 4341 46b8791e577f
equal deleted inserted replaced
4287:7dbdc862097c 4290:82f1f1d819c0
   140     [theLabel.layer setCornerRadius:8.0f];
   140     [theLabel.layer setCornerRadius:8.0f];
   141     [theLabel.layer setMasksToBounds:YES];
   141     [theLabel.layer setMasksToBounds:YES];
   142     
   142     
   143     return theLabel;
   143     return theLabel;
   144 }
   144 }
       
   145 
       
   146 // this routine checks for the PNG size without loading it in memory
       
   147 // https://github.com/steipete/PSFramework/blob/master/PSFramework%20Version%200.3/PhotoshopFramework/PSMetaDataFunctions.m
       
   148 CGSize PSPNGSizeFromMetaData (NSString *aFileName) {
       
   149     // File Name to C String.
       
   150     const char *fileName = [aFileName UTF8String];
       
   151     // source file
       
   152     FILE *infile = fopen(fileName, "rb");
       
   153     if (infile == NULL) {
       
   154         DLog(@"Can't open the file: %@", aFileName);
       
   155         return CGSizeZero;
       
   156     }
       
   157 
       
   158     // Bytes Buffer.
       
   159     unsigned char buffer[30];
       
   160     // Grab Only First Bytes.
       
   161     fread(buffer, 1, 30, infile);
       
   162     // Close File.
       
   163     fclose(infile);
       
   164 
       
   165     // PNG Signature.
       
   166     unsigned char png_signature[8] = {137, 80, 78, 71, 13, 10, 26, 10};
       
   167 
       
   168     // Compare File signature.
       
   169     if ((int)(memcmp(&buffer[0], &png_signature[0], 8))) {
       
   170         DLog(@"The file (%@) is not a PNG file", aFileName);
       
   171         return CGSizeZero;
       
   172     }
       
   173 
       
   174     // Calc Sizes. Isolate only four bytes of each size (width, height).
       
   175     int width[4];
       
   176     int height[4];
       
   177     for (int d = 16; d < (16 + 4); d++) {
       
   178         width[d-16] = buffer[d];
       
   179         height[d-16] = buffer[d+4];
       
   180     }
       
   181 
       
   182     // Convert bytes to Long (Integer)
       
   183     long resultWidth = (width[0] << (int)24) | (width[1] << (int)16) | (width[2] << (int)8) | width[3];
       
   184     long resultHeight = (height[0] << (int)24) | (height[1] << (int)16) | (height[2] << (int)8) | height[3];
       
   185 
       
   186     // Return Size.
       
   187     return CGSizeMake(resultWidth,resultHeight);
       
   188 }