examples/SFExamples/Graphics_Images/src/Image_Reader.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 "Image_Reader.h"
00031 
00032 
00033 
00034 
00035 
00036 /*
00037 -----------------------------------------------------------------------------
00038 -----------------------------------------------------------------------------
00039 */ 
00040 CImage_Reader* CImage_Reader::NewL(MImageReaderCallBack& aCallBack,const TDesC& aFileName)
00041         {
00042         CImage_Reader* self = CImage_Reader::NewLC(aCallBack,aFileName);
00043         CleanupStack::Pop( self );
00044         return self;
00045         }
00046 /*
00047 -----------------------------------------------------------------------------
00048 -----------------------------------------------------------------------------
00049 */ 
00050 CImage_Reader* CImage_Reader::NewLC(MImageReaderCallBack& aCallBack,const TDesC& aFileName)
00051         {
00052         CImage_Reader* self = new ( ELeave ) CImage_Reader(aCallBack);
00053         CleanupStack::PushL( self );
00054         self->ConstructL(aFileName);
00055         return self;
00056         }
00057         
00058 /*
00059 -----------------------------------------------------------------------------
00060 -----------------------------------------------------------------------------
00061 */ 
00062 CImage_Reader::CImage_Reader(MImageReaderCallBack& aCallBack)
00063 :CActive(EPriorityNormal),iCallBack(aCallBack)
00064     {
00065     }
00066 /*
00067 -----------------------------------------------------------------------------
00068 -----------------------------------------------------------------------------
00069 */ 
00070 CImage_Reader::~CImage_Reader()
00071 {
00072         Cancel();
00073         delete iImageDecoder;
00074         delete iFrameImg;
00075         iFsSession.Close();
00076 }
00077 
00078 /*
00079 -----------------------------------------------------------------------------
00080 -----------------------------------------------------------------------------
00081 */ 
00082 void CImage_Reader::ConstructL(const TDesC& aFileName)
00083         {
00084         CActiveScheduler::Add(this);
00085         
00086         User::LeaveIfError(iFsSession.Connect());
00087         
00088         TBuf8<255> imageType;
00089         
00090 //      TInt errVal = GetFileType(aFileName,imageType);// check the file's MIME type
00091         
00092         TInt errVal(KErrNone);
00093         TRAP(errVal,CImageDecoder::GetMimeTypeFileL(iFsSession,aFileName,imageType));
00094         
00095         if(errVal == KErrNone)
00096                 {
00097                 // image decoder user for reading the image file to a bitmap
00098                 iImageDecoder = CImageDecoder::FileNewL(iFsSession,aFileName,imageType);
00099                 if(iImageDecoder->FrameCount() > 0)// check that we have image(s) in the file
00100                         {
00101                         iFrameImg = new(ELeave)CFbsBitmap();
00102                         iFrameImg->Create(iImageDecoder->FrameInfo(0).iOverallSizeInPixels,iImageDecoder->FrameInfo(0).iFrameDisplayMode);
00103                                 
00104                         iImageDecoder->Convert(&iStatus,*iFrameImg,0);
00105                         SetActive();
00106                         }
00107                 else
00108                         {       // indicate that we didn't find any images inside the file
00109                         TRequestStatus* status=&iStatus;
00110                         User::RequestComplete(status, KErrNotFound);
00111                         SetActive();
00112                         }
00113                 }
00114         else // indicate that we had an error
00115                 {
00116                 TRequestStatus* status=&iStatus;
00117                 User::RequestComplete(status, errVal);
00118                 SetActive();
00119                 }
00120         }
00121 
00122 /*
00123 -----------------------------------------------------------------------------
00124 -----------------------------------------------------------------------------
00125 */
00126 TInt CImage_Reader::GetFileType(const TDesC& aFileName, TDes8& aFileType)
00127         {
00128         TInt ret(KErrNone);
00129         TEntry fileEntry;
00130 
00131         // Get entry, fails if the file can not be found or accessed.
00132         ret = iFsSession.Entry(aFileName,fileEntry);
00133         
00134         if(ret == KErrNone)
00135                 {
00136                 TBuf8<255> fileBuffer;
00137                 
00138                 if(!fileEntry.IsDir())
00139                         {
00140                         TInt fileSize = fileEntry.iSize;
00141 
00142                         if(fileSize > 255)
00143                                 {// if file is bigger than 255, we only use first 255 bytes for recognizer
00144                                 fileSize = 255;
00145                                 }
00146                         
00147                         // Read data from the file for recognizing it.
00148                         ret = iFsSession.ReadFileSection(aFileName,0,fileBuffer,fileSize);
00149                         if(ret == KErrNone)
00150                                 {
00151                                 RApaLsSession rSession;
00152                                 ret = rSession.Connect();// open session 
00153                                 if(ret == KErrNone)
00154                                         {       
00155                                         TDataRecognitionResult fileDataType;
00156                                         // Use session to recognize the file's MIME type
00157                                         ret = rSession.RecognizeData(aFileName,fileBuffer,*&fileDataType);
00158                                         
00159                                         aFileType.Copy(fileDataType.iDataType.Des8());
00160                                         
00161                                         rSession.Close();// close session
00162                                         }
00163                                 }
00164                         }       
00165                 }
00166         
00167         return ret;
00168         }
00169 /*
00170 -----------------------------------------------------------------------------
00171 -----------------------------------------------------------------------------
00172 */ 
00173 void CImage_Reader::DoCancel()
00174         {
00175         if(iImageDecoder)
00176                 {       // Called when somebody calls Cancel(), so we need to cancel any conversion
00177                 iImageDecoder->Cancel();
00178                 }
00179         }
00180 /*
00181 -----------------------------------------------------------------------------
00182 -----------------------------------------------------------------------------
00183 */ 
00184         
00185 void CImage_Reader::RunL()
00186         {
00187         // conversion has finished, all we need to do is to infrm the owner..
00188         iCallBack.ImageReadDoneL(iStatus.Int());
00189         }
00190 

Generated by  doxygen 1.6.2