fix CKContext prevobj error. finish bitmap reader / writer

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

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 Dst[out] 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
* @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 Dst[out] 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 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.
*/
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 ==========
//
///**
// * @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.
* @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
* @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.
* @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.
* @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
* @remark The VxImageDescEx holds basically an VxImageDesc with additionnal support for
* Colormap, Image pointer and is ready for future enhancements.
* VxImageDescEx describe the height, width,
* and etc for image.
* 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 {
VX_PIXELFORMAT Flags; /**< Reserved for special formats (such as compressed ) 0 otherwise */
class VxImageDescEx {
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 Height; /**< Height in pixel of the image */
union {
CK2::CKDWORD BytesPerLine; /**< Pitch (width in bytes) of the image */
CK2::CKDWORD TotalImageSize; /**< For compressed image (DXT1...) the total size of the 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);
CK2::CKDWORD GetImageSize() const {
return static_cast<CK2::CKDWORD>(sizeof(uint32_t) * m_Width * m_Height);
}
CK2::CKBYTE* GetImage() {
return m_Image;
}
bool operator==(const VxImageDescEx& rhs) const {
return (
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
);
CK2::CKDWORD GetPixelCount() const {
return static_cast<CK2::CKDWORD>(m_Width * m_Height);
}
bool operator!=(const VxImageDescEx& rhs) const {
return !((*this) == rhs);
CK2::CKDWORD* GetPixels() {
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 */
};
}