mobilemessaging/unieditor/utils/inc/UniImageProcessor.h
changeset 79 2981cb3aa489
parent 0 72b543305e3a
equal deleted inserted replaced
25:84d9eb65b26f 79:2981cb3aa489
       
     1 /*
       
     2 * Copyright (c) 2006-2007 Nokia Corporation and/or its subsidiary(-ies).
       
     3 * All rights reserved.
       
     4 * This component and the accompanying materials are made available
       
     5 * under the terms of "Eclipse Public License v1.0"
       
     6 * which accompanies this distribution, and is available
       
     7 * at the URL "http://www.eclipse.org/legal/epl-v10.html".
       
     8 *
       
     9 * Initial Contributors:
       
    10 * Nokia Corporation - initial contribution.
       
    11 *
       
    12 * Contributors:
       
    13 *
       
    14 * Description:   Unified Message Editor image scaler - view independent component for
       
    15 *                scaling and saving images
       
    16 *
       
    17 */
       
    18 
       
    19 
       
    20 
       
    21 #ifndef C_UNIIMAGEPROCESSOR_H
       
    22 #define C_UNIIMAGEPROCESSOR_H
       
    23 
       
    24 // INCLUDES
       
    25 #include <e32base.h>
       
    26 
       
    27 // CONSTANTS
       
    28 
       
    29 // MACROS
       
    30 
       
    31 // DATA TYPES
       
    32 
       
    33 // FUNCTION PROTOTYPES
       
    34 
       
    35 // FORWARD DECLARATIONS
       
    36 
       
    37 class CImageDecoder;
       
    38 class CImageEncoder;
       
    39 class CBitmapScaler;
       
    40 class CFrameImageData;
       
    41 class TJpegImageData;
       
    42 class CFbsBitmap;
       
    43 class RFile;
       
    44 
       
    45 // CLASS DECLARATION
       
    46 
       
    47 // ==========================================================
       
    48 
       
    49 /**
       
    50 * Callback mixin class used by CUniImageProcessor
       
    51 *
       
    52 * @since 3.2
       
    53 */
       
    54 class MUniImageProcessorCallback
       
    55     {
       
    56 
       
    57     public:
       
    58 
       
    59     /**
       
    60     * Callback function called when image processing is ready.
       
    61     * If an error occured during scaling/compressing it can
       
    62     * be queried with CUniImageProcessor::Error() function.
       
    63     *
       
    64     * @since 3.2
       
    65     *
       
    66     * @param aBitmapSize    IN Size (dimensions) of target bitmap
       
    67     * @param aFileSize      IN Size (bytes) of target file
       
    68     *                          0, if image was not encoded into a file
       
    69     * @param aCompresseed   IN ETrue if image was compressed
       
    70     */
       
    71     virtual void ImageProcessingReady( TSize aBitmapSize, TInt aFileSize, TBool aCompressed ) = 0;
       
    72     };
       
    73 
       
    74 
       
    75 /**
       
    76 * Common utility class for scaling images in e.g. UniEditor,
       
    77 * SMIL Editor and PostCard Editor.
       
    78 *
       
    79 * @lib UniUtils.lib
       
    80 *
       
    81 * @since 3.2
       
    82 */
       
    83 NONSHARABLE_CLASS( CUniImageProcessor ) : public CActive
       
    84     {
       
    85     public:  // Constructor and destructor
       
    86          
       
    87         /**
       
    88         * C++ constructor.
       
    89         *
       
    90         * @since 3.2
       
    91         *
       
    92         * @param aCallback, call back implementation
       
    93         */
       
    94         IMPORT_C CUniImageProcessor( MUniImageProcessorCallback* aCallback );
       
    95 
       
    96         /**
       
    97         * Destructor.
       
    98         *
       
    99         * @since 3.2
       
   100         */
       
   101         virtual ~CUniImageProcessor();
       
   102 
       
   103     public:
       
   104 
       
   105         /**
       
   106         * Scale and optionally compress image from file to file.
       
   107         * Compression possible only for target type "image/jpeg".
       
   108         *
       
   109         * @since 3.2
       
   110         *
       
   111         * @param aSourceFile, full path for the source file.
       
   112         * @param aDestFile, full path for the destination file.
       
   113         * @param IN/OUT aTargetSize, (max) size for the target bitmap.
       
   114         *                            MAY change if aspect ratio is maintained.
       
   115         *                            MAY change if only decode plane scaling is used.
       
   116         * @param aTargetType, MIME type of target image
       
   117         * @param aAspectRatio, ETrue if want to maintain aspect ratio
       
   118         * @param aTargetFileSize, If set and "aTargetType" is "image/jpeg" tries
       
   119         *                         to compress the bitmap to given value. 
       
   120         *                         If "aTargetSize" is smaller than the original
       
   121         *                         size scaling is performed first.
       
   122         *                         If zero or "aTargetType" is not "image/jpeg"
       
   123         *                         no compression is performed.
       
   124         */
       
   125         IMPORT_C void ProcessImageL(
       
   126             RFile& aSourceFile,
       
   127             RFile& aDestFile,
       
   128             TSize& aTargetSize,
       
   129             const TDesC8& aTargetType,
       
   130             TBool aAspectRatio = ETrue,
       
   131             TInt aTargetFileSize = 0 );
       
   132 
       
   133         /**
       
   134         * Scale image from bitmap to file.
       
   135         *
       
   136         * @since 3.2
       
   137         *
       
   138         * @param aSourceBitmap, source bitmap
       
   139         * @param aDestFile, full path for the destination file.
       
   140         * @param aTargetSize, (max) size for the target bitmap.
       
   141         *                     (If aspect ratio is maintained either
       
   142         *                     width or height might be smaller.)
       
   143         * @param aTargetType, MIME type of target image
       
   144         * @param aAspectRatio, ETrue if want to maintain aspect ratio
       
   145         */
       
   146         IMPORT_C void ScaleImageL(
       
   147             CFbsBitmap* aSourceBitmap,
       
   148             RFile& aDestFile,
       
   149             const TSize& aTargetSize,
       
   150             const TDesC8& aTargetType,
       
   151             TBool aAspectRatio = ETrue );
       
   152 
       
   153         /**
       
   154         * Scale image from bitmap to bitmap.
       
   155         *
       
   156         * @since 3.2
       
   157         *
       
   158         * @param IN aSourceBitmap, source bitmap
       
   159         * @param IN/OUT aDestBitmap, target bitmap, must be initialized.
       
   160         * @param IN aAspectRatio, ETrue if want to maintain aspect ratio
       
   161         */
       
   162         IMPORT_C void ScaleImageL(
       
   163             CFbsBitmap* aSourceBitmap,
       
   164             CFbsBitmap* aDestBitmap,
       
   165             TBool aAspectRatio = ETrue );
       
   166 
       
   167         /**
       
   168         * Scale image from file to bitmap.
       
   169         *
       
   170         * @since 3.2
       
   171         *
       
   172         * @param IN aSourceFile, full path for the source file.
       
   173         * @param IN/OUT aDestBitmap, pointer reference, target bitmap created
       
   174         *                            by CUniImageProcessor.
       
   175         * @param IN/OUT aDestMask, pointer reference, target bitmap mask
       
   176         *                          created by CUniImageProcessor if applicable.
       
   177         *                          NULL if not created.
       
   178         * @param IN/OUT aTargetSize, (max) size for the target bitmap.
       
   179         *                            MAY change if aspect ratio is maintained.
       
   180         *                            MAY change if only decode plane scaling is used.
       
   181         * @param IN aAspectRatio, ETrue if want to maintain aspect ratio
       
   182         */
       
   183         IMPORT_C void ScaleImageL(
       
   184             RFile& aSourceFile,
       
   185             CFbsBitmap*& aDestBitmap,
       
   186             CFbsBitmap*& aDestMask,
       
   187             TSize& aTargetSize,
       
   188             TBool aAspectRatio = ETrue );
       
   189 
       
   190         /**
       
   191         * Reset (the internal state of) the image scaler.
       
   192         *
       
   193         * Call this when you know you don't need to access
       
   194         * the data of the image scaler anymore. Typically
       
   195         * this can be called inside ScalingReady event.
       
   196         *
       
   197         * This must be called before reusing the image scaler.
       
   198         *
       
   199         * @since 3.2
       
   200         */
       
   201         IMPORT_C void Reset();
       
   202 
       
   203         /**
       
   204         * Error
       
   205         *
       
   206         * @since 3.2
       
   207         *
       
   208         * @return standard Symbian error code
       
   209         */
       
   210         inline TInt Error() const;
       
   211         
       
   212         /**
       
   213         * SetOnlyDecodePlaneScaling
       
   214         *
       
   215         * With this method it is possible to control whether the 
       
   216         * scaler uses only decode plane scaling or both decode
       
   217         * and bitmap plane scaling.
       
   218         * 
       
   219         * If only decode plane scaling is used the resulting image MAY
       
   220         * be smaller than target size IF the used codec supports decode
       
   221         * time scaling only in powers of two. Using only decode plane
       
   222         * scaling reduces RAM consumption and increases performance
       
   223         * especially with large images and target sizes.
       
   224         *
       
   225         * If both decode and bitmap plane scaling are used the
       
   226         * resulting image will always be scaled to exact target
       
   227         * size (maintaining aspect ratio if requested, however).
       
   228         * With this option bitmap plane scaling MAY be needed
       
   229         * if the image cannot be fully scaled down in decode plane.
       
   230         * This depends on the capabilities of the codec and the
       
   231         * source & target sizes of the image. Bitmap plane scaling
       
   232         * consumes lots of memory with large images.
       
   233         *
       
   234         * @since 3.2
       
   235         *
       
   236         * @param IN aOnlyDecodeTimeScaling
       
   237         *           if ETrue only decode time scaling is used.
       
   238         *           This may result in smaller image than target size
       
   239         *           since some codecs support decode time scaling
       
   240         *              
       
   241         *
       
   242         */
       
   243         inline void SetOnlyDecodePlaneScaling( TBool aOnlyDecodeTimeScaling );
       
   244         
       
   245         /**
       
   246         * SetAllowScalingUp
       
   247         *
       
   248         * @since 3.2
       
   249         *
       
   250         * @param IN aAllowScalingUp
       
   251         *           if ETrue then image is allowed to be scaled up.
       
   252         */
       
   253         inline void SetAllowScalingUp( TBool aAllowScalingUp );
       
   254 
       
   255 
       
   256     protected:
       
   257 
       
   258         /**
       
   259         * From CActive
       
   260         */
       
   261         void DoCancel();
       
   262 
       
   263         /**
       
   264         * From CActive
       
   265         */
       
   266         void RunL();
       
   267         
       
   268         /**
       
   269         * From CActive
       
   270         */
       
   271         TInt RunError( TInt aError );
       
   272 
       
   273         /**
       
   274         * Finds out the optimum decode and target sizes.
       
   275         * If the target size is smaller than 1/2, 1/4 or 1/8
       
   276         * of the original image size we can scale down
       
   277         * the image already at decoding.
       
   278         *
       
   279         * @since 3.2
       
   280         *
       
   281         * @param aTargetSize IN/OUT, target size in pixels
       
   282         *                            MAY change if only decode plane scaling is used.
       
   283         * @param aDecodeSize OUT, optimum size in pixels for which
       
   284         *                         to decode the image
       
   285         * @param aAspectRatio IN, ETrue if want to maintain aspect ratio
       
   286         * @return ETrue, if image can be decoded to target size
       
   287         *                (thus NO bitmap plane scaling needed)
       
   288         *         EFalse, otherwise (bitmap plane scaling IS needed)
       
   289         */
       
   290         TBool CalculateDecodeAndTargetSizes(
       
   291             TSize& aTargetSize,
       
   292             TSize& aDecodeSize,
       
   293             TBool aAspectRatio );
       
   294         
       
   295         /**
       
   296         * Starts image decoding.
       
   297         *
       
   298         * @since 3.2
       
   299         */
       
   300         void DecodeImage();
       
   301 
       
   302         /**
       
   303         * Starts image scaling (on bitmap level)
       
   304         *
       
   305         * @since 3.2
       
   306         *
       
   307         * @param aMask, scale mask if ETrue,
       
   308         *               otherwise scale bitmap
       
   309         */
       
   310         void ScaleImage( TBool aMask );
       
   311 
       
   312         /**
       
   313         * Starts image encoding.
       
   314         *
       
   315         * @since 3.2
       
   316         */
       
   317         void EncodeImageL( TBool aCompressing );
       
   318 
       
   319         /**
       
   320         * Creates image (encoding) data for specified
       
   321         * image type.
       
   322         *
       
   323         * @since 3.2
       
   324         *
       
   325         * @param aTargetType, MIME type of target image
       
   326         * @param aTargetSize, size for the target bitmap.
       
   327         */
       
   328         void CreateImageDataL( const TDesC8& aTargetType, const TSize& aTargetSize );
       
   329     
       
   330         /**
       
   331         * Calculate scaling ratio when aspect ratio is maintained.
       
   332         *
       
   333         * @since 3.2
       
   334         *
       
   335         * @param IN target size
       
   336         * @param IN original size
       
   337         * @return scaling ratio
       
   338         */
       
   339         TReal CalculateScaleRatio( const TSize& aTargetSize, const TSize& aOriginalSize ) const;
       
   340 
       
   341         /**
       
   342         * Tries to solve quality factor of new image based on size, 
       
   343         * dimensions and color depth of the original.
       
   344         *
       
   345         * @since 3.2
       
   346         */
       
   347         void ResolveQFactorL();    
       
   348 
       
   349         /**
       
   350         * Checks result image size against target size
       
   351         *
       
   352         * @since 3.2
       
   353         */
       
   354         TBool CheckEncodedFileSize();
       
   355 
       
   356         /**
       
   357         * Calculates iMPixFactor used for compressing
       
   358         *
       
   359         * @since 5.0
       
   360         */
       
   361         void CalculateMPixfactorL();
       
   362 
       
   363     private: //Data
       
   364     
       
   365         enum
       
   366             {
       
   367             ESourceBitmapOwned      = 0x0001,
       
   368             EDestBitmapOwned        = 0x0002,
       
   369             ESourceMaskOwned        = 0x0004,
       
   370             EDestMaskOwned          = 0x0008,
       
   371             EOnlyDecodeTimeScaling  = 0x0010,
       
   372             ECompressOnly           = 0x0020,
       
   373             EImplicitCompress       = 0x0040,
       
   374             EAllowScalingUp         = 0x0080
       
   375             };
       
   376         
       
   377         MUniImageProcessorCallback* iCallback;
       
   378         
       
   379         // General image processing
       
   380         CImageDecoder* iDecoder;
       
   381         CImageEncoder* iEncoder;
       
   382         CBitmapScaler* iScaler;
       
   383 
       
   384         CFrameImageData* iFrameImageData;
       
   385         TJpegImageData* iJpegImageData;
       
   386 
       
   387         CFbsBitmap* iSourceBitmap;
       
   388         CFbsBitmap* iDestBitmap;
       
   389         CFbsBitmap* iSourceMask;
       
   390         CFbsBitmap* iDestMask;
       
   391         RFile* iDestFile;
       
   392         
       
   393         // Scaling related
       
   394         TBool iAspectRatio;
       
   395         
       
   396         // Compression related
       
   397         TInt iOriginalFileSize;
       
   398         TInt iTargetFileSize;
       
   399         TInt iResultFileSize;
       
   400         TInt iCompressTriesLeft;
       
   401         TInt iQFIteration;
       
   402         
       
   403         // General
       
   404         TInt iFlags;
       
   405         TInt iError;
       
   406     
       
   407         // 1 iteration is calculated for scaling
       
   408         TInt iQFactorCalculated;
       
   409         TInt iMPixFactor;
       
   410         TInt iOriginalQualityFactor;
       
   411     };
       
   412 
       
   413 #include <uniimageprocessor.inl>
       
   414 
       
   415 #endif // C_UNIIMAGEPROCESSOR_H