Advertisement
adilima

DPX Viewer using Cocoa

Feb 21st, 2012
340
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #import <Cocoa/Cocoa.h>
  2. #import "dpx.h"
  3.  
  4. @interface DPXView : NSView
  5. {
  6.     NSBitmapImageRep* image;
  7. }
  8. @property (assign) NSBitmapImageRep* image;
  9. -(BOOL)loadDPX:(NSString*)strPath;
  10. @end
  11.  
  12. @implementation DPXView
  13. @synthesize image;
  14. -(void)drawRect:(NSRect)rcBound
  15. {
  16.     if (!image)
  17.     {
  18.         [[NSColor blackColor] set];
  19.         NSRectFill(rcBound);
  20.         return;
  21.     }
  22.     [image drawInRect:rcBound
  23.              fromRect:NSZeroRect
  24.             operation:NSCompositeCopy
  25.              fraction:1
  26.        respectFlipped:YES
  27.                 hints:NULL];
  28. }
  29. -(BOOL)loadDPX:(NSString*)strPath
  30. {
  31.     NSData* data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:strPath]];
  32.     DPXHeader* hdr = (DPXHeader*)[data bytes];
  33.     DPXImageHeader* imgHdr = (DPXImageHeader*)(hdr+1);
  34.     uint32_t* pBits = (uint32_t*)(((uint8_t*)[data bytes]) + hdr->image_offset);
  35.    
  36.     if (image)
  37.         [image release];
  38.    
  39.     /**
  40.      * Create 16-Bit per channel RGB Planar image
  41.      */
  42.     image = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
  43.                                                     pixelsWide:imgHdr->img_width
  44.                                                     pixelsHigh:imgHdr->img_height
  45.                                                  bitsPerSample:16
  46.                                                samplesPerPixel:3
  47.                                                       hasAlpha:FALSE
  48.                                                       isPlanar:YES
  49.                                                 colorSpaceName:NSCalibratedRGBColorSpace
  50.                                                    bytesPerRow:imgHdr->img_width*2
  51.                                                   bitsPerPixel:16];
  52.    
  53.     int nCount = imgHdr->img_width*imgHdr->img_height;
  54.     uint16_t* pR = (uint16_t*)[image bitmapData];
  55.     uint16_t* pG = pR + nCount;
  56.     uint16_t* pB = pG + nCount;
  57.     int i;
  58.     if (imgHdr->img_element[0].bit_size == 10)
  59.     {
  60.         uint32_t val32;
  61.         for (i = 0; i < nCount; i++)
  62.         {
  63.             val32 = *pBits;
  64.             if (imgHdr->img_element[0].packing == 1)
  65.                 val32 >>= 2;
  66.             *pB++ = VALUE_10_TO_16(val32);
  67.             *pG++ = VALUE_10_TO_16(val32>>10);
  68.             *pR++ = VALUE_10_TO_16(val32>>20);
  69.             pBits++;
  70.         }
  71.     }
  72.     else if (imgHdr->img_element[0].bit_size == 16)
  73.     {
  74.         RGB16* pRGB = (RGB16*)pBits;
  75.         for (i = 0; i < nCount; i++)
  76.         {
  77.             *pR++ = pRGB->red;
  78.             *pG++ = pRGB->green;
  79.             *pB++ = pRGB->blue;
  80.             pRGB++;
  81.         }
  82.     }
  83.    
  84.     return TRUE;
  85. }
  86. @end
  87.  
  88. int main(int argc, const char** argv)
  89. {
  90.     NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
  91.     [NSApplication sharedApplication];
  92.    
  93.     NSOpenPanel* dlg = [NSOpenPanel openPanel];
  94.     [dlg setTitle:@"Open DPX File"];
  95.     [dlg setCanChooseFiles:TRUE];
  96.     [dlg setCanChooseDirectories:FALSE];
  97.     [dlg setAllowedFileTypes:[NSArray arrayWithObject:@"dpx"]];
  98.     if ([dlg runModal] != NSOKButton)
  99.         return 0;
  100.    
  101.     DPXView* view = [DPXView new];
  102.     [view loadDPX:[[dlg URL] path]];
  103.    
  104.     NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 720, 576)
  105.                                                    styleMask:NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask
  106.                                                      backing:NSBackingStoreBuffered
  107.                                                        defer:NO];
  108.     [window setTitle:@"Test DPX"];
  109.     [window setContentView:view];
  110.     [window orderFront:NULL];
  111.     [NSApp run];
  112.     [pool release];
  113.     return 0;
  114. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement