2012-04-19 14:35:52 +08:00
|
|
|
/****************************************************************************
|
2014-01-07 11:25:07 +08:00
|
|
|
Copyright (c) 2010-2012 cocos2d-x.org
|
2018-01-29 16:25:32 +08:00
|
|
|
Copyright (c) 2013-2016 Chukong Technologies Inc.
|
|
|
|
Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd.
|
2022-04-25 19:15:46 +08:00
|
|
|
Copyright (c) 2020 C4games Ltd
|
2022-04-23 17:49:42 +08:00
|
|
|
Copyright (c) 2021-2022 Bytedance Inc.
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
https://axis-project.github.io/
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
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.
|
|
|
|
****************************************************************************/
|
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#ifndef __AX_IMAGE_H__
|
|
|
|
#define __AX_IMAGE_H__
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-04-27 01:11:22 +08:00
|
|
|
#include "base/CCRef.h"
|
2014-05-17 05:36:00 +08:00
|
|
|
#include "renderer/CCTexture2D.h"
|
2019-11-20 18:19:24 +08:00
|
|
|
#include "base/CCData.h"
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2018-05-30 20:41:59 +08:00
|
|
|
// premultiply alpha, or the effect will be wrong when using other pixel formats in Texture2D,
|
2013-06-13 05:46:32 +08:00
|
|
|
// such as RGB888, RGB5A1
|
2022-07-15 19:17:01 +08:00
|
|
|
#define AX_RGB_PREMULTIPLY_ALPHA(vr, vg, vb, va) \
|
2021-12-25 10:04:45 +08:00
|
|
|
(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))
|
2013-06-13 05:46:32 +08:00
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_BEGIN
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
/**
|
|
|
|
* @addtogroup platform
|
|
|
|
* @{
|
|
|
|
*/
|
2013-08-01 15:53:52 +08:00
|
|
|
|
2013-07-25 21:35:00 +08:00
|
|
|
/**
|
2013-08-01 15:53:52 +08:00
|
|
|
@brief Structure which can tell where mipmap begins and how long is it
|
|
|
|
*/
|
2013-08-06 16:14:36 +08:00
|
|
|
typedef struct _MipmapInfo
|
|
|
|
{
|
2020-09-01 14:57:18 +08:00
|
|
|
uint8_t* address;
|
2013-07-25 21:35:00 +08:00
|
|
|
int len;
|
2021-12-25 10:04:45 +08:00
|
|
|
_MipmapInfo() : address(NULL), len(0) {}
|
|
|
|
} MipmapInfo;
|
2012-06-20 18:09:11 +08:00
|
|
|
|
2022-07-09 01:23:11 +08:00
|
|
|
/** The Image class for loading all images supported by axis . */
|
2022-07-15 19:17:01 +08:00
|
|
|
class AX_DLL Image : public Ref
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
|
|
|
public:
|
2013-06-26 16:42:11 +08:00
|
|
|
friend class TextureCache;
|
2013-09-13 16:46:31 +08:00
|
|
|
/**
|
|
|
|
* @js ctor
|
|
|
|
*/
|
2013-06-20 14:13:12 +08:00
|
|
|
Image();
|
2013-09-13 11:41:20 +08:00
|
|
|
/**
|
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
|
|
|
*/
|
2013-07-23 18:26:26 +08:00
|
|
|
virtual ~Image();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-07-26 05:49:43 +08:00
|
|
|
/** Supported formats for Image */
|
|
|
|
enum class Format
|
2012-04-19 14:35:52 +08:00
|
|
|
{
|
2013-07-26 05:49:43 +08:00
|
|
|
//! JPEG
|
|
|
|
JPG,
|
|
|
|
//! PNG
|
|
|
|
PNG,
|
2019-11-25 02:28:38 +08:00
|
|
|
//! BMP,
|
|
|
|
BMP,
|
2013-07-26 05:49:43 +08:00
|
|
|
//! WebP
|
|
|
|
WEBP,
|
2013-07-27 22:06:30 +08:00
|
|
|
//! PVR
|
|
|
|
PVR,
|
2020-09-01 11:43:03 +08:00
|
|
|
//! ETC1
|
|
|
|
ETC1,
|
2020-08-30 17:58:39 +08:00
|
|
|
//! ETC2
|
|
|
|
ETC2,
|
2013-08-06 11:19:45 +08:00
|
|
|
//! S3TC
|
|
|
|
S3TC,
|
2013-08-16 11:02:44 +08:00
|
|
|
//! ATITC
|
|
|
|
ATITC,
|
2013-11-29 14:31:42 +08:00
|
|
|
//! TGA
|
|
|
|
TGA,
|
2021-12-24 20:43:40 +08:00
|
|
|
//! ASTC
|
2020-02-05 22:45:19 +08:00
|
|
|
ASTC,
|
2013-07-26 05:49:43 +08:00
|
|
|
//! Raw Data
|
|
|
|
RAW_DATA,
|
|
|
|
//! Unknown format
|
2015-02-24 01:36:20 +08:00
|
|
|
UNKNOWN
|
2013-07-25 20:29:53 +08:00
|
|
|
};
|
2021-12-25 10:04:45 +08:00
|
|
|
|
|
|
|
struct CompressedImagePMAFlag
|
|
|
|
{
|
|
|
|
enum : uint32_t
|
|
|
|
{
|
2021-06-27 20:58:50 +08:00
|
|
|
ASTC = 1,
|
2021-12-25 10:04:45 +08:00
|
|
|
DUAL_SAMPLER = 1 << 1, // for dual sampler, such as ETC1_RGB + ETC1_Alpha
|
2021-06-27 20:58:50 +08:00
|
|
|
ETC2 = 1 << 2,
|
|
|
|
PVR = 1 << 3,
|
|
|
|
ALL = 0xffff,
|
2020-11-16 12:21:27 +08:00
|
|
|
};
|
|
|
|
};
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2016-05-17 12:06:28 +08:00
|
|
|
/**
|
|
|
|
* Enables or disables premultiplied alpha for PNG files.
|
|
|
|
*
|
|
|
|
* @param enabled (default: true)
|
|
|
|
*/
|
|
|
|
static void setPNGPremultipliedAlphaEnabled(bool enabled) { PNG_PREMULTIPLIED_ALPHA_ENABLED = enabled; }
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-11-16 12:21:27 +08:00
|
|
|
/** The new APIs to treats (or not) compressed image files as if they have alpha premultiplied.
|
2021-12-25 10:04:45 +08:00
|
|
|
*
|
|
|
|
* By default, ETC1 + ETC1_ALPHA is enabled, because we do PMA at shader etc1.frag
|
|
|
|
* !!!IMPORTANT:
|
|
|
|
* The spine-runtimes official always regard texture has PMA, so you needs
|
|
|
|
* do compressed images PMA at texture convert tools or GPU fragment shader, such as
|
|
|
|
* positionTexture.frag, positionTextureColor.frag and others which have texture sampler,
|
|
|
|
* otherwise, the spine animations can't be render properly
|
|
|
|
*/
|
2020-11-16 12:21:27 +08:00
|
|
|
static void setCompressedImagesHavePMA(uint32_t targets, bool havePMA);
|
|
|
|
static bool isCompressedImageHavePMA(uint32_t target);
|
2016-05-17 12:06:28 +08:00
|
|
|
|
2012-12-29 10:27:43 +08:00
|
|
|
/**
|
2013-08-01 21:40:13 +08:00
|
|
|
@brief Load the image from the specified path.
|
|
|
|
@param path the absolute file path.
|
|
|
|
@return true if loaded correctly.
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool initWithImageFile(std::string_view path);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-08-01 21:40:13 +08:00
|
|
|
@brief Load image from stream buffer.
|
2013-07-25 21:35:00 +08:00
|
|
|
@param data stream buffer which holds the image data.
|
|
|
|
@param dataLen data length expressed in (number of) bytes.
|
2020-08-31 19:24:23 +08:00
|
|
|
@param ownData whether take the data memory management.
|
2012-09-15 06:26:38 +08:00
|
|
|
@return true if loaded correctly.
|
2013-09-13 11:41:20 +08:00
|
|
|
* @js NA
|
|
|
|
* @lua NA
|
2012-04-19 14:35:52 +08:00
|
|
|
*/
|
2021-05-24 13:26:56 +08:00
|
|
|
bool initWithImageData(const uint8_t* data, ssize_t dataLen);
|
|
|
|
bool initWithImageData(uint8_t* data, ssize_t dataLen, bool ownData);
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2013-06-13 05:46:32 +08:00
|
|
|
// @warning kFmtRawData only support RGBA8888
|
2021-12-25 10:04:45 +08:00
|
|
|
bool initWithRawData(const uint8_t* data,
|
|
|
|
ssize_t dataLen,
|
|
|
|
int width,
|
|
|
|
int height,
|
|
|
|
int bitsPerComponent,
|
|
|
|
bool preMulti = false);
|
2013-06-13 05:46:32 +08:00
|
|
|
|
2013-07-27 22:06:30 +08:00
|
|
|
// Getters
|
2021-12-25 10:04:45 +08:00
|
|
|
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; }
|
2016-09-12 09:44:21 +08:00
|
|
|
std::string getFilePath() const { return _filePath; }
|
2013-05-01 07:36:14 +08:00
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
int getBitPerPixel();
|
|
|
|
bool hasAlpha();
|
|
|
|
bool isCompressed();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
/**
|
2013-07-23 18:26:26 +08:00
|
|
|
@brief Save Image data to the specified file, with specified format.
|
2013-08-01 21:40:13 +08:00
|
|
|
@param filePath the file's absolute path, including file suffix.
|
|
|
|
@param isToRGB whether the image is saved as RGB format.
|
2013-07-23 18:26:26 +08:00
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool saveToFile(std::string_view filename, bool isToRGB = true);
|
2019-07-26 17:09:00 +08:00
|
|
|
void premultiplyAlpha();
|
2021-12-25 10:04:45 +08:00
|
|
|
void reversePremultipliedAlpha();
|
2012-04-19 14:35:52 +08:00
|
|
|
|
|
|
|
protected:
|
2020-08-31 23:09:42 +08:00
|
|
|
typedef struct sImageTGA tImageTGA;
|
2020-09-01 11:43:03 +08:00
|
|
|
|
2020-09-01 14:57:18 +08:00
|
|
|
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);
|
2013-11-29 14:31:42 +08:00
|
|
|
bool initWithTGAData(tImageTGA* tgaData);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-01 11:43:03 +08:00
|
|
|
// All internal init function have chance to own the data for fast forward data to hardware decoder
|
|
|
|
// see: initWithImageData
|
2020-09-01 14:57:18 +08:00
|
|
|
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);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-08-31 23:09:42 +08:00
|
|
|
// fast forward pixels to GPU if ownData
|
2020-09-01 14:57:18 +08:00
|
|
|
void forwardPixels(uint8_t* data, ssize_t dataLen, int offset, bool ownData);
|
2013-08-06 16:14:36 +08:00
|
|
|
|
2021-12-26 23:26:34 +08:00
|
|
|
bool saveImageToPNG(std::string_view filePath, bool isToRGB = true);
|
|
|
|
bool saveImageToJPG(std::string_view filePath);
|
2019-07-26 17:09:00 +08:00
|
|
|
|
2014-03-25 11:13:44 +08:00
|
|
|
protected:
|
2013-08-01 15:53:52 +08:00
|
|
|
/**
|
|
|
|
@brief Determine how many mipmaps can we have.
|
2015-09-22 16:08:23 +08:00
|
|
|
It's same as define but it respects namespaces
|
2013-08-01 15:53:52 +08:00
|
|
|
*/
|
|
|
|
static const int MIPMAP_MAX = 16;
|
2016-05-11 16:35:36 +08:00
|
|
|
/**
|
|
|
|
@brief Determine whether we premultiply alpha for png files.
|
|
|
|
*/
|
|
|
|
static bool PNG_PREMULTIPLIED_ALPHA_ENABLED;
|
2020-11-16 12:21:27 +08:00
|
|
|
static uint32_t COMPRESSED_IMAGE_PMA_FLAGS;
|
|
|
|
|
2021-12-25 10:04:45 +08:00
|
|
|
uint8_t* _data;
|
2013-12-05 17:19:01 +08:00
|
|
|
ssize_t _dataLen;
|
2021-12-25 10:04:45 +08:00
|
|
|
ssize_t _offset; // useful for hardware decoder present to hold data without copy
|
2013-07-25 21:35:00 +08:00
|
|
|
int _width;
|
|
|
|
int _height;
|
2014-03-20 17:07:28 +08:00
|
|
|
bool _unpack;
|
2013-07-27 22:06:30 +08:00
|
|
|
Format _fileType;
|
2019-06-03 09:39:51 +08:00
|
|
|
backend::PixelFormat _pixelFormat;
|
2021-12-25 10:04:45 +08:00
|
|
|
MipmapInfo _mipmaps[MIPMAP_MAX]; // pointer to mipmap images
|
2013-07-25 21:35:00 +08:00
|
|
|
int _numberOfMipmaps;
|
2015-09-22 16:08:23 +08:00
|
|
|
// false if we can't auto detect the image is premultiplied or not.
|
2013-07-30 11:10:05 +08:00
|
|
|
bool _hasPremultipliedAlpha;
|
2013-11-29 14:31:42 +08:00
|
|
|
std::string _filePath;
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2014-03-25 11:13:44 +08:00
|
|
|
protected:
|
2012-04-19 14:35:52 +08:00
|
|
|
// noncopyable
|
2016-05-11 16:35:36 +08:00
|
|
|
Image(const Image& rImg);
|
|
|
|
Image& operator=(const Image&);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
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.
|
|
|
|
*/
|
2021-12-26 23:26:34 +08:00
|
|
|
bool initWithImageFileThreadSafe(std::string_view fullpath);
|
2021-12-25 10:04:45 +08:00
|
|
|
|
2020-09-01 14:57:18 +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 isASTC(const uint8_t* data, ssize_t dataLen);
|
2012-04-19 14:35:52 +08:00
|
|
|
};
|
|
|
|
|
2012-06-20 18:09:11 +08:00
|
|
|
// end of platform group
|
|
|
|
/// @}
|
|
|
|
|
2022-07-11 17:50:21 +08:00
|
|
|
NS_AX_END
|
2012-04-19 14:35:52 +08:00
|
|
|
|
2022-07-15 19:17:01 +08:00
|
|
|
#endif // __AX_IMAGE_H__
|