axmol/cocos/platform/CCImage.h

253 lines
8.6 KiB
C
Raw Normal View History

/****************************************************************************
Copyright (c) 2010-2012 cocos2d-x.org
Copyright (c) 2013-2016 Chukong Technologies Inc.
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
Copyright (c) 2020 c4games.com
http://www.cocos2d-x.org
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
****************************************************************************/
#ifndef __CC_IMAGE_H__
#define __CC_IMAGE_H__
2015-03-24 20:23:51 +08:00
/// @cond DO_NOT_SHOW
#include "base/CCRef.h"
Squashed commit of the following: commit a794d107ad85667e3d754f0b6251fc864dfbf288 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 14:33:49 2014 -0700 Yeah... everything compiles on win32 and wp8 commit 4740be6e4a0d16f742c27996e7ab2c100adc76af Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:58:38 2014 -0700 CCIME moved to base and compiles on Android commit ff3e1bf1eb27a01019f4e1b56d1aebbe2d385f72 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 13:02:57 2014 -0700 compiles Ok for Windows Phone 8 commit 8160a4eb2ecdc61b5bd1cf56b90d2da6f11e3ebd Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 12:25:31 2014 -0700 fixes for Windows Phone 8 commit 418197649efc93032aee0adc205e502101cdb53d Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 11:15:13 2014 -0700 Compiles on Win32 commit 08813ed7cf8ac1079ffadeb1ce78ea9e833e1a33 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 10:08:31 2014 -0700 Compiles on linux! commit 118896521e5b335a5257090b6863f1fb2a2002fe Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 09:30:42 2014 -0700 moves cocos/2d/platform -> cocos/platform commit 4fe9319d7717b0c1bccb2db0156eeb86255a89e0 Merge: bd68ec2 511295e Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Fri May 16 08:24:41 2014 -0700 Merge remote-tracking branch 'cocos2d/v3' into files commit bd68ec2f0e3a826d8b2f4b60564ba65ce766bc56 Author: Ricardo Quesada <ricardoquesada@gmail.com> Date: Thu May 15 19:36:23 2014 -0700 files in the correct directory
2014-05-17 05:36:00 +08:00
#include "renderer/CCTexture2D.h"
#include "base/CCData.h"
// premultiply alpha, or the effect will be wrong when using other pixel formats in Texture2D,
// such as RGB888, RGB5A1
#define CC_RGB_PREMULTIPLY_ALPHA(vr, vg, vb, va) \
(unsigned)(((unsigned)((uint8_t)(vr) * ((uint8_t)(va) + 1)) >> 8) | \
((unsigned)((uint8_t)(vg) * ((uint8_t)(va) + 1) >> 8) << 8) | \
((unsigned)((uint8_t)(vb) * ((uint8_t)(va) + 1) >> 8) << 16) | \
((unsigned)(uint8_t)(va) << 24))
NS_CC_BEGIN
2012-06-20 18:09:11 +08:00
/**
* @addtogroup platform
* @{
*/
/**
@brief Structure which can tell where mipmap begins and how long is it
*/
2013-08-06 16:14:36 +08:00
typedef struct _MipmapInfo
{
uint8_t* address;
int len;
_MipmapInfo():address(NULL),len(0){}
}MipmapInfo;
2012-06-20 18:09:11 +08:00
class CC_DLL Image : public Ref
{
public:
2013-06-26 16:42:11 +08:00
friend class TextureCache;
/**
* @js ctor
*/
Image();
/**
* @js NA
* @lua NA
*/
virtual ~Image();
/** Supported formats for Image */
enum class Format
{
//! JPEG
JPG,
//! PNG
PNG,
2019-11-25 02:28:38 +08:00
//! BMP,
BMP,
//! WebP
WEBP,
//! PVR
PVR,
//! ETC1
ETC1,
//! ETC2
ETC2,
2013-08-06 11:19:45 +08:00
//! S3TC
S3TC,
//! ATITC
ATITC,
//! TGA
TGA,
2020-02-05 22:45:19 +08:00
//<2F><>ASTC
ASTC,
//! Raw Data
RAW_DATA,
//! Unknown format
2015-02-24 01:36:20 +08:00
UNKNOWN
};
/**
* Enables or disables premultiplied alpha for PNG files.
*
* @param enabled (default: true)
*/
static void setPNGPremultipliedAlphaEnabled(bool enabled) { PNG_PREMULTIPLIED_ALPHA_ENABLED = enabled; }
/** treats (or not) PVR files as if they have alpha premultiplied.
Since it is impossible to know at runtime if the PVR images have the alpha channel premultiplied, it is
possible load them as if they have (or not) the alpha channel premultiplied.
By default it is disabled.
*/
static void setPVRImagesHavePremultipliedAlpha(bool haveAlphaPremultiplied);
2012-12-29 10:27:43 +08:00
/**
@brief Load the image from the specified path.
@param path the absolute file path.
@return true if loaded correctly.
*/
bool initWithImageFile(const std::string& path);
/**
@brief Load image from stream buffer.
@param data stream buffer which holds the image data.
@param dataLen data length expressed in (number of) bytes.
@param ownData whether take the data memory management.
@return true if loaded correctly.
* @js NA
* @lua NA
*/
bool initWithImageData(const uint8_t* data, ssize_t dataLen, bool ownData = false);
// @warning kFmtRawData only support RGBA8888
bool initWithRawData(const uint8_t* data, ssize_t dataLen, int width, int height, int bitsPerComponent, bool preMulti = false);
// Getters
uint8_t * getData() { return _data + _offset; }
ssize_t getDataLen() { return _dataLen - _offset; }
Format getFileType() { return _fileType; }
backend::PixelFormat getPixelFormat() { return _pixelFormat; }
int getWidth() { return _width; }
int getHeight() { return _height; }
int getNumberOfMipmaps() { return _numberOfMipmaps; }
MipmapInfo* getMipmaps() { return _mipmaps; }
bool hasPremultipliedAlpha() { return _hasPremultipliedAlpha; }
std::string getFilePath() const { return _filePath; }
int getBitPerPixel();
bool hasAlpha();
bool isCompressed();
/**
@brief Save Image data to the specified file, with specified format.
@param filePath the file's absolute path, including file suffix.
@param isToRGB whether the image is saved as RGB format.
*/
bool saveToFile(const std::string &filename, bool isToRGB = true);
void premultiplyAlpha();
void reversePremultipliedAlpha();
protected:
2020-08-31 23:09:42 +08:00
typedef struct sImageTGA tImageTGA;
bool initWithJpgData(uint8_t* data, ssize_t dataLen);
bool initWithPngData(uint8_t* data, ssize_t dataLen);
bool initWithBmpData(uint8_t* data, ssize_t dataLen);
bool initWithWebpData(uint8_t* data, ssize_t dataLen);
bool initWithTGAData(tImageTGA* tgaData);
2020-08-31 23:09:42 +08:00
// All internal init function have chance to own the data for fast forward data to hardware decoder
// see: initWithImageData
bool initWithPVRData(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithPVRv2Data(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithPVRv3Data(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithETCData(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithETC2Data(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithASTCData(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithS3TCData(uint8_t* data, ssize_t dataLen, bool ownData);
bool initWithATITCData(uint8_t* data, ssize_t dataLen, bool ownData);
2020-08-31 23:09:42 +08:00
// fast forward pixels to GPU if ownData
void forwardPixels(uint8_t* data, ssize_t dataLen, int offset, bool ownData);
2013-08-06 16:14:36 +08:00
bool saveImageToPNG(const std::string& filePath, bool isToRGB = true);
bool saveImageToJPG(const std::string& filePath);
protected:
/**
@brief Determine how many mipmaps can we have.
It's same as define but it respects namespaces
*/
static const int MIPMAP_MAX = 16;
/**
@brief Determine whether we premultiply alpha for png files.
*/
static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
uint8_t *_data;
2013-12-05 17:19:01 +08:00
ssize_t _dataLen;
2020-08-31 21:04:47 +08:00
ssize_t _offset; // useful for hardware decoder present to hold data without copy
int _width;
int _height;
bool _unpack;
Format _fileType;
backend::PixelFormat _pixelFormat;
MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
int _numberOfMipmaps;
// false if we can't auto detect the image is premultiplied or not.
bool _hasPremultipliedAlpha;
std::string _filePath;
protected:
// noncopyable
Image(const Image& rImg);
Image& operator=(const Image&);
2013-06-26 16:42:11 +08:00
/*
@brief The same result as with initWithImageFile, but thread safe. It is caused by
loadImage() in TextureCache.cpp.
@param fullpath full path of the file.
@param imageType the type of image, currently only supporting two types.
@return true if loaded correctly.
*/
bool initWithImageFileThreadSafe(const std::string& fullpath);
2013-08-08 14:11:22 +08:00
Format detectFormat(const uint8_t* data, ssize_t dataLen);
bool isPng(const uint8_t* data, ssize_t dataLen);
bool isJpg(const uint8_t* data, ssize_t dataLen);
bool isBmp(const uint8_t* data, ssize_t dataLen);
bool isWebp(const uint8_t* data, ssize_t dataLen);
bool isPvr(const uint8_t* data, ssize_t dataLen);
bool isEtc1(const uint8_t* data, ssize_t dataLen);
bool isEtc2(const uint8_t* data, ssize_t dataLen);
bool isS3TC(const uint8_t* data, ssize_t dataLen);
bool isATITC(const uint8_t* data, ssize_t dataLen);
bool isASTC(const uint8_t* data, ssize_t dataLen);
};
2012-06-20 18:09:11 +08:00
// end of platform group
/// @}
NS_CC_END
2015-03-24 20:23:51 +08:00
/// @endcond
#endif // __CC_IMAGE_H__