NSImageView: display large images

887 Views Asked by At

I am working on an image-browser app for OS X. It is very similar to QuickLook: you pick a folder, the app displays the first image in it, and you can use the arrow keys to quickly move to the next/previous picture.

Now, I'm testing my app with RAW format pictures that are very large: for instance, I have a .cr2 file that is almost 25 MB. NSImageView takes 5 to 10 seconds to load this kind of pictures. I would like the transition from a picture to the next to be as fast as possible. I see 2 options:

1) load the picture incrementally and display it as it is being loaded (either by chunks or by gradually improving resolution). Is it possible to do this? How?

2) scale down pictures so that NSImageView can display them instantly (I don't care too much about resolution). How fast can you scale a picture?

Thanks!

2

There are 2 best solutions below

1
On

You can preload previous and next images while a given image is displayed. You just have to construct a table of 3 NSImage, shift them as needed and load a new one. So that your images were almost all the time already loaded before displaying them. Of course, this will not work if you press keys very fast. You can also preload any number of image if needed. Remember that NSImage has a caching mechanism inside.

1
On

That 5-10 seconds is probably due to converting the RAW data to a format that NSImageView can display. Just reading 25MB from the disk is going to impose a delay, and processing all that data will add more. Whenever possible, you'll want to avoid that conversion process.

CR2 files can contain small JPEG preview images, and it'd surely be a lot faster to read a small thumbnail than to convert a 25MB file. The web page Inside the Canon RAW format version 2 has a lot of information about the structure of CR2 files that should help you find any thumbnail images. Another page, Extracting thumbnails from camera RAW files (.CR2 and .NEF) with PHP, demonstrates how to extract thumbnail images from your CR2 files.

If the thumbnail images included in the file isn't sufficient for your needs, you'll have to either let NSImageView or some other component convert the file for you, or else write your own converter. The CR2 format information from the first link above should help you find the RAW data itself, and you could potentially construct a preview image more quickly than converting the whole thing by reading only every nth line of pixels (and then using only every nth pixel from the lines you do read). See section 4 in the Extracting thumbnails... document for help interpreting the RAW data.