首页 > 焦点 > 图像载入和色彩剪裁
图像载入和色彩剪裁
网上收集 2007/11/26 16:48:04 (205)

   这是一个用程序实现如何高效率处理一般图形图象数据的小例子:
用Series 60 图像格式转换加载文件中(jpg/png/gif 三种格式为例) 的图像
从外部同步化进程,以便你得到一个图像加载的阻断方法
以12位元彩色的256色调色板执行简单色彩剪裁把图像转换成一个带有索引的位图。
注意: 此例子使用嵌套调度。 (嵌套 CActiveScheduler::Start(),CActiveScheduler::Stop())
这种方法如果误用可能是危险的, 但谨慎地用,它是简单高效的。

  以下是 CImageLoader 和 CTexture 这两个类的源文件;  CTexture 是一个承载数据,
调色板和画面高度/宽度的图像类,但是因为我在3D texture mapper中直接使用了此代码,它叫做:texture。
程序中写了注释,读起来应该很好理解。当然希望大家从中真正地学到知识,而不是直接拷贝。
并且程序是受版权保护的。

Happy hacking!

--------------------------------------------------------------------------------

texture.h:

#ifndef __TEXTURE_H
#define __TEXTURE_H

#include

/**
* Represents a texture used by the texture mapping routines.
*/
class CTexture : public CBase
{
   public:
       /*!
         @函数NewL
         @讨论建立CTexture的一个对象
         @param aWidth texture宽度 (须在2的3次幂到2的10次幂之间,从而使宽度在8-1024的范围内)
                  @param aHeight texture高度
                  @param aWidthShift==log2(aWidth)
                  @param aPalette调色板信息,这个被复制到本地缓冲器中
                  @param aData 指向调色板指针的索引texture数据,它不被拷贝到本地缓冲器,所
                   以起始缓冲器在销毁这个对象之前不可以被释放     */
       static CTexture * NewL(TInt aWidth, TInt aHeight, TInt aWidthShift, TUint16 *aPalette, TUint8 *aData);
       ~CTexture();
       TInt GetWidth() { return iWidth; }
       TInt GetHeight() { return iHeight; }
       TInt GetWidthShift() { return iWidthShift; }
       TUint16 * GetPalette() { return iPalette; }
       TUint8 * GetData() { return iData; }

   private:
       CTexture();
       void ConstructL(TInt aWidth, TInt aHeight, TInt aWidthShift, TUint16 *aPalette, TUint8 *aData);
       TInt iWidth;
       TInt iHeight;
       TInt iWidthShift;
       TUint16 *iPalette;
       TUint8 *iData;
};

#endif

--------------------------------------------------------------------------------

texture.cpp:

#include "texture.h"

CTexture * CTexture::NewL(TInt aWidth, TInt aHeight, TInt aWidthShift, TUint16 *aPalette, TUint8 *aData)
{
        CTexture *texture = new (ELeave) CTexture();
        CleanupStack::PushL(texture);
        texture->ConstructL(aWidth, aHeight, aWidthShift,
                                            aPalette, aData);
        CleanupStack::Pop(); // texture

        return texture;
}

void CTexture::ConstructL(TInt aWidth, TInt aHeight, TInt aWidthShift,
                                                  TUint16 *aPalette, TUint8 *aData)
{
   iWidth = aWidth;
   iHeight = aHeight;
   iWidthShift = aWidthShift;
   iData = aData;

        // 创建一个调色板并复制它到本地指针
        iPalette = (TUint16 *)User::AllocL(256 * sizeof(TUint16));
        Mem::Copy(iPalette, aPalette, 256 * sizeof(TUint16));
}

CTexture::CTexture()
{
}

CTexture::~CTexture()
{
   User::Free(iData);
   User::Free(iPalette);
}

--------------------------------------------------------------------------------

imageloader.h:

#ifndef __IMAGELOADER_H
#define __IMAGELOADER_H

#include
#include
#include

#include "texture.h"

/**
*色彩频率调度
*/
struct FreqItem {
        TUint16 freq;
        TUint16 color;
};

/**
* 加载图象的有效方法.
*/
class CImageLoader : public CActive, public MMdaImageUtilObserver
{
   public:
       /**
        * 加载texture的错误代码
       */
       enum TTextureLoadingError { KBadImageWidth = 1666001 };

       /*!
         @函数LoadTextureL
 
         @加载一个图象文件并将其转化成的CTexture一个对象
         @param aFilename 图象文件的文件名
         */
       static CTexture * LoadTextureL(const TDesC &aFilename);

       // 来自MMdaImageUtilObserver
       virtual void MiuoOpenComplete(TInt aError);
       virtual void MiuoConvertComplete(TInt aError);
       virtual void MiuoCreateComplete(TInt aError);

      // 来自CActive
       void RunL();
       void DoCancel();
   private:
       CImageLoader(const TDesC *aFilename);
       ~CImageLoader();
       void ReadImageL();
       void CreateTexture();
       void SortFreqTable(TInt aLeft, TInt aRight);
       TUint8 FindNearestColor(TUint16 aColor, TInt aPaletteSize);

       TDesC *iFilename;
       CMdaImageFileToBitmapUtility *iConverter;
       RTimer *iTimer;
       CTexture *iTexture;
  

阅读(205) (责任编辑:城市网)
关于我们 - 联系我们 - 网站荣誉 - 广告服务 - 版权声明 - 网站地图
Copyright© 2007-2018 bj1.com.cn 首都热线 版权所有 QQ:165687462
中国·北京 粤ICP备14047004号-20