fix CKContext prevobj error. finish bitmap reader / writer

This commit is contained in:
yyc12345 2023-09-07 16:27:41 +08:00
parent 8a75eb9f21
commit f7f1478ecf
10 changed files with 655 additions and 57 deletions

View File

@ -87,11 +87,17 @@ namespace LibCmo::CK2 {
} }
ObjImpls::CKObject* GeneralPrevFinder(XContainer::XObjectPointerArray& objptrs, ObjImpls::CKObject* previous) { ObjImpls::CKObject* GeneralPrevFinder(XContainer::XObjectPointerArray& objptrs, ObjImpls::CKObject* previous) {
auto finder = std::find(objptrs.begin(), objptrs.end(), previous); if (objptrs.empty()) return nullptr;
if (finder == objptrs.end()) return nullptr;
++finder; if (previous == nullptr) {
if (finder == objptrs.end()) return nullptr; return objptrs.front();
return *finder; } else {
auto finder = std::find(objptrs.begin(), objptrs.end(), previous);
if (finder == objptrs.end()) return nullptr;
++finder;
if (finder == objptrs.end()) return nullptr;
return *finder;
}
} }
ObjImpls::CKObject* CKContext::GetObjectByName(CKSTRING name, ObjImpls::CKObject* previous) { ObjImpls::CKObject* CKContext::GetObjectByName(CKSTRING name, ObjImpls::CKObject* previous) {
if (name == nullptr) return nullptr; if (name == nullptr) return nullptr;

View File

@ -262,12 +262,11 @@ namespace LibCmo::CK2 {
//class CKFile; //class CKFile;
class CKDependencies; class CKDependencies;
class CKDependenciesContext; class CKDependenciesContext;
class CKPluginManager;
class CKDebugContext; class CKDebugContext;
class CKObjectArray; class CKObjectArray;
class CKObjectDeclaration; class CKObjectDeclaration;
//class CKContext; //class CKContext;
struct CKBitmapProperties; //struct CKBitmapProperties;
class CKVertexBuffer; class CKVertexBuffer;
//--- Managers //--- Managers
@ -284,6 +283,13 @@ namespace LibCmo::CK2 {
class CKPathManager; class CKPathManager;
class CKVariableManager; class CKVariableManager;
class CKSceneObjectDesc; class CKSceneObjectDesc;
class CKPluginManager;
}
namespace DataHandlers {
class CKBitmapHandler;
class CKMovieHandler;
class CKSoundHandler;
} }
//--- Important classes //--- Important classes
@ -360,5 +366,41 @@ namespace LibCmo::CK2 {
} }
}; };
/**
* The struct describe the bitmap handler's infomation,
* including its GUID and supported file extension.
* This struct also will store some parameters related to bitmap handler,
* such as jpeg compress level and etc. But currently there are no
* such parameters.
*/
struct CKBitmapProperties {
CKBitmapProperties() :
m_ReaderGuid(), m_Ext(3, '\0') {}
CKBitmapProperties(CKGUID& guid, const char* ext) :
m_ReaderGuid(guid), m_Ext(3, '\0') {
SetExt(ext);
}
LIBCMO_DEFAULT_COPY_MOVE(CKBitmapProperties);
void SetExt(const char* s) {
if (s == nullptr) {
m_Ext[0] = '\0';
} else {
if (s[0] == '.') ++s; // skip dot
size_t len = std::strlen(s);
if (len > m_Ext.size()) len = m_Ext.size();
std::memcpy(m_Ext.data(), s, len);
}
}
CKSTRING ExtToCKSTRING() {
return m_Ext.c_str();
}
CKGUID m_ReaderGuid; /**< CKGUID that uniquely identifies the reader that created this properties structure */
std::string m_Ext; /**< File Extension of the image being described by this structure */
};
} }

View File

@ -0,0 +1,274 @@
#include "CKBitmapHandler.hpp"
#include "stb_image.h"
#include "stb_image_write.h"
#include "stb_image_resize.h"
namespace LibCmo::CK2::DataHandlers {
static void RGBAToARGB(CK2::CKDWORD count, const void* _rgba, void* _argb) {
const char* rgba = reinterpret_cast<const char*>(_rgba);
char* argb = reinterpret_cast<char*>(_argb);
// copy RGB
VxMath::VxCopyStructure(
count,
argb + sizeof(uint8_t),
4 * sizeof(uint8_t),
3 * sizeof(uint8_t),
rgba,
4 * sizeof(uint8_t)
);
// copy A
VxMath::VxCopyStructure(
count,
argb,
4 * sizeof(uint8_t),
sizeof(uint8_t),
rgba + (3 * sizeof(uint8_t)),
4 * sizeof(uint8_t)
);
}
static void ARGBToRGBA(CK2::CKDWORD count, const void* _argb, void* _rgba) {
const char* argb = reinterpret_cast<const char*>(_argb);
char* rgba = reinterpret_cast<char*>(_rgba);
// copy RGB
VxMath::VxCopyStructure(
count,
rgba,
4 * sizeof(uint8_t),
3 * sizeof(uint8_t),
argb + sizeof(uint8_t),
4 * sizeof(uint8_t)
);
// copy A
VxMath::VxCopyStructure(
count,
rgba + (3 * sizeof(uint8_t)),
4 * sizeof(uint8_t),
sizeof(uint8_t),
argb,
4 * sizeof(uint8_t)
);
}
static void PostRead(stbi_uc* data, int x, int y, VxMath::VxImageDescEx* read_image) {
// scale image if need
if (x != read_image->GetWidth() || y != read_image->GetHeight()) {
// alloc
CKBYTE* newdata = new CKBYTE[read_image->GetImageSize()];
// resize
stbir_resize(
data, x, y, 0,
newdata, static_cast<int>(read_image->GetWidth()), static_cast<int>(read_image->GetHeight()), 0,
STBIR_TYPE_UINT8, 4, STBIR_ALPHA_CHANNEL_NONE, 0, // no alpha channel, mean we treat alpha channel as a normal color factor.
STBIR_EDGE_CLAMP, STBIR_EDGE_CLAMP,
STBIR_FILTER_BOX, STBIR_FILTER_BOX,
STBIR_COLORSPACE_SRGB, nullptr
);
// copy data
RGBAToARGB(read_image->GetPixelCount(), newdata, read_image->GetImage());
// free
delete[] newdata;
} else {
// copy data RGBA -> ARGB
RGBAToARGB(read_image->GetPixelCount(), data, read_image->GetImage());
}
}
static bool StbReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) {
if (u8filename == nullptr || read_image == nullptr) return false;
FILE* fs = EncodingHelper::U8FOpen(u8filename, "rb");
if (fs == nullptr) return false;
// read data
int x, y, channels_in_file;
stbi_uc* data = stbi_load_from_file(fs, &x, &y, &channels_in_file, 4); // 4 == RGBA8888
std::fclose(fs);
if (data == nullptr) return false;
// scale image if need
PostRead(data, x, y, read_image);
// clear data
stbi_image_free(data);
return true;
}
static bool StbReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) {
// same like ReadFile
if (memory == nullptr || read_image == nullptr) return false;
// read data
int x, y, channels_in_file;
stbi_uc* data = stbi_load_from_memory(
reinterpret_cast<const stbi_uc*>(memory),
static_cast<int>(size),
&x, &y, &channels_in_file, 4 // 4 == RGBA8888
);
if (data == nullptr) return false;
// scale image if need
PostRead(data, x, y, read_image);
// clear data
stbi_image_free(data);
return true;
}
struct FileSaveContext {
FileSaveContext(FILE* fs) :
m_Fs(fs), m_Counter(0) {}
FILE* m_Fs;
size_t m_Counter;
};
static void FileWriteFunction(void* context, void* data, int size) {
FileSaveContext* ctx = reinterpret_cast<FileSaveContext*>(context);
if (ctx->m_Fs != nullptr) {
std::fwrite(data, sizeof(char), size, ctx->m_Fs);
}
ctx->m_Counter += size;
}
struct MemorySaveContext {
MemorySaveContext(void* mem) :
m_Mem(mem), m_Counter(0) {}
void* m_Mem;
size_t m_Counter;
};
static void MemoryWriteFunction(void* context, void* data, int size) {
MemorySaveContext* ctx = reinterpret_cast<MemorySaveContext*>(context);
if (ctx->m_Mem != nullptr) {
std::memcpy(ctx->m_Mem, data, size);
ctx->m_Mem = reinterpret_cast<char*>(ctx->m_Mem) + size;
}
ctx->m_Counter += size;
}
using SaveOperation = std::function<int(stbi_write_func*, void*, int, int, int, const void*)>;
static bool StbSaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, SaveOperation oper) {
if (u8filename == nullptr || write_image == nullptr) return false;
FILE* fs = EncodingHelper::U8FOpen(u8filename, "wb");
if (fs == nullptr) return;
// allocate buffer and convert data from ARGB to RGBA
VxMath::VxImageDescEx* wi = const_cast<VxMath::VxImageDescEx*>(write_image);
CKBYTE* data = new CKBYTE[write_image->GetImageSize()];
ARGBToRGBA(write_image->GetPixelCount(), wi->GetImage(), data);
// write data
FileSaveContext* ctx = new FileSaveContext(fs);
int ret = oper(
&FileWriteFunction, ctx,
static_cast<int>(wi->GetWidth()), static_cast<int>(wi->GetHeight()),
4, data // 4 == RGBA8888
);
// free data
delete ctx;
delete[] data;
std::fclose(fs);
return ret == 0;
}
static CKDWORD StbSaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, SaveOperation oper) {
if (write_image == nullptr) return 0;
// allocate buffer and convert data from ARGB to RGBA
VxMath::VxImageDescEx* wi = const_cast<VxMath::VxImageDescEx*>(write_image);
CKBYTE* data = new CKBYTE[write_image->GetImageSize()];
ARGBToRGBA(write_image->GetPixelCount(), wi->GetImage(), data);
// write data
MemorySaveContext* ctx = new MemorySaveContext(memory);
int ret = oper(
&FileWriteFunction, ctx,
static_cast<int>(wi->GetWidth()), static_cast<int>(wi->GetHeight()),
4, data // 4 == RGBA8888
);
// free data
CKDWORD expected = static_cast<CKDWORD>(ctx->m_Counter);
delete ctx;
delete[] data;
if (ret == 0) return 0;
else return expected;
}
#pragma region CKBitmapBMPHandler
CKBitmapBMPHandler::CKBitmapBMPHandler() :
CKBitmapHandler(), c_DefaultProp() {
c_DefaultProp.SetExt("Tga");
c_DefaultProp.m_ReaderGuid.d1 = 0xBCA97223u;
c_DefaultProp.m_ReaderGuid.d2 = 0x48578BCAu;
}
CKBitmapBMPHandler::~CKBitmapBMPHandler() {}
const CKBitmapProperties& CKBitmapBMPHandler::GetBitmapDefaultProperties() {
return c_DefaultProp;
}
bool CKBitmapBMPHandler::ReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) {
StbReadFile(u8filename, read_image);
}
bool CKBitmapBMPHandler::ReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) {
StbReadMemory(memory, size, read_image);
}
bool CKBitmapBMPHandler::SaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) {
return StbSaveFile(u8filename, write_image,
[&codec_param](stbi_write_func* func, void* context, int w, int h, int comp, const void* data) -> int {
return stbi_write_bmp_to_func(func, context, w, h, comp, data);
});
}
CKDWORD CKBitmapBMPHandler::SaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) {
return StbSaveMemory(memory, write_image,
[&codec_param](stbi_write_func* func, void* context, int w, int h, int comp, const void* data) -> int {
return stbi_write_bmp_to_func(func, context, w, h, comp, data);
});
}
#pragma endregion
#pragma region CKBitmapTGAHandler
CKBitmapTGAHandler::CKBitmapTGAHandler() :
CKBitmapHandler(), c_DefaultProp() {
c_DefaultProp.SetExt("Tga");
c_DefaultProp.m_ReaderGuid.d1 = 0x585C7216u;
c_DefaultProp.m_ReaderGuid.d2 = 0x33302657u;
}
CKBitmapTGAHandler::~CKBitmapTGAHandler() {}
const CKBitmapProperties& CKBitmapTGAHandler::GetBitmapDefaultProperties() {
return c_DefaultProp;
}
bool CKBitmapTGAHandler::ReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) {
StbReadFile(u8filename, read_image);
}
bool CKBitmapTGAHandler::ReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) {
StbReadMemory(memory, size, read_image);
}
bool CKBitmapTGAHandler::SaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) {
return StbSaveFile(u8filename, write_image,
[&codec_param](stbi_write_func* func, void* context, int w, int h, int comp, const void* data) -> int {
return stbi_write_bmp_to_func(func, context, w, h, comp, data);
});
}
CKDWORD CKBitmapTGAHandler::SaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) {
return StbSaveMemory(memory, write_image,
[&codec_param](stbi_write_func* func, void* context, int w, int h, int comp, const void* data) -> int {
return stbi_write_bmp_to_func(func, context, w, h, comp, data);
});
}
#pragma endregion
}

View File

@ -0,0 +1,98 @@
#pragma once
#include "../../VTAll.hpp"
namespace LibCmo::CK2::DataHandlers {
/**
* The interface about processing bitmap data between raw data and specific data.
* This interface will be capable to converting specific bitmap data into raw ARGB8888 raw data,
* or convert them back.
*/
class CKBitmapHandler {
public:
CKBitmapHandler() {}
virtual ~CKBitmapHandler() {}
LIBCMO_DISABLE_COPY_MOVE(CKBitmapHandler);
/**
* @brief Returns the current default bitmap options.
* @return Current default bitmap options
*/
virtual const CKBitmapProperties& GetBitmapDefaultProperties() = 0;
/**
@brief Loads a bitmap file.
@return Returns true if successful.
@param u8filename[in] The file ready to read.
@param read_image[out] The pointer point to existed image desc which describe this image HW.
@see ReadMemory
*/
virtual bool ReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) = 0;
/**
@brief Loads a bitmap file stored in memory.
@return Returns true if successful.
@param memory[in] The pointer to memory.
@param size[in] The size of memory.
@param read_image[out] The pointer point to existed image desc which describe this image HW.
@see ReadFile
*/
virtual bool ReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) = 0;
/**
@brief Saves a image to a file
@return Returns true if successful.
@param u8filename[in] The file ready to write.
@param write_image[in] The image will be written in file.
@param codec_param[in] The written image format parameter.
@see SaveMemory
*/
virtual bool SaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) = 0;
/**
@brief Saves an image into a memory block.
@return Returns the number of written bytes if successful, 0 otherwise.
@param memory[in] The memory where the image will be written. nullptr if you need a dry run to get how many bytes you need to allocate.
@param write_image[in] The image will be written in file.
@param codec_param[in] The written image format parameter.
@remark
+ You should call this method first to get how much bytes will be written.
+ Then you allocate a proper buffer.
+ Finally, call this method with your allocated buffer to get the result.
@see SaveFile
*/
virtual CKDWORD SaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) = 0;
};
class CKBitmapBMPHandler : public CKBitmapHandler {
public:
CKBitmapBMPHandler();
virtual ~CKBitmapBMPHandler();
LIBCMO_DISABLE_COPY_MOVE(CKBitmapBMPHandler);
virtual const CKBitmapProperties& GetBitmapDefaultProperties() override;
virtual bool ReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) override;
virtual bool ReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) override;
virtual bool SaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) override;
virtual CKDWORD SaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) override;
protected:
CKBitmapProperties c_DefaultProp;
};
class CKBitmapTGAHandler : public CKBitmapHandler {
public:
CKBitmapTGAHandler();
virtual ~CKBitmapTGAHandler();
LIBCMO_DISABLE_COPY_MOVE(CKBitmapTGAHandler);
virtual const CKBitmapProperties& GetBitmapDefaultProperties() override;
virtual bool ReadFile(CKSTRING u8filename, VxMath::VxImageDescEx* read_image) override;
virtual bool ReadMemory(const void* memory, CKDWORD size, VxMath::VxImageDescEx* read_image) override;
virtual bool SaveFile(CKSTRING u8filename, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) override;
virtual CKDWORD SaveMemory(void* memory, const VxMath::VxImageDescEx* write_image, const CKBitmapProperties& codec_param) override;
protected:
CKBitmapProperties c_DefaultProp;
};
}

View File

@ -178,6 +178,7 @@
<ClCompile Include="CK2\CKFileOthers.cpp" /> <ClCompile Include="CK2\CKFileOthers.cpp" />
<ClCompile Include="CK2\CKFileWriter.cpp" /> <ClCompile Include="CK2\CKFileWriter.cpp" />
<ClCompile Include="CK2\CKGlobals.cpp" /> <ClCompile Include="CK2\CKGlobals.cpp" />
<ClCompile Include="CK2\DataHandlers\CKBitmapHandler.cpp" />
<ClCompile Include="CK2\MgrImpls\CKBaseManager.cpp" /> <ClCompile Include="CK2\MgrImpls\CKBaseManager.cpp" />
<ClCompile Include="CK2\CKContext.cpp" /> <ClCompile Include="CK2\CKContext.cpp" />
<ClCompile Include="CK2\MgrImpls\CKObjectManager.cpp" /> <ClCompile Include="CK2\MgrImpls\CKObjectManager.cpp" />
@ -191,7 +192,9 @@
<ClCompile Include="VTEncoding.cpp" /> <ClCompile Include="VTEncoding.cpp" />
<ClCompile Include="CK2\CKFileReader.cpp" /> <ClCompile Include="CK2\CKFileReader.cpp" />
<ClCompile Include="CK2\CKStateChunk.cpp" /> <ClCompile Include="CK2\CKStateChunk.cpp" />
<ClCompile Include="VTImage.cpp" />
<ClCompile Include="VTUtils.cpp" /> <ClCompile Include="VTUtils.cpp" />
<ClCompile Include="VxMath\VxMath.cpp" />
<ClCompile Include="VxMath\VxMemoryMappedFile.cpp" /> <ClCompile Include="VxMath\VxMemoryMappedFile.cpp" />
<ClCompile Include="XContainer\XBitArray.cpp" /> <ClCompile Include="XContainer\XBitArray.cpp" />
</ItemGroup> </ItemGroup>
@ -201,6 +204,7 @@
<ClInclude Include="CK2\CKFile.hpp" /> <ClInclude Include="CK2\CKFile.hpp" />
<ClInclude Include="CK2\CKGlobals.hpp" /> <ClInclude Include="CK2\CKGlobals.hpp" />
<ClInclude Include="CK2\CKIdentifiers.hpp" /> <ClInclude Include="CK2\CKIdentifiers.hpp" />
<ClInclude Include="CK2\DataHandlers\CKBitmapHandler.hpp" />
<ClInclude Include="CK2\MgrImpls\CKBaseManager.hpp" /> <ClInclude Include="CK2\MgrImpls\CKBaseManager.hpp" />
<ClInclude Include="CK2\CKContext.hpp" /> <ClInclude Include="CK2\CKContext.hpp" />
<ClInclude Include="CK2\CKStateChunk.hpp" /> <ClInclude Include="CK2\CKStateChunk.hpp" />

View File

@ -43,6 +43,12 @@
<Filter Include="Sources\CK2\ObjImpls"> <Filter Include="Sources\CK2\ObjImpls">
<UniqueIdentifier>{a8cd2188-b552-478c-9801-a6286b3d48a0}</UniqueIdentifier> <UniqueIdentifier>{a8cd2188-b552-478c-9801-a6286b3d48a0}</UniqueIdentifier>
</Filter> </Filter>
<Filter Include="Headers\CK2\DataHandlers">
<UniqueIdentifier>{d66924c0-b040-4f5d-8a8e-87d6d412af38}</UniqueIdentifier>
</Filter>
<Filter Include="Sources\CK2\DataHandlers">
<UniqueIdentifier>{cff15b64-e076-47af-9719-74af31d1804d}</UniqueIdentifier>
</Filter>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClCompile Include="VTEncoding.cpp"> <ClCompile Include="VTEncoding.cpp">
@ -102,6 +108,15 @@
<ClCompile Include="CK2\MgrImpls\CKPathManager.cpp"> <ClCompile Include="CK2\MgrImpls\CKPathManager.cpp">
<Filter>Sources\CK2\MgrImpls</Filter> <Filter>Sources\CK2\MgrImpls</Filter>
</ClCompile> </ClCompile>
<ClCompile Include="CK2\DataHandlers\CKBitmapHandler.cpp">
<Filter>Sources\CK2\DataHandlers</Filter>
</ClCompile>
<ClCompile Include="VTImage.cpp">
<Filter>Sources</Filter>
</ClCompile>
<ClCompile Include="VxMath\VxMath.cpp">
<Filter>Sources\VxMath</Filter>
</ClCompile>
</ItemGroup> </ItemGroup>
<ItemGroup> <ItemGroup>
<ClInclude Include="VTUtils.hpp"> <ClInclude Include="VTUtils.hpp">
@ -185,5 +200,8 @@
<ClInclude Include="VxMath\VxMath.hpp"> <ClInclude Include="VxMath\VxMath.hpp">
<Filter>Headers\VxMath</Filter> <Filter>Headers\VxMath</Filter>
</ClInclude> </ClInclude>
<ClInclude Include="CK2\DataHandlers\CKBitmapHandler.hpp">
<Filter>Headers\CK2\DataHandlers</Filter>
</ClInclude>
</ItemGroup> </ItemGroup>
</Project> </Project>

9
LibCmo/VTImage.cpp Normal file
View File

@ -0,0 +1,9 @@
// Bad wrapper for stb image library.
#define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h"
#define STB_IMAGE_WRITE_IMPLEMENTATION
#include "stb_image_write.h"
#define STB_IMAGE_RESIZE_IMPLEMENTATION
#include "stb_image_resize.h"

109
LibCmo/VxMath/VxMath.cpp Normal file
View File

@ -0,0 +1,109 @@
#include "VxMath.hpp"
namespace LibCmo::VxMath {
#pragma region Structure copying
void VxFillStructure(CK2::CKDWORD Count, void* Dst, CK2::CKDWORD Stride, CK2::CKDWORD SizeSrc, const void* Src) {
VxCopyStructure(Count, Dst, Stride, SizeSrc, Src, SizeSrc);
}
void VxCopyStructure(CK2::CKDWORD Count, void* Dst, CK2::CKDWORD OutStride, CK2::CKDWORD SizeSrc, const void* Src, CK2::CKDWORD InStride) {
if (Dst == nullptr || Src == nullptr) return;
char* cdst = reinterpret_cast<char*>(Dst);
const char* csrc = reinterpret_cast<const char*>(Src);
for (CK2::CKDWORD i = 0; i < Count; ++i) {
std::memcpy(cdst, csrc, SizeSrc);
cdst += OutStride;
csrc += InStride;
}
}
#pragma endregion
#pragma region Graphic Utilities
//CK2::CKDWORD VxGetBitCount(CK2::CKDWORD dwMask) {
// if (dwMask == 0u) return 0;
// dwMask >>= VxGetBitShift(dwMask);
// CK2::CKDWORD count = 0;
// while ((dwMask & 1u) == 0u) {
// dwMask >>= 1u;
// ++count;
// }
// return count;
//}
//CK2::CKDWORD VxGetBitShift(CK2::CKDWORD dwMask) {
// if (dwMask == 0u) return 0;
// CK2::CKDWORD count = 0;
// while ((dwMask & 1u) != 0u) {
// dwMask >>= 1u;
// ++count;
// }
// return count;
//}
//CK2::CKDWORD VxScaleFactor(CK2::CKDWORD val, CK2::CKDWORD srcBitCount, CK2::CKDWORD dstBitCount) {
// if (srcBitCount == dstBitCount) return val;
// if (srcBitCount > dstBitCount) {
// return val >> (srcBitCount - dstBitCount);
// } else {
// return val << (dstBitCount - srcBitCount);
// }
//}
void VxDoAlphaBlit(VxImageDescEx* dst_desc, CK2::CKBYTE AlphaValue) {
if (dst_desc == nullptr) return;
CK2::CKDWORD* pixels = dst_desc->GetPixels();
CK2::CKDWORD pixelcount = dst_desc->GetPixelCount();
for (CK2::CKDWORD i = 0; i < pixelcount; ++i) {
*pixels = (*pixels) & 0xFF000000 | AlphaValue;
++pixels;
}
//CK2::CKDWORD* pixels = dst_desc->GetPixels();
//CK2::CKDWORD pixelcount = dst_desc->GetPixelCount(),
// alphashift = VxGetBitShift(dst_desc->m_AlphaMask),
// alphacount = VxGetBitCount(dst_desc->m_AlphaMask);
//CK2::CKDWORD av = VxScaleFactor(AlphaValue, static_cast<CK2::CKDWORD>(sizeof(CK2::CKBYTE) * 8), alphacount) << alphashift;
//for (CK2::CKDWORD i = 0; i < pixelcount; ++i) {
// *pixels = (*pixels) & (~dst_desc->m_AlphaMask) | av;
// ++pixels;
//}
}
void VxDoAlphaBlit(VxImageDescEx* dst_desc, CK2::CKBYTE* AlphaValues) {
if (dst_desc == nullptr) return;
CK2::CKDWORD* pixels = dst_desc->GetPixels();
CK2::CKDWORD pixelcount = dst_desc->GetPixelCount();
for (CK2::CKDWORD i = 0; i < pixelcount; ++i) {
*pixels = (*pixels) & 0xFF000000 | (*AlphaValues);
++pixels;
++AlphaValues;
}
//CK2::CKDWORD* pixels = dst_desc->GetPixels();
//CK2::CKDWORD pixelcount = dst_desc->GetPixelCount(),
// alphashift = VxGetBitShift(dst_desc->m_AlphaMask),
// alphacount = VxGetBitCount(dst_desc->m_AlphaMask);
//for (CK2::CKDWORD i = 0; i < pixelcount; ++i) {
// *pixels = (*pixels) & (~dst_desc->m_AlphaMask) | (VxScaleFactor(*AlphaValues, static_cast<CK2::CKDWORD>(sizeof(CK2::CKBYTE) * 8), alphacount) << alphashift);
// ++pixels;
// ++AlphaValues;
//}
}
#pragma endregion
}

View File

@ -11,7 +11,7 @@ namespace LibCmo::VxMath {
* @param Count[in] Number of element to set in the destination buffer * @param Count[in] Number of element to set in the destination buffer
* @param Dst[out] Destination buffer * @param Dst[out] Destination buffer
* @param Stride[in] Amount in bytes between each element in the destination buffer * @param Stride[in] Amount in bytes between each element in the destination buffer
* @param SizeSrc[in] Size in bytes (but must be a multiple of 4) of an element int the Src buffer * @param SizeSrc[in] Size in bytes of an element int the Src buffer
* @param Src[in] Source buffer * @param Src[in] Source buffer
* @remark This function can be used to initialized an array of structure when only some members should be modified. * @remark This function can be used to initialized an array of structure when only some members should be modified.
*/ */
@ -21,29 +21,56 @@ namespace LibCmo::VxMath {
* @param Count[in] Number of element to copy in the destination buffer * @param Count[in] Number of element to copy in the destination buffer
* @param Dst[out] Destination buffer * @param Dst[out] Destination buffer
* @param OutStride[in] Amount in bytes between each element in the destination buffer * @param OutStride[in] Amount in bytes between each element in the destination buffer
* @param SizeSrc[in] Size in bytes (but must be a multiple of 4) of an element * @param SizeSrc[in] Size in bytes of an element
* @param Src[in] Source buffer. * @param Src[in] Source buffer.
* @param InStride[in] Amount in bytes between each element in the source buffer * @param InStride[in] Amount in bytes between each element in the source buffer
* @remark This function can be used to initialized an array of structure when only some members should be modified. * @remark This function can be used to initialized an array of structure when only some members should be modified.
*/ */
void VxCopyStructure(CK2::CKDWORD Count, void* Dst, CK2::CKDWORD OutStride, CK2::CKDWORD SizeSrc, const void* Src,CK2::CKDWORD InStride); void VxCopyStructure(CK2::CKDWORD Count, void* Dst, CK2::CKDWORD OutStride, CK2::CKDWORD SizeSrc, const void* Src, CK2::CKDWORD InStride);
// ========== Graphic Utilities ========== // ========== Graphic Utilities ==========
//
///**
// * @brief Copy origin image to dest image.
// * Supporting for size scaling. If the size is matched, just a direct copy.
// * @param dest[out] The dest image.
// * @param origin[in] The origin image.
//*/
//void VxDoBlit(VxImageDescEx* dst, const VxImageDescEx* origin);
///**
// * @brief Counts number of bits to representing a value in dwMask
//*/
//CK2::CKDWORD VxGetBitCount(CK2::CKDWORD dwMask);
///**
// * @brief Counts number of bits to shift to acces a non zero value in dwMask.
//*/
//CK2::CKDWORD VxGetBitShift(CK2::CKDWORD dwMask);
///**
// * @brief scale the integer to a new range.
// * @param val The int need to be scale
// * @param srcBitCount The bit count which source integer consumed.
// * @param dstBitCount The bit count which dest integer consumed.
// * @remark This function usually used in image color factor assign with mask.
// * @return The result integer.
//*/
//CK2::CKDWORD VxScaleFactor(CK2::CKDWORD val, CK2::CKDWORD srcBitCount, CK2::CKDWORD dstBitCount);
/** /**
* @brief Sets the alpha component of an image. * @brief Sets the alpha component of an image.
* @param dst_desc[in] A pointer to a structure describing the destination image format. * @param dst_desc[in] A pointer to a structure describing the destination image format.
* @param AlphaValue[in] A CKBYTE value containing the alpha value to set to the whole image * @param AlphaValue[in] A CKBYTE value containing the alpha value to set to the whole image
* @remark If the destination image does not have alpha information the function returns immediatly. * @remark If the destination image does not have alpha information the function returns immediatly.
*/ */
void VxDoAlphaBlit(const VxImageDescEx* dst_desc, CK2::CKBYTE AlphaValue); void VxDoAlphaBlit(VxImageDescEx* dst_desc, CK2::CKBYTE AlphaValue);
/** /**
* @brief Sets the alpha component of an image. * @brief Sets the alpha component of an image.
* @param dst_desc[in] A pointer to a structure describing the destination image format. * @param dst_desc[in] A pointer to a structure describing the destination image format.
* @param AlphaValues[in] A BYTE array containing the alpha values for each pixel. This array should be allocated to Width*Height bytes. * @param AlphaValues[in] A BYTE array containing the alpha values for each pixel. This array should be allocated to Width*Height bytes.
* @remark If the destination image does not have alpha information the function returns immediatly. * @remark If the destination image does not have alpha information the function returns immediatly.
*/ */
void VxDoAlphaBlit(const VxImageDescEx* dst_desc, CK2::CKBYTE* AlphaValues); void VxDoAlphaBlit(VxImageDescEx* dst_desc, CK2::CKBYTE* AlphaValues);
} }

View File

@ -70,57 +70,68 @@ namespace LibCmo::VxMath {
}; };
/** /**
* @brief Enhanced Image description * VxImageDescEx describe the height, width,
* @remark The VxImageDescEx holds basically an VxImageDesc with additionnal support for * and etc for image.
* Colormap, Image pointer and is ready for future enhancements. * Also it hold a pointer to raw image data.
* The image data must be 32bit format.
* Thus the size of Image must be 4 * Width * Height.
*/ */
struct VxImageDescEx { class VxImageDescEx {
VX_PIXELFORMAT Flags; /**< Reserved for special formats (such as compressed ) 0 otherwise */ public:
VxImageDescEx(CK2::CKDWORD height, CK2::CKDWORD width) :
m_Width(width), m_Height(height),
//m_RedMask(0), m_GreenMask(0), m_BlueMask(0), m_AlphaMask(0),
m_Image(nullptr){
m_Image = new CK2::CKBYTE[GetImageSize()];
}
~VxImageDescEx() {
delete[] m_Image;
}
LIBCMO_DISABLE_COPY_MOVE(VxImageDescEx);
CK2::CKDWORD Width; /**< Width in pixel of the image */ CK2::CKDWORD GetImageSize() const {
CK2::CKDWORD Height; /**< Height in pixel of the image */ return static_cast<CK2::CKDWORD>(sizeof(uint32_t) * m_Width * m_Height);
union { }
CK2::CKDWORD BytesPerLine; /**< Pitch (width in bytes) of the image */ CK2::CKBYTE* GetImage() {
CK2::CKDWORD TotalImageSize; /**< For compressed image (DXT1...) the total size of the image */ return m_Image;
};
CK2::CKINT BitsPerPixel; /**< Number of bits per pixel */
union {
CK2::CKDWORD RedMask; /**< Mask for Red component */
CK2::CKDWORD BumpDuMask; /**< Mask for Bump Du component */
};
union {
CK2::CKDWORD GreenMask; /**< Mask for Green component */
CK2::CKDWORD BumpDvMask; /**< Mask for Bump Dv component */
};
union {
CK2::CKDWORD BlueMask; /**< Mask for Blue component */
CK2::CKDWORD BumpLumMask; /**< Mask for Luminance component */
};
CK2::CKDWORD AlphaMask; /**< Mask for Alpha component */
CK2::CKWORD BytesPerColorEntry; /**< ColorMap Stride */
CK2::CKWORD ColorMapEntries; /**< If other than 0 image is palletized */
CK2::CKBYTE* ColorMap; /**< Palette colors */
CK2::CKBYTE* Image; /**< Image */
bool HasAlpha() {
return (AlphaMask == 0 || Flags >= VX_PIXELFORMAT::_DXT1);
} }
bool operator==(const VxImageDescEx& rhs) const { CK2::CKDWORD GetPixelCount() const {
return ( return static_cast<CK2::CKDWORD>(m_Width * m_Height);
Height == rhs.Height && Width == rhs.Width &&
BitsPerPixel == rhs.BitsPerPixel && BytesPerLine == rhs.BytesPerLine &&
RedMask == rhs.RedMask && GreenMask == rhs.GreenMask && BlueMask == rhs.BlueMask && AlphaMask == rhs.AlphaMask &&
BytesPerColorEntry == rhs.BytesPerColorEntry && ColorMapEntries == rhs.ColorMapEntries
);
} }
bool operator!=(const VxImageDescEx& rhs) const { CK2::CKDWORD* GetPixels() {
return !((*this) == rhs); return reinterpret_cast<CK2::CKDWORD*>(m_Image);
} }
CK2::CKDWORD GetWidth() const {
return m_Width;
}
CK2::CKDWORD GetHeight() const {
return m_Height;
}
bool IsHWEqual(const VxImageDescEx& rhs) const {
return (m_Width == rhs.m_Width && m_Height == rhs.m_Height);
}
// bool IsMaskEqual(const VxImageDescEx& rhs) const {
// return (
// m_RedMask == rhs.m_RedMask &&
// m_GreenMask == rhs.m_GreenMask &&
// m_BlueMask == rhs.m_BlueMask &&
// m_AlphaMask == rhs.m_AlphaMask
// );
// }
//public:
// CK2::CKDWORD m_RedMask;
// CK2::CKDWORD m_GreenMask;
// CK2::CKDWORD m_BlueMask;
// CK2::CKDWORD m_AlphaMask;
protected:
CK2::CKDWORD m_Width; /**< Width in pixel of the image */
CK2::CKDWORD m_Height; /**< Height in pixel of the image */
CK2::CKBYTE* m_Image; /**< A pointer point to current processing image */
}; };
} }