Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -(void)saveAsPAL:(id)sender
- {
- NSSavePanel* dlg = [NSSavePanel savePanel];
- [dlg setCanCreateDirectories:TRUE];
- [dlg setAllowedFileTypes:[NSArray arrayWithObject:@"dpx"]];
- [dlg setShowsHiddenFiles:NO];
- [dlg setTitle:@"Save As DPX File"];
- if ([dlg runModal] != NSOKButton)
- return;
- /*
- * First we should create CGBitmapContext as a layer to draw scaled bitmap,
- * and then take the result as bitmap data,
- * transfer it into 10-Bit RGB format.
- */
- CGColorSpaceRef rgbColorspace = CGColorSpaceCreateDeviceRGB();
- CGContextRef palBitmap = CGBitmapContextCreate(NULL,
- 720, 576,
- 8, 720*4,
- rgbColorspace,
- kCGImageAlphaPremultipliedFirst|kCGBitmapByteOrder32Little);
- CFRelease(rgbColorspace);
- CGFloat scaledHeight = (720*[image size].height)/[image size].width;
- CGFloat yOffset = 0;
- if (scaledHeight < 576)
- yOffset = (576-scaledHeight)/2;
- CGContextSetFillColorWithColor(palBitmap, CGColorCreateGenericRGB(0, 0, 0, 1));
- CGContextFillRect(palBitmap, CGRectMake(0, 0, 720, 576));
- CGContextDrawImage(palBitmap,
- CGRectMake(0, yOffset, 720, scaledHeight),
- [image CGImage]);
- uint32_t* pImgData = (uint32_t*)CGBitmapContextGetData(palBitmap);
- size_t iLen = sizeof(DPXHeader) + sizeof(DPXImageHeader) +
- sizeof(DPXImageOrientation) + sizeof(DPXFilmHeader) +
- sizeof(DPXTVHeader) + 32;
- int bmSize = 720*576*4;
- iLen += bmSize;
- uint8_t* pData = (uint8_t*)malloc(iLen);
- memset(pData, 0xFF, iLen);
- DPXHeader* hdr = (DPXHeader*)pData;
- DPXImageHeader* imgHdr = (DPXImageHeader*)(hdr+1);
- DPXImageOrientation* orient = (DPXImageOrientation*)(imgHdr+1);
- DPXFilmHeader* filmHdr = (DPXFilmHeader*)(orient+1);
- DPXTVHeader* tvHdr = (DPXTVHeader*)(filmHdr+1);
- hdr->magic = SDPX_MAGIC;
- hdr->image_offset = iLen - bmSize;
- strcpy((char*)hdr->version, "v2.0");
- hdr->version[4] = 0;
- hdr->file_size = iLen;
- hdr->ditto_key = 0;
- hdr->generic_hdr_size = sizeof(DPXHeader)+sizeof(DPXImageHeader)+sizeof(DPXImageOrientation);
- hdr->industry_hdr_size = sizeof(DPXFilmHeader) + sizeof(DPXTVHeader);
- hdr->user_data_size = 32;
- hdr->encrypt_key = 0;
- strcpy((char*)hdr->file_name,
- [[[[dlg URL] path] lastPathComponent] UTF8String]);
- NSDate* strNow = [NSDate date];
- NSCalendar* calendar = [NSCalendar currentCalendar];
- NSDateComponents* components =
- [calendar components:NSYearCalendarUnit|NSMonthCalendarUnit|
- NSDayCalendarUnit|NSHourCalendarUnit|
- NSMinuteCalendarUnit|NSSecondCalendarUnit
- fromDate:strNow];
- NSString* strTest = [NSString stringWithFormat:@"%i:%i:%i:%i:%i:%i",
- [components year],
- [components month],
- [components day],
- [components hour],
- [components minute],
- [components second]];
- strcpy((char*)hdr->creation_date, [strTest UTF8String]);
- strcpy((char*)hdr->creator, "Adi");
- strcpy((char*)hdr->project, "Test PAL");
- strcpy((char*)hdr->copyright, "2012, FX. J. Adi Lima");
- imgHdr->orientation = 0;
- imgHdr->number_of_elements = 1;
- imgHdr->img_width = 720;
- imgHdr->img_height = 576;
- imgHdr->img_element[0].data_sign = 0;
- imgHdr->img_element[0].data_offset = hdr->image_offset;
- imgHdr->img_element[0].eo_image_padding = 0;
- imgHdr->img_element[0].eol_padding = 0;
- imgHdr->img_element[0].bit_size = 10;
- strcpy((char*)imgHdr->img_element[0].description, "TESTING");
- imgHdr->img_element[0].descriptor = 50;
- imgHdr->img_element[0].packing = 2; // 10 Bit DPX Method B
- imgHdr->img_element[0].encoding = 0;
- strcpy((char*)filmHdr->format, "PAL Method B");
- filmHdr->framerate = 25;
- filmHdr->shutter_angle = 180.0f;
- //strcpy((char*)tvHdr->timecode, "00:00:00:01");
- tvHdr->timecode.value = 1;
- int nCount = 720*576;
- uint32_t* pDest = (uint32_t*)(pData+hdr->image_offset);
- uint32_t* pSrc = pImgData;
- int i;
- uint32_t nVal = 0;
- uint32_t rgbVal;
- for (i = 0; i < nCount; i++)
- {
- nVal = *pSrc++;
- // Alpha Premultiplied First means ARGB (same as BGRA_Interleaved)
- // Has to be saved the same order, but minus alpha.
- rgbVal = (VALUE_8_TO_10(nVal)) | (VALUE_8_TO_10(nVal>>8)<<10) |
- (VALUE_8_TO_10(nVal>>16)<<20);
- *pDest++ = rgbVal;
- }
- NSData* pRes = [[NSData alloc] initWithBytes:pData length:iLen];
- [pRes writeToURL:[dlg URL] atomically:YES];
- [pRes release];
- NSRunAlertPanel(@"Report",
- [NSString stringWithFormat:@"Saving %@ completed.\n", [[dlg URL] path]],
- @"OK", NULL, NULL);
- CGContextRelease(palBitmap);
- free(pData);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement