From 3b3a03814b70bfa4975281fa27be9d0b6ac098cf Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 18 Jan 2021 15:11:19 +0800 Subject: [PATCH 1/2] add ILImage::Copy/Refresh functions. --- ILImage.h | 4 ++ ILImageSupport.cpp | 106 ++++++++++++++++++++++++++------------------- 2 files changed, 66 insertions(+), 44 deletions(-) diff --git a/ILImage.h b/ILImage.h index 4733a87..0ef03e1 100644 --- a/ILImage.h +++ b/ILImage.h @@ -12,6 +12,8 @@ class ILImage uint channel_count; + void Refresh(); + private: bool Convert(ILuint,ILuint); @@ -36,11 +38,13 @@ public: public: ILImage(); + ILImage(ILImage *); ~ILImage(); bool LoadFile(const OSString &); void Bind(); + void Copy(ILImage *); bool Resize(uint,uint); diff --git a/ILImageSupport.cpp b/ILImageSupport.cpp index d945b16..f7cf9b5 100644 --- a/ILImageSupport.cpp +++ b/ILImageSupport.cpp @@ -52,11 +52,72 @@ ILImage::ILImage() ilGenImages(1,&il_index); } +ILImage::ILImage(ILImage *img):ILImage() +{ + ilGenImages(1,&il_index); + Bind(); + ilCopyImage(img->il_index); + Refresh(); +} + ILImage::~ILImage() { ilDeleteImages(1,&il_index); } +void ILImage::Copy(ILImage *img) +{ + ilCopyImage(img->il_index); + Refresh(); +} + +void ILImage::Refresh() +{ + il_width =ilGetInteger(IL_IMAGE_WIDTH); + il_height =ilGetInteger(IL_IMAGE_HEIGHT); + il_depth =ilGetInteger(IL_IMAGE_DEPTH); + il_bit =ilGetInteger(IL_IMAGE_BITS_PER_PIXEL); + il_format =ilGetInteger(IL_IMAGE_FORMAT); + il_type =ilGetInteger(IL_IMAGE_TYPE); + + if(ilGetInteger(IL_IMAGE_ORIGIN)==IL_ORIGIN_LOWER_LEFT) + iluFlipImage(); + + if(il_format==IL_COLOR_INDEX) + { + uint il_pattle=ilGetInteger(IL_PALETTE_TYPE); + + if(il_pattle==IL_PAL_RGB24||il_pattle==IL_PAL_BGR24 + ||il_pattle==IL_PAL_RGB32||il_pattle==IL_PAL_BGR32) + { + channel_count=3; + il_format=IL_RGB; + il_type=IL_UNSIGNED_BYTE; + ilConvertImage(il_format,il_type); + } + else + if(il_pattle==IL_PAL_RGBA32||il_pattle==IL_PAL_BGRA32) + { + channel_count=4; + il_format=IL_RGBA; + il_type=IL_UNSIGNED_BYTE; + ilConvertImage(il_format,il_type); + } + else + { + LOG_ERROR("Don't support the pattle format."); + } + } + + il_bit =ilGetInteger(IL_IMAGE_BITS_PER_PIXEL); + + if(il_format==IL_LUMINANCE||il_format==IL_ALPHA)channel_count=1;else + if(il_format==IL_LUMINANCE_ALPHA) channel_count=2;else + if(il_format==IL_RGB||il_format==IL_BGR) channel_count=3;else + if(il_format==IL_RGBA||il_format==IL_BGRA) channel_count=4;else + channel_count=0; +} + constexpr ILenum format_by_channel[]= { IL_LUMINANCE, @@ -154,15 +215,7 @@ bool ILImage::LoadFile(const OSString &filename) LOG_INFO(OS_TEXT("\nFile: ")+filename); - il_width =ilGetInteger(IL_IMAGE_WIDTH); - il_height =ilGetInteger(IL_IMAGE_HEIGHT); - il_depth =ilGetInteger(IL_IMAGE_DEPTH); - il_bit =ilGetInteger(IL_IMAGE_BITS_PER_PIXEL); - il_format =ilGetInteger(IL_IMAGE_FORMAT); - il_type =ilGetInteger(IL_IMAGE_TYPE); - - if(ilGetInteger(IL_IMAGE_ORIGIN)==IL_ORIGIN_LOWER_LEFT) - iluFlipImage(); + Refresh(); LOG_INFO(OS_TEXT("\t width: ")+OSString::valueOf(il_width)); LOG_INFO(OS_TEXT("\theight: ")+OSString::valueOf(il_height)); @@ -171,41 +224,6 @@ bool ILImage::LoadFile(const OSString &filename) LOG_INFO(OS_TEXT("\tformat: ")+GetILFormatName(il_format)); LOG_INFO(OS_TEXT("\t type: ")+GetILTypeName(il_type)); - if(il_format==IL_COLOR_INDEX) - { - uint il_pattle=ilGetInteger(IL_PALETTE_TYPE); - - if(il_pattle==IL_PAL_RGB24||il_pattle==IL_PAL_BGR24 - ||il_pattle==IL_PAL_RGB32||il_pattle==IL_PAL_BGR32) - { - channel_count=3; - il_format=IL_RGB; - il_type=IL_UNSIGNED_BYTE; - ilConvertImage(il_format,il_type); - return(true); - } - else - if(il_pattle==IL_PAL_RGBA32||il_pattle==IL_PAL_BGRA32) - { - channel_count=4; - il_format=IL_RGBA; - il_type=IL_UNSIGNED_BYTE; - ilConvertImage(il_format,il_type); - return(true); - } - else - { - LOG_ERROR("Don't support the pattle format."); - return(false); - } - } - - if(il_format==IL_LUMINANCE||il_format==IL_ALPHA)channel_count=1;else - if(il_format==IL_LUMINANCE_ALPHA) channel_count=2;else - if(il_format==IL_RGB||il_format==IL_BGR) channel_count=3;else - if(il_format==IL_RGBA||il_format==IL_BGRA) channel_count=4;else - channel_count=0; - return(true); } From a45a13f12d20e28bfba240951b0969b111198412 Mon Sep 17 00:00:00 2001 From: hyzboy Date: Mon, 18 Jan 2021 15:11:37 +0800 Subject: [PATCH 2/2] add Bitmap.h --- Bitmap.h | 342 +++++++++++++++++++++++++++++++++++++++++++++++ ConvertImage.cpp | 1 + 2 files changed, 343 insertions(+) create mode 100644 Bitmap.h diff --git a/Bitmap.h b/Bitmap.h new file mode 100644 index 0000000..70a0ea0 --- /dev/null +++ b/Bitmap.h @@ -0,0 +1,342 @@ +#ifndef HGL_GRAPH_BITMAP2D_INCLUDE +#define HGL_GRAPH_BITMAP2D_INCLUDE + +#include +#include"pixel_format.h" +namespace hgl +{ + namespace graph + { + class Bitmap + { + protected: + + uint32 width,height; + uint32 channel; + uint32 bit; + + uint8 component[4]; //成份排列顺序 + + uint32 total_pixels; + uint32 line_bytes; + uint32 total_bytes; + + uint8 *data; + + public: + + const uint32 GetWidth()const{return width;} + const uint32 GetHeight()const{return height;} + const uint32 GetCahnnel()const{return channel;} + const uint32 GetBit()const{return bit;} + const uint32 GetTotalPixels()const{return total_pixels;} + const uint32 GetTotalBytes()const{return total_bytes;} + + void *GetData(){return data;} + + public: + + Bitmap() + { + hgl_zero(*this); + } + + Bitmap(const uint32 w,const uint32 h,const uint32 c,const uint32 b) + { + width=w; + height=h; + channel=c; + bit=b; + + total_pixels=width*height; + line_bytes=(width*channel*bit)>>3; + total_bytes=line_bytes*height; + + data=new uint8[total_bytes]; + } + + virtual ~Bitmap() + { + SAFE_CLEAR_ARRAY(data); + } + + void SetComponentOrder(int n,const uint8 *c) + { + if(n<0||n>4)return; + if(!c)return; + + memcpy(component,c,n); + } + + void Flip() + { + uint8 *tmp=new uint8[line_bytes]; + + uint8 *tp=tmp; + uint8 *bp=tmp+line_bytes*(height-1); + + while(tp + class BitmapUInteger:public Bitmap + { + public: + + using Bitmap::Bitmap; + virtual ~BitmapUInteger()=default; + + BitmapUInteger *Half() + { + } + }; + + template + class BitmapR:public Bitmap + { + T *pixels; + + public: + + BitmapR():Bitmap() + { + pixels=nullptr; + } + + BitmapR(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,1,b) + { + pixels=data; + } + + virtual ~BitmapR()=default; + };//class BitmapR:public Bitmap + + template + class BitmapRG:public Bitmap + { + T *pixels; + + public: + + BitmapRG():Bitmap() + { + pixels=nullptr; + } + + BitmapRG(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,2,b) + { + pixels=data; + } + + virtual ~BitmapRG()=default; + };//class BitmapRG:public Bitmap + + enum class RGBOrder + { + RGB=0, + BGR=1, + };// + + template + class BitmapRGB:public Bitmap + { + T *pixels; + + RGBOrder component_order; + + public: + + BitmapRGB():Bitmap() + { + pixels=nullptr; + } + + BitmapRGB(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,3,b) + { + pixels=data; + } + + virtual ~BitmapRGB()=default; + + bool SetComponentOrder(RGBOrder order) + { + if(order==RGBOrder::RGB)Bitmap::SetComponent(3,"RGB");else + if(order==RGBOrder::BGR)Bitmap::SetComponent(3,"BGR");else + return(false); + + component_order=order; + return(true); + } + + void ChangeComponentOrder(RGBOrder order) + { + if(component_order==order)return; + + T *p=pixels; + T temp; + + for(uint32 i=0;i void BitmapRGB::FlipG() + { + uint8 *p=pixels+1; + + for(uint i=0;i void BitmapRGB::FlipG() + { + uint16 *p=pixels+1; + + for(uint i=0;i void BitmapRGB::FlipG() + { + uint32 *p=pixels+1; + + for(uint i=0;i void BitmapRGB::FlipG() + { + float *p=pixels+1; + + for(uint i=0;i void BitmapRGB::FlipG() + { + double *p=pixels+1; + + for(uint i=0;i + class BitmapRGBA:public Bitmap + { + T *pixels; + + RGBAOrder component_order; + + public: + + BitmapRGBA():Bitmap() + { + pixels=nullptr; + } + + BitmapRGBA(const uint32 w,const uint32 h,const uint32 b):Bitmap(w,h,4,b) + { + pixels=data; + } + + virtual ~BitmapRGBA()=default; + + bool SetComponentOrder(RGBAOrder order) + { + if(order==RGBAOrder::RGBA)Bitmap::SetComponent(4,"RGBA",b);else + if(order==RGBAOrder::BGRA)Bitmap::SetComponent(4,"BGRA",b);else + if(order==RGBAOrder::ARGB)Bitmap::SetComponent(4,"ARGB",b);else + if(order==RGBAOrder::ABGR)Bitmap::SetComponent(4,"ABGR",b);else + return(false); + + component_order=order; + return(true); + } + + void ChangeComponentOrder(RGBAOrder order) + { + if(component_order==order)return; + + T *p=pixels; + T temp; + + if((component_order==RGBAOrder::RGBA&&order==RGBAOrder::BGRA) + ||(component_order==BGRAOrder::BGRA&&order==RGBAOrder::RGBA)){for(uint32 i=0;i; \ + using Bitmap##n##ub =Bitmap##t; \ + using Bitmap##n##s =Bitmap##t; \ + using Bitmap##n##us =Bitmap##t; \ + using Bitmap##n##i =Bitmap##t; \ + using Bitmap##n##u =Bitmap##t; \ + using Bitmap##n##f =Bitmap##t; \ + using Bitmap##n##d =Bitmap##t; + + DEF_BITMAP(1,R) + DEF_BITMAP(2,RG) + DEF_BITMAP(3,RGB) + DEF_BITMAP(4,RGBA) + + #undef DEF_BITMAP + }//namespace graph +}//namespace hgl +#endif//HGL_GRAPH_BITMAP2D_INCLUDE diff --git a/ConvertImage.cpp b/ConvertImage.cpp index 5c7ef54..b4d6731 100644 --- a/ConvertImage.cpp +++ b/ConvertImage.cpp @@ -1,4 +1,5 @@ #include +#include #include"ILImage.h" #include"TextureFileCreater.h"