Accessing preview images

Since release 0.18, Exiv2 contains functionality to easily access preview images embedded in the metadata (Exif, IPTC or XMP).

RAW images may contain several (I've seen up to four) preview images in addition to the main image in different sizes and usually in JPEG or TIFF format. They range from tiny thumbnails to full-sized JPEG images.

Applications can use the preview list to select which preview image is suitable and work with a preview of the actual picture without having to extract and potentially decode the large main image.

For examples of how to access the preview images with the Exiv2 command line tool, see Franz Buchinger's blog entry Ultra-fast RAW to JPEG conversion with exiv2.

For programmers, the main API of the new functionality is Exiv2::PreviewManager. The interface is designed to make it transparent for applications where and how exactly the preview images are stored in the metadata. It has little overhead in order to allow fast access to even the tiniest thumbnail.

This code snippet demonstrates how the API to access preview images can be used:

Exiv2::Image::AutoPtr image = Exiv2::ImageFactory::open(filename);
image->readMetadata();

// Get a list of preview images available in the image. The list is sorted
// by the preview image pixel size, starting with the smallest preview.
Exiv2::PreviewManager loader(*image);
Exiv2::PreviewPropertiesList list = loader.getPreviewProperties();

// Some application logic to select one of the previews from the list
Exiv2::PreviewPropertiesList::const_iterator pos = selectPreview(list);

// Get the selected preview image
Exiv2::PreviewImage preview = loader.getPreviewImage(*pos);

// Write the preview image to a file
std::string name = "preview" + preview.extension();
std::cout << "Writing preview" << " " 
          << preview.width() << "x" << preview.height() << " " 
          << "to file" << " " << name << std::endl;
preview.writeFile(name);
Redmine Appliance - Powered by TurnKey Linux