I'm trying to make a scientific program that works with 16-bit tiff directories (of images) but displays the images as 8-bit. I also want to allow for the adjusting of the white and black levels. As of now, I'm reading in the 16-bit tiff, getting out the data, editing it, and then loading it into a new image. However, I can't figure out how to set the IKImageView settings correctly. It displays my 16-bit tiff as two (almost) duplicate versions in the top left quadrant and top right quadrant with black in the bottom quadrants. I suspect this is a problem with the number of bytes per row and bits per component in my ikimageview output, but I can't figure out how to edit the NSDictionary metadata.
Code:
if ([[(NSDictionary *)imageProperties valueForKey:(NSString *)kCGImagePropertyDepth] intValue] == 16) {
CGImageRef imR = CGImageSourceCreateImageAtIndex(imageSource, index, NULL);
CFDataRef pixData = CGDataProviderCopyData(CGImageGetDataProvider(imR));
UInt16 * pixels = (UInt16 *) CFDataGetBytePtr(pixData);
UInt8 * eightPix = (UInt8 *) CFDataGetBytePtr(pixData);
int plen = CFDataGetLength(pixData);
int i;
for (i = 0; i < heightInPixels*widthInPixels; i++) {
eightPix[i] = (UInt8) pixels[i];
}
CGContextRef imcont = CGBitmapContextCreate(pixels, CGImageGetWidth(imR), CGImageGetHeight(imR), CGImageGetBitsPerComponent(imR), CGImageGetBytesPerRow(imR), CGImageGetColorSpace(imR), CGImageGetBitmapInfo(imR));
CGImageRef outR = CGBitmapContextCreateImage(imcont);
CFMutableDictionaryRef outProperties = CFDictionaryCreateMutableCopy(NULL, 0, imageProperties);
// const UInt8 * outVal;
// outVal = 8;
// NSString * outVal = (NSString *) 8;
// CFDictionarySetValue(outProperties, kCGImagePropertyDepth, outVal);
// outVal = (NSString *) CGImageGetWidth(imR);
// CFDictionarySetValue(outProperties, kCGImagePropertyDepth, outVal);
[imageView setImage: outR imageProperties: (NSDictionary *)outProperties];