symba (779893), страница 31
Текст из файла (страница 31)
Most likely, if you really need to use this API youneed knowledge of the underlying MIDI engine and how it operates.If you merely want to play a MIDI file, then usually using CMdaAudioPlayerUtility is fine – it will play the file with default parameters.6Image Conversion Library6.1 IntroductionThe Image Conversion Library (ICL) is very useful for any application thatneeds to do some image manipulation.1 Many applications with a userinterface fall into this category, for example, if they need to perform someapplication-specific animation effects or make use of well-known imageformats such as JPEG, PNG, GIF, etc.The main use cases for the ICL are:• conversion of an image in a well-defined image format into a nativeSymbian OS bitmap2• conversion of a native Symbian OS bitmap into a well-defined imageformat.• image transformations (rotation, scaling, etc.) applied to native Symbian OS bitmaps or directly to an image in a well-defined format.All the other use cases are extensions to these three, with one exception – there are some (fairly small) sets of classes which can only handlenative bitmaps.Despite being called a ‘library’, the ICL is more than that (seeFigure 6.1).
It comprises a diverse set of APIs and is not just a singlelibrary.1 We assume that you have some knowledge of computer graphic principles. We usesome computer graphics terms without explanation of their meaning. For example, we donot define words such as pixel, color depth, bitmap, JPEG, PNG, compressed image, paletteor animated image. Those topics are well covered by other documentation.2 Native bitmaps are represented by the class CFbsBitmap on Symbian OS. See theSymbian Developer Library documentation in your chosen SDK for details.142IMAGE CONVERSION LIBRARYMedia Client ImageImageConversionGIF ScalerCImageWriteCodecCImageEncoderPluginCImageDecoderPluginCImageReadCodecTBitmapUtilImage ConversionCodecsImage TransformBitmapTransformationExif UtilityICL RecognizerFigure 6.1 Structure of the Symbian ICL subsystem (licensee subsystems may have their own variations)The biggest part of the subsystem is the Image Conversion component,3which provides applications with the ability to decode an image from awell-defined format to a native Symbian OS bitmap and vice versa.
Mostof the other components have dependencies on the Image Conversioncomponent, which means that they extend a possible set of use casesprovided by that component.3In this chapter, to avoid any confusion arising because part of the subsystem has thesame name as the whole, we refer to the part of the ICL that actually deals with convertingimages from one format to another as the ‘Image Conversion component’.INTRODUCTION143Other parts of the subsystem include:• Media Client Image: this component contains old (before SymbianOS v7.0s) APIs which are included for source compatibility but whichshould not be used by new applications.• GIF Scaler: this component performs GIF image scaling without theneed to convert a GIF image into a native bitmap.• Image Conversion Codecs: These are the workhorses (plug-ins) whichperform tasks specific to an image format.• Image Processor: This is a type of utility class to help with low-levelimage manipulation and color conversion; it owns a TBitmapUtilobject.• TBitmapUtil: This is a set of utility methods that allow pixel-wisenative bitmap operations.• Bitmap Transformation: This is a set of classes which can do simplebitmap transformations (scaling, rotation, etc.)• Exif Utility: This is a set of helper classes which simplify Exif metadataprocessing.• Image Transform: This API can be used to perform transformations(such as scaling and thumbnail addition or removal) of images in awell-defined format.• Image Display: This can be used by applications for easy display ofanimated image formats.
However, S60 doesn’t include this component so it can’t be used for code that is intended to be portable acrossUI platforms.• ICL Recognizer: This is a set of services which implement MIMEtype and image type recognition for both the ICL and system-wideapplication services.The main purpose of the Image Conversion component is to provideimage conversion services between well-defined formats and native Symbian OS bitmaps (known as encoding and decoding). The API has beendesigned so that it provides a developer with a unified interface capableof handling virtually any image format. The high-level class diagram ofthis API is shown in Figure 6.2.
Although a fair number of classes areshown, there are additional classes that have been omitted for clarity.44This chapter does not describe the plug-in APIs of the ICL. These lie outside the scopeof this book, because they are specifically for use by those writing their own plug-ins foruse by CImageDecoder, CImageEncoder, CImageTransform or CImageDisplay.144«enumeration»ImageConversion::CImageDecoder::TImageTypeIMAGE CONVERSION LIBRARY«enumeration»ImageConversion::CImageDecoder::TOptions«use»ImageConversion::CBufferedImageDecoderImageConversion::CImageTypeDescription«enumeration»ImageConversion::CImageEncoder::TOptionsImageConversion::CImageDecoderImageConversion::CImplementationInformationTypeImageConversion::CImageEncoderImageConversion::CJPEGExifDecoderImageConversion::CFileExtensionMIMETypeImageConversion::CJPEGExifEncoderImageConversion::CIclRecognizerUtilFigure 6.2 Image Conversion component6.2 Decoding ImagesThe key principle behind the design of the Image Decoder component isto allow API users to handle any image supported by the system withouta need to know about the underlying image format.
Another importantthing to note is that this component does not use the client–serverdesign pattern, which is commonly used in Symbian OS. Instead, it isimplemented as a client-side dynamic link library, because this approachinvolves much less runtime overhead, in comparison to the cost of IPCcalls for client–server interaction.DECODING IMAGES145The key player in image decoding is the CImageDecoder class. ThisAPI is an interface to an extendable plug-in-based system. The plug-inmanagement tasks are handled by ECOM.The API can be used in just three simple steps:1.
Create an image decoder object by providing a factory method withimage data.2. Initiate the image-conversion process.3. Wait for a request completion and handle the conversion result.6.2.1 Simple Decoding Example#include <imageconversion.h>// these are class members of CSimpleDecodeHandler// RFs iFs;// CImageDecoder* iDecoder;// CFbsBitmap* iBitmap; // Native Symbian OS bitmap// TRequestStatus iStatus;void CSimpleDecodeHandler::BeginDecodingL(){// we assume that iFs is already initialized, i.e. connection to the// file server has been established, and initialize a decoder object // normally a filename passed in from elsewhere rather than hard// coding a literal value.iDecoder = CImageDecoder::FileNewL(iFs, _LIT("jpeg_image.jpeg"));// create a destination bitmap objectiBitmap = new (ELeave) CFbsBitmap();// initialize bitmap by making its size 20x20 and 24bpp color modeUser::LeaveIfError(iBitmap->Create(TSize(20,20), EColor16M));// kick off decodingiDecoder->Convert(&iStatus, *iBitmap);SetActive();}// This will be called once decoding is completevoid CSimpleDecodeHandler::RunL(){// handle decoding resultif (iStatus.Int() == KErrNone){// do something with converted bitmap}}The good news is that it could be as simple as this; the bad news is thatthis piece of code may not work on some devices.
However, we willcome back to this example later (see Section 6.2.4), as it has been madeas clear as possible by using the simplest API methods.146IMAGE CONVERSION LIBRARYWe passed a TRequestStatus as one of the parameters to theCImageDecoder::Convert() method.
CImageDecoder is notan active object, despite the fact that it has a Cancel() method;instead it is a façade for a set of objects that perform a number of tasksasynchronously.We’ve just decoded a JPEG image into a native bitmap. It is rather asimple piece of code, but it is missing one important step – the properway to create a native bitmap, as we just used a hard-coded size andcolor mode. This is the place where we should take a closer look at theImage Decoder API.6.2.2 Setting up Initial ParametersUsually image decoding is not as simple as we’ve shown, assuming wecare about the end result and performance.
In most cases, compressedimages are encoded using a certain color depth. The destination devicefor our native bitmap has its own capabilities too, such as color depthand resolution.So, in order to achieve the best possible results we have to set up somedecoding options before starting the decoding process.There are three options for loading the right plug-in into memory:• Fully automatic plug-in resolution: Image conversion implicitly usessubcomponents (recognizer and resolver) to detect the given imagetype and loads the best matching plug-in for that image. This is whatwe used in the code above, just by providing the RFs instance andfilename.• Semi-automatic plug-in resolution: The API user requests a specifictype of decoder by supplying either the MIME type or the image typeUID and the best matching plug-in is used to handle this image.