Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Namespace Members   Compound Members   File Members  

ATGAImage.cpp

Go to the documentation of this file.
00001 /*
00002 CS Senior Project 2003
00003 Team : Leftfield
00004 Project : ModernWarfare
00005 Members :
00006 - Russ Christensen              <rchriste@cs.utah.edu>
00007 - Todd Smith                    <tcsmith@cs.utah.edu>
00008 - Usit Duongsaa                 <duongsaa@cs.utah.edu>
00009 
00010 Copyright 2003 Russ Christensen, Usit Duongsaa, and Todd Smith. All rights reserved.
00011 
00012 Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
00013 
00014 Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. 
00015 Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. 
00016 THIS SOFTWARE IS PROVIDED BY RUSS CHRISTENSEN, USIT DUONGSAA, AND TODD SMITH ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL RUSS, USIT, TODD OR OTHER CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00017 */
00018 
00022 #include "ATGAImage.h"
00023 
00025 // Construction/Destruction
00027 
00028 CImage::CImage()
00029 {
00030         m_ubImageData = NULL;
00031         m_dwHeight = 0;
00032         m_dwWidth = 0;
00033         m_ubBpp = 0;
00034         m_ubAlphaValue = 128;
00035         m_bAlpha = false;
00036 }
00037 
00038 CImage::~CImage()
00039 {
00040 /*      if (m_ubImageData != NULL)
00041                 delete [] m_ubImageData;
00042         m_ubImageData = NULL;*/
00043 }
00044 
00045 void CImage::LoadBmp(const string& strFileName)
00046 {
00047         
00048 }
00049 
00050 void CImage::LoadTga(const string& strFileName)
00051 {
00052         FILE* pFile;
00053         tgaHeader Header;
00054 
00055         pFile = fopen(strFileName.data(), "rb");
00056 
00057         if (!pFile)
00058         {
00059                 return;
00060         }
00061 
00062         // Load header
00063         fread (&Header, sizeof(tgaHeader), 1, pFile);
00064 
00065         m_dwWidth = Header.Width;
00066         m_dwHeight = Header.Height;
00067         m_ubBpp = Header.BitsPerPixel / 8;
00068 
00069         // if we want an alpha channel for the 24 bit graphic and make it 32 bit 
00070         int ColorDepth = m_ubBpp;
00071 
00072         if (m_bAlpha)
00073                 ColorDepth++;
00074 
00075         int nImageSize = m_dwWidth * m_dwHeight * ColorDepth;
00076 
00077         m_ubImageData = new UBYTE [nImageSize];
00078         if (m_ubImageData == NULL)
00079         {
00080                 fclose(pFile);
00081                 return;
00082         }
00083 
00084         // Is the data read equal to the amount we wanted?
00085         if (nImageSize != 
00086                 fread (m_ubImageData, sizeof(UBYTE), nImageSize, pFile) && Header.DataTypeCode == 2)
00087         {
00088                 fclose(pFile);
00089                 return;
00090         }
00091 
00092         if (Header.DataTypeCode == 2)   // uncompressed data
00093                 DecodeT2Tga24(Header, nImageSize);
00094 
00095         else if (Header.DataTypeCode == 10)   // RLE compressed
00096                 DecodeT10Tga24(Header, nImageSize);
00097 
00098         // close file
00099         fclose(pFile);
00100 }
00101 
00102 UBYTE* CImage::GetImageData()
00103 {
00104         return m_ubImageData;
00105 }
00106 
00107 
00108 UBYTE CImage::GetBPP()
00109 {
00110         return m_ubBpp;
00111 }
00112 
00113 UINT CImage::GetHeight()
00114 {
00115         return m_dwHeight;
00116 }
00117         
00118 UINT CImage::GetWidth()
00119 {
00120         return m_dwWidth;
00121 }
00122 
00123 
00124 void CImage::DecodeT2Tga24(const tgaHeader& Header, const int& nImageSize)
00125 {
00126         UBYTE ColorSwap;
00127 
00128         // BGAR to ARGB
00129         for (int i = 0; i < nImageSize; i+=m_ubBpp)
00130         {
00131                 ColorSwap = m_ubImageData[i];
00132                 m_ubImageData[i] = m_ubImageData[i + 2];
00133                 m_ubImageData[i + 2] = ColorSwap;
00134         }
00135 }
00136 
00137 void CImage::DecodeT10Tga24(const tgaHeader& Header, int& nImageSize)
00138 {
00139         UBYTE p[4];
00140 
00141         int tempImageSize;
00142 
00143         if (m_bAlpha)
00144                         tempImageSize = Header.Width * Header.Height * 4;
00145                 else
00146                         tempImageSize = nImageSize;
00147 
00148         UBYTE * TempData = new UBYTE [nImageSize];
00149         if (TempData != NULL)
00150         {
00151                 memcpy(TempData, m_ubImageData, sizeof(UBYTE) * tempImageSize);
00152         }
00153         
00154         int nReadCnt = 0;
00155 
00156         UBYTE ubPacketInfo;
00157         short PacketType;
00158         short PixelCount;
00159 
00160         for (int i = 0; i < tempImageSize; /*i +=m_ubBpp */)
00161         {
00162                 ubPacketInfo = TempData[nReadCnt];
00163                 nReadCnt++;
00164 
00165                 // Check to see if it's encoded RLE or not
00166                 PacketType = 0x80 & ubPacketInfo;
00167 
00168                 // Get number of pixels in run - can go beyond current line
00169                 //short PixelCount2 = ( 0x007f & ubPacketInfo );
00170                 PixelCount = ubPacketInfo;
00171 
00172                 if (PixelCount < 128)
00173                         PixelCount++;
00174                 else
00175                         PixelCount -= 127;
00176 
00177                 if (PacketType)
00178                 {
00179                         // read the colors swap BGAR to RGBA
00180                         p[3] = TempData[nReadCnt + 0]; // B
00181                         p[2] = TempData[nReadCnt + 1]; // G
00182                         p[1] = TempData[nReadCnt + 2]; // R
00183 
00184                         nReadCnt += 3;
00185 
00186                         if (m_ubBpp == 4)
00187                                 p[0] = TempData[nReadCnt + 3];  //change when done
00188                         else if (m_bAlpha)
00189                         {
00190                                 p[0] = m_ubAlphaValue;          // Alpha channel color change to variable
00191                         }
00192 
00193                         
00194                         while(PixelCount--)
00195                         {
00196                                 // read the colors swap BGAR to RGBA
00197                                 m_ubImageData[i++] = p[1];
00198                                 m_ubImageData[i++] = p[2];
00199                                 m_ubImageData[i++] = p[3];
00200 
00201                                 if (m_ubBpp == 4)
00202                                         m_ubImageData[i++] = p[0];
00203                                 else if (m_bAlpha)
00204                                         m_ubImageData[i++] = p[0];              // Alpha channel color change to variable*/
00205 
00206                         }
00207                 }
00208                 else
00209                 {
00210                         while(PixelCount--)
00211                         {
00212                                 p[3] = TempData[nReadCnt + 0]; // B
00213                                 p[2] = TempData[nReadCnt + 1]; // G
00214                                 p[1] = TempData[nReadCnt + 2]; // R
00215 
00216                                 nReadCnt += 3;
00217                                 m_ubImageData[i++] = p[1];
00218                                 m_ubImageData[i++] = p[2];
00219                                 m_ubImageData[i++] = p[3];
00220 
00221                                 if (m_ubBpp == 4)
00222                                 {
00223                                         m_ubImageData[i++] = TempData[nReadCnt];
00224                                 
00225                                         nReadCnt++;
00226                                 }
00227                                 else if (m_bAlpha)
00228                                 {
00229                                         m_ubImageData[i++] = m_ubAlphaValue;            // Alpha channel color change to variable
00230                                 }
00231                         }
00232                 }       
00233         }
00234 
00235         delete [] TempData;
00236 }
00237 
00238 void CImage::SetAlpha(UBYTE AlphaValue)
00239 {
00240         m_ubAlphaValue = AlphaValue;
00241 }
00242 
00243 void CImage::bAlphaChannel(bool bAlpha)
00244 {
00245         m_bAlpha = bAlpha;
00246 }
00247 
00248 void CImage::LoadRaw(const string &strName, int nSize)
00249 {
00250         FILE *pFile = NULL;
00251         m_ubBpp = 1;
00252 
00253         // Open The File In Read / Binary Mode.
00254         pFile = fopen(strName.data(), "rb");
00255 
00256         // Check To See If We Found The File And Could Open It
00257         if (pFile == NULL)
00258         {
00259                 // Display Error Message And Stop The Function
00260                 MessageBox(NULL, "Can't Find The Height Map!", "Error", MB_OK);
00261                 return;
00262         }
00263 
00264         m_ubImageData = new UBYTE [nSize];
00265         fread(m_ubImageData, 1, nSize, pFile);
00266 
00267         // After We Read The Data, It's A Good Idea To Check If Everything Read Fine
00268         int result = ferror(pFile);
00269 
00270         // Check If We Received An Error
00271         if (result)
00272         {
00273                 MessageBox(NULL, "Failed To Get Data!", "Error", MB_OK);
00274         }
00275 
00276         // Close The File.*/
00277         fclose(pFile);
00278 }

Generated on Wed Apr 23 05:50:14 2003 for Modern Warfare by doxygen1.3-rc2