javauis/eswt_akn/org.eclipse.ercp.swt.s60/native/inc/swtimage.h
branchRCL_3
changeset 14 04becd199f91
child 24 6c158198356e
equal deleted inserted replaced
13:f5050f1da672 14:04becd199f91
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2005, 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     3  * All rights reserved. This program and the accompanying materials
       
     4  * are made available under the terms of the Eclipse Public License v1.0
       
     5  * which accompanies this distribution, and is available at
       
     6  * http://www.eclipse.org/legal/epl-v10.html
       
     7  *
       
     8  * Contributors:
       
     9  *     Nokia Corporation - S60 implementation
       
    10  *******************************************************************************/
       
    11 // SwtImage.h
       
    12 
       
    13 
       
    14 #ifndef SWTIMAGE_H
       
    15 #define SWTIMAGE_H
       
    16 
       
    17 
       
    18 #include "eswtgraphics.h"
       
    19 
       
    20 
       
    21 class CSwtGrPaletteData;
       
    22 
       
    23 
       
    24 /**
       
    25  * CSwtImage
       
    26  * C++ implementation of org.eclipse.swt.graphics.Image.
       
    27  * Due to lack of support for paletted modes in CFbsBitmap ( the API exists
       
    28  * but is empty implemented ), colour images of bit depths 1, 2, 4 and 8 are
       
    29  * converted to 24 bits per pixel. This, however, does not affect grayscale
       
    30  * images.
       
    31  * @lib eswt
       
    32  */
       
    33 NONSHARABLE_CLASS(CSwtImage)
       
    34         : public CBase
       
    35         , public MSwtImage
       
    36         , public MSwtResourceChangeObserver
       
    37 {
       
    38 // Data types
       
    39 private:
       
    40     enum TMaskType
       
    41     {
       
    42         ENoMask = 0,    // No transparency
       
    43         EBinaryMask,    // Binary transparency
       
    44         EColorKey,      // Colour key: a colour is considered transparent.
       
    45         EDirtyColorKey, // Same as EColorKey but the mask needs to be regenerated
       
    46         EAlphaMask      // Alpha transparency
       
    47     };
       
    48 
       
    49 // Own methods
       
    50 public:
       
    51     /**
       
    52      * Two phase constructor.
       
    53      * @param aDevice graphic device
       
    54      * @param aSize initial image dimensions
       
    55      * @param aDisplay parent display
       
    56      * @param aMode display mode
       
    57      * @return pointer to blank image
       
    58      */
       
    59     static CSwtImage* NewL(MSwtDevice& aDevice, const TSize& aSize,
       
    60                            MSwtDisplay& aDisplay, TDisplayMode aMode = ENone);
       
    61 
       
    62     /**
       
    63      * Two phase constructor.
       
    64      * @param aDevice graphic device
       
    65      * @param aData image data
       
    66      * @param aDisplay parent display
       
    67      * @return pointer to image constructed from image data
       
    68      */
       
    69     static CSwtImage* NewL(MSwtDevice* aDevice, const MSwtImageData& aData,
       
    70                            MSwtDisplay* aDisplay);
       
    71 
       
    72     /**
       
    73      * Two phase constructor.
       
    74      * @param aBitmap source bitmap
       
    75      * @param aBitmap source mask
       
    76      * @param aTopLeft initial position
       
    77      * @param aDelayTime frame delay
       
    78      * @param aDisposalMethod disposal method of previous frames
       
    79      * @return pointer to image constructed bitmap + mask
       
    80      */
       
    81     static CSwtImage* NewL(CFbsBitmap& aBitmap, CFbsBitmap* aMask,
       
    82                            const TPoint& aTopLeft, TInt aDelayTime, TSwtGifDisposal aDisposalMethod);
       
    83 
       
    84 private:
       
    85     /**
       
    86      * First phase constructor.
       
    87      * @param aDevice graphic device
       
    88      * @param aDisplay parent display
       
    89      */
       
    90     inline CSwtImage(MSwtDevice* aDevice, MSwtDisplay* aDisplay);
       
    91 
       
    92     /**
       
    93      * Destructor
       
    94      */
       
    95     ~CSwtImage();
       
    96 
       
    97     /**
       
    98      * Second phase constructor.
       
    99      * @param aSize initial image dimensions
       
   100      * @param aMode display mode
       
   101      */
       
   102     void ConstructL(const TSize& aSize, TDisplayMode aMode);
       
   103 
       
   104     /**
       
   105      * Second phase constructor.
       
   106      * @param aData image data
       
   107      */
       
   108     void ConstructL(const MSwtImageData& aData);
       
   109 
       
   110     /**
       
   111      * Second phase constructor.
       
   112      * @param aBitmap source bitmap
       
   113      * @param aBitmap source mask
       
   114      * @param aTopLeft initial position
       
   115      * @param aDelayTime frame delay
       
   116      * @param aDisposalMethod disposal method of previous frames
       
   117      */
       
   118     void ConstructL(CFbsBitmap& aBitmap, CFbsBitmap* aMask, const TPoint& aTopLeft,
       
   119                     TInt aDelayTime, TSwtGifDisposal aDisposalMethod);
       
   120 
       
   121     /**
       
   122      * Color key setter.
       
   123      * @param aData image data
       
   124      */
       
   125     void SetColorKeyL(const MSwtImageData& aData);
       
   126 
       
   127     /**
       
   128      * Ensures the mask is up to date. This method, although const, may actually
       
   129      * modify the image's mask but this will only occur if the image's content
       
   130      * has been modified and the mask wasn't updated at that moment.
       
   131      */
       
   132     void EnsureMaskIsUpToDate() const;
       
   133 
       
   134     /**
       
   135      * Computes the binary mask from the colour key.
       
   136      */
       
   137     void ComputeColorKeyMask();
       
   138 
       
   139     /**
       
   140      * Tests whether the image is a grayscale one.
       
   141      */
       
   142     TBool IsGrayscale() const;
       
   143 
       
   144     /**
       
   145      * Creates a new palette data object for the image.
       
   146      */
       
   147     CSwtGrPaletteData* GetPaletteDataL() const;
       
   148 
       
   149     /**
       
   150      * Computes the gray mode for the provided palette.
       
   151      * @return The gray mode suitable for the current palette or ENone if none
       
   152      *         has been found.
       
   153      */
       
   154     static TDisplayMode PaletteGrayMode(TInt aDepth, const CPalette& aPalette);
       
   155 
       
   156     /**
       
   157      * Computes the display mode of the provided data.
       
   158      * @return The display mode, or ENone if no suitable mode could be found.
       
   159      */
       
   160     static TDisplayMode DisplayMode(const MSwtImageData& aData);
       
   161 
       
   162     /**
       
   163      * Returns the bit depth for the specified display mode.
       
   164      */
       
   165     static TInt BitDepth(TDisplayMode aMode);
       
   166 
       
   167     /**
       
   168      * Returns the image depth bpp according to the bitmap display type.
       
   169      */
       
   170     TInt ImageDepth() const;
       
   171 
       
   172     /**
       
   173      * Returns the scanline padding for the specified bit depth.
       
   174      */
       
   175     static TInt ScanlinePadding(TInt aBitDepth);
       
   176 
       
   177     /**
       
   178      * Computes the number of bytes needed in a line.
       
   179      */
       
   180     static TInt BytesPerLine(TInt aPixelsPerLine, TInt aBitsPerPixel,
       
   181                              TInt aPadding);
       
   182 
       
   183     /**
       
   184      * Fills the provided bitmap with the provided colour.
       
   185      */
       
   186     static void FillBitmapL(CFbsBitmap& aBitmap, const TRgb& aColor,
       
   187                             const TRect& aRect);
       
   188 
       
   189     /**
       
   190      * Bitblt the bitmap with another bitmap.
       
   191      */
       
   192     void BitBltBitmapL(CFbsBitmapDevice& aDevice, const CFbsBitmap& aBitmap);
       
   193 
       
   194 
       
   195     /**
       
   196      * Fills the provided bitmap with the provided colour.
       
   197      */
       
   198     static void FillBitmapL(CFbsBitmapDevice& aDevice, const TRgb& aColor,
       
   199                             const TRect& aRect);
       
   200 
       
   201     /**
       
   202      * Creates a new palette object for a given gray mode.
       
   203      */
       
   204     static CPalette* NewGrayPaletteL(TDisplayMode aMode);
       
   205 
       
   206     /**
       
   207      * Shifts an integer by a certain number of bits.
       
   208      * @param aValue The integer whose bits are to be shifted
       
   209      * @param aShift The number of bits to shift by; a positive value shifts
       
   210      *               to the left, a negative value shifts to the right.
       
   211      */
       
   212     static inline TUint BitShift(TUint aValue, TInt aShift);
       
   213 
       
   214     /**
       
   215      * Extracts the red component of a pixel. The result is a value in [0;255].
       
   216      */
       
   217     static inline TUint GetRed(TUint aPixel,
       
   218                                const MSwtPaletteData::TDirectData& aPalette);
       
   219 
       
   220     /**
       
   221      * Extracts the green component of a pixel. The result is a value in [0;255].
       
   222      */
       
   223     static inline TUint GetGreen(TUint aPixel,
       
   224                                  const MSwtPaletteData::TDirectData& aPalette);
       
   225 
       
   226     /**
       
   227      * Extracts the blue component of a pixel. The result is a value in [0;255].
       
   228      */
       
   229     static inline TUint GetBlue(TUint aPixel,
       
   230                                 const MSwtPaletteData::TDirectData& aPalette);
       
   231 
       
   232     /**
       
   233      * Extracts the RGB components of a pixel.
       
   234      */
       
   235     static inline TRgb GetRgb(TUint aPixel,
       
   236                               const MSwtPaletteData::TDirectData& aPalette);
       
   237 
       
   238     /**
       
   239      * Writes a 16-bit pixel in the Symbian format ( [rrrrrggg gggbbbbb] binary ).
       
   240      * @param aPtr     Where to write the pixel, the pointer is modified to point
       
   241      *                 after the pixel upon return.
       
   242      * @param aPixel   The pixel to write
       
   243      * @param aPalette Information about aPixel's format.
       
   244      */
       
   245     static inline void WritePixel16(TUint8*& aPtr, TUint aPixel,
       
   246                                     const MSwtPaletteData::TDirectData& aPalette);
       
   247 
       
   248     /**
       
   249      * Writes a True Color pixel in the Symbian format ( BGR ).
       
   250      * @param aPtr     Where to write the pixel, the pointer is modified to point
       
   251      *                 after the pixel upon return.
       
   252      * @param aPixel   The pixel to write
       
   253      * @param aPalette Information about aPixel's format.
       
   254      * @param a24Bit   Do the write in 24- or 32-bit format
       
   255      */
       
   256     static inline void WritePixelTrueColor(TUint8*& aPtr, TUint aPixel,
       
   257                                            const MSwtPaletteData::TDirectData& aPalette, TBool a24Bit);
       
   258 
       
   259     /**
       
   260      * Writes a True Color pixel in the Symbian format ( BGR ).
       
   261      * @param aPtr   Where to write the pixel, the pointer is modified to point
       
   262      *               after the pixel upon return.
       
   263      * @param aColor The colour to write at the specified location.
       
   264      */
       
   265     static inline void WritePixelTrueColor(TUint8*& aPtr, const TRgb& aColor,
       
   266                                            TBool a24Bit);
       
   267 
       
   268     /**
       
   269      * Copies data from a buffer of bytes to a bitmap.
       
   270      */
       
   271     static void CopyData(const CFbsBitmap& aBitmap, const TDesC8& aData,
       
   272                          TInt aLineCount, TInt aDepth, TInt aBytesPerLine,
       
   273                          const MSwtPaletteData* aPalette);
       
   274 
       
   275     /**
       
   276      * Add mask bitmap's alpha channel.
       
   277      */
       
   278     void AddMaskToAlphaChannel(const CFbsBitmap& aBitmap, const CFbsBitmap& aMask);
       
   279 
       
   280     /**
       
   281      * Copies data between two buffers arranged in units of 1 bit.
       
   282      * The bits of each byte are reversed on the fly.
       
   283      */
       
   284     static void Copy1(TUint8* aDest, TInt aBytesPerDestLine,
       
   285                       const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount);
       
   286 
       
   287     /**
       
   288      * Copies data between two buffers arranged in units of 4 bits.
       
   289      * The four quarters of each byte are reversed on the fly.
       
   290      */
       
   291     static void Copy2(TUint8* aDest, TInt aBytesPerDestLine,
       
   292                       const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount);
       
   293 
       
   294     /**
       
   295      * Copies data between two buffers arranged in units of 4 bits.
       
   296      * The two halves of each byte are swapped on the fly.
       
   297      */
       
   298     static void Copy4(TUint8* aDest, TInt aBytesPerDestLine,
       
   299                       const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount);
       
   300 
       
   301     /**
       
   302      * Copies data between two buffers arranged in units of 8 bits.
       
   303      */
       
   304     static void Copy8(TUint8* aDest, TInt aBytesPerDestLine,
       
   305                       const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount);
       
   306 
       
   307     /**
       
   308      * Copies data between two buffers arranged in units of 16 bits.
       
   309      * The first and last bytes of each unit are swapped on the fly to account
       
   310      * for the fact that Symbian's and SWT's red and blue channels are inverted.
       
   311      */
       
   312     static void Copy16(TUint8* aDest, TInt aBytesPerDestLine,
       
   313                        const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   314                        const MSwtPaletteData::TDirectData& aPalette);
       
   315 
       
   316     /**
       
   317      * Copies data from a buffer of 1-bit paletted pixels to a buffer of
       
   318      * 24/32-bit pixels.
       
   319      */
       
   320     static void Copy1ToTrueColor(TUint8* aDest, TInt aBytesPerDestLine,
       
   321                                  const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   322                                  const CPalette& aPalette, TBool a24Bit);
       
   323 
       
   324     /**
       
   325      * Copies data from a buffer of 2-bit paletted pixels to a buffer of
       
   326      * 24/32-bit pixels.
       
   327      */
       
   328     static void Copy2ToTrueColor(TUint8* aDest, TInt aBytesPerDestLine,
       
   329                                  const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   330                                  const CPalette& aPalette, TBool a24Bit);
       
   331 
       
   332     /**
       
   333      * Copies data from a buffer of 4-bit paletted pixels to a buffer of
       
   334      * 24/32-bit pixels
       
   335      */
       
   336     static void Copy4ToTrueColor(TUint8* aDest, TInt aBytesPerDestLine,
       
   337                                  const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   338                                  const CPalette& aPalette, TBool a24Bit);
       
   339 
       
   340     /**
       
   341      * Copies data from a buffer of 8-bit paletted pixels to a buffer of
       
   342      * 24/32-bit pixels
       
   343      */
       
   344     static void Copy8ToTrueColor(TUint8* aDest, TInt aBytesPerDestLine,
       
   345                                  const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   346                                  const CPalette& aPalette, TBool a24Bit);
       
   347 
       
   348     /**
       
   349      * Copies data from a buffer of 8-bit pixels to a buffer of 24/32-bit pixels.
       
   350      */
       
   351     static void Copy8ToTrueColor(TUint8* aDest, TInt aBytesPerDestLine,
       
   352                                  const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   353                                  const MSwtPaletteData::TDirectData& aPalette, TBool a24Bit);
       
   354 
       
   355     /**
       
   356      * Copies data between two buffers arranged in units of 24 bits.
       
   357      */
       
   358     static void Copy24(TUint8* aDest, TInt aBytesPerDestLine,
       
   359                        const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   360                        const MSwtPaletteData::TDirectData& aPalette);
       
   361 
       
   362     /**
       
   363      * Copies data between two buffers arranged in units of 24 bits.
       
   364      */
       
   365     static void Copy32(TUint8* aDest, TInt aBytesPerDestLine,
       
   366                        const TUint8* aSrce, TInt aBytesPerSrceLine, TInt aLineCount,
       
   367                        const MSwtPaletteData::TDirectData& aPalette);
       
   368 
       
   369     /**
       
   370      * Returns a newly allocated buffer to the data of the specified bitmap.
       
   371      */
       
   372     static HBufC8* GetPixelDataLC(const CFbsBitmap& aBitmap);
       
   373 
       
   374     /**
       
   375      * ?
       
   376      */
       
   377     static void GetPixels16(TUint8* aDest, TInt aBytesPerDestLine,
       
   378                             const TUint8* aSrce, TInt aBytesPerSrceLine, const TSize& aSize);
       
   379 
       
   380 // From MSwtBitmapDrawable
       
   381 public:
       
   382     MSwtGc* NewGcL();
       
   383     CBitmapDevice& GraphicsDevice();
       
   384     void HandleUpdate();
       
   385 private:
       
   386     static void DestroyNativeGc(CBitmapContext* aGc);
       
   387 
       
   388 // From MSwtImage
       
   389 protected:
       
   390     CFbsBitmap& GetBitmap() const;
       
   391     CFbsBitmap& GetSubBitmap(const TSize& aSize) const;
       
   392 
       
   393 // From MSwtImage
       
   394 public:
       
   395     void Dispose();
       
   396     const CFbsBitmap* MaskBitmap(TBool aInvertedIfMonochrome = EFalse) const;
       
   397     const CFbsBitmap* SubMaskBitmap(const TSize& aSize, TBool aInvertedIfMonochrome = EFalse) const;
       
   398     TRect GetBounds() const;
       
   399     MSwtImageData* GetImageDataL() const;
       
   400     TInt AddSubRef(const TSize& aSize) const;
       
   401     TInt RemoveSubRef(const TSize& aSize) const;
       
   402     TInt SubRefCount(const TSize& aSize) const;
       
   403     CFbsBitmap* BitmapWithAlphaLC();
       
   404 
       
   405 // From MSwtResourceChangeObserver
       
   406 public:
       
   407     void OfferResourceChangeL(TInt aType);
       
   408 
       
   409 // Data
       
   410 private:
       
   411     /**
       
   412      * The device on which the image is created, may be NULL.
       
   413      * Not own.
       
   414      */
       
   415     MSwtDevice* iDevice;
       
   416 
       
   417     /**
       
   418      * Colour data, may be owned, cannot be NULL.
       
   419      * May be own or not own.
       
   420      */
       
   421     CFbsBitmap* iBitmap;
       
   422 
       
   423     /**
       
   424      * Device for drawing onto the bitmap, cannot be NULL.
       
   425      * Own.
       
   426      */
       
   427     CFbsBitmapDevice* iBitmapDevice;
       
   428 
       
   429     /**
       
   430      * The type of mask we're dealing with
       
   431      */
       
   432     TMaskType iMaskType;
       
   433 
       
   434     /**
       
   435      * Transparency mask, binary ( 1 bit ) or alpha ( 8 bits ), may be NULL.
       
   436      * May be own or not own.
       
   437      */
       
   438     CFbsBitmap* iMask;
       
   439 
       
   440     /**
       
   441      * Transparency mask inverted, binary ( 1 bit ) or alpha ( 8 bits ), may be NULL.
       
   442      * Created only upon request.
       
   443      * Own.
       
   444      */
       
   445     mutable CFbsBitmap* iMaskInverted;
       
   446 
       
   447     /**
       
   448      * The transparent colour, may be NULL.
       
   449      * Own.
       
   450      */
       
   451     TRgb* iColorKey;
       
   452 
       
   453     /**
       
   454      * The coordinates the bitmap's origin is at within an animation.
       
   455      */
       
   456     TPoint iOffset;
       
   457 
       
   458     /**
       
   459      * The original image information
       
   460      */
       
   461     MSwtImageData::TInfo iInfo;
       
   462 
       
   463     /**
       
   464      * The iBitmap and iMask object have been created outside of the object.
       
   465      * Do not delete them in the destructor.
       
   466      */
       
   467     TBool iOwnExternally;
       
   468 
       
   469     /**
       
   470      * The Display the image belongs to, may be NULL.
       
   471      * Not own.
       
   472      */
       
   473     MSwtDisplay* iDisplay;
       
   474 
       
   475     /**
       
   476      * Array holding the reference count for scaled copies of the main bitmap.
       
   477      */
       
   478     mutable RArray<TInt> iScaledBitmapRefs;
       
   479 
       
   480     /**
       
   481      * Array holding the scaled copies of the main bitmap.
       
   482      */
       
   483     mutable RPointerArray<CFbsBitmap> iScaledBitmaps;
       
   484 
       
   485     /**
       
   486      * Array holding the scaled copies of the main mask.
       
   487      */
       
   488     mutable RPointerArray<CFbsBitmap> iScaledMasks;
       
   489 
       
   490     /**
       
   491      * Array holding the scaled inverted copies of the main mask.
       
   492      * Entries valid only if mask is monochrome, otherwise NULL.
       
   493      */
       
   494     mutable RPointerArray<CFbsBitmap> iScaledMasksInverted;
       
   495 };
       
   496 
       
   497 
       
   498 #endif // SWTIMAGE_H