Improved Bitmap<>,BlendBitmap<>,DrawGeometry<>

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-09-15 18:19:51 +08:00
parent 1c22233101
commit 24ab6f5f9f
3 changed files with 93 additions and 8 deletions

View File

@ -2,6 +2,7 @@
#define HGL_2D_BITMAP_INCLUDE #define HGL_2D_BITMAP_INCLUDE
#include<hgl/type/DataType.h> #include<hgl/type/DataType.h>
#include<hgl/type/String.h>
#include<hgl/math/HalfFloat.h> #include<hgl/math/HalfFloat.h>
#include<iterator> #include<iterator>
namespace hgl namespace hgl
@ -17,7 +18,7 @@ namespace hgl
/** /**
* 2D象素处理 * 2D象素处理
*/ */
template<typename T> class Bitmap template<typename T,uint C> class Bitmap
{ {
int width,height; int width,height;
@ -36,6 +37,9 @@ namespace hgl
delete[] data; 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 GetWidth ()const{return width;}
const int GetHeight ()const{return height;} const int GetHeight ()const{return height;}
const uint GetTotalPixels ()const{return width*height;} const uint GetTotalPixels ()const{return width*height;}
@ -115,6 +119,15 @@ namespace hgl
delete[] temp; delete[] temp;
} }
};//template<typename T> class Bitmap };//template<typename T> class Bitmap
using BitmapGrey8=Bitmap<uint8,1>;
using BitmapRG8=Bitmap<Vector2u8,2>;
using BitmapRGB8=Bitmap<Vector3u8,3>;
using BitmapRGBA8=Bitmap<Vector4u8,4>;
using BitmapU16=Bitmap<uint16,1>;
using BitmapU32=Bitmap<uint32,1>;
}//namespace bitmap }//namespace bitmap
}//namespace hgl }//namespace hgl
#endif//HGL_2D_BITMAP_INCLUDE #endif//HGL_2D_BITMAP_INCLUDE

View File

@ -27,7 +27,80 @@ namespace hgl
{ {
public: public:
void operator ()(const Bitmap<ST> *src,Bitmap<DT> *dst,const float alpha)const; void operator ()(const ST *src,DT *dst,const float alpha)const;
};//template<typename ST,typename DT> class BlendBitmap };//template<typename ST,typename DT> class BlendBitmap
struct BlendColorU32Additive:public bitmap::BlendColor<uint32>
{
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<Vector4u8>
{
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<BitmapRGBA8,BitmapRGB8>::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;i<width*height;i++)
{
a=src->a*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<BitmapRGBA8,BitmapRGB8>;
}//namespace bitmap }//namespace bitmap
}//namespace hgl }//namespace hgl

View File

@ -1,19 +1,15 @@
#pragma once #pragma once
#include<hgl/2d/Bitmap.h> #include<hgl/2d/Bitmap.h>
#include<hgl/2d/blend.h> #include<hgl/2d/Blend.h>
#include<hgl/math/FastTriangle.h> #include<hgl/math/FastTriangle.h>
namespace hgl namespace hgl
{ {
namespace bitmap namespace bitmap
{ {
template<typename T> class DrawGeometry template<typename T,typename FormatBitmap> class DrawGeometry
{ {
public:
using FormatBitmap=Bitmap<T>;
protected: protected:
FormatBitmap *bitmap; FormatBitmap *bitmap;
@ -476,5 +472,8 @@ namespace hgl
} }
} }
};//template<typename T,uint CHANNELS> class DrawGeometry };//template<typename T,uint CHANNELS> class DrawGeometry
using DrawGeometryU32=DrawGeometry<uint32,BitmapU32>;
using DrawGeometryRGBA8=DrawGeometry<Vector4u8,BitmapRGBA8>;
}//namespace bitmap }//namespace bitmap
}//namespace hgl }//namespace hgl