From 24ab6f5f9f5334f9ff5cdce48b81fb5625a182c3 Mon Sep 17 00:00:00 2001 From: "HuYingzhuo(hugo/hyzboy)" Date: Fri, 15 Sep 2023 18:19:51 +0800 Subject: [PATCH] Improved Bitmap<>,BlendBitmap<>,DrawGeometry<> --- inc/hgl/2d/Bitmap.h | 15 +++++++- inc/hgl/2d/Blend.h | 75 ++++++++++++++++++++++++++++++++++++++- inc/hgl/2d/DrawGeometry.h | 11 +++--- 3 files changed, 93 insertions(+), 8 deletions(-) diff --git a/inc/hgl/2d/Bitmap.h b/inc/hgl/2d/Bitmap.h index bbc6812..d54232e 100644 --- a/inc/hgl/2d/Bitmap.h +++ b/inc/hgl/2d/Bitmap.h @@ -2,6 +2,7 @@ #define HGL_2D_BITMAP_INCLUDE #include +#include #include #include namespace hgl @@ -17,7 +18,7 @@ namespace hgl /** * 简单的2D象素处理 */ - template class Bitmap + template class Bitmap { int width,height; @@ -36,6 +37,9 @@ namespace hgl delete[] data; } + const uint GetChannels ()const{return C;} + const uint GetChannelBits ()const{return (sizeof(T)/C)<<3;} + const int GetWidth ()const{return width;} const int GetHeight ()const{return height;} const uint GetTotalPixels ()const{return width*height;} @@ -115,6 +119,15 @@ namespace hgl delete[] temp; } };//template class Bitmap + + using BitmapGrey8=Bitmap; + using BitmapRG8=Bitmap; + using BitmapRGB8=Bitmap; + using BitmapRGBA8=Bitmap; + + using BitmapU16=Bitmap; + using BitmapU32=Bitmap; }//namespace bitmap }//namespace hgl #endif//HGL_2D_BITMAP_INCLUDE + diff --git a/inc/hgl/2d/Blend.h b/inc/hgl/2d/Blend.h index 618ec91..f805e89 100644 --- a/inc/hgl/2d/Blend.h +++ b/inc/hgl/2d/Blend.h @@ -27,7 +27,80 @@ namespace hgl { public: - void operator ()(const Bitmap *src,Bitmap
*dst,const float alpha)const; + void operator ()(const ST *src,DT *dst,const float alpha)const; };//template class BlendBitmap + + struct BlendColorU32Additive:public bitmap::BlendColor + { + const uint32 operator()(const uint32 &src,const uint32 &dst)const + { + uint64 result=src+dst; + + return (result>HGL_U32_MAX)?HGL_U32_MAX:(result&HGL_U32_MAX); + } + + const uint32 operator()(const uint32 &src,const uint32 &dst,const float &alpha)const + { + uint64 result=src*alpha+dst; + + return (result>HGL_U32_MAX)?HGL_U32_MAX:(result&HGL_U32_MAX); + } + }; + + struct BlendColorRGBA8:public bitmap::BlendColor + { + const Vector4u8 operator()(const Vector4u8 &src,const Vector4u8 &dst)const + { + uint8 na=255-src.a; + + return Vector4u8((src.r*src.a+dst.r*na)/255, + (src.g*src.a+dst.g*na)/255, + (src.b*src.a+dst.b*na)/255, + dst.a); + } + + const Vector4u8 operator()(const Vector4u8 &src,const Vector4u8 &dst,const float &alpha)const + { + uint8 a=src.a*alpha; + uint8 na=255-src.a; + + return Vector4u8((src.r*src.a+dst.r*na)/255, + (src.g*src.a+dst.g*na)/255, + (src.b*src.a+dst.b*na)/255, + dst.a); + } + }; + + template<> void bitmap::BlendBitmap::operator()(const BitmapRGBA8 *src_bitmap,BitmapRGB8 *dst_bitmap,const float alpha)const + { + if(!src_bitmap||!dst_bitmap||alpha<=0)return; + + const uint width=src_bitmap->GetWidth(); + const uint height=src_bitmap->GetHeight(); + + if(width!=dst_bitmap->GetWidth()||height!=dst_bitmap->GetHeight()) + return; + + Vector3u8 *dst=dst_bitmap->GetData(); + const Vector4u8 *src=src_bitmap->GetData(); + + float a; + float na; + + for(uint i=0;ia*alpha; + na=255-src->a; + + dst->r=(src->r*a+dst->r*na)/255; + dst->g=(src->g*a+dst->g*na)/255; + dst->b=(src->b*a+dst->b*na)/255; + + ++dst; + ++src; + } + } + + using BlendBitmapRGBA8toRGB8=bitmap::BlendBitmap; }//namespace bitmap }//namespace hgl diff --git a/inc/hgl/2d/DrawGeometry.h b/inc/hgl/2d/DrawGeometry.h index 9ed3b9a..171e0e0 100644 --- a/inc/hgl/2d/DrawGeometry.h +++ b/inc/hgl/2d/DrawGeometry.h @@ -1,19 +1,15 @@ #pragma once #include -#include +#include #include namespace hgl { namespace bitmap { - template class DrawGeometry + template class DrawGeometry { - public: - - using FormatBitmap=Bitmap; - protected: FormatBitmap *bitmap; @@ -476,5 +472,8 @@ namespace hgl } } };//template class DrawGeometry + + using DrawGeometryU32=DrawGeometry; + using DrawGeometryRGBA8=DrawGeometry; }//namespace bitmap }//namespace hgl