2023-09-07 16:27:41 +08:00
|
|
|
#include "VxMath.hpp"
|
2023-10-01 23:48:55 +08:00
|
|
|
#include <cmath>
|
2024-11-04 17:19:51 +08:00
|
|
|
#include <deprecated/stb_image_resize.h>
|
2023-09-24 20:56:23 +08:00
|
|
|
|
2023-09-07 16:27:41 +08:00
|
|
|
namespace LibCmo::VxMath {
|
|
|
|
|
|
|
|
#pragma region Structure copying
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
void VxFillStructure(CKDWORD Count, void* Dst, CKDWORD Stride, CKDWORD SizeSrc, const void* Src) {
|
2023-09-07 16:27:41 +08:00
|
|
|
VxCopyStructure(Count, Dst, Stride, SizeSrc, Src, SizeSrc);
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
void VxCopyStructure(CKDWORD Count, void* Dst, CKDWORD OutStride, CKDWORD SizeSrc, const void* Src, CKDWORD InStride) {
|
2024-08-21 17:57:35 +08:00
|
|
|
if ((Dst == nullptr || Src == nullptr) && Count != 0u)
|
|
|
|
throw LogicException("Source or destination buffer should not be nullptr.");
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-17 10:38:46 +08:00
|
|
|
CKBYTE* cdst = static_cast<CKBYTE*>(Dst);
|
|
|
|
const CKBYTE* csrc = static_cast<const CKBYTE*>(Src);
|
2023-09-16 18:31:25 +08:00
|
|
|
for (CKDWORD i = 0; i < Count; ++i) {
|
2023-09-07 16:27:41 +08:00
|
|
|
std::memcpy(cdst, csrc, SizeSrc);
|
|
|
|
cdst += OutStride;
|
|
|
|
csrc += InStride;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
|
|
|
#pragma region Graphic Utilities
|
|
|
|
|
2023-09-11 14:39:07 +08:00
|
|
|
void VxDoBlit(const VxImageDescEx* origin, VxImageDescEx* dst) {
|
2024-08-21 17:57:35 +08:00
|
|
|
if (dst == nullptr || origin == nullptr)
|
|
|
|
throw LogicException("VxImageDescEx* should not be nullptr.");
|
|
|
|
if (!dst->IsValid() || !origin->IsValid())
|
|
|
|
throw LogicException("VxImageDescEx* should not be invalid.");
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-11 14:39:07 +08:00
|
|
|
// if have same size, directly copy it
|
|
|
|
if (dst->IsHWEqual(*origin)) {
|
|
|
|
std::memcpy(dst->GetMutableImage(), origin->GetImage(), dst->GetImageSize());
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// perform resize by stb
|
|
|
|
stbir_resize(
|
|
|
|
origin->GetImage(), static_cast<int>(origin->GetWidth()), static_cast<int>(origin->GetHeight()), 0,
|
|
|
|
dst->GetMutableImage(), static_cast<int>(dst->GetWidth()), static_cast<int>(dst->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
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void VxDoBlitUpsideDown(const VxImageDescEx* origin, VxImageDescEx* dst) {
|
2024-08-21 17:57:35 +08:00
|
|
|
if (dst == nullptr || origin == nullptr)
|
|
|
|
throw LogicException("VxImageDescEx* should not be nullptr.");
|
|
|
|
if (!dst->IsValid() || !origin->IsValid())
|
|
|
|
throw LogicException("VxImageDescEx* should not be invalid.");
|
2023-09-11 14:39:07 +08:00
|
|
|
|
|
|
|
// if size is not matched, return
|
|
|
|
if (!dst->IsHWEqual(*origin)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// copy and swap data by line
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD height = dst->GetHeight(),
|
2023-09-12 20:49:19 +08:00
|
|
|
rowsize = VxImageDescEx::PixelSize * dst->GetWidth();
|
2023-09-16 18:31:25 +08:00
|
|
|
for (CKDWORD row = 0; row < height; ++row) {
|
2023-09-11 14:39:07 +08:00
|
|
|
std::memcpy(
|
|
|
|
dst->GetMutableImage() + (row * rowsize),
|
2023-09-12 20:49:19 +08:00
|
|
|
origin->GetImage() + ((height - row - 1) * rowsize),
|
2023-09-11 14:39:07 +08:00
|
|
|
rowsize
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD VxGetBitCount(CKDWORD dwMask) {
|
2023-09-11 14:39:07 +08:00
|
|
|
if (dwMask == 0u) return 0;
|
|
|
|
dwMask >>= VxGetBitShift(dwMask);
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD count = 0;
|
2023-09-11 14:39:07 +08:00
|
|
|
while ((dwMask & 1u) == 0u) {
|
|
|
|
dwMask >>= 1u;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD VxGetBitShift(CKDWORD dwMask) {
|
2023-09-11 14:39:07 +08:00
|
|
|
if (dwMask == 0u) return 0;
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD count = 0;
|
2023-09-11 14:39:07 +08:00
|
|
|
while ((dwMask & 1u) != 0u) {
|
|
|
|
dwMask >>= 1u;
|
|
|
|
++count;
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
//CKDWORD VxScaleFactor(CKDWORD val, CKDWORD srcBitCount, CKDWORD dstBitCount) {
|
2023-09-07 16:27:41 +08:00
|
|
|
// if (srcBitCount == dstBitCount) return val;
|
|
|
|
// if (srcBitCount > dstBitCount) {
|
|
|
|
// return val >> (srcBitCount - dstBitCount);
|
|
|
|
// } else {
|
|
|
|
// return val << (dstBitCount - srcBitCount);
|
|
|
|
// }
|
|
|
|
//}
|
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
void VxDoAlphaBlit(VxImageDescEx* dst_desc, CKBYTE AlphaValue) {
|
2024-08-21 17:57:35 +08:00
|
|
|
if (dst_desc == nullptr || !dst_desc->IsValid())
|
|
|
|
throw LogicException("VxImageDescEx* should not be nullptr or invalid.");
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD* pixels = dst_desc->GetMutablePixels();
|
|
|
|
CKDWORD pixelcount = dst_desc->GetPixelCount();
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
for (CKDWORD i = 0; i < pixelcount; ++i) {
|
|
|
|
*pixels = (*pixels) & 0x00FFFFFF | (static_cast<CKDWORD>(AlphaValue) << 24);
|
2023-09-07 16:27:41 +08:00
|
|
|
++pixels;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-18 23:11:33 +08:00
|
|
|
void VxDoAlphaBlit(VxImageDescEx* dst_desc, const CKBYTE* AlphaValues) {
|
2024-08-21 17:57:35 +08:00
|
|
|
if (dst_desc == nullptr || !dst_desc->IsValid() || AlphaValues == nullptr)
|
|
|
|
throw LogicException("VxImageDescEx* should not be nullptr or invalid.");
|
|
|
|
if (AlphaValues == nullptr)
|
|
|
|
throw LogicException("Alpha channel buffer should not be nullptr.");
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
CKDWORD* pixels = dst_desc->GetMutablePixels();
|
|
|
|
CKDWORD pixelcount = dst_desc->GetPixelCount();
|
2023-09-07 16:27:41 +08:00
|
|
|
|
2023-09-16 18:31:25 +08:00
|
|
|
for (CKDWORD i = 0; i < pixelcount; ++i) {
|
|
|
|
*pixels = (*pixels) & 0x00FFFFFF | (static_cast<CKDWORD>(*AlphaValues) << 24);
|
2023-09-07 16:27:41 +08:00
|
|
|
++pixels;
|
|
|
|
++AlphaValues;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
|
|
|
|
2023-09-19 15:20:40 +08:00
|
|
|
#pragma region Patched
|
|
|
|
|
|
|
|
namespace NSVxVector {
|
|
|
|
|
2023-09-24 23:27:20 +08:00
|
|
|
float DotProduct(const VxVector2& lhs, const VxVector2& rhs) {
|
2023-09-19 15:20:40 +08:00
|
|
|
return lhs * rhs;
|
|
|
|
}
|
|
|
|
|
2023-09-24 23:27:20 +08:00
|
|
|
float DotProduct(const VxVector3& lhs, const VxVector3& rhs) {
|
2023-09-19 15:20:40 +08:00
|
|
|
return lhs * rhs;
|
|
|
|
}
|
|
|
|
|
2023-09-24 23:27:20 +08:00
|
|
|
float DotProduct(const VxVector4& lhs, const VxVector4& rhs) {
|
2023-09-19 15:20:40 +08:00
|
|
|
return lhs * rhs;
|
|
|
|
}
|
|
|
|
|
|
|
|
VxVector3 CrossProduct(const VxVector3& lhs, const VxVector3& rhs) {
|
|
|
|
return VxVector3(
|
|
|
|
lhs.y * rhs.z - lhs.z * rhs.y,
|
|
|
|
lhs.z * rhs.x - lhs.x * rhs.z,
|
|
|
|
lhs.x * rhs.y - lhs.y * rhs.x
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-10-01 23:48:55 +08:00
|
|
|
void Abs(VxVector3& lhs) {
|
|
|
|
lhs.x = std::fabs(lhs.x);
|
|
|
|
lhs.y = std::fabs(lhs.y);
|
|
|
|
lhs.z = std::fabs(lhs.z);
|
|
|
|
}
|
|
|
|
|
2023-09-19 15:20:40 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
#pragma endregion
|
2023-09-07 16:27:41 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|