ganeswidgets/src/hgvgimage.cpp
changeset 0 89c329efa980
child 1 e48454f237ca
equal deleted inserted replaced
-1:000000000000 0:89c329efa980
       
     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:
       
    13 *
       
    14 * Description:    
       
    15 *
       
    16 */
       
    17 
       
    18 #include "HgVgImage.h"
       
    19 #include "HgImageFader.h"
       
    20 #include "hgvgquadrenderer.h"
       
    21 
       
    22 const int KMaxMirrorWidth(128);
       
    23 const int KMaxMirrorHeight(64);
       
    24 
       
    25 static QSize getMirrorSize(const QImage& image)
       
    26 {
       
    27     return QSize(
       
    28         qMin(image.width() / 2, KMaxMirrorWidth), 
       
    29         qMin(image.height() / 4, KMaxMirrorHeight) 
       
    30         );
       
    31 }
       
    32 
       
    33 HgVgImage::HgVgImage(HgVgQuadRenderer* renderer) :
       
    34     mVgImage(VG_INVALID_HANDLE),
       
    35     mMirrorImage(VG_INVALID_HANDLE),
       
    36     mMirrorSize(0,0),
       
    37     mRenderer(renderer)
       
    38 {
       
    39     setAlpha(1.0);
       
    40 }
       
    41 
       
    42 HgVgImage::~HgVgImage()
       
    43 {
       
    44     releaseImage();
       
    45 }
       
    46 
       
    47 int HgVgImage::width() const
       
    48 {
       
    49     return mQImage.width();
       
    50 }
       
    51 
       
    52 int HgVgImage::height() const
       
    53 {
       
    54     return mQImage.height();
       
    55 }
       
    56 
       
    57 VGImage HgVgImage::image() const
       
    58 {
       
    59     return mVgImage;
       
    60 }
       
    61 
       
    62 int HgVgImage::mirrorImageWidth() const
       
    63 {
       
    64     return mMirrorSize.width();
       
    65 }
       
    66 
       
    67 int HgVgImage::mirrorImageHeight() const
       
    68 {
       
    69     return mMirrorSize.height();
       
    70 }
       
    71     
       
    72 VGImage HgVgImage::mirrorImage() const
       
    73 {
       
    74     return mMirrorImage;
       
    75 }
       
    76 
       
    77 
       
    78 void HgVgImage::setImage(QImage& image)
       
    79 {
       
    80     if (image.isNull())
       
    81         return;
       
    82 
       
    83     // release previous images vg data
       
    84     releaseImage();
       
    85         
       
    86     mQImage = image;
       
    87 
       
    88     mMirrorSize = getMirrorSize(mQImage);
       
    89 
       
    90 }
       
    91 
       
    92 void HgVgImage::releaseImage()
       
    93 {
       
    94     if (mVgImage != VG_INVALID_HANDLE)
       
    95     {
       
    96         vgDestroyImage(mVgImage);
       
    97         mVgImage = VG_INVALID_HANDLE;            
       
    98     }
       
    99     if (mMirrorImage != VG_INVALID_HANDLE)
       
   100     {
       
   101         vgDestroyImage(mMirrorImage);
       
   102         mMirrorImage = VG_INVALID_HANDLE;            
       
   103     }
       
   104     
       
   105     // TODO, check that this will free all the previously reserved image resources.
       
   106     mQImage = QImage();
       
   107 }
       
   108 
       
   109 void HgVgImage::upload(bool mirror)
       
   110 {
       
   111     
       
   112     if (mVgImage == VG_INVALID_HANDLE)
       
   113     {            
       
   114                 
       
   115         VGImageFormat format;
       
   116 
       
   117         switch (mQImage.format())
       
   118         {
       
   119         case QImage::Format_ARGB32_Premultiplied:
       
   120             format = VG_sARGB_8888_PRE;
       
   121             break;
       
   122         case QImage::Format_ARGB32:
       
   123             format = VG_sARGB_8888;
       
   124             break;
       
   125         case QImage::Format_RGB16:
       
   126             format = VG_sRGB_565;
       
   127             break;
       
   128         default:
       
   129             mQImage = mQImage.convertToFormat(QImage::Format_RGB16);
       
   130             format = VG_sRGB_565;
       
   131             break;
       
   132         }
       
   133         
       
   134 
       
   135         mVgImage = vgCreateImage(format, 
       
   136             mQImage.width(), mQImage.height(),VG_IMAGE_QUALITY_NONANTIALIASED);
       
   137         if (mVgImage == VG_INVALID_HANDLE)
       
   138         {
       
   139             qWarning("HgMediaWall run out of video memory for images!");
       
   140             return;
       
   141         }
       
   142         
       
   143         vgImageSubData(mVgImage, mQImage.bits(), mQImage.bytesPerLine(), 
       
   144             format, 0, 0, mQImage.width(), mQImage.height() );        
       
   145     }
       
   146     
       
   147     if (mirror && mMirrorImage == VG_INVALID_HANDLE)
       
   148     {
       
   149         mMirrorImage = vgCreateImage(VG_sARGB_8888_PRE, mMirrorSize.width(), mMirrorSize.height(), VG_IMAGE_QUALITY_NONANTIALIASED);
       
   150         if (mMirrorImage == VG_INVALID_HANDLE)
       
   151         {
       
   152             qWarning("HgMediaWall run out of video memory for images!");
       
   153             return;
       
   154         }
       
   155         
       
   156         // cut half of the image, then scale to desired width/height if needed
       
   157         QImage mirrorImage = mQImage.copy(0, mQImage.height()/2, mQImage.width(), mQImage.height()/2).scaled(mMirrorSize).convertToFormat(QImage::Format_ARGB32);
       
   158         
       
   159         // apply gradient to alpha channel so that mirror image looks like
       
   160         // it fades under the floor
       
   161         for (int i = 0; i < mirrorImage.height(); i++)
       
   162         {
       
   163             qreal t = qreal(i) / qreal(mirrorImage.height());
       
   164             int a = (int)(t * 255.0);
       
   165             for (int j = 0; j < mirrorImage.width(); j++)
       
   166             {
       
   167                 QRgb rgb = mirrorImage.pixel(j, i);
       
   168                 mirrorImage.setPixel(j, i, qRgba(qRed(rgb), qGreen(rgb), qBlue(rgb), a));
       
   169             }        
       
   170         }
       
   171         
       
   172         // copy data to vg image
       
   173         vgImageSubData(mMirrorImage, mirrorImage.bits(), mirrorImage.bytesPerLine(), 
       
   174             VG_sARGB_8888, 0, 0, mirrorImage.width(), mirrorImage.height() );
       
   175 
       
   176     }
       
   177     
       
   178 }
       
   179