#import <Cocoa/Cocoa.h>
#import "dpx.h"
@interface DPXView : NSView
{
NSBitmapImageRep* image;
}
@property (assign) NSBitmapImageRep* image;
-(BOOL)loadDPX:(NSString*)strPath;
@end
@implementation DPXView
@synthesize image;
-(void)drawRect:(NSRect)rcBound
{
if (!image)
{
[[NSColor blackColor] set];
NSRectFill(rcBound);
return;
}
[image drawInRect:rcBound
fromRect:NSZeroRect
operation:NSCompositeCopy
fraction:1
respectFlipped:YES
hints:NULL];
}
-(BOOL)loadDPX:(NSString*)strPath
{
NSData* data = [NSData dataWithContentsOfURL:[NSURL fileURLWithPath:strPath]];
DPXHeader* hdr = (DPXHeader*)[data bytes];
DPXImageHeader* imgHdr = (DPXImageHeader*)(hdr+1);
uint32_t* pBits = (uint32_t*)(((uint8_t*)[data bytes]) + hdr->image_offset);
if (image)
[image release];
/**
* Create 16-Bit per channel RGB Planar image
*/
image = [[NSBitmapImageRep alloc] initWithBitmapDataPlanes:NULL
pixelsWide:imgHdr->img_width
pixelsHigh:imgHdr->img_height
bitsPerSample:16
samplesPerPixel:3
hasAlpha:FALSE
isPlanar:YES
colorSpaceName:NSCalibratedRGBColorSpace
bytesPerRow:imgHdr->img_width*2
bitsPerPixel:16];
int nCount = imgHdr->img_width*imgHdr->img_height;
uint16_t* pR = (uint16_t*)[image bitmapData];
uint16_t* pG = pR + nCount;
uint16_t* pB = pG + nCount;
int i;
if (imgHdr->img_element[0].bit_size == 10)
{
uint32_t val32;
for (i = 0; i < nCount; i++)
{
val32 = *pBits;
if (imgHdr->img_element[0].packing == 1)
val32 >>= 2;
*pB++ = VALUE_10_TO_16(val32);
*pG++ = VALUE_10_TO_16(val32>>10);
*pR++ = VALUE_10_TO_16(val32>>20);
pBits++;
}
}
else if (imgHdr->img_element[0].bit_size == 16)
{
RGB16* pRGB = (RGB16*)pBits;
for (i = 0; i < nCount; i++)
{
*pR++ = pRGB->red;
*pG++ = pRGB->green;
*pB++ = pRGB->blue;
pRGB++;
}
}
return TRUE;
}
@end
int main(int argc, const char** argv)
{
NSAutoreleasePool* pool = [[NSAutoreleasePool alloc] init];
[NSApplication sharedApplication];
NSOpenPanel* dlg = [NSOpenPanel openPanel];
[dlg setTitle:@"Open DPX File"];
[dlg setCanChooseFiles:TRUE];
[dlg setCanChooseDirectories:FALSE];
[dlg setAllowedFileTypes:[NSArray arrayWithObject:@"dpx"]];
if ([dlg runModal] != NSOKButton)
return 0;
DPXView* view = [DPXView new];
[view loadDPX:[[dlg URL] path]];
NSWindow* window = [[NSWindow alloc] initWithContentRect:NSMakeRect(0, 0, 720, 576)
styleMask:NSTitledWindowMask|NSClosableWindowMask|NSResizableWindowMask
backing:NSBackingStoreBuffered
defer:NO];
[window setTitle:@"Test DPX"];
[window setContentView:view];
[window orderFront:NULL];
[NSApp run];
[pool release];
return 0;
}