• <ins id="pjuwb"></ins>
    <blockquote id="pjuwb"><pre id="pjuwb"></pre></blockquote>
    <noscript id="pjuwb"></noscript>
          <sup id="pjuwb"><pre id="pjuwb"></pre></sup>
            <dd id="pjuwb"></dd>
            <abbr id="pjuwb"></abbr>
            隨筆 - 132  文章 - 51  trackbacks - 0
            <2011年4月>
            272829303112
            3456789
            10111213141516
            17181920212223
            24252627282930
            1234567

            常用鏈接

            留言簿(7)

            隨筆分類

            隨筆檔案

            文章分類

            文章檔案

            cocos2d-x

            OGRE

            OPenGL

            搜索

            •  

            最新評論

            閱讀排行榜

            評論排行榜

             

            實例LoadTGA.rar下載

            Image.h
            #ifndef _IMAGE_H_
            #define _IMAGE_H_

            #pragma once

            // These defines are used to tell us about the type of TARGA file it is
            #define TGA_RGB         2        // This tells us it's a normal RGB (really BGR) file
            #define TGA_A         3        // This tells us it's a ALPHA file
            #define TGA_RLE        10        // This tells us that the targa is Run-Length Encoded (RLE)


            #pragma pack( push, 
            1 )            //禁止字節自動對齊
            typedef struct
            {
                GLubyte        descLen;
                GLubyte        cmapType;
                GLubyte        imageType;
                GLshort        cmapStart;
                GLushort    cmapEntries;
                GLubyte        cmapBits;
                GLushort    xOffset;
                GLushort    yOffset;
            }
            Header;

            typedef 
            struct 
            {
                Header        head;
                GLushort    width;
                GLushort    height;
                GLubyte        bpp;
                GLubyte        attrib;
            }
            TGAHeader;


            typedef 
            struct _tTexImage
            {
                GLubyte
            *    imageData;
                GLuint        width;
                GLuint        height;
                GLuint        bpp;                
            //Image color depth in bits per pixel
                GLuint        texID;
                GLuint        imageType;
                GLboolean    bCompressed;        
            //Compressed or Uncompressed

                _tTexImage():imageData(NULL)
            {}
            }
            TexImage;

            #pragma pack( pop )

            class Image
            {
            public:
                Image(
            void);
                
            ~Image(void);

                
            public:
                
            /** 加載TGA圖片 **/
                BOOL    loadTGA( TexImage
            * texture, LPCSTR filename );
                BOOL    release( TexImage
            * texture );
                
                
            /** 生成紋理 **/
                
            void    generateTexture( TexImage* texture, BOOL bMipmap = TRUE );
            protected:
                BOOL    loadUncompressedTGA( TexImage
            * texture, FILE* file );
                BOOL    loadCompressedTGA( TexImage
            * texture, FILE* file );
            }
            ;

            #endif

            Image.cpp
            /*
                    說說異或運算^和他的一個常用作用。
                    異或的運算方法是一個二進制運算:
                    1^1=0
                    0^0=0
                    1^0=1
                    0^1=1

                    兩者相等為0,不等為1.

                    這樣我們發現交換兩個整數的值時可以不用第三個參數。
                    如a=11,b=9.以下是二進制
                    a=a^b=1011^1001=0010;
                    b=b^a=1001^0010=1011;
                    a=a^b=0010^1011=1001;
                    這樣一來a=9,b=13了。

                    舉一個運用, 按一個按鈕交換兩個mc的位置可以這樣。

                    mybt.onPress=function()
                    {
                        mc1._x=mc1._x^mc2._x;
                        mc2._x=mc2._x^mc1._x;
                        mc1._x=mc1._x^mc2._x;

                        mc1._y=mc1._y^mc2._y; 
                        mc2._y=mc2._y^mc1._y;
                        mc1._y=mc1._y^mc2._y;
                    }

                    這樣就可以不通過監時變量來傳遞了。

                    最后要聲明:只能用于整數。

                    vertex[0].position = vec2( -width/2, height/2 );
                    vertex[1].position = vec2( width/2, height/2 );
                    vertex[2].position = vec2( width/2, -height/2 );
                    vertex[3].position = vec2( -width/2, -height/2 );
            */

            #include 
            "StdAfx.h"
            #include 
            "global.h"

            Image::Image(
            void)
            {
            }


            Image::
            ~Image(void)
            {
            }



            //-----------------------------------------------------------------------
            // 函數名    : Image::loadTGA
            // 說明      : 加載TGA圖片
            // 返回      : BOOL 
            // 參數      : TexImage* texture
            // 參數      : LPCSTR filename 
            // 作者      : Teng
            // 創建時間  : 2010-4-14 15:35:32
            // 最后修改  : 2010-4-14
            //-----------------------------------------------------------------------
            BOOL Image::loadTGA( TexImage* texture, LPCSTR filename )
            {
                
            if ( filename == NULL )
                    
            return FALSE;
                    
                Header uTGAcompare 
            = 0,0,2,0,0,0,0,0};        //2為非壓縮RGB格式        3  -  未壓縮的,黑白圖像
                Header cTGAcompare = 0,0,10,0,0,0,0,0};        //10為壓縮RGB格式

                TGAHeader header;
                FILE
            * file = fopen( filename, "rb" );
                
            if ( !file ){
                    TRACE(
            "Openf file %s failed!\n", filename );
                    
            return FALSE;
                }

                
                
            if ( fread( &header, 1sizeof(TGAHeader), file ) != sizeof( TGAHeader ) ){        //讀取TGA整個頭結構體
                    if ( file )
                        fclose( file );
                    TRACE(
            "Read data failed\n");
                    
            return FALSE;
                }

                
                texture
            ->width = header.width;
                texture
            ->height = header.height;
                texture
            ->bpp = header.bpp;

                
            if ( header.bpp == 32 )
                    texture
            ->imageType = GL_RGBA;
                
            else if ( header.bpp = 24 )
                    texture
            ->imageType = GL_RGB;
                
            else{
                    TRACE(
            "Image type error!\n");
                    
            return FALSE;
                }

                    

                
            if ( memcmp( &uTGAcompare, &header.head, sizeof(header.head) )== 0 ){            //未壓縮TGA
                    texture->bCompressed = FALSE;
                    
            if ( !loadUncompressedTGA( texture, file ) ){
                        TRACE(
            "Load uncompressed TGA failed!\n");
                        
            return FALSE;
                    }

                }
            else if ( memcmp( &cTGAcompare, &header.head ,sizeof(header.head) ) == 0 ){    //壓縮TGA
                    texture->bCompressed = TRUE;
                    
            if ( !loadCompressedTGA( texture, file ) ){
                        TRACE(
            "Load compressed TGA failed!\n");
                        
            return FALSE;
                    }

                }
            else{
                    TRACE(
            "Error TGA type!\n");
                    
            return FALSE;
                }


                
            return TRUE;
            }



            //-----------------------------------------------------------------------
            // 函數名    : Image::generateTexture
            // 說明      : 生成一張紋理
            // 返回      : void 
            // 參數      : TexImage* texture
            // 參數      : BOOL bMipmap /*= TRUE*/ 
            // 作者      : Teng
            // 創建時間  : 2010-5-25 17:06:42
            // 最后修改  : 2010-5-25
            //-----------------------------------------------------------------------
            void Image::generateTexture( TexImage* texture, BOOL bMipmap /*= TRUE*/  )
            {
                
            //Bulid a textue from the data.
                glGenTextures( 1,  &texture->texID ); 
                
                
            //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, m_texWrap);
                
            //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, m_texWrap);
                
            //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, m_texFilter);
                
            //glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, m_texFilter);
                
            //glTexEnvf(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, m_texMode);

                glBindTexture( GL_TEXTURE_2D, texture
            ->texID );

                
            if ( bMipmap ){
                    gluBuild2DMipmaps( GL_TEXTURE_2D,  texture
            ->bpp/8, texture->width,texture->height, texture->imageType, GL_UNSIGNED_BYTE, texture->imageData );
                    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR_MIPMAP_LINEAR );
                    glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);    
                }
            else{
                    glTexImage2D( GL_TEXTURE_2D, 
            0, texture->bpp/8, texture->width, texture->height, 0, texture->imageType, GL_UNSIGNED_BYTE, texture->imageData ); 
                    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR );
                    glTexParameterf( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR );
                }

            }


            //-----------------------------------------------------------------------
            // 函數名    : Image::loadUncompressedTGA
            // 說明      : 加載未壓縮TGA紋理
            // 返回      : BOOL 
            // 參數      : TexImage* texture
            // 參數      : FILE* file        當前file指針,指向TGA圖像第一個像素地址
            // 作者      : Teng
            // 創建時間  : 2010-4-14 14:39:22
            // 最后修改  : 2010-4-14
            //-----------------------------------------------------------------------
            BOOL Image::loadUncompressedTGA( TexImage* texture, FILE* file )
            {
                ASSERT( file 
            != NULL && texture!=NULL );

                GLuint bytePerPixel 
            = texture->bpp/8;
                GLuint imgSize 
            = texture->width*texture->height*bytePerPixel;                //圖像總字節數
                texture->imageData = new GLubyte[ imgSize ];

                
            if ( fread( texture->imageData, 1, imgSize, file ) != imgSize  )
                
            {
                    TRACE(
            "Read texture imagedata failed!\n");
                    
            return FALSE;
                }


                
            //TGA采用了逆OpenGL的格式,要將BGR轉換成為RGB
                
            // Go through all of the pixels and swap the B and R values since TGA
                
            // files are stored as BGR instead of RGB (or use GL_BGR_EXT verses GL_RGB)
                forint i = 0; i < (int)imgSize; i+=bytePerPixel ){
                    
            /*GLushort temp = texture->imageData[i];
                    texture->imageData[i] = texture->imageData[i+2];
                    texture->imageData[i+2] = temp;
            */

                    texture
            ->imageData[i] ^= texture->imageData[i+2^= texture->imageData[i] ^= texture->imageData[ i+2 ];        //位操作提高速度,更換B,R分量
                }


                ::fclose( file );
                
            return TRUE;
            }


            //-----------------------------------------------------------------------
            // 函數名    : Image::loadCompressedTGA
            // 說明      : 加載壓縮TGA紋理
            // 返回      : BOOL 
            // 參數      : TexImage* texture
            // 參數      : FILE* file 
            // 作者      : Teng
            // 創建時間  : 2010-4-14 14:38:55
            // 最后修改  : 2010-4-14
            //-----------------------------------------------------------------------
            BOOL Image::loadCompressedTGA( TexImage* texture, FILE* file )
            {
                ASSERT( file 
            != NULL && texture!=NULL );
                
                GLuint bytePerPixel 
            = texture->bpp/8;
                GLuint imgSize 
            = texture->width*texture->height*bytePerPixel;
                texture
            ->imageData = new GLubyte[ imgSize ];

                GLuint pixelcount 
            = texture->width * texture->height;
                GLuint currentPixel 
            = 0;        //當前正在讀取的像素
                GLuint currentByte = 0;            //當前正在向圖像中寫入的像素
                GLubyte *colorbuffer = (GLubyte *)malloc( bytePerPixel );    // 一個像素的存儲空間s

                
            do
                
            {
                    GLubyte chunkHeader  
            = 0;        //存儲ID塊值的變量
                    if ( !fread( &chunkHeader,1sizeof( GLubyte ), file ) ){
                        
            return FALSE;
                    }

                    
            if ( chunkHeader < 128 )            //RAW塊
                    {
                        chunkHeader
            ++;                // 變量值加1以獲取RAW像素的總數

                        
            forint i = 0; i < chunkHeader; i++ ){
                            
            if ( fread( colorbuffer, 1,sizeof( bytePerPixel ), file ) != sizeof( bytePerPixel ) ){
                                TRACE(
            "Read pixel failed!\n");
                                
            return FALSE;
                            }

                            texture
            ->imageData[currentByte] = colorbuffer[ 2 ];
                            texture
            ->imageData[currentByte+1= colorbuffer[1];
                            texture
            ->imageData[currentByte+2= colorbuffer[0];
                            
            if ( bytePerPixel == 4 )
                                texture
            ->imageData[ currentByte+3= colorbuffer[3];

                            currentPixel
            ++;
                            currentByte 
            += bytePerPixel;
                        }

                    }

                     
            //下一段處理描述RLE段的“塊”頭。首先我們將chunkheader減去127來得到獲取下一個顏色重復的次數。 
                    else        
                    
            {
                        chunkHeader 
            -= 127;            //減去127獲得ID bit 的rid    開始循環拷貝我們多次讀到內存中的像素,這由RLE頭中的值規定。

                        
            if ( fread( colorbuffer,1sizeof( bytePerPixel ), file ) != sizeof( bytePerPixel ) ){
                            TRACE(
            "Read pixel failed!\n");
                            
            return FALSE;
                        }

                        
                        
            forint i = 0; i < chunkHeader; i++ ){
                            texture
            ->imageData[ currentByte ] = colorbuffer[ 2 ];                // 拷貝“R”字節
                            texture->imageData[ currentByte +1 ] = colorbuffer[ 1 ];            // 拷貝“G”字節
                            texture->imageData[ currentByte + 2 ] = colorbuffer[ 0 ];            // 拷貝“B”字節
                            if ( bytePerPixel == 4 )
                                texture
            ->imageData[ currentByte+3 ] = colorbuffer[ 3 ];            // 拷貝“A”字節

                            currentPixel
            ++;
                            currentByte 
            += bytePerPixel;
                        }

                    }

                }
            while( currentPixel < pixelcount );

                free( colorbuffer );
                ::fclose( file );
                
            return TRUE;
            }



            //-----------------------------------------------------------------------
            // 函數名    : Image::release
            // 說明      : 釋放資源
            // 返回      : BOOL 
            // 參數      : TexImage* texture 
            // 作者      : Teng
            // 創建時間  : 2010-5-25 17:42:59
            // 最后修改  : 2010-5-25
            //-----------------------------------------------------------------------
            BOOL Image::release( TexImage* texture )
            {
                
            if ( !texture )
                    
            return FALSE;
                
                
            if ( texture->imageData )
                    delete[] texture
            ->imageData;
                glDeleteTextures( 
            1&texture->texID );
                
            return TRUE;
            }

            posted on 2010-05-26 10:05 風輕云淡 閱讀(2492) 評論(1)  編輯 收藏 引用 所屬分類: 圖像讀取

            FeedBack:
            # re: TGA讀取(二) 2013-09-04 14:51 miye
            為什么無法運行。  回復  更多評論
              
            亚洲综合婷婷久久| 久久久久久九九99精品| 久久精品国产一区| 少妇人妻88久久中文字幕| 久久天天躁夜夜躁狠狠躁2022| 国产成人综合久久久久久| 欧美久久精品一级c片片| 久久久久亚洲AV成人片| 久久婷婷五月综合97色一本一本| 久久狠狠爱亚洲综合影院| 无码精品久久久久久人妻中字| 午夜久久久久久禁播电影| 久久亚洲精品成人av无码网站 | 精品国产91久久久久久久| 久久久精品2019免费观看| 久久精品人成免费| 91久久精品国产成人久久| 狠狠精品久久久无码中文字幕| 久久国产热这里只有精品| 青青草国产97免久久费观看| 久久亚洲国产精品成人AV秋霞| 久久综合给合久久国产免费| 69国产成人综合久久精品| 国产精品无码久久四虎| 国产精品成人久久久| 丁香五月网久久综合| 久久人人爽人爽人人爽av| 狠狠色综合网站久久久久久久高清| 国产成人精品综合久久久久 | 久久97久久97精品免视看秋霞| 久久久国产精华液| 伊人久久久AV老熟妇色| 国内精品久久久久久久coent| 国产aⅴ激情无码久久| 亚洲国产精品久久久久婷婷软件 | 久久久久亚洲?V成人无码| 午夜精品久久久久久中宇| 久久99亚洲综合精品首页| 久久精品aⅴ无码中文字字幕重口 久久精品a亚洲国产v高清不卡 | 亚洲国产成人久久精品影视| 99久久精品免费看国产一区二区三区|