Advertisement
adilima

Save any DPX with PAL Resolutions

Feb 23rd, 2012
583
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. -(void)saveAsPAL:(id)sender
  2. {
  3.     NSSavePanel* dlg = [NSSavePanel savePanel];
  4.     [dlg setCanCreateDirectories:TRUE];
  5.     [dlg setAllowedFileTypes:[NSArray arrayWithObject:@"dpx"]];
  6.     [dlg setShowsHiddenFiles:NO];
  7.     [dlg setTitle:@"Save As DPX File"];
  8.     if ([dlg runModal] != NSOKButton)
  9.         return;
  10.  
  11.     /*
  12.      * First we should create CGBitmapContext as a layer to draw scaled bitmap,
  13.      * and then take the result as bitmap data,
  14.      * transfer it into 10-Bit RGB format.
  15.      */
  16.     CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
  17.     CGContextRef palBitmap = CGBitmapContextCreate(NULL,
  18.                     720, 576,
  19.                     8, 720*4,
  20.                     rgbColorspace,
  21.                     kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Little);
  22.     CFRelease(rgbColorspace);
  23.     CGFloat scaledHeight = (720*[image size].height)/[image size].width;
  24.     CGFloat yOffset = 0;
  25.     if (scaledHeight < 576)
  26.         yOffset = (576-scaledHeight)/2;
  27.    
  28.     CGContextSetFillColorWithColor(palBitmap, CGColorCreateGenericRGB(0, 0, 0, 1));
  29.     CGContextFillRect(palBitmap, CGRectMake(0, 0, 720, 576));
  30.     CGContextDrawImage(palBitmap,
  31.             CGRectMake(0, yOffset, 720, scaledHeight),
  32.             [image CGImage]);
  33.     uint32_t* pImgData = (uint32_t*)CGBitmapContextGetData(palBitmap);
  34.    
  35.     size_t iLen = sizeof(DPXHeader) + sizeof(DPXImageHeader) +
  36.         sizeof(DPXImageOrientation) + sizeof(DPXFilmHeader) +
  37.         sizeof(DPXTVHeader) + 32;
  38.    
  39.     int bmSize = 720*576*4;
  40.     iLen += bmSize;
  41.    
  42.     uint8_t* pData = (uint8_t*)malloc(iLen);
  43.     memset(pData, 0xFF, iLen);
  44.    
  45.     DPXHeader* hdr = (DPXHeader*)pData;
  46.     DPXImageHeader* imgHdr = (DPXImageHeader*)(hdr+1);
  47.     DPXImageOrientation* orient = (DPXImageOrientation*)(imgHdr+1);
  48.     DPXFilmHeader* filmHdr = (DPXFilmHeader*)(orient+1);
  49.     DPXTVHeader* tvHdr = (DPXTVHeader*)(filmHdr+1);
  50.     hdr->magic = SDPX_MAGIC;
  51.     hdr->image_offset = iLen - bmSize;
  52.     strcpy((char*)hdr->version, "v2.0");
  53.     hdr->version[4] = 0;
  54.     hdr->file_size = iLen;
  55.     hdr->ditto_key = 0;
  56.     hdr->generic_hdr_size = sizeof(DPXHeader)+sizeof(DPXImageHeader)+sizeof(DPXImageOrientation);
  57.     hdr->industry_hdr_size = sizeof(DPXFilmHeader) + sizeof(DPXTVHeader);
  58.     hdr->user_data_size = 32;
  59.     hdr->encrypt_key = 0;
  60.     strcpy((char*)hdr->file_name,
  61.            [[[[dlg URL] path] lastPathComponent] UTF8String]);
  62.    
  63.     NSDate* strNow = [NSDate date];
  64.     NSCalendar* calendar = [NSCalendar currentCalendar];
  65.     NSDateComponents* components =
  66.         [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|
  67.             NSDayCalendarUnit|NSHourCalendarUnit|
  68.             NSMinuteCalendarUnit|NSSecondCalendarUnit
  69.                 fromDate:strNow];
  70.     NSString* strTest = [NSString stringWithFormat:@"%i:%i:%i:%i:%i:%i",
  71.                          [components year],
  72.                          [components month],
  73.                          [components day],
  74.                          [components hour],
  75.                          [components minute],
  76.                          [components second]];
  77.     strcpy((char*)hdr->creation_date, [strTest UTF8String]);
  78.     strcpy((char*)hdr->creator, "Adi");
  79.     strcpy((char*)hdr->project, "Test PAL");
  80.     strcpy((char*)hdr->copyright, "2012, FX. J. Adi Lima");
  81.     imgHdr->orientation = 0;
  82.     imgHdr->number_of_elements = 1;
  83.     imgHdr->img_width = 720;
  84.     imgHdr->img_height = 576;
  85.    
  86.     imgHdr->img_element[0].data_sign = 0;
  87.     imgHdr->img_element[0].data_offset = hdr->image_offset;
  88.     imgHdr->img_element[0].eo_image_padding = 0;
  89.     imgHdr->img_element[0].eol_padding = 0;
  90.     imgHdr->img_element[0].bit_size = 10;
  91.     strcpy((char*)imgHdr->img_element[0].description, "TESTING");
  92.     imgHdr->img_element[0].descriptor = 50;
  93.     imgHdr->img_element[0].packing = 2; // 10 Bit DPX Method B
  94.     imgHdr->img_element[0].encoding = 0;
  95.     strcpy((char*)filmHdr->format, "PAL Method B");
  96.     filmHdr->framerate = 25;
  97.     filmHdr->shutter_angle = 180.0f;
  98.     //strcpy((char*)tvHdr->timecode, "00:00:00:01");
  99.     tvHdr->timecode.value = 1;
  100.    
  101.     int nCount = 720*576;
  102.     uint32_t* pDest = (uint32_t*)(pData+hdr->image_offset);
  103.     uint32_t* pSrc = pImgData;
  104.     int i;
  105.     uint32_t nVal = 0;
  106.     uint32_t rgbVal;
  107.     for (i = 0; i < nCount; i++)
  108.     {
  109.         nVal = *pSrc++;
  110.         // Alpha Premultiplied First means ARGB (same as BGRA_Interleaved)
  111.         // Has to be saved the same order, but minus alpha.
  112.         rgbVal = (VALUE_8_TO_10(nVal)) | (VALUE_8_TO_10(nVal>>8)<<10) |
  113.             (VALUE_8_TO_10(nVal>>16)<<20);
  114.         *pDest++ = rgbVal;
  115.     }
  116.     NSData* pRes = [[NSData alloc] initWithBytes:pData length:iLen];
  117.     [pRes writeToURL:[dlg URL] atomically:YES];
  118.     [pRes release];
  119.    
  120.     NSRunAlertPanel(@"Report",
  121.             [NSString stringWithFormat:@"Saving %@ completed.\n", [[dlg URL] path]],
  122.             @"OK", NULL, NULL);
  123.    
  124.     CGContextRelease(palBitmap);
  125.     free(pData);
  126. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement