蔡军生先生第二人生的源码分析(五十二) 纹理图片的格式之LLImageFormatted类

来源:互联网 发布:登陆淘宝买的苹果id 编辑:程序博客网 时间:2024/05/12 22:21
前面介绍了图片的基类,现在来介绍一下图片的格式化表示LLImageFormatted类。那么为什么需要设计这个格式化类呢?而不是直接从图片的基类继承到JPEG2000或者TGA呢?其实,这个类LLImageFormatted只是一个接口,让不同压缩方式的图片数据转换为中间表示方式,也就是原始数据表示方式。它的类声明如下:
#001 class LLImageFormatted : public LLImageBase
#002 {
#003 public:
#004      static LLImageFormatted* createFromType(S8 codec);
#005      static LLImageFormatted* createFromExtension(const LLString& instring);  
#006 
#007 protected:
#008      /*virtual*/ ~LLImageFormatted();
#009     
#010 public:
 
设置采用什么方式压缩数据。
#011      LLImageFormatted(S8 codec);
#012 
#013      // LLImageBase
#014 public:
 
分配内存。
#015      /*virtual*/ void deleteData();
#016      /*virtual*/ U8* allocateData(S32 size = -1);
#017      /*virtual*/ U8* reallocateData(S32 size);
#018     
#019      /*virtual*/ void dump();
#020      /*virtual*/ void sanityCheck();
#021 
#022      // New methods
#023 public:
 
计算文件头数据大小。
#024      // calcHeaderSize() returns the maximum size of header;
#025      //   0 indicates we don't know have a header and have to lead the entire file
#026      virtual S32 calcHeaderSize() { return 0; };
 
计算文件数据占用的大小。
#027      // calcDataSize() returns how many bytes to read to load discard_level (including header)
#028      virtual S32 calcDataSize(S32 discard_level);
#029      // calcDiscardLevelBytes() returns the smallest valid discard level based on the number of input bytes
#030      virtual S32 calcDiscardLevelBytes(S32 bytes);
#031      // getRawDiscardLevel()by default returns mDiscardLevel, but may be overridden (LLImageJ2C)
#032      virtual S8 getRawDiscardLevel() { return mDiscardLevel; }
#033     
 
下面从文件里加载数据和保存压缩的数据到文件里。
#034      BOOL load(const LLString& filename);
#035      BOOL save(const LLString& filename);
#036 
#037      virtual BOOL updateData() = 0; // pure virtual
#038      void setData(U8 *data, S32 size);
#039      void appendData(U8 *data, S32 size);
#040 
 
下面函数是解压图片数据的接口。
#041      // Loads first 4 channels.
#042      virtual BOOL decode(LLImageRaw* raw_image, F32 decode_time=0.0) = 0; 
#043      // Subclasses that can handle more than 4 channels should override this function.
#044      virtual BOOL decode(LLImageRaw* raw_image, F32 decode_time, S32 first_channel, S32 max_channel);
#045 
 
下面函数是压缩数据的接口。
#046      virtual BOOL encode(const LLImageRaw* raw_image, F32 encode_time=0.0) = 0;
#047 
#048      S8 getCodec() const;
#049      BOOL isDecoding() const { return mDecoding ? TRUE : FALSE; }
#050      BOOL isDecoded() const { return mDecoded ? TRUE : FALSE; }
#051      void setDiscardLevel(S8 discard_level) { mDiscardLevel = discard_level; }
#052      S8 getDiscardLevel() const { return mDiscardLevel; }
#053 
#054 protected:
#055      BOOL copyData(U8 *data, S32 size); // calls updateData()
#056     
#057 protected:
#058      S8 mCodec;
#059      S8 mDecoding;
#060      S8 mDecoded;
#061      S8 mDiscardLevel;
#062 
#063 public:
#064      static S32 sGlobalFormattedMemory;
#065 };
 
有了这个中间表示,就可以把不同压缩的文件格式统一到一种操作方式了。