Restoring Pictures Tutorial

Pictures can be restored in two different ways. One way is to restore the picture header and restore the data immediately afterwards. The other way is to restore the picture header and defer the loading of the picture data until a later time.

This second way is commonly used when loading rich text documents that include pictures. Using a deferred loading scheme, pictures that occur towards the end of a document need not be loaded until they are needed to be displayed, thus economizing on memory use.

Restoring the picture header

This fragment assumes that the picture header is stored in the root stream of the store, making it easy to restore:

// The file used in the example
_LIT(KFileName,"C:\\grpict.dat");
    
// Open the direct file store
CDirectFileStore* store = CDirectFileStore::OpenLC(fsSession,KFileName,EFileRead);
    
// Read the header stream
RStoreReadStream stream;
stream.OpenLC(*store,store->Root());
TPictureHeader header;
header.InternalizeL(stream);
// Close store
CleanupStack::PopAndDestroy(2); // stream, store
  1. Open the direct file store containing the picture.

  2. Open the root stream of the store, and InternalizeL the picture header.

  3. Close the stream and store.

Restoring the picture data: an example of a MPictureFactory-derived class

To restore the picture data itself, it must have an associated entry in a picture factory. The picture factory ensures that the picture data in the store is restored to the correct picture type, which is specified by a UID in the picture header. A picture factory can allow a number of different types of picture to be restored, calling the correct constructor for the class indicated by the UID. In the following example code the picture factory allows only pictures of type CSmileyPicture to be restored, and panics if the picture data in the store is of another type.

Restoring the picture

// The file used in the example
_LIT(KFileName,"C:\\grpict.dat");
    
// Open the direct file store
CDirectFileStore* store = CDirectFileStore::OpenLC(fsSession,KFileName,EFileRead);
    
// Read the picture
TExamplePictureFactory factory;
TPictureHeader header;
factory.NewPictureL(header,*store);
iPicture = (CSmileyPicture *) header.iPicture.AsPtr();
    
// Close store
CleanupStack::PopAndDestroy();

Picture factory

class TExamplePictureFactory: public MPictureFactory
    {
public:
    void NewPictureL(TPictureHeader& aHeader,
            const CStreamStore& aDeferredPictureStore) const;
    };
void TExamplePictureFactory::NewPictureL(TPictureHeader& aHeader,
            const CStreamStore& aDeferredPictureStore) const
    {
    if (aHeader.iPictureType == KUidExampleSmileyPicture)
        {
            // Restore new picture from store into 
            // the TSwizzle, which changes from
            // stream id to pointer.
            // Construct CSmileyPicture object and 
            // restore from stream.
        if (aHeader.iPicture.IsId())
            aHeader.iPicture = CSmileyPicture::NewL(aDeferredPictureStore,aHeader.iPicture.AsId());
        }
    else
        {
            // Leave
        User::Leave(KErrNoMemory);
        }
    }

Related concepts