diff --git a/inc/hgl/2d/TGA.h b/inc/hgl/2d/TGA.h new file mode 100644 index 0000000..3a44b88 --- /dev/null +++ b/inc/hgl/2d/TGA.h @@ -0,0 +1,51 @@ +#pragma once + +#include +namespace hgl +{ + namespace imgfmt + { + constexpr const uint TGA_IMAGE_TYPE_COLOR_MAP =1; + constexpr const uint TGA_IMAGE_TYPE_TRUE_COLOR =2; + constexpr const uint TGA_IMAGE_TYPE_GRAYSCALE =3; + + constexpr const uint TGA_DIRECTION_LOWER_LEFT =0; + constexpr const uint TGA_DIRECTION_UPPER_LEFT =1; + +#pragma pack(push,1) + struct TGAHeader + { + uint8 id; + uint8 color_map_type; + uint8 image_type; // 1 colormap image ,2 true-color,3 grayscale + + uint16 color_map_first; + uint16 color_map_length; + uint8 color_map_size; + + uint16 x_origin; + uint16 y_origin; + + uint16 width; + uint16 height; + uint8 bit; + uint8 image_desc; + }; + + union TGAImageDesc + { + uint8 image_desc; + struct + { + uint alpha_depth:4; + uint reserved:1; + uint direction:1; //0 lower-left,1 upper left + }; + }; +#pragma pack(pop) + + constexpr size_t TGAHeaderSize=sizeof(TGAHeader); + + bool FillTGAHeader(TGAHeader *header,const uint16 width,const uint16 height,const uint8 channels,const uint8 single_channel_bits=8); + }//namespace imgfmt +}//namespace hgl diff --git a/src/Bitmap/tga.cpp b/src/Bitmap/tga.cpp new file mode 100644 index 0000000..67c8c9c --- /dev/null +++ b/src/Bitmap/tga.cpp @@ -0,0 +1,45 @@ +#include + +namespace hgl +{ + namespace imgfmt + { + bool FillTGAHeader(TGAHeader *header,const uint16 width,const uint16 height,const uint8 channels,const uint8 single_channel_bits) + { + if(!header)return(false); + if(!width||!height)return(false); + + if(channels!=1 + &&channels!=3 + &&channels!=4)return(false); + + memset(header,0,sizeof(TGAHeader)); + + header->width=width; + header->height=height; + + TGAImageDesc desc; + + desc.image_desc=0; + + if(channels==1) + { + header->image_type=3; + header->bit=single_channel_bits; + } + else + { + header->image_type=2; + header->bit=channels*single_channel_bits; + + if(channels==4) + desc.alpha_depth=single_channel_bits; + } + + desc.direction=TGA_DIRECTION_UPPER_LEFT; + + header->image_desc=desc.image_desc; + return(true); + } + }//namespace imgfmt +}//namespace hgl \ No newline at end of file