javauis/eswt_qt/org.eclipse.swt/Eclipse_SWT_PI/qt/library/graphics/qt/imageimpl.cpp
changeset 80 d6dafc5d983f
equal deleted inserted replaced
78:71ad690e91f5 80:d6dafc5d983f
       
     1 /*******************************************************************************
       
     2  * Copyright (c) 2008, 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 - initial API and implementation
       
    10  *******************************************************************************/
       
    11 
       
    12 #include <QPixmap>
       
    13 #include "imageimpl.h"
       
    14 #include "gfxlog.h"
       
    15 
       
    16 namespace Java { namespace GFX {
       
    17 
       
    18 ImageImpl::ImageImpl() : ImageBase()
       
    19     {
       
    20     GFX_LOG_FUNC_CALL();
       
    21     }
       
    22 
       
    23 ImageImpl::~ImageImpl()
       
    24     {
       
    25     GFX_LOG_FUNC_CALL();
       
    26     }
       
    27 
       
    28 void ImageImpl::createBySize(int aWidth, int aHeight, int aFillColor, TImageFormat aFormat)
       
    29     {
       
    30     GFX_LOG_FUNC_CALL();
       
    31     Q_ASSERT(mImage.isNull());
       
    32 
       
    33     mImage = QImage(aWidth, aHeight, mapInternalFormatToQt(aFormat));
       
    34     // Check if creation was successful
       
    35     if(mImage.isNull())
       
    36         {
       
    37         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
    38         }
       
    39     // Finally fill with given fillColor, also clears the image mem area
       
    40     // otherwise there might be some random coloured pixels in image
       
    41     mImage.fill(aFillColor);
       
    42     }
       
    43 
       
    44 
       
    45 void ImageImpl::createFromQImage(const QImage& aImage)
       
    46     {
       
    47     GFX_LOG_FUNC_CALL();
       
    48     Q_ASSERT(mImage.isNull());
       
    49 
       
    50     mImage = QImage(aImage);
       
    51     // Validate allocation
       
    52     if(mImage.isNull())
       
    53         {
       
    54         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
    55         }
       
    56     }
       
    57     
       
    58 void ImageImpl::createFromQPixmap(const QPixmap& aPixmap)
       
    59     {
       
    60     GFX_LOG_FUNC_CALL();
       
    61     Q_ASSERT(mImage.isNull());
       
    62 
       
    63     mImage = aPixmap.toImage();
       
    64     // Validate allocation
       
    65     if(mImage.isNull())
       
    66         {
       
    67         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
    68         }    
       
    69     }
       
    70 
       
    71 void ImageImpl::createFromImage(Image* aImage, int aX, int aY, int aWidth, int aHeight)
       
    72     {
       
    73     GFX_LOG_FUNC_CALL();
       
    74     Q_ASSERT(mImage.isNull());
       
    75 
       
    76     // Check if the copy should be exactly same as the original
       
    77     if((aImage->getWidth() == aWidth) && (aImage->getHeight() == aHeight) || 
       
    78         QRect(aX, aY, aWidth, aHeight).isEmpty())
       
    79         {
       
    80         mImage = aImage->toImage();
       
    81         }
       
    82     // it's needed to make partial copy of the original
       
    83     else
       
    84         {
       
    85         switch(aImage->type())
       
    86             {
       
    87             case EImage:
       
    88                 {
       
    89                 mImage = aImage->getConstImage()->copy(aX, aY, aWidth, aHeight);
       
    90                 break;
       
    91                 }
       
    92             case EPixmap:
       
    93                 {
       
    94                 mImage = (aImage->getConstPixmap()->copy(aX, aY, aWidth, aHeight)).toImage();
       
    95                 break;
       
    96                 }
       
    97             default:
       
    98                 throw GfxException(EGfxErrorIllegalArgument, "Unsupported source image type");
       
    99             }
       
   100         }
       
   101     if(mImage.isNull())
       
   102         {
       
   103         throw GfxException(EGfxErrorNoMemory, "Image (pixmap) creation failed");
       
   104         }
       
   105     }
       
   106 
       
   107 void ImageImpl::createFromImageData(ImageDataWrapper* aData)
       
   108     {
       
   109     GFX_LOG_FUNC_CALL();
       
   110     Q_ASSERT(mImage.isNull());
       
   111 
       
   112     mImage = imageDataToQImage(aData);
       
   113 
       
   114     if(mImage.isNull())
       
   115         {
       
   116         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
   117         }
       
   118     }
       
   119 
       
   120 void ImageImpl::createFromRGB(int* aRgbdata, int aWidth, int aHeight, bool aHasAlpha) 
       
   121     {
       
   122     GFX_LOG_FUNC_CALL();
       
   123     Q_ASSERT(mImage.isNull());
       
   124 
       
   125     // Create QImage from rgbdata 
       
   126     mImage = QImage(reinterpret_cast<unsigned char*>(aRgbdata), aWidth, aHeight,
       
   127         (aHasAlpha ? QImage::Format_ARGB32 : QImage::Format_RGB32));
       
   128 
       
   129     // Validate creation
       
   130     if(mImage.isNull())
       
   131         {
       
   132         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
   133         }
       
   134     }
       
   135     
       
   136 void ImageImpl::dispose()
       
   137     {
       
   138     GFX_LOG_FUNC_CALL();
       
   139     delete this;
       
   140     }
       
   141 
       
   142 QPaintDevice* ImageImpl::getBindable()
       
   143     {
       
   144     GFX_LOG_FUNC_CALL();
       
   145     return static_cast<QPaintDevice*>(&mImage);
       
   146     }
       
   147 
       
   148 TImageFormat ImageImpl::getFormat()
       
   149     {
       
   150     return mapQtFormatToInternal(mImage.format());
       
   151     }
       
   152 
       
   153 int ImageImpl::getHeight()
       
   154     {
       
   155     GFX_LOG_FUNC_CALL();
       
   156     return mImage.height();
       
   157     }
       
   158 
       
   159 const QImage* ImageImpl::getConstImage()
       
   160     {
       
   161     GFX_LOG_FUNC_CALL();
       
   162     return &mImage;
       
   163     }
       
   164         
       
   165         
       
   166 QImage* ImageImpl::getImage()
       
   167     {
       
   168     GFX_LOG_FUNC_CALL();
       
   169     return &mImage;
       
   170     }
       
   171 
       
   172 const QPixmap* ImageImpl::getConstPixmap()
       
   173     {
       
   174     GFX_LOG_FUNC_CALL();
       
   175     return NULL;
       
   176     }
       
   177 
       
   178 QPixmap* ImageImpl::getPixmap()
       
   179     {
       
   180     GFX_LOG_FUNC_CALL();
       
   181     return NULL;
       
   182     }
       
   183 
       
   184 void ImageImpl::getRgb(int* aRgbdata, int aOffset, int aScanlength, int aX, int aY, int aWidth, int aHeight)
       
   185     {
       
   186     GFX_LOG_FUNC_CALL();
       
   187     doGetRgb(mImage, aRgbdata, aOffset, aScanlength, aX, aY, aWidth, aHeight);
       
   188     }
       
   189 
       
   190 void ImageImpl::getRgb(char* aRgbdata, char* aTransparencyMask,int aOffset, int aScanlength, int aX, int aY, int aWidth, int aHeight, int aFormat)
       
   191     {
       
   192     GFX_LOG_FUNC_CALL();
       
   193     doGetRgb(mImage, aRgbdata, aTransparencyMask, aOffset, aScanlength, aX, aY, aWidth, aHeight, aFormat);
       
   194     }
       
   195 
       
   196 void ImageImpl::getRgb(short* aRgbdata, int aOffset, int aScanlength, int aX, int aY, int aWidth, int aHeight, int aFormat)
       
   197     {
       
   198     GFX_LOG_FUNC_CALL();
       
   199     doGetRgb(mImage, aRgbdata, aOffset, aScanlength, aX, aY, aWidth, aHeight, aFormat);
       
   200     }
       
   201 int ImageImpl::getWidth()
       
   202     {
       
   203     GFX_LOG_FUNC_CALL();
       
   204     return mImage.width();
       
   205     }
       
   206 
       
   207 const QImage ImageImpl::toConstImage()
       
   208     {
       
   209     GFX_LOG_FUNC_CALL();
       
   210     return QImage(mImage);
       
   211     }
       
   212 
       
   213 QImage ImageImpl::toImage()
       
   214     {
       
   215     GFX_LOG_FUNC_CALL();
       
   216     return QImage(mImage);
       
   217     }
       
   218 
       
   219 QPixmap ImageImpl::toPixmap()
       
   220     {
       
   221     GFX_LOG_FUNC_CALL();
       
   222     return QPixmap::fromImage(mImage);
       
   223     }
       
   224 
       
   225 void ImageImpl::transform(TTransform aTransform) 
       
   226     {
       
   227     GFX_LOG_FUNC_CALL();
       
   228     mImage = mImage.transformed(generateTransformMatrix(aTransform), Qt::FastTransformation);
       
   229     // Validate creation
       
   230     if(mImage.isNull())
       
   231         {
       
   232         throw GfxException(EGfxErrorNoMemory, "Image (QImage) creation failed");
       
   233         }
       
   234     }
       
   235 
       
   236 TImageType ImageImpl::type()
       
   237     {
       
   238     return EImage;
       
   239     }
       
   240 
       
   241 bool ImageImpl::hasAlphaChannel()
       
   242     {
       
   243     GFX_LOG_FUNC_CALL();
       
   244     return mImage.hasAlphaChannel();
       
   245     }
       
   246 
       
   247 } // namespace GFX
       
   248 } // namespace Java