I am trying to get a custom image loaded as a depth map for an image.
I thought I could load an regular image from a url as a CIImage and then load the depth image as a separate CIImage. Then all I need to do was use CIContext to write the combined image (based on the WWDC session "Image Editing with Depth").
CIImage *theSourceImage = [CIImage imageWithContentsOfURL:theImageUrl];
CIImage *theDepthImage = [CIImage imageWithContentsOfURL:theDepthUrl ];
NSURL *theOutputUrl = [self getOutputURL];
CIContext *theCIContext = [CIContext context];
NSError* theWriteError;
BOOL theResult = [theCIContext writeJPEGRepresentationOfImage:theImageWithoutDepth
toURL:theOutputUrl
colorSpace:CGColorSpaceCreateDeviceRGB()
options:@{kCIImageRepresentationDisparityImage: theDepthOnly,
}
error: &theWriteError];
However when I do this it write the image but also logs a warning in the XCode console:
addAuxiliaryDataInfo:2720: *** 'auxiliaryDataInfoDictionary' is missing 'kCGImageAuxiliaryDataInfoMetadata' key
I think this indicates that my image that I am trying to use as a disparity image is not formatted correctly but I am uncertain of how to convert it into the proper structure so that I can combine them together.
If instead I take an image with depth data in it already I can break it into two pieces and then write a new file to combine them back together and it seems to work fine (no error and the photo is recognized as having depth data in it):
CIImage *theDepthOnly = [CIImage imageWithContentsOfURL: theImageWithDepthUrl options: @{ kCIImageAuxiliaryDepth: @YES } ];
CIImage *theImageWithoutDepth = [CIImage imageWithContentsOfURL: theImageWithDepthUrl options: @{ kCIImageAuxiliaryDepth: @NO } ];
NSURL *theOutputUrl = [self getOutputURL];
CIContext *theCIContext = [CIContext context];
NSError* theWriteError;
BOOL theResult = [theCIContext writeJPEGRepresentationOfImage:theImageWithoutDepth
toURL:theOutputUrl
colorSpace:CGColorSpaceCreateDeviceRGB()
options:@{kCIImageRepresentationDisparityImage: theDepthOnly,
}
error: &theWriteError];
I noticed that the depth image extracted from the image that already has depth has a different color space:
Printing description of theDepthOnly in debugging console:
affine [1 0 0 -1 0 576] extent=[0 0 768 576] opaque
colormatch "Linear Gray"_to_workingspace extent=[0 0 768 576] opaque
provider 7:initWithCVImage Lh alpha_one extent=[0 0 768 576] opaque
where as the initial custom depth image I was trying to use has:
Printing description of theDepthImage in debugging console:
affine [1 0 0 -1 0 722] extent=[0 0 1280 722] opaque
colormatch "Generic Gray Gamma 2.2 Profile"_to_workingspace extent=[0 0 1280 722] opaque
IOSurface 0x600000a00b60(2) seed:0 L8 alpha_one extent=[0 0 1280 722] opaque
Any guidance or advice would be greatly appreciated.