Skip navigation.
 
mlMore CoreImage / CGImage questions
FROM : Jim Crate
DATE : Mon Nov 19 19:20:31 2007

Thanks for all replies to my previous question.

I've gotten acceptable performance with large images in my program, by 
not using the large images until necessary. 
CGImageSourceCreateThumbnailAtIndex will create thumbnails at any 
size, so a simple thing to do is create a preview reference the size 
of the screen, which means it will always be scaled down to fit the 
view anyway.  However, I now have a different problem.

I left the view as a normal NSImageView, and I create an NSImage with 
an NSCIImageRep.  This works well, performance is great and the image 
displays correctly, although there are memory leaks that have to be 
chased down. Googling has found several people mentioning that using 
that method leaks lots of memory.

I also have to save these files, and since the existing code used an 
NSBitmapImageRep, I decided a category on CIImage might be nice:

-(NSImage *)NSImageWithNSCIImageRep
{
   NSImage *image = [[[NSImage alloc] initWithSize:NSMakeSize([self 
extent].size.width, [self extent].size.height)] autorelease];
   [image addRepresentation:[NSCIImageRep imageRepWithCIImage:self]];
   return image;
}

However, the images wrote out with lines and other artifacts.  I 
thought I had seen someone mention this before, but I cannot find that 
now.  If I change the code that updates the view to create the view's 
NSImage using this method instead of the NSCIImageRep, the image 
displays rendered incorrectly as well, whether from the preview image 
or the full-sized image.

Since my end goal is to save the images using CGImage and Image I/O, I 
set up that code to see if it worked any better.  As a category on 
CIImage:


- (CGImageRef)createCGImage
{
   int width = [self extent].size.width;
   int rows = [self extent].size.height;
   int rowBytes = (width * 4);
   
   CGColorSpaceRef colorSpace = 
CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
   CGBitmapInfo bitmapInfo = kCGImageAlphaPremultipliedLast | 
kCGBitmapByteOrderDefault;
   
   CGContextRef cgContext = CGBitmapContextCreate(NULL, width, rows, 8, 
rowBytes, colorSpace, bitmapInfo);
   
   // get the CIContext and render the CIImage
   CIContext* ciContext = [CIContext contextWithCGContext:cgContext 
options:nil];
   CGImageRef cgImage = [ciContext createCGImage:self fromRect:[self 
extent]];
   
   CGContextRelease(cgContext);
   CGColorSpaceRelease(colorSpace);
   
   return cgImage;
}


I also tried using this to create the CGImage in this method:

   [ciContext drawImage:self atPoint:CGPointZero fromRect: [self extent]];
   CGImageRef cgImage = CGBitmapContextCreateImage(cgContext);

However, the file saved has the same type of artifacts.  The overlaid 
image may not display at all, or the file may have anything from a few 
thin white lines to being completely illegible, like this one:

http://www.quevivadev.com/test.jpg

In addition, while a large image (18000 x 14400) will render using 
NSCIImageRep or (incorrectly) into an NSBitmapImageRep, if I try to 
create a CGImage it will crash deep in the rendering code when calling 
[ciContext createCGImage:self fromRect:[self extent]].  Images up to 
10000 x 8000 render into a CGImage, albeit similar to the test image 
above.

Has anyone seen this kind of problem before, or have any pointers on 
where to continue searching for a solution?

Thanks,

Jim

Related mailsAuthorDate
mlCoreImage problems with very large images Jim Crate Nov 16, 09:07
mlRe: CoreImage problems with very large images Chris Blackburn Nov 16, 15:17
mlRe: CoreImage problems with very large images Raffael Cavallaro Nov 16, 16:18
mlre: CoreImage problems with very large images Scott Squires Nov 16, 17:45
mlRe: CoreImage problems with very large images Jim Crate Nov 16, 18:11
mlRe: CoreImage problems with very large images Ilan Volow Nov 16, 18:17
mlMore CoreImage / CGImage questions Jim Crate Nov 19, 19:20