added tga.h/.cpp at util/imgfmt

This commit is contained in:
HuYingzhuo(hugo/hyzboy) 2023-02-10 16:00:14 +08:00
parent 0e31543779
commit 60218cd0a1
3 changed files with 92 additions and 0 deletions

46
inc/hgl/util/imgfmt/tga.h Normal file
View File

@ -0,0 +1,46 @@
#ifndef HGL_UTIL_IMAGE_FILE_TGA_INCLUDE
#define HGL_UTIL_IMAGE_FILE_TGA_INCLUDE
#include<hgl/platform/Platform.h>
namespace hgl
{
namespace util
{
#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);
}//namespace util
}//namespace hgl
#endif//HGL_UTIL_IMAGE_FILE_TGA_INCLUDE

View File

@ -78,9 +78,15 @@ SET(DATABLOCK_SOURCE ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/datablock/DataBlock
source_group("Data Block" FILES ${DATABLOCK_SOURCE})
SET(IMG_FILE_FILES ${CMUTIL_ROOT_INCLUDE_PATH}/hgl/util/imgfmt/tga.h
imgfmt/tga.cpp)
source_group("Image Format" FILES ${IMG_FILE_FILES})
add_cm_library(CMUtil "CM" ${CMD_SOURCE}
# ${TIME_SOURCE}
# ${DATABLOCK_SOURCE}
${IMG_FILE_FILES}
${XML_PARSE_SOURCE}
${JSON_TOOL_SOURCE}

40
src/imgfmt/tga.cpp Normal file
View File

@ -0,0 +1,40 @@
#include<hgl/util/imgfmt/tga.h>
namespace hgl
{
namespace util
{
bool FillTGAHeader(TGAHeader *header,const uint16 width,const uint16 height,const uint8 channels)
{
if(!header)return(false);
if(!width||!height)return(false);
if(channels!=1
&&channels!=3
&&channels!=4)return(false);
memset(header,0,sizeof(TGAHeader));
TGAImageDesc desc;
desc.image_desc=0;
if(channels==1)
{
header->image_type=3;
header->bit=8;
}
else
{
header->image_type=2;
header->bit=channels*8;
if(channels==4)
desc.alpha_depth=8;
}
header->image_desc=desc.image_desc;
return(true);
}
}//namespace util
}//namespace hgl