diff --git a/inc/hgl/type/Color.h b/inc/hgl/color/Color.h similarity index 87% rename from inc/hgl/type/Color.h rename to inc/hgl/color/Color.h index 728daae..5d86920 100644 --- a/inc/hgl/type/Color.h +++ b/inc/hgl/color/Color.h @@ -1,34 +1,10 @@ #ifndef HGL_COLOR_TABLE_INCLUDE #define HGL_COLOR_TABLE_INCLUDE -#include #include -#include +#include namespace hgl { - #define DEF_RGB_U8_TO_F(r,g,b) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f} - #define DEF_RGBA_U8_TO_F(r,g,b,a) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f,float(a)/255.0f} - - #define HEXColor3(value) (0x##value>>16),((0x##value&0xFF00)>>8),(0x##value&0xFF) - #define HEXColor3f(value) float(0x##value>>16)/255.0f,float((0x##value&0xFF00)>>8)/255.0f,float(0x##value&0xFF)/255.0f - - const Color3f GetSpectralColor(const double l); ///<根据光谱值获取对应的RGB值 - - /** - * 颜色数据定义 - */ - struct COLOR_DEF ///颜色数据定义 - { - int red,green,blue; ///<三原色 - int yum; ///<亮度 - - float r,g,b; ///<三原色浮点值 - float y; ///<亮度浮点值 - - char eng_name[32]; ///<英文名称 - u16char chs_name[16]; ///<中文名称 - }; - /** * 颜色枚举 */ @@ -281,20 +257,19 @@ namespace hgl ENUM_CLASS_RANGE(AliceBlue,YellowGreen) };//enum COLOR_ENUM - extern COLOR_DEF prv_color[size_t(COLOR::RANGE_SIZE)]; + const uint32 GetRGBA(const enum class COLOR &ce,const uint8 &alpha); + const uint32 GetABGR(const enum class COLOR &ce,const uint8 &alpha); - inline const Color3f GetColor3f(const enum class COLOR &ce) - { - const COLOR_DEF &c=prv_color[size_t(ce)]; + const Color3f GetColor3f(const enum class COLOR &ce); + const Color4f GetColor4f(const enum class COLOR &ce,const float &alpha); - return Color3f(c.r,c.g,c.b); - } - - inline const Color4f GetColor4f(const enum class COLOR &ce,const float &alpha) - { - const COLOR_DEF &c=prv_color[size_t(ce)]; - - return Color4f(c.r,c.g,c.b,alpha); - } + const Color3f GetYCbCrColor3f(const enum class COLOR &ce); + const Color4f GetYCbCrColor4f(const enum class COLOR &ce,const float &alpha); + + /** + * 根据光谱值获取对应的RGB值 + * @param l 光谱值(从400到700) + */ + const Color3f GetSpectralColor(const double l); }//namespace hgl #endif//HGL_COLOR_TABLE_INCLUDE diff --git a/inc/hgl/type/Color3f.h b/inc/hgl/color/Color3f.h similarity index 95% rename from inc/hgl/type/Color3f.h rename to inc/hgl/color/Color3f.h index 792f5ab..7e4148f 100644 --- a/inc/hgl/type/Color3f.h +++ b/inc/hgl/color/Color3f.h @@ -1,6 +1,7 @@ #ifndef HGL_COLOR_3_FLOAT_INCLUDE #define HGL_COLOR_3_FLOAT_INCLUDE +#include namespace hgl { /** @@ -53,8 +54,7 @@ namespace hgl void Yellow(){r=1,g=1,b=0;} ///<黄色 void Purple(){r=1,g=0,b=1;} ///<紫色 - void Grey(float v){r=v,g=v,b=v;Clamp();} ///<灰色 - void Grey(float,float,float); ///<指彩色变成灰色 + float ToGrey(){return RGB2Lum(r,g,b);} void Grey(); ///<将当前色彩变成灰色 //操作符重载 diff --git a/inc/hgl/type/Color4f.h b/inc/hgl/color/Color4f.h similarity index 98% rename from inc/hgl/type/Color4f.h rename to inc/hgl/color/Color4f.h index b30e823..3dcce1c 100644 --- a/inc/hgl/type/Color4f.h +++ b/inc/hgl/color/Color4f.h @@ -1,8 +1,7 @@ #ifndef HGL_COLOR_4_FLOAT_INCLUDE #define HGL_COLOR_4_FLOAT_INCLUDE -#include -#include +#include namespace hgl { #define HGL_FLOAT_TO_U32(c1,c2,c3,c4) uint32( \ @@ -65,6 +64,7 @@ namespace hgl uint32 ToARGB8()const{ return HGL_FLOAT_TO_ARGB8(r, g, b, a); } ///<输出一个argb8格式的颜色数据 uint32 ToABGR8()const{ return HGL_FLOAT_TO_ABGR8(r, g, b, a); } ///<输出一个abgr8格式的颜色数据 + float ToGrey(){return RGB2Lum(r,g,b);} void Grey(); ///<将当前色彩变成灰色 //操作符重载 diff --git a/inc/hgl/color/ColorFormat.h b/inc/hgl/color/ColorFormat.h new file mode 100644 index 0000000..2c523a6 --- /dev/null +++ b/inc/hgl/color/ColorFormat.h @@ -0,0 +1,142 @@ +#ifndef HGL_COLOR_FORMAT_INCLUDE +#define HGL_COLOR_FORMAT_INCLUDE + +#include + +namespace hgl +{ + constexpr uint16 RGB8toRGB565(const uint8 r,const uint8 g,const uint8 b) + { + return ((r<<8)&0xF800) + |((g<<3)&0x7E0) + | (b>>3); + } + + inline void RGB8toRGB565(uint16 *target,uint8 *src,uint size) + { + for(uint i=0;i>3); + + ++target; + src+=3; + } + } + + // Bit depth Sign bit present Exponent bits Mantissa bits + // 32 Yes 8 23 + // 16 Yes 5 10 + // 11 No 5 6 + // 10 No 5 5 + + constexpr uint32 RGB16FtoB10GR11UF(const half_float r, const half_float g, const half_float b) + { + return ((b&0x7FE0)<<17) + |((g&0x7FF0)<<7) + | (r&0x7FF0)>>4; + } + + inline void RGB16FtoB10GR11UF(uint32 *target,half_float *src,uint size) + { + for(uint i=0;i>4; + + ++target; + src+=3; + } + } + + constexpr uint16 RGBA8toBGRA4(const uint8 r, const uint8 g, const uint8 b, const uint8 a) + { + return ((b<<8)&0xF000) + |((g<<4)&0xF00) + |((r )&0xF0) + | (a>>4); + } + + inline void RGBA8toBGRA4(uint16 *target,uint8 *src,uint size) + { + for(uint i=0;i>4); + + ++target; + src+=4; + } + } + + constexpr uint16 RGBA8toRGBA4(const uint8 r, const uint8 g, const uint8 b, const uint8 a) + { + return ((r<<8)&0xF000) + |((g<<4)&0xF00) + |((b )&0xF0) + | (a>>4); + } + + inline void RGBA8toRGBA4(uint16 *target,uint8 *src,uint size) + { + for(uint i=0;i>4); + + ++target; + src+=4; + } + } + + constexpr uint16 RGBA8toA1RGB5(const uint8 r,const uint8 g,const uint8 b,const uint8 a) + { + return ((a<<8)&0x8000) + |((r<<7)&0x7C00) + |((g<<2)&0x3E0) + | (b>>3); + } + + inline void RGBA8toA1RGB5(uint16 *target,uint8 *src,uint size) + { + for(uint i=0;i>3); + + ++target; + src+=4; + } + } + + constexpr uint32 RGBA16toA2BGR10(const uint16 r,const uint16 g,const uint16 b,const uint16 a) + { + return ((a<<16)&0xC0000000) + |((b<<14)&0x3FF00000) + |((g<< 4)&0xFFC00) + |(a>> 6); + } + + inline void RGBA16toA2BGR10(uint32 *target,uint16 *src,uint size) + { + for(uint i=0;i> 6); + + ++target; + src+=4; + } + } +}//namespace hgl +#endif//HGL_COLOR_FORMAT_INCLUDE diff --git a/inc/hgl/color/Gamma.h b/inc/hgl/color/Gamma.h new file mode 100644 index 0000000..4cadf07 --- /dev/null +++ b/inc/hgl/color/Gamma.h @@ -0,0 +1,118 @@ +#ifndef HGL_GAMMA_INCLUDE +#define HGL_GAMMA_INCLUDE + +#include + +namespace hgl +{ + constexpr double GAMMA =2.4f; + constexpr double INV_GAMMA =1.0f/GAMMA; + constexpr double SRGB_ALPHA =0.055f; + + template + inline constexpr T sRGB2Linear(const T &in) + { + if(in<=0.04045f) + return (double)in/12.92f; + else + return pow((double(in)+SRGB_ALPHA)/(1.0f+SRGB_ALPHA), GAMMA); + } + + template<> + inline constexpr uint8 sRGB2Linear(const uint8 &in) + { + if(in<=0x0A) + return in/12.92f; + else + return pow((double(in)+SRGB_ALPHA)/(1.0f+SRGB_ALPHA), GAMMA); + } + + template + inline constexpr T sRGB2Linear(const T &in,const double &gamma,const double &srgb_alpha) + { + if(in<=0.04045f) + return (double)in/12.92f; + else + return pow((double(in)+srgb_alpha)/(1.0f+srgb_alpha),gamma); + } + + template<> + inline constexpr uint8 sRGB2Linear(const uint8 &in,const double &gamma,const double &srgb_alpha) + { + if(in<=0x0A) + return in/12.92f; + else + return pow((double(in)+srgb_alpha)/(1.0f+srgb_alpha), gamma); + } + + template + inline constexpr T Linear2sRGB(const T &in) + { + if(in<=0.0031308f) + return double(in)*12.92f; + else + return pow(double(in), INV_GAMMA)*(1.0f+SRGB_ALPHA)-SRGB_ALPHA; + } + + template<> + inline constexpr uint8 Linear2sRGB(const uint8 &in) + { + if(in<=0x03) + return double(in)*12.92f; + else + return pow(double(in), INV_GAMMA)*(1.0f+SRGB_ALPHA)-SRGB_ALPHA; + } + + template + inline constexpr T Linear2sRGB(const T &in,const double &inv_gamma,const double &srgb_alpha) + { + if(in<=0.0031308f) + return double(in)*12.92f; + else + return pow(double(in),inv_gamma)*(1.0f+srgb_alpha)-srgb_alpha; + } + + template<> + inline constexpr uint8 Linear2sRGB(const uint8 &in,const double &inv_gamma,const double &srgb_alpha) + { + if(in<=0x03) + return double(in)*12.92f; + else + return pow(double(in), inv_gamma)*(1.0f+srgb_alpha)-srgb_alpha; + } + + template + inline constexpr T sRGB2LinearCheaper(const T &in,const double &gamma=GAMMA) + { + return (T)pow(double(in),gamma); + } + + template + inline constexpr T Linear2sRGBCheaper(const T &in,const double &inv_gamma=INV_GAMMA) + { + return (T)pow((double)in,inv_gamma); + } + + template + inline constexpr T sRGB2LinearCheapest(const T &in) + { + return in*in; + } + + template + inline void sRGB2LinearFast(T &x,T &y,T &z,const T &r,const T &g,const T &b) + { + x=0.4124f*r+0.3576f*g+0.1805f*b; + y=0.2126f*r+0.7152f*g+0.0722f*b; + z=0.0193f*r+0.1192f*g+0.9505f*b; + } + + template + inline void Linear2sRGBFast(T &r,T &g,T &b,const T &x,const T &y,const T &z) + { + r= 3.2406f*x-1.5373f*y-0.4986f*z; + g=-0.9689f*x+1.8758f*y+0.0416f*z; + b= 0.0557f*x-0.2040f*y+1.0570f*z; + } +}//namespace hgl +#endif//HGL_GAMMA_INCLUDE diff --git a/inc/hgl/color/Lum.h b/inc/hgl/color/Lum.h new file mode 100644 index 0000000..f5cdc8f --- /dev/null +++ b/inc/hgl/color/Lum.h @@ -0,0 +1,18 @@ +#ifndef HGL_COLOR_LUM_INCLUDE +#define HGL_COLOR_LUM_INCLUDE + +#include + +namespace hgl +{ + template constexpr T RGB2Lum(const T &r, const T &g, const T &b) + { + return 0.2126f*r+0.7152f*g+0.0722f*b; + } + + template<> constexpr uint8 RGB2Lum(const uint8 &r, const uint8 &g, const uint8 &b) + { + return (r*54+g*183+b*19)>>8; + } +}//namespace hgl +#endif//HGL_COLOR_LUM_INCLUDE diff --git a/inc/hgl/color/YCbCr.h b/inc/hgl/color/YCbCr.h new file mode 100644 index 0000000..42e2f88 --- /dev/null +++ b/inc/hgl/color/YCbCr.h @@ -0,0 +1,62 @@ +#ifndef HGL_COLOR_YCbCr_INCLUDE +#define HGL_COLOR_YCbCr_INCLUDE + +#include +namespace hgl +{ + template + inline void RGB2YCbCr(T &y,T &cb,T &cr,const T &r,const T &g,const T &b) + { + y=0.299f*r+0.587f*g+0.114f*b; + cb=0.5f-0.168736f*r-0.331264f*g+0.5f*b; + cr=0.5f+0.5f*r-0.418688f*g-0.081312f*b; + } + + template + inline void YCbCr2RGB(T &r,T &g,T &b,const T &y,const T &cb,const T &cr) + { + r=y+1.402f*(cr-0.5f); + g=y-0.344136f*(cb-0.5f)-0.714136f*(cr-0.5f); + b=y+1.772f*(cb-0.5f); + } + + inline void RGB2YCbCr(uint8 &y,uint8 &cb,uint8 &cr,const uint8 &r,const uint8 &g,const uint8 &b) + { + y =uint8(Clamp(0.299f*r+0.587f*g+0.114f*b)); + cb=uint8(Clamp(0.5f-0.168736f*r-0.331264f*g+0.5f*b)); + cr=uint8(Clamp(0.5f+0.5f*r-0.418688f*g-0.081312f*b)); + } + + inline void YCbCr2RGB(uint8 &r,uint8 &g,uint8 &b,const uint8 &y,const uint8 &cb,const uint8 &cr) + { + r=uint8(Clamp(y+1.402f*(cr-0.5f))); + g=uint8(Clamp(y-0.344136f*(cb-0.5f)-0.714136f*(cr-0.5f))); + b=uint8(Clamp(y+1.772f*(cb-0.5f))); + } + + template + inline constexpr T RGB2Cb(const T &r, const T &g, const T &b) + { + return 0.5f-0.168736f*r-0.331264f*g+0.5f*b; + } + + template<> + inline constexpr uint8 RGB2Cb(const uint8 &r, const uint8 &g, const uint8 &b) + { + return uint8(Clamp(0.5f-0.168736f*r-0.331264f*g+0.5f*b)); + } + + template + inline constexpr T RGB2Cr(const T &r, const T &g, const T &b) + { + return 0.5f+0.5f*r-0.418688f*g-0.081312f*b; + } + + template<> + inline constexpr uint8 RGB2Cr(const uint8 &r, const uint8 &g, const uint8 &b) + { + return uint8(Clamp(0.5f+0.5f*r-0.418688f*g-0.081312f*b)); + } +}//namespace hgl +#endif//HGL_COLOR_YCbCr_INCLUDE + \ No newline at end of file diff --git a/inc/hgl/math/FastTriangle.h b/inc/hgl/math/FastTriangle.h index 25c6705..3ddd668 100644 --- a/inc/hgl/math/FastTriangle.h +++ b/inc/hgl/math/FastTriangle.h @@ -13,7 +13,7 @@ namespace hgl * Approximates acos(x) with a max absolute error of 9.0x10^-3. * Valid in the range -1..1. */ - inline float Lacos(float x) + inline const float Lacos(float x) { // Lagarde 2014, "Inverse trigonometric functions GPU optimization for AMD GCN architecture" // This is the approximation of degree 1, with a max absolute error of 9.0x10^-3 @@ -27,16 +27,35 @@ namespace hgl * Approximates acos(x) with a max absolute error of 9.0x10^-3. * Valid only in the range 0..1. */ - inline float LacosPositive(float x) + inline const float LacosPositive(float x) { - float p = -0.1565827 * x + 1.570796; - return p * sqrt(1.0 - x); + return (-0.1565827 * x + 1.570796) * sqrt(1.0 - x); } + inline constexpr double ApproxSin(double angle) + { + return angle*(1.27323954f-angle*angle*(0.405284735f-angle*angle*(0.075133f-angle*angle*0.0078f))); + } + + inline constexpr double ApproxCos(double angle) + { + return 1.27323954f-angle*angle*(0.405284735f-angle*angle*(0.075133f-angle*angle*0.0078f)); + } + + inline constexpr double ApproxAtan2(double x,double y) + { + return x/(1.27323954f+y*y*(0.405284735f+y*y*(0.075133f+y*y*0.0078f))); + } + + inline constexpr double ApproxAtan(double z) + { + return z*(1.27323954f-z*z*(0.405284735f-z*z*(0.075133f-z*z*0.0078f))); + } + /** * 低精度atan函数 */ - inline double Latan(double z) + inline constexpr double Latan(double z) { constexpr double n1 = 0.97239411f; constexpr double n2 = -0.19194795f; @@ -49,7 +68,7 @@ namespace hgl /** * 近似Pow2.2,使用此函数等于pow(x,2.2) */ - inline double LPow22(double x) + inline constexpr double LPow22(double x) { return x*(1.12*x - 0.12); } diff --git a/inc/hgl/math/HalfFloat.h b/inc/hgl/math/HalfFloat.h new file mode 100644 index 0000000..cd54fdb --- /dev/null +++ b/inc/hgl/math/HalfFloat.h @@ -0,0 +1,50 @@ +#ifndef HGL_HALF_FLOAT_INCLUDE +#define HGL_HALF_FLOAT_INCLUDE + +#include + +namespace hgl +{ + using half_float=uint16; + + inline const half_float float2half(const float &f) + { + const uint32 x = *((uint32 *)&f); + + return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); + } + + inline const float half2float(const half_float &hf) + { + union + { + float f; + uint32 u; + }x; + + x.u=(((hf&0x8000)<<16) | (((hf&0x7c00)+0x1C000)<<13) | ((hf&0x03FF)<<13)); + + return x.f; + } + + inline void half_to_float(uint32 *target,const half_float *source,uint32 count) + { + while (count--) + { + *target = (((*source & 0x8000) << 16) | (((*source & 0x7c00) + 0x1C000) << 13) | ((*source & 0x03FF) << 13)); + ++target; + ++source; + } + } + + inline void float_to_half(half_float *target,const float *source,uint32 count) + { + while (count--) + { + *target = float2half(*source); + ++target; + ++source; + } + } +}//namespace hgl +#endif//HGL_HALF_FLOAT_INCLUDE diff --git a/inc/hgl/math/MathConst.h b/inc/hgl/math/MathConst.h index 4487377..a79b3ad 100644 --- a/inc/hgl/math/MathConst.h +++ b/inc/hgl/math/MathConst.h @@ -76,56 +76,7 @@ namespace hgl return double(floor(value*per))/per; } - - inline const float half_to_float(const uint16 &h) - { - union - { - float f; - uint32 u; - }x; - - x.u=(((h&0x8000)<<16) | (((h&0x7c00)+0x1C000)<<13) | ((h&0x03FF)<<13)); - - return x.f; - } - - inline void half_to_float(uint32 *target,const uint16 *source,uint32 count) - { - while (count--) - { - *target = (((*source & 0x8000) << 16) | (((*source & 0x7c00) + 0x1C000) << 13) | ((*source & 0x03FF) << 13)); - ++target; - ++source; - } - } - - inline void half_to_uint16(uint16 *target, const uint16 *source, uint32 count) - { - union - { - float f; - uint32 u; - }x; - - while (count--) - { - x.u = (((*source & 0x8000) << 16) | (((*source & 0x7c00) + 0x1C000) << 13) | ((*source & 0x03FF) << 13)); - - *target=x.f*65535; - - ++target; - ++source; - } - } - - inline constexpr uint16 float_to_half(const float &f) - { - const uint32 x = *((uint32 *)&f); - - return ((x>>16)&0x8000)|((((x&0x7f800000)-0x38000000)>>13)&0x7c00)|((x>>13)&0x03ff); - } - + /** * 正圆面积计算 * @param radius 半径 diff --git a/inc/hgl/math/PrimaryMathematics.h b/inc/hgl/math/PrimaryMathematics.h index 963432c..d360619 100644 --- a/inc/hgl/math/PrimaryMathematics.h +++ b/inc/hgl/math/PrimaryMathematics.h @@ -35,5 +35,20 @@ namespace hgl return result; } + + template + inline constexpr T Clamp(const T &value,const T2 &min_value,const T2 &max_value) + { + if(valuemax_value)return max_value; + + return value; + } + + template + inline constexpr T Clamp(const T &value) + { + return Clamp(value,0,1); + } }//namespace hgl #endif//HGL_Primary_Mathematics_INCLUDE diff --git a/inc/hgl/type/ColorSpace.h b/inc/hgl/type/ColorSpace.h deleted file mode 100644 index dca1711..0000000 --- a/inc/hgl/type/ColorSpace.h +++ /dev/null @@ -1,105 +0,0 @@ -#ifndef HGL_COLOR_SPACE_INCLUDE -#define HGL_COLOR_SPACE_INCLUDE - -#include - -namespace hgl -{ - constexpr double GAMMA =2.4f; - constexpr double INV_GAMMA =1.0f/GAMMA; - constexpr double SRGB_ALPHA =0.055f; - - template - inline constexpr T sRGB2Linear(const T &in,const T &gamma=GAMMA,const T &srgb_alpha=SRGB_ALPHA) - { - if(in<=0.4045) - return (double)in/12.92; - else - return pow((double(in)+srgb_alpha)/(1.0f+srgb_alpha),gamma); - } - - template - inline constexpr T Linear2sRGB(const T &in,const T &inv_gamma=INV_GAMMA,const T &srgb_alpha=SRGB_ALPHA) - { - if(in<=0.0031308f) - return double(in)*12.92f; - else - return pow(double(in),inv_gamma)*(1.0f+srgb_alpha)-srgb_alpha; - } - - template - inline constexpr T sRGB2LinearCheaper(const T &in,const T &gamma=GAMMA) - { - return (T)pow(double(in),gamma); - } - - template - inline constexpr T Linear2sRGBCheaper(const T &in,const T &inv_gamma=INV_GAMMA) - { - return (T)pow((double)in,inv_gamma); - } - - template - inline constexpr T sRGB2LinearCheapest(const T &in) - { - return in*in; - } - - template - inline void sRGB2LinearFast(T &x,T &y,T &z,const T &r,const T &g,const T &b) - { - x=0.4124f*r+0.3576f*g+0.1805f*b; - y=0.2126f*r+0.7152f*g+0.0722f*b; - z=0.0193f*r+0.1192f*g+0.9505f*b; - } - - template - inline void Linear2sRGBFast(T &r,T &g,T &b,const T &x,const T &y,const T &z) - { - r= 3.2406f*x-1.5373f*y-0.4986f*z; - g=-0.9689f*x+1.8758f*y+0.0416f*z; - b= 0.0557f*x-0.2040f*y+1.0570f*z; - } - - template - inline constexpr T Clamp(const T &value,const T &min_value,const T &max_value) - { - if(valuemax_value)return max_value; - - return value; - } - - template - inline constexpr T Clamp(const T &value) - { - return Clamp(value,T(0),T(1)); - } - - template - inline constexpr T RGB2Lum(const T &r,const T &g,const T &b) - { - return 0.299f*r+0.587f*g+0.114f*b; - } - - template - inline constexpr T RGB2Cb(const T &r,const T &g,const T &b) - { - return -0.168736f*r-0.331264f*g+0.5f*b; - } - - template - inline constexpr T RGB2Cr(const T &r,const T &g,const T &b) - { - return 0.5f*r-0.418688f*g-0.081312f*b; - } - - template - inline void RGB2YCbCr(T &y,T &cb,T &cr,const T &r,const T &g,const T &b) - { - y =RGB2Lum(r,g,b); - cb=RGB2Cb(r,g,b); - cr=RGB2Cr(r,g,b); - } -}//namespace hgl -#endif//HGL_COLOR_SPACE_INCLUDE diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index b7ee97b..0db8114 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -42,14 +42,8 @@ SOURCE_GROUP("Math\\Header Files" FILES ${MATH_HEADER_FILES}) SOURCE_GROUP("Math\\Source Files" FILES ${MATH_SOURCE_FILES}) ##Color-------------------------------------------------------- -SET(COLOR_HEADER_FILES ${TYPE_INCLUDE_PATH}/Color.h - ${TYPE_INCLUDE_PATH}/ColorSpace.h - ${TYPE_INCLUDE_PATH}/Color3f.h - ${TYPE_INCLUDE_PATH}/Color4f.h) - -SET(COLOR_SOURCE_FILES Color/Color.cpp - Color/Color3f.cpp - Color/Color4f.cpp) +FILE(GLOB COLOR_HEADER_FILES ${CMCORE_ROOT_INCLUDE_PATH}/hgl/color/*.h) +FILE(GLOB COLOR_SOURCE_FILES Color/*.cpp) SOURCE_GROUP("Datatype\\Color\\Header Files" FILES ${COLOR_HEADER_FILES}) SOURCE_GROUP("Datatype\\Color\\Source Files" FILES ${COLOR_SOURCE_FILES}) diff --git a/src/Color/Color.cpp b/src/Color/Color.cpp index eb285aa..d53ac86 100644 --- a/src/Color/Color.cpp +++ b/src/Color/Color.cpp @@ -1,22 +1,42 @@ -#include +#include +#include namespace hgl { + #define DEF_RGB_U8_TO_F(r,g,b) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f} + #define DEF_RGBA_U8_TO_F(r,g,b,a) {float(r)/255.0f,float(g)/255.0f,float(b)/255.0f,float(a)/255.0f} + + #define HEXColor3(value) (0x##value>>16),((0x##value&0xFF00)>>8),(0x##value&0xFF) + #define HEXColor3f(value) float(0x##value>>16)/255.0f,float((0x##value&0xFF00)>>8)/255.0f,float(0x##value&0xFF)/255.0f + + struct COLOR_DEF + { + uint8 red,green,blue; + uint32 rgba; + uint32 abgr; + + float r,g,b; + float y,cb,cr; + + char eng_name[32]; + u16char chs_name[16]; + }; + #undef DEF_COLOR - #define DEF_COLOR(eng_name,red,green,blue,chs_name) { \ - red, \ - green, \ - blue, \ - int(double(red)*0.299+double(green)*0.587+double(blue)*0.114), \ - \ - float(double(red)/255.0f), \ - float(double(green)/255.0f), \ - float(double(blue)/255.0f), \ - float((double(red)*0.299+double(green)*0.587+double(blue)*0.114)/255.0f),\ - \ - #eng_name, \ - U16_TEXT(chs_name) \ - }, + #define DEF_COLOR(eng_name,red,green,blue,chs_name) { \ + red,green,blue, \ + (red<<24)|(green<<16)|(blue<<8), \ + (blue<<16)|(green<<8)|red, \ + float(double(red)/255.0f), \ + float(double(green)/255.0f), \ + float(double(blue)/255.0f), \ + float(RGB2Lum(double(red)/255.0f,double(green)/255.0f,double(blue)/255.0f)),\ + float(RGB2Cb(double(red)/255.0f,double(green)/255.0f,double(blue)/255.0f)),\ + float(RGB2Cr(double(red)/255.0f,double(green)/255.0f,double(blue)/255.0f)),\ + \ + #eng_name, \ + U16_TEXT(chs_name) \ + }, COLOR_DEF prv_color[size_t(COLOR::RANGE_SIZE)]= { @@ -278,6 +298,48 @@ namespace hgl #undef DEF_COLOR + const uint32 GetRGBA(const enum class COLOR &ce,const uint8 &alpha) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return c.rgba|alpha; + } + + const uint32 GetABGR(const enum class COLOR &ce,const uint8 &alpha) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return c.abgr|(alpha<<24); + } + + const Color3f GetColor3f(const enum class COLOR &ce) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return Color3f(c.r,c.g,c.b); + } + + const Color4f GetColor4f(const enum class COLOR &ce,const float &alpha) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return Color4f(c.r,c.g,c.b,alpha); + } + + const Color3f GetYCbCrColor3f(const enum class COLOR &ce) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return Color3f(c.y,c.cb,c.cr); + } + + const Color4f GetYCbCrColor4f(const enum class COLOR &ce,const float &alpha) + { + const COLOR_DEF &c=prv_color[size_t(ce)]; + + return Color4f(c.y,c.cb,c.cr,alpha); + } + /** * 根据光谱值获取对应的RGB值 * @param l 光谱值(从400到700) diff --git a/src/Color/Color3f.cpp b/src/Color/Color3f.cpp index bc5b0b2..07ba0a6 100644 --- a/src/Color/Color3f.cpp +++ b/src/Color/Color3f.cpp @@ -1,6 +1,5 @@ -#include -#include -#include +#include +#include namespace hgl { void Color3f::Clamp() @@ -31,15 +30,6 @@ namespace hgl b+=(nb-b)*pos; } //-------------------------------------------------------------------------------------------------- - void Color3f::Grey(float v1,float v2,float v3) - { - float lum=RGB2Lum(v1,v2,v3); - - r=lum; - g=lum; - b=lum; - } - //-------------------------------------------------------------------------------------------------- void Color3f::Grey() { float lum=RGB2Lum(r,g,b); diff --git a/src/Color/Color4f.cpp b/src/Color/Color4f.cpp index 889a0ee..79e10c0 100644 --- a/src/Color/Color4f.cpp +++ b/src/Color/Color4f.cpp @@ -1,6 +1,5 @@ -#include -#include -#include +#include +#include namespace hgl { void Color4f::Clamp() diff --git a/src/IO/InputStream.cpp b/src/IO/InputStream.cpp index c656f99..1c9d080 100644 --- a/src/IO/InputStream.cpp +++ b/src/IO/InputStream.cpp @@ -23,15 +23,15 @@ namespace hgl const int64 total=is->Available(); - if(size) - *size=total; - if(total<=0) return(nullptr); void *result=new char[total]; - *size=is->ReadFully(result,total); + int64 read_size=is->ReadFully(result,total); + + if(size) + *size=read_size; return result; } }//namespace io