tags:

views:

231

answers:

4

I am trying to allocate memory NSImage*originalLocationImage;

NSURL *fileURL = [NSURL fileURLWithPath:originalLocation]; //originalLocation is file path in my disk

originalLocationImage = [[NSImage alloc]initByReferencingURL:fileURL];

NSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc]initWithData:[SourceImage TIFFRepresentation]]  //this line fails to allocate memory

and it fails for large Images/

and throws error

malloc: *** mmap(size=268435456) failed (error code=12)
*** error: can't allocate region
*** set a breakpoint in malloc_error_break to debug

for small images it works properly.

any help??

+1  A: 

I'd question why you want to load all of the image into memory in the first place - surely dealing with it like a tile set would be a better option?

You are getting this message because, whilst you might have this much memory free, you don't have this much contiguous memory free

Chaos
after that I want to create preview of that Image so what would be the better way to do that. I tried so many thing but now could not find any way.
ashish
+1  A: 

If all you want is a preview, you could read it line by line, skipping every n lines and for each line, skip every n pixels. You're left with a (way) smaller image that you can pass through more sophisticated scaling algorithms if necessary, or you can just save it as your thumbnail image or whatever.

Blindy
Your proposed solution sounds nice but since the format is likely to be compressed he can't binary skip input - he has to decompress and then skip data out of the decompressed output.
diciu
Yes but you can decompress in chunks, you don't have to read the entire image to do it.
Blindy
+2  A: 

Using libjpeg might be an option, it seems to be able to do an optimized sub-scaling up to 16: http://jpegclub.org/djpeg/.

libjpeg is plain C, so you can call it from Objective-C.

diciu
Ihave problem with this line of code that too with tiff images I tried Images 1 GB size psd that works fineNSBitmapImageRep *sourceRep = [[NSBitmapImageRep alloc]initWithData:[SourceImage TIFFRepresentation]] //this line fails to allocate memoryany suggestion for this how to allocate memory in this case.
ashish
additional error I am getting for tiff images is <Error>: tiff data provider: No space to fetch tag value.
ashish
if the same cline of code will load one GB psd Image then why not tiff?
ashish
You cannot achieve what you want using NSImage. Do not forget that JPEG is a compressed format so a 800 MB JPEG is a representation for a 2-3 Gb file. NSImage will try to decompress the 800 MB JPEG so you can't use it - you have to use something low level such as libjpeg or implement the access yourself as Blidy suggested.
diciu
Although NSImage appears as a single implementation behind the scenes it treats image formats using different code. PSD != JPG != TIFF. There's one implementation per image format even though you don't see that when calling initWithData.
diciu
This brings up an interesting question though. I'm pretty sure I've loaded 800mb+ NASA images in Preview.app, Safari, etc before, so what is Apple using behind the scenes if not NSImage?
Marc Charbonneau
thanks for all your help. I have the trouble for now how should I read it line by line.
ashish
I've seen Preview behaving admirably on large JPEGs too but I wouldn't be surprised is there's private frameworks at play there - there's a number of CGImage calls showing up in "nm" that look like private calls such as CGImageCreateByScaling. If I were to tackle this problem I'd investigate CGImageRef and CGImageSourceCreateThumbnailAtIndex - my hunch is CGImage is better suited for large stuff.
diciu
or is there any other to do this. I have gone through Quicklook framework but there I could not see more useful API to create preview and thumbnail the way I want.
ashish
file size is my concern
ashish
CGImageRef ref = QLThumbnailImageCreate(kCFAllocatorMallocZone, (CFURLRef)fileURL, CGSizeMake(size.width, size.height), (CFDictionaryRef)dict);there is no way to set resolution thr this API
ashish
A: 

If you only want to create a preview of the image, CGImageSource has APIs specifcally for that. They are very first and don't require reading the entire image into memory.

Mike Abdullah
No body till told me about this class. Can I be able to create previews with different sizes as well as resolutions?
ashish