examples/SFExamples/Graphics_Images/src/Gif_AnimationReader.cpp

00001 /*
00002 Copyright (c) 2002-2011 Nokia Corporation and/or its subsidiary(-ies). All rights reserved.
00003 
00004 Redistribution and use in source and binary forms, with or without
00005 modification, are permitted provided that the following conditions are met:
00006 
00007 * Redistributions of source code must retain the above copyright notice, this
00008   list of conditions and the following disclaimer.
00009 * Redistributions in binary form must reproduce the above copyright notice,
00010   this list of conditions and the following disclaimer in the documentation
00011   and/or other materials provided with the distribution.
00012 * Neither the name of Nokia Corporation nor the names of its contributors
00013   may be used to endorse or promote products derived from this software
00014   without specific prior written permission.
00015 
00016 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00017 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00018 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00019 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00020 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00021 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00022 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00023 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00024 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00025 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00026 
00027 Description:  
00028 */ 
00029 
00030 #include "Gif_AnimationReader.h"
00031 
00032 
00033 _LIT8(KtxTypeImageGif_8                 ,"image/gif");
00034 
00035 /*
00036 -----------------------------------------------------------------------------
00037 -----------------------------------------------------------------------------
00038 */ 
00039 CAniFrame_Reader* CAniFrame_Reader::NewL(MAniReaderCallBack& aCallBack,const TDesC& aFileName)
00040         {
00041         CAniFrame_Reader* self = CAniFrame_Reader::NewLC(aCallBack,aFileName);
00042         CleanupStack::Pop( self );
00043         return self;
00044         }
00045 /*
00046 -----------------------------------------------------------------------------
00047 -----------------------------------------------------------------------------
00048 */ 
00049 CAniFrame_Reader* CAniFrame_Reader::NewLC(MAniReaderCallBack& aCallBack,const TDesC& aFileName)
00050         {
00051         CAniFrame_Reader* self = new ( ELeave ) CAniFrame_Reader(aCallBack);
00052         CleanupStack::PushL( self );
00053         self->ConstructL(aFileName);
00054         return self;
00055         }
00056         
00057 /*
00058 -----------------------------------------------------------------------------
00059 -----------------------------------------------------------------------------
00060 */ 
00061 CAniFrame_Reader::CAniFrame_Reader(MAniReaderCallBack& aCallBack)
00062 :CActive(EPriorityNormal),iCallBack(aCallBack),iCurrImg(-1),iCurrCount(-1)
00063     {
00064     }
00065 /*
00066 -----------------------------------------------------------------------------
00067 -----------------------------------------------------------------------------
00068 */ 
00069 CAniFrame_Reader::~CAniFrame_Reader()
00070         {
00071         Cancel();
00072         delete iImageDecoder;
00073         delete iFrameImg;
00074         delete iFrameMsk;
00075         }
00076 
00077 /*
00078 -----------------------------------------------------------------------------
00079 -----------------------------------------------------------------------------
00080 */ 
00081 void CAniFrame_Reader::ConstructL(const TDesC& aFileName)
00082         {
00083         CActiveScheduler::Add(this);// Active objects need to be added to the AO scheduler
00084         
00085         // construct the image decoder, that converts frames to bitmap & mask pairs
00086         iImageDecoder = CImageDecoder::FileNewL(CCoeEnv::Static()->FsSession(),aFileName,KtxTypeImageGif_8);
00087         // get total count of the frames
00088         iCurrCount = iImageDecoder->FrameCount();       
00089         }
00090 /*
00091 -----------------------------------------------------------------------------
00092 -----------------------------------------------------------------------------
00093 */ 
00094 TSize CAniFrame_Reader::AniFrameSizeL(void)
00095         {
00096         TSize ret(0,0);
00097         
00098         if(iCurrCount > 0 && iImageDecoder)
00099                 {       // first frame should be the real animation size...
00100                 ret = iImageDecoder->FrameInfo(0).iOverallSizeInPixels;
00101                 }
00102         
00103         return ret;
00104         }
00105 /*
00106 -----------------------------------------------------------------------------
00107 -----------------------------------------------------------------------------
00108 */ 
00109 void CAniFrame_Reader::NextImageL(void)
00110         {       
00111         if(iCurrCount > 0 && !IsActive())
00112                 {
00113                 iCurrImg++;// add 1 to the index, so we can read the next frame
00114                 if(iCurrImg >= iCurrCount || iCurrImg < 0)
00115                         {// incase the index is non-valid, we set it to zero
00116                          // should only happen after reading the last frame
00117                         iCurrImg = 0;
00118                         }
00119                 
00120                 // delete and costruct new frame bitmap to the right size for next frame
00121                 delete iFrameImg;
00122                 iFrameImg = NULL;
00123                 iFrameImg = new(ELeave)CFbsBitmap();
00124                 iFrameImg->Create(iImageDecoder->FrameInfo(iCurrImg).iOverallSizeInPixels,iImageDecoder->FrameInfo(iCurrImg).iFrameDisplayMode);
00125                         
00126                 // delete and costruct new mask bitmap to the right size for next frame mask
00127                 delete iFrameMsk;
00128                 iFrameMsk = NULL;
00129                 iFrameMsk = new(ELeave)CFbsBitmap();
00130                 iFrameMsk->Create(iImageDecoder->FrameInfo(iCurrImg).iOverallSizeInPixels,EGray2);
00131 
00132                 // start reading the frame to the bitmaps
00133                 iImageDecoder->Convert(&iStatus,*iFrameImg,*iFrameMsk,iCurrImg);
00134                 SetActive();    
00135                 }
00136         }
00137 
00138 
00139 /*
00140 -----------------------------------------------------------------------------
00141 -----------------------------------------------------------------------------
00142 */ 
00143 void CAniFrame_Reader::DoCancel()
00144         {
00145         if(iImageDecoder)
00146                 {   //only AC, thus we need to cancel it here..
00147                 iImageDecoder->Cancel();
00148                 }
00149         }
00150 /*
00151 -----------------------------------------------------------------------------
00152 -----------------------------------------------------------------------------
00153 */ 
00154         
00155 void CAniFrame_Reader::RunL()
00156         {
00157         if(iFrameMsk && iFrameImg)// if both images were constructed
00158                 {
00159                 if(iFrameMsk->Handle() && iFrameImg->Handle())// and they have valid image in them
00160                         {
00161                         TBool reDraw(EFalse);
00162                         // initialize the draw areas to be the whole image 
00163                         TRect  drawArea(TPoint(0,0),iFrameImg->SizeInPixels());
00164                         
00165                         if(TFrameInfo::ERestoreToBackground & iImageDecoder->FrameInfo(iCurrImg).iFlags)
00166                                 {
00167                                 reDraw = ETrue;
00168                                 // Draw whole image again, thus we are not checking the coordinates
00169                                 // usually happens at least for the first frame
00170                                 }
00171                         else
00172                                 {
00173                                 // frame does not draw the whole image area, but some other rectange area in it
00174                                 // thus we need to check it from the frame info.
00175                                 drawArea = iImageDecoder->FrameInfo(iCurrImg).iFrameCoordsInPixels;
00176                                 }
00177                         
00178                         // tell the owner to draw the frame into its own bitmap..
00179                         iCallBack.AppendFrameL(*iFrameImg,*iFrameMsk,drawArea,reDraw);
00180                         }
00181                 }
00182         }
00183 

Generated by  doxygen 1.6.2