AppSrc/TextureLoader.cpp
changeset 3 93fff7023be8
equal deleted inserted replaced
2:e1e28b0273b0 3:93fff7023be8
       
     1 /*
       
     2 * Copyright (c) 2009 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: Juha Kauppinen, Mika Hokkanen
       
    13 * 
       
    14 * Description: Photo Browser
       
    15 *
       
    16 */
       
    17 
       
    18 // INCLUDE FILES
       
    19 #include "TextureLoader.h"
       
    20 #include <GLES\egl.h>
       
    21 #include "ImagicContainerBrowser.h"
       
    22 #include "ImagicViewBrowser.h"
       
    23 #include <hal.h>
       
    24 
       
    25 // INCLUDES
       
    26 #include <coecntrl.h>
       
    27 #include <aknnotewrappers.h>
       
    28 #include "ImagicAppUi.h"
       
    29 
       
    30 /*----------------------------------------------------------------------*/
       
    31 // Constructor and destructor
       
    32 //
       
    33 CTextureLoader::CTextureLoader(CImagicAppUi* aImagicAppUi, CImagicContainerBrowser* aContainer, CImagicViewBrowser* aView, 
       
    34                                RCriticalSection* aDrawLock) :
       
    35         //CActive(CActive::EPriorityStandard),
       
    36         CActive(CActive::EPriorityLow),
       
    37         //CActive(CActive::EPriorityStandard),
       
    38         //CActive(CActive::EPriorityUserInput),
       
    39         //CActive(CActive::EPriorityHigh),
       
    40         iImagicAppUi(aImagicAppUi),
       
    41         iContainer(aContainer),
       
    42         iView(aView),
       
    43         iDrawLock(aDrawLock),
       
    44         iData(NULL)
       
    45         
       
    46     {
       
    47     DP0_IMAGIC(_L("CTextureLoader::CTextureLoader++"));
       
    48     
       
    49     // Add loader to active scheduler
       
    50     CActiveScheduler::Add(this);
       
    51     
       
    52     DP0_IMAGIC(_L("CTextureLoader::CTextureLoader--"));
       
    53     }
       
    54 
       
    55 void CTextureLoader::ConstructL()
       
    56     {
       
    57     DP0_IMAGIC(_L("CTextureLoader::ConstructL++"));
       
    58     
       
    59     // Create small default bitmap
       
    60     iBitmap = new (ELeave) CFbsBitmap();
       
    61     iSmileBitmap = new (ELeave) CFbsBitmap();
       
    62     iZoomBitmap = new (ELeave) CFbsBitmap();
       
    63 #ifdef USE_RGBA
       
    64     iBitmap->Create(TSize(10, 10), EColor16MU);
       
    65 #else
       
    66     iBitmap->Create(TSize(10, 10), EColor16M);
       
    67 #endif
       
    68     
       
    69     // Create bitmap scaler
       
    70     iBitmapScaler = CBitmapScaler::NewL();
       
    71     iBitmapScaler->UseLowMemoryAlgorithm(ETrue);
       
    72     
       
    73     iRGB2BGRDone = EFalse;
       
    74 
       
    75 #ifdef ICONS_ENABLAD
       
    76     LoadIcons();
       
    77 #endif
       
    78     
       
    79     DP0_IMAGIC(_L("CTextureLoader::ConstructL--"));
       
    80     }
       
    81 
       
    82 CTextureLoader::~CTextureLoader()
       
    83     {
       
    84     DP0_IMAGIC(_L("CTextureLoader::~CTextureLoader++"));
       
    85     
       
    86     // Cancel ongoing process
       
    87     Cancel();
       
    88     
       
    89     // Free memory
       
    90     delete iBitmapScaler;
       
    91     delete iBitmap;
       
    92     delete iSmileBitmap;
       
    93     delete iZoomBitmap;
       
    94     
       
    95     iBitmapArray.Close();
       
    96     
       
    97     DP0_IMAGIC(_L("CTextureLoader::~CTextureLoader--"));
       
    98     }
       
    99 
       
   100 void CTextureLoader::GetPngL(TFileName& afilepath, CFbsBitmap* aBitmap) 
       
   101     {
       
   102     DP0_IMAGIC(_L("CTextureLoader::GetPngL++"));
       
   103     
       
   104     CImageDecoder* idecoder = CImageDecoder::FileNewL(CEikonEnv::Static()->FsSession(), 
       
   105             afilepath, _L8("image/png")/*, CImageDecoder::EOptionAlwaysThread*/);
       
   106     
       
   107     TFrameInfo iFrameInfo = idecoder->FrameInfo(0);
       
   108     aBitmap->Create(iFrameInfo.iOverallSizeInPixels, EColor16MA );
       
   109     
       
   110     TRequestStatus aStatus = KRequestPending; 
       
   111     
       
   112     TRAPD(err1, idecoder->Convert( &aStatus, *aBitmap, 0 ));
       
   113     if(err1 == KErrNone)
       
   114         User::WaitForRequest( aStatus );   
       
   115 
       
   116     delete idecoder;
       
   117     
       
   118     DP0_IMAGIC(_L("CTextureLoader::GetPngL--"));
       
   119     }
       
   120 
       
   121 
       
   122 void CTextureLoader::LoadIcons()
       
   123     {
       
   124     TInt err = KErrNone;
       
   125 
       
   126     TFileName filename;
       
   127     filename = KSmileFileName;
       
   128     TRAP(err, GetPngL(filename, iSmileBitmap));
       
   129     if(err == KErrNone)
       
   130         iBitmapArray.Append(iSmileBitmap);
       
   131     
       
   132     err = KErrNone;
       
   133 
       
   134     filename = KZoomFileName;
       
   135     TRAP(err, GetPngL(filename, iZoomBitmap));
       
   136     if(err == KErrNone)
       
   137         iBitmapArray.Append(iZoomBitmap);
       
   138     
       
   139 
       
   140     }
       
   141 
       
   142 
       
   143 /*----------------------------------------------------------------------*/
       
   144 // Returns loader loading status
       
   145 //
       
   146 TBool CTextureLoader::IsActiveAndRunning()
       
   147     {
       
   148     // Make sure that loader is not active
       
   149     if (IsRunning())
       
   150         return ETrue;
       
   151     else
       
   152         return EFalse;
       
   153     }
       
   154     
       
   155 
       
   156 /*----------------------------------------------------------------------*/
       
   157 // Loads picture and stores the OpenGL index into specified variable
       
   158 //
       
   159 void CTextureLoader::LoadL(CImageData* aData, TThumbSize aResolution)
       
   160     {
       
   161     DP1_IMAGIC(_L("CTextureLoader::LoadL++ - resolution: %d"), aResolution);
       
   162 
       
   163     // Make sure that loader is not active
       
   164     if (IsRunning())
       
   165         {
       
   166         DP0_IMAGIC(_L("CTextureLoader::LoadL - already running, KErrInUse"));
       
   167         User::Leave(KErrInUse);
       
   168         }
       
   169     // Check that image is not already loaded
       
   170     else if (aData->iGridData.iGlSuperHQTextIndex && aResolution == EFullSize)
       
   171         {
       
   172         DP0_IMAGIC(_L("CTextureLoader::LoadL - EFullRes KErrAlreadyExists"));
       
   173         iData = NULL;
       
   174         User::Leave(KErrAlreadyExists);
       
   175         }
       
   176     else if (aData->iGridData.iGlHQ512TextIndex && aResolution == ESize512x512)
       
   177         {
       
   178         DP0_IMAGIC(_L("CTextureLoader::LoadL - ESize512x512 KErrAlreadyExists"));
       
   179         iData = NULL;
       
   180         User::Leave(KErrAlreadyExists);
       
   181         }
       
   182     else if (aData->iGridData.iGlLQ128TextIndex && aResolution == ESize128x128)
       
   183         {
       
   184         DP0_IMAGIC(_L("CTextureLoader::LoadL -  ESize128x128 KErrAlreadyExists"));
       
   185         iData = NULL;
       
   186         User::Leave(KErrAlreadyExists);
       
   187         }
       
   188     else if (aData->iGridData.iGlLQ32TextIndex && aResolution == ESize32x32)
       
   189         {
       
   190         DP0_IMAGIC(_L("CTextureLoader::LoadL - ESize32x32 KErrAlreadyExists"));
       
   191         iData = NULL;
       
   192         User::Leave(KErrAlreadyExists);
       
   193         }
       
   194     
       
   195     iContainer->DynamicUnLoading();
       
   196     
       
   197     // Store image data
       
   198     iData = aData;
       
   199     iResolution = aResolution;
       
   200     iHighQuality = EFalse;
       
   201     if(aResolution == EFullSize || aResolution == ESize512x512)
       
   202         {
       
   203         iHighQuality = ETrue;
       
   204         //iContainer->DynamicUnLoading();
       
   205         }
       
   206     
       
   207     // Call engine to load picture
       
   208     TRAPD(err, iView->LoadBitmapsToBrowserL(iData, aResolution));
       
   209     if (err != KErrNone)
       
   210         {
       
   211         //if (err == KErrNotFound || err == KErrPathNotFound)
       
   212             iData->SetImageReady(aResolution, EFalse);
       
   213         iData = NULL;
       
   214         User::Leave(err);
       
   215         }
       
   216 
       
   217     DP0_IMAGIC(_L("CTextureLoader::LoadL--"));
       
   218     }
       
   219 
       
   220 /*----------------------------------------------------------------------*/
       
   221 // Releases 512x512 and Max resolution textures
       
   222 //
       
   223 void CTextureLoader::ReleaseSuperHResTexture(CImageData* aGridData)
       
   224     {
       
   225     DP0_IMAGIC(_L("CTextureLoader::ReleaseHResTextures++"));
       
   226     
       
   227     if(aGridData->iGridData.iGlSuperHQTextIndex != 0)
       
   228         {
       
   229         glDeleteTextures(1, &(aGridData->iGridData.iGlSuperHQTextIndex));
       
   230         aGridData->iGridData.iGlSuperHQTextIndex=0;
       
   231         DP0_IMAGIC(_L("CTextureLoader::ReleaseHResTextures - glSuperHQTextIndex released"));
       
   232         }
       
   233     
       
   234     DP0_IMAGIC(_L("CTextureLoader::ReleaseHResTextures--"));
       
   235     }
       
   236 
       
   237 /*----------------------------------------------------------------------*/
       
   238 // Unloads picture from specified index
       
   239 //
       
   240 void CTextureLoader::ReleaseHQ512Textures()
       
   241     {
       
   242     DP0_IMAGIC(_L("CTextureLoader::ReleaseQ512Textures++"));
       
   243     
       
   244     TInt mem = 0;
       
   245     TInt ret = HAL::Get(HALData::EMemoryRAMFree, mem);
       
   246     DP1_IMAGIC(_L("CTextureLoader::ReleaseQ512Textures - Free RAM: %d"), mem);
       
   247     
       
   248     for(TInt i=0; i<iContainer->iIEngine->GetTotalNumOfImages(); i++)
       
   249         {
       
   250         if(i >= iContainer->GetCurrentIndex() + CImagicContainerBrowser::K512TNImageBuffer || 
       
   251            i <= iContainer->GetCurrentIndex() - CImagicContainerBrowser::K512TNImageBuffer)
       
   252             {
       
   253             CImageData* imageData = iImagicAppUi->GetEngine()->GetImageData(i);
       
   254             
       
   255             if(imageData->iGridData.iGlSuperHQTextIndex!=0)
       
   256                 {
       
   257                 glDeleteTextures(1, &(imageData->iGridData.iGlSuperHQTextIndex));
       
   258                 imageData->iGridData.iGlSuperHQTextIndex=0;
       
   259                 }
       
   260             
       
   261             if(i != iContainer->GetCurrentIndex())
       
   262                 {
       
   263                 if (imageData->iGridData.iGlHQ512TextIndex!=0)
       
   264                     {
       
   265                     glDeleteTextures(1, &(imageData->iGridData.iGlHQ512TextIndex));
       
   266                     imageData->iGridData.iGlHQ512TextIndex=0;
       
   267                     }
       
   268                 }
       
   269             }
       
   270         }
       
   271     
       
   272     ret = HAL::Get(HALData::EMemoryRAMFree, mem);
       
   273     DP1_IMAGIC(_L("CTextureLoader::ReleaseQ512Textures - Free RAM: %d"), mem);
       
   274     
       
   275     DP0_IMAGIC(_L("CTextureLoader::ReleaseQ512Textures--"));
       
   276     }
       
   277 
       
   278 
       
   279 /*----------------------------------------------------------------------*/
       
   280 // Unloads picture from specified index
       
   281 //
       
   282 void CTextureLoader::UnloadLQ512Tex(CImageData* aData) const
       
   283     {
       
   284     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ512Tex++"));
       
   285     
       
   286     /*TInt mem = 0;
       
   287     TInt ret = HAL::Get(HALData::EMemoryRAMFree, mem);
       
   288     DP1_IMAGIC(_L("CTextureLoader::UnloadLQ512Tex - Free RAM: %d"), mem);*/
       
   289                 
       
   290     // Delete the picture
       
   291     if (aData->iGridData.iGlHQ512TextIndex!=0)
       
   292         {
       
   293         glDeleteTextures(1, &(aData->iGridData.iGlHQ512TextIndex));
       
   294         aData->iGridData.iGlHQ512TextIndex=0;
       
   295         }
       
   296     
       
   297     /*ret = HAL::Get(HALData::EMemoryRAMFree, mem);
       
   298     DP1_IMAGIC(_L("CTextureLoader::UnloadLQ512Tex - Free RAM: %d"), mem);*/
       
   299     
       
   300     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ512Tex--"));
       
   301     }
       
   302 
       
   303 /*----------------------------------------------------------------------*/
       
   304 // Unloads picture from specified index
       
   305 //
       
   306 void CTextureLoader::UnloadLQ128Tex(CImageData* aData) const
       
   307     {
       
   308     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ128Tex++"));
       
   309     
       
   310     // Delete the picture
       
   311     if (aData->iGridData.iGlLQ128TextIndex!=0)
       
   312         {
       
   313         glDeleteTextures(1, &(aData->iGridData.iGlLQ128TextIndex));
       
   314         aData->iGridData.iGlLQ128TextIndex=0;
       
   315         }
       
   316     
       
   317     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ128Tex--"));
       
   318     }
       
   319 
       
   320 /*----------------------------------------------------------------------*/
       
   321 // Unloads picture from specified index
       
   322 //
       
   323 void CTextureLoader::UnloadLQ32Tex(CImageData* aData) const
       
   324     {
       
   325     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ32Tex++"));
       
   326     
       
   327     if (aData->iGridData.iGlLQ32TextIndex!=0)
       
   328         {
       
   329         glDeleteTextures(1, &(aData->iGridData.iGlLQ32TextIndex));
       
   330         aData->iGridData.iGlLQ32TextIndex=0;
       
   331         }
       
   332     
       
   333     DP0_IMAGIC(_L("CTextureLoader::UnloadLQ32Tex--"));
       
   334     }
       
   335 
       
   336 
       
   337 /*----------------------------------------------------------------------*/
       
   338 // Image is loaded by engine, iScale it for OpenGL if needed
       
   339 //
       
   340 void CTextureLoader::ImageLoadedL(TInt aError, CFbsBitmap* aBitmap, TInt aGLMaxRes)
       
   341     {
       
   342     DP0_IMAGIC(_L("CTextureLoader::ImageLoadedL++"));
       
   343     
       
   344     if(aError == KErrNotFound || aError == KErrPathNotFound)
       
   345         {
       
   346         DP0_IMAGIC(_L("CTextureLoader::ImageLoadedL - Error: KErrNotFound/KErrPathNotFound"));
       
   347         iData->SetImageReady(iResolution, EFalse);
       
   348         iData = NULL;
       
   349         return;
       
   350         }
       
   351     else if(aError == KErrCorrupt)
       
   352         {
       
   353         DP0_IMAGIC(_L("CTextureLoader::ImageLoadedL - Error: KErrCorrupt"));
       
   354         iData->SetImageReady(iResolution, EFalse);
       
   355         iData->iGridData.iCorrupted = ETrue;
       
   356         iData = NULL;
       
   357         return;
       
   358         }
       
   359     else if(aError == KErrCancel)
       
   360         {
       
   361         //Image loading is cancelled only for superzoom iage loading
       
   362         DP1_IMAGIC(_L("CTextureLoader::ImageLoadedL - Error: %d"), aError);
       
   363         //iData->SetImageReady(iResolution, EFalse);
       
   364         iData = NULL;
       
   365         return;
       
   366         }
       
   367     else if(aError != KErrNone)
       
   368         {
       
   369         DP1_IMAGIC(_L("CTextureLoader::ImageLoadedL - Error: %d"), aError);
       
   370         iData->SetImageReady(iResolution, EFalse);
       
   371         iData = NULL;
       
   372         return;
       
   373         }
       
   374     
       
   375     // Set image ready to database
       
   376     //iData->SetImageReady(iResolution, ETrue);
       
   377     
       
   378     iGLMaxRes = aGLMaxRes;
       
   379     iImageSize = aBitmap->SizeInPixels();
       
   380     
       
   381     //Check loaded image real size and check if scaling is needed
       
   382     iScalingNeeded = IsScalingNeeded(iImageSize);
       
   383         
       
   384     TSize size;
       
   385     // Check does image need to be scaled
       
   386     if(!iScalingNeeded)
       
   387         { //Image is already proper size, just create texture
       
   388         CreateThumbnailTexture(aBitmap);
       
   389         }
       
   390     //Calculate new image size if scaling is needed(in case size is not pow^2)
       
   391     else
       
   392         {
       
   393         size.iWidth = ScaleDown( iImageSize.iWidth );
       
   394         size.iHeight = ScaleDown( iImageSize.iHeight );
       
   395 
       
   396         // Setup target bitmap to be large enough
       
   397         iBitmap->Reset();
       
   398 #ifdef USE_RGBA
       
   399         iBitmap->Create(size, EColor16MU);
       
   400 #else
       
   401         iBitmap->Create(size, EColor16M);
       
   402 #endif
       
   403     
       
   404         // Setup image quality
       
   405         CBitmapScaler::TQualityAlgorithm quality = CBitmapScaler::EMinimumQuality;
       
   406         /*if(iHighQuality)*/
       
   407             quality=CBitmapScaler::EMaximumQuality;
       
   408     
       
   409         iBitmapScaler->SetQualityAlgorithm(quality);
       
   410     
       
   411         // Start scaling the bitmap, RunL will be called when complete
       
   412         iBitmapScaler->Scale(&iStatus, *aBitmap, *iBitmap, EFalse);
       
   413         if(!IsActive())
       
   414             SetActive();
       
   415         }
       
   416     
       
   417     DP0_IMAGIC(_L("CTextureLoader::ImageLoadedL--"));
       
   418     }
       
   419 
       
   420 /*----------------------------------------------------------------------*/
       
   421 // Creates OpenGL texture of given bitmap
       
   422 //
       
   423 TInt CTextureLoader::CreateTexture(CFbsBitmap* aBitmap, TBool aHighQuality)
       
   424     {
       
   425     DP0_IMAGIC(_L("CTextureLoader::CreateTexture++"));
       
   426     
       
   427     // Get image data size
       
   428     TInt width = aBitmap->SizeInPixels().iWidth;
       
   429     TInt height = aBitmap->SizeInPixels().iHeight;
       
   430     TInt dataSize = width * height;
       
   431     
       
   432     // Lock bitmap before modifying its data
       
   433     aBitmap->LockHeap( EFalse );
       
   434     
       
   435     // The data in the texture are in RGBA order but is read in BGRA order.
       
   436     // So we have to swap the 1st and 3rd bytes.
       
   437     TUint8* data = (TUint8 *)aBitmap->DataAddress();
       
   438 
       
   439 #ifdef USE_RGBA
       
   440     dataSize*=4;
       
   441     for (TInt i=0; i<dataSize; i+=4)
       
   442         {
       
   443         TUint8 temp = data[i];
       
   444         data[i] = data[i+2];
       
   445         data[i+2] = temp;
       
   446         }
       
   447 #else
       
   448     dataSize*=3;
       
   449     //RDebug::Print(_L("CTextureLoader::CreateTexture - Bitmap Data seize: %d"), dataSize);
       
   450     for(TInt i=0; i<dataSize; i+=3)
       
   451         {
       
   452         TUint8 temp = data[i];
       
   453         data[i] = data[i+2];
       
   454         data[i+2] = temp;
       
   455         }
       
   456 #endif
       
   457     
       
   458     // Generate OpenGL texture index
       
   459     GLuint index;
       
   460     glGenTextures(1, &index);
       
   461     glBindTexture(GL_TEXTURE_2D, index);
       
   462     
       
   463 //#define MIPMAPPING
       
   464 #ifdef MIPMAPPING
       
   465        //Set mipmapping on
       
   466        //glHint( GL_GENERATE_MIPMAP_HINT, GL_NICEST );
       
   467        glHint( GL_GENERATE_MIPMAP_HINT, GL_FASTEST );
       
   468        glTexParameteri( GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE );
       
   469        //Select mipmapping filtering mode
       
   470        glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);
       
   471        //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR);//linear gives better image quality but slover rendering
       
   472 #endif
       
   473        
       
   474     // Set texture parameters
       
   475     //GL_NEAREST is faster than GL_LINEAR but quality is better in linear
       
   476     if(!aHighQuality)
       
   477         {
       
   478         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
       
   479         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
       
   480         }
       
   481     else
       
   482         {
       
   483         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   484         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   485         }
       
   486     
       
   487     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
       
   488     glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
       
   489 
       
   490     
       
   491 #ifdef USE_RGBA
       
   492     // Load texture into OpenGL memory
       
   493     glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height,
       
   494                   0, GL_RGBA, GL_UNSIGNED_BYTE, data );
       
   495 #else
       
   496        glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
       
   497 #endif
       
   498     
       
   499     // Unlock bitmap
       
   500     aBitmap->UnlockHeap( EFalse );
       
   501     
       
   502     return index;
       
   503     }
       
   504     
       
   505 void CTextureLoader::CreateThumbnailTexture(CFbsBitmap* aBitmap)
       
   506     {
       
   507     DP0_IMAGIC(_L("CTextureLoader::CreateThumbnailTexture++"));
       
   508     
       
   509     TInt index = CreateTexture(aBitmap, iHighQuality);
       
   510     iContainer->SetTextIndex(index);
       
   511     
       
   512     //Image has been loaded, store index
       
   513     if(iResolution == ESize512x512)
       
   514         {
       
   515         //Check that we really have high resolution image
       
   516         if((aBitmap->SizeInPixels().iHeight == 512 && aBitmap->SizeInPixels().iWidth == 1024) || 
       
   517            (aBitmap->SizeInPixels().iHeight == 1024 && aBitmap->SizeInPixels().iWidth == 512) ||
       
   518            (aBitmap->SizeInPixels().iHeight == 512 && aBitmap->SizeInPixels().iWidth == 512))
       
   519             {
       
   520             iData->iGridData.iGlHQ512TextIndex = index;
       
   521             }
       
   522         else
       
   523             {//if no high resolution image, delete existing texture from 128x128 slot and bind newly created texture there
       
   524             if(aBitmap->SizeInPixels().iHeight == 128)
       
   525                 {
       
   526                 if(iData->iGridData.iGlLQ128TextIndex != 0)
       
   527                     glDeleteTextures(1, &(iData->iGridData.iGlLQ128TextIndex));
       
   528                 iData->iGridData.iGlLQ128TextIndex = index;
       
   529                 }
       
   530             if(aBitmap->SizeInPixels().iHeight == 32)
       
   531                 {
       
   532                 if(iData->iGridData.iGlLQ32TextIndex != 0)
       
   533                     glDeleteTextures(1, &(iData->iGridData.iGlLQ32TextIndex));
       
   534                 iData->iGridData.iGlLQ32TextIndex = index;
       
   535                 }
       
   536             }
       
   537         iPreviousData = iData;
       
   538         }
       
   539     else if(iResolution == ESize128x128)
       
   540         {
       
   541         //Check if we had exif tn already binded and delete it
       
   542         if(iData->iGridData.iGlLQ128TextIndex != 0)
       
   543             {
       
   544             glDeleteTextures(1, &(iData->iGridData.iGlLQ128TextIndex));
       
   545             }
       
   546         iData->iGridData.iGlLQ128TextIndex = index;
       
   547         }
       
   548     else if(iResolution == ESize32x32)
       
   549         {
       
   550         //Check if we had exif tn already binded and delete it
       
   551         if(iData->iGridData.iGlLQ32TextIndex != 0)
       
   552             {
       
   553             glDeleteTextures(1, &(iData->iGridData.iGlLQ32TextIndex));
       
   554             }
       
   555         iData->iGridData.iGlLQ32TextIndex = index;
       
   556         }
       
   557 #ifdef SUPERZOOM
       
   558     else if(iResolution == EFullSize)
       
   559         {
       
   560         iData->iGridData.iGlSuperHQTextIndex = index;
       
   561         }
       
   562 #endif
       
   563 
       
   564     //Order container to draw scaled image
       
   565     /*if(iContainer->GetDrawMode() == EOneByOne)
       
   566         {
       
   567         //Check if loaded image is current one and draw it
       
   568         CImageData* imageData = iImagicAppUi->GetEngineL()->GetImageData(iContainer->GetCurrentIndex(), iImagicAppUi->GetUIDrawMode());
       
   569         if(imageData->iGridData.iGlSuperHQTextIndex != 0)
       
   570             iContainer->DrawNow();
       
   571         else if(imageData->iGridData.iGlHQ512TextIndex == iData->iGridData.iGlHQ512TextIndex )
       
   572             iContainer->DrawNow();
       
   573         else if(imageData->iGridData.iGlLQ128TextIndex == iData->iGridData.iGlLQ128TextIndex )
       
   574             iContainer->DrawNow();
       
   575         }
       
   576     else*/
       
   577         {
       
   578         iContainer->DrawNow();
       
   579         DP0_IMAGIC(_L("<----------- CTextureLoader DrawNow ----------->"));
       
   580         }
       
   581     
       
   582     //Loader is finished
       
   583     iData = NULL;
       
   584     aBitmap->Reset();
       
   585     
       
   586     //Set loader back on
       
   587     //iContainer->SetLoadingOn(ETrue);
       
   588         
       
   589     DP0_IMAGIC(_L("CTextureLoader::CreateThumbnailTexture--"));
       
   590     }
       
   591 
       
   592 /*----------------------------------------------------------------------*/
       
   593 // Active object's RunL
       
   594 //
       
   595 void CTextureLoader::RunL()
       
   596     {
       
   597     ASSERT(iBitmap);
       
   598     CreateThumbnailTexture(iBitmap);
       
   599     }
       
   600 
       
   601 /*----------------------------------------------------------------------*/
       
   602 // Active object's DoCancel
       
   603 //
       
   604 void CTextureLoader::DoCancel()
       
   605     {
       
   606     DP0_IMAGIC(_L("CTextureLoader::DoCancel++"));
       
   607     
       
   608     // Reset bitmap and cancel scaler
       
   609     if (IsActive())
       
   610         {
       
   611         iBitmap->Reset();
       
   612         iBitmapScaler->Cancel();
       
   613         }
       
   614     
       
   615     DP0_IMAGIC(_L("CTextureLoader::DoCancel--"));
       
   616     }
       
   617 
       
   618 /*----------------------------------------------------------------------*/
       
   619 // Active object's RunError
       
   620 //
       
   621 void CTextureLoader::RunError()
       
   622     {
       
   623     // Nothing here
       
   624     }
       
   625 
       
   626 /*----------------------------------------------------------------------*/
       
   627 // Scales given value to power of two
       
   628 //
       
   629 TInt CTextureLoader::ScaleDown(TInt aSize)
       
   630     {
       
   631     DP0_IMAGIC(_L("CTextureLoader::ScaleDown++"));
       
   632     
       
   633     if(iResolution == ESize32x32)
       
   634         {
       
   635         return 32;
       
   636         }
       
   637     else if(iResolution == ESize128x128 || iResolution == ESize512x512)
       
   638         {
       
   639         return 128;
       
   640         }
       
   641         
       
   642     // Find new size for picture
       
   643     TInt newSize;
       
   644     for (newSize=1; newSize<aSize; newSize*=2)
       
   645         ;   // Just do nothing
       
   646     
       
   647     // Do not iScale up
       
   648     if(newSize > aSize)
       
   649         newSize/=2;
       
   650     
       
   651     // Limit size to some maximum value
       
   652     TInt maxSize=256;
       
   653     TInt minSize = 32;
       
   654     
       
   655     
       
   656     if(iHighQuality)    // High quality pictures are lot bigger
       
   657         {
       
   658 /*#ifdef __WINS__
       
   659         maxSize=512;
       
   660 #endif*/
       
   661 
       
   662 #ifdef SUPERZOOM
       
   663         //maxSize=iGLMaxRes;
       
   664         if(iGLMaxRes >= 1024)
       
   665             maxSize=1024;
       
   666         else
       
   667             maxSize=iGLMaxRes;
       
   668         
       
   669 #else
       
   670         maxSize=512;
       
   671 #endif
       
   672     
       
   673         }
       
   674     // Check that size is below maximum and min size
       
   675     if(newSize > maxSize)
       
   676         newSize=maxSize;
       
   677     if(newSize < minSize)
       
   678         newSize=minSize;
       
   679 
       
   680     DP0_IMAGIC(_L("CTextureLoader::ScaleDown--"));
       
   681     return newSize;
       
   682     }
       
   683 
       
   684 /*----------------------------------------------------------------------*/
       
   685 // Creates OpenGL texture of given bitmap
       
   686 //
       
   687 void CTextureLoader::CreateIconTextures()
       
   688     {
       
   689     DP0_IMAGIC(_L("CTextureLoader::CreateIconTextures++"));
       
   690     
       
   691     for(TInt i=0; i<iBitmapArray.Count(); i++)
       
   692         {
       
   693         // Get image data size
       
   694         TInt width = iBitmapArray[i]->SizeInPixels().iWidth;
       
   695         TInt height = iBitmapArray[i]->SizeInPixels().iHeight;
       
   696         TInt dataSize = width * height;
       
   697         
       
   698         // Lock bitmap before modifying its data
       
   699         iBitmapArray[i]->LockHeap( EFalse );
       
   700         
       
   701         // The data in the texture are in RGBA order but is read in BGRA order.
       
   702         // So we have to swap the 1st and 3rd bytes.
       
   703         TUint8* data = (TUint8 *)iBitmapArray[i]->DataAddress();
       
   704     
       
   705         if(!iRGB2BGRDone)
       
   706             {
       
   707 //#ifdef USE_RGBA
       
   708             dataSize*=4;
       
   709             for (TInt i=0; i<dataSize; i+=4)
       
   710                 {
       
   711                 TUint8 temp = data[i];
       
   712                 data[i] = data[i+2];
       
   713                 data[i+2] = temp;
       
   714                 }
       
   715 /*#else
       
   716             dataSize*=3;
       
   717             for(TInt i=0; i<dataSize; i+=3)
       
   718                 {
       
   719                 TUint8 temp = data[i];
       
   720                 data[i] = data[i+2];
       
   721                 data[i+2] = temp;
       
   722                 }
       
   723 #endif*/
       
   724             }
       
   725         
       
   726         // Generate OpenGL texture index
       
   727         //GLuint index;
       
   728         glGenTextures(1, &iSmileTexIndex);
       
   729         glBindTexture(GL_TEXTURE_2D, iSmileTexIndex);
       
   730         iIconTextureIndexes.Append(iSmileTexIndex);
       
   731         //iContainer->SetTextIndex(index);
       
   732         
       
   733         // Set texture parameters
       
   734         //GL_NEAREST is faster than GL_LINEAR but quality is better in linear
       
   735         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
       
   736         glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
       
   737         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
       
   738         glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
       
   739     
       
   740         
       
   741     //#ifdef USE_RGBA
       
   742         // Load texture into OpenGL memory
       
   743         glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, data );
       
   744     /*#else
       
   745            glTexImage2D( GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGB, GL_UNSIGNED_BYTE, data );
       
   746     #endif*/
       
   747         
       
   748         // Unlock bitmap
       
   749        iBitmapArray[i]->UnlockHeap( EFalse );
       
   750        }
       
   751     
       
   752     iRGB2BGRDone = ETrue;
       
   753     
       
   754     iContainer->IconTexturesLoaded(iIconTextureIndexes);
       
   755     
       
   756     DP0_IMAGIC(_L("CTextureLoader::CreateIconTextures--"));
       
   757     }
       
   758 
       
   759 
       
   760 /*----------------------------------------------------------------------*/
       
   761 // Check if scaling is needed
       
   762 //
       
   763 TBool CTextureLoader::IsScalingNeeded(TSize aImageSize)
       
   764     {
       
   765 //Check loaded image real size and over write high quality flag
       
   766     if((aImageSize.iHeight == 1024 && aImageSize.iWidth == 1024) ||
       
   767        (aImageSize.iHeight == 512 && aImageSize.iWidth == 1024) || 
       
   768        (aImageSize.iHeight == 1024 && aImageSize.iWidth == 512) ||
       
   769        (aImageSize.iHeight == 512 && aImageSize.iWidth == 512))
       
   770         {
       
   771         // Set image ready to database, only if resolution was maching with Imagic TN size
       
   772         iData->SetImageReady(ESize512x512, ETrue);
       
   773         return EFalse;
       
   774         }
       
   775     if((aImageSize.iHeight == 128 && aImageSize.iWidth == 128))
       
   776         {
       
   777         // Set image ready to database, only if resolution was maching with Imagic TN size
       
   778         iData->SetImageReady(ESize128x128, ETrue);
       
   779         return EFalse;
       
   780         }
       
   781     if((aImageSize.iHeight == 32 && aImageSize.iWidth == 32))
       
   782         {
       
   783         // Set image ready to database, only if resolution was maching with Imagic TN size
       
   784         iData->SetImageReady(ESize32x32, ETrue);
       
   785         return EFalse;
       
   786         }
       
   787     
       
   788     return ETrue;
       
   789     }