Advertisement
Guest User

ARCommon - Texture

a guest
Apr 5th, 2013
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*==============================================================================
  2.                 Texture.h
  3.  ==============================================================================*/
  4.  
  5. /*==============================================================================
  6.  Copyright (c) 2012 QUALCOMM Austria Research Center GmbH.
  7.  All Rights Reserved.
  8.  Qualcomm Confidential and Proprietary
  9.  ==============================================================================*/
  10.  
  11.  
  12. #import <Foundation/Foundation.h>
  13.  
  14. @interface Texture : NSObject {
  15.     int width;
  16.     int height;
  17.     int channels;
  18.     int textureID;
  19.     unsigned char* pngData;
  20. }
  21.  
  22. @property (nonatomic, readonly) int width;
  23. @property (nonatomic, readonly) int height;
  24. @property (nonatomic) int textureID;
  25. @property (nonatomic, readonly) unsigned char* pngData;
  26.  
  27. - (BOOL)loadImage:(NSString*)filename;
  28.  
  29. @end
  30.  
  31. /*==============================================================================
  32.                 Texture.mm
  33.  ==============================================================================*/
  34.  
  35.  
  36. /*==============================================================================
  37.  Copyright (c) 2010-2012 QUALCOMM Austria Research Center GmbH.
  38.  All Rights Reserved.
  39.  Qualcomm Confidential and Proprietary
  40.  ==============================================================================*/
  41.  
  42.  
  43. #import "Texture.h"
  44.  
  45. // Private method declarations
  46. @interface Texture (PrivateMethods)
  47. - (BOOL)copyImageDataForOpenGL:(CFDataRef)imageData;
  48. @end
  49.  
  50. @implementation Texture
  51.  
  52. @synthesize width, height, textureID, pngData;
  53.  
  54.  
  55. - (id)init
  56. {
  57.     self = [super init];
  58.     pngData = NULL;
  59.    
  60.     return self;
  61. }
  62.  
  63.  
  64. - (BOOL)loadImage:(NSString*)filename
  65. {
  66.     BOOL ret = NO;
  67.    
  68.     // Build the full path of the image file
  69.     NSString* resourcePath = [[NSBundle mainBundle] resourcePath];
  70.     NSString* fullPath = [resourcePath stringByAppendingPathComponent:filename];
  71.    
  72.     // Create a UIImage with the contents of the file
  73.     UIImage* uiImage = [UIImage imageWithContentsOfFile:fullPath];
  74.    
  75.     if (uiImage) {
  76.         // Get the inner CGImage from the UIImage wrapper
  77.         CGImageRef cgImage = uiImage.CGImage;
  78.        
  79.         // Get the image size
  80.         width = CGImageGetWidth(cgImage);
  81.         height = CGImageGetHeight(cgImage);
  82.        
  83.         // Record the number of channels
  84.         channels = CGImageGetBitsPerPixel(cgImage)/CGImageGetBitsPerComponent(cgImage);
  85.        
  86.         // Generate a CFData object from the CGImage object (a CFData object represents an area of memory)
  87.         CFDataRef imageData = CGDataProviderCopyData(CGImageGetDataProvider(cgImage));
  88.        
  89.         // Copy the image data for use by Open GL
  90.         ret = [self copyImageDataForOpenGL: imageData];
  91.         CFRelease(imageData);
  92.     }
  93.    
  94.     return ret;
  95. }
  96.  
  97.  
  98. - (void)dealloc
  99. {
  100.     if (pngData) {
  101.         delete[] pngData;
  102.     }
  103.    
  104.     [super dealloc];
  105. }
  106.  
  107. @end
  108.  
  109.  
  110. @implementation Texture (TexturePrivateMethods)
  111.  
  112. - (BOOL)copyImageDataForOpenGL:(CFDataRef)imageData
  113. {    
  114.     if (pngData) {
  115.         delete[] pngData;
  116.     }
  117.    
  118.     pngData = new unsigned char[width * height * channels];
  119.     const int rowSize = width * channels;
  120.     const unsigned char* pixels = (unsigned char*)CFDataGetBytePtr(imageData);
  121.  
  122.     // Copy the row data from bottom to top
  123.     for (int i = 0; i < height; ++i) {
  124.         memcpy(pngData + rowSize * i, pixels + rowSize * (height - 1 - i), width * channels);
  125.     }
  126.    
  127.     return YES;
  128. }
  129.  
  130. @end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement