cocos2dx之CCImage

CCImage在"CCImage.h"中定义,表示一张加载到内存的纹理图片。在其内部的实现中,纹理以每个像素的颜色值保存在内存之中。CCImage通常作为文件和显卡间数据交换的一个工具,因此主要提供了两个方面的功能:一方面是文件的加载与保存,另一方面是内存缓冲区的读写。

我们可以使用CCImage轻松地读写图片文件。目前,CCImage支持PNG、JPEG和TIFF三种主流的图片格式。下面列举与文件读写相关的方法:

  /** 
    @brief  Load the image from the specified path. 
    @param strPath   the absolute file path
    @param imageType the type of image, now only support tow types.
    @return  true if load correctly
    */
    bool initWithImageFile(const char * strPath, EImageFormat imageType = kFmtPng);

    /*
     @brief The same meaning as initWithImageFile, but it is thread safe. It is casued by
            loadImage() in CCTextureCache.cpp.
     @param fullpath  full path of the file   
     @param imageType the type of image, now only support tow types.
     @return  true if load correctly
     */
    bool initWithImageFileThreadSafe(const char *fullpath, EImageFormat imageType = kFmtPng);


    /**
    @brief    Save the CCImage data to specified file with specified format.
    @param    pszFilePath        the file's absolute path, including file subfix
    @param    bIsToRGB        if the image is saved as RGB format
    */
    bool saveToFile(const char *pszFilePath, bool bIsToRGB = true);



CCImage也提供了读写内存的接口。getData和getDataLen这两个方法提供了获取当前纹理的缓冲区的功能,而initWithImageData方法提供了使用像素数据初始化图片的功能。相关的方法定义如下:

    unsigned char *   getData()               { return m_pData; }
    int         getDataLen()            { return m_nWidth * m_nHeight; }


    /**
    @brief  Load image from stream buffer.

    @warning kFmtRawData only support RGBA8888
    @param pBuffer  stream buffer that hold the image data
    @param nLength  the length of data(managed in byte)
    @param nWidth, nHeight, nBitsPerComponent are used for kFmtRawData
    @return true if load correctly
    */
    bool initWithImageData(void * pData, 
                           int nDataLen, 
                           EImageFormat eFmt = kFmtUnKnown,
                           int nWidth = 0,
                           int nHeight = 0,
                           int nBitsPerComponent = 8);




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值