00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00022 #include "ATGAImage.h"
00023
00025
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
00041
00042
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
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
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
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)
00093 DecodeT2Tga24(Header, nImageSize);
00094
00095 else if (Header.DataTypeCode == 10)
00096 DecodeT10Tga24(Header, nImageSize);
00097
00098
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
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; )
00161 {
00162 ubPacketInfo = TempData[nReadCnt];
00163 nReadCnt++;
00164
00165
00166 PacketType = 0x80 & ubPacketInfo;
00167
00168
00169
00170 PixelCount = ubPacketInfo;
00171
00172 if (PixelCount < 128)
00173 PixelCount++;
00174 else
00175 PixelCount -= 127;
00176
00177 if (PacketType)
00178 {
00179
00180 p[3] = TempData[nReadCnt + 0];
00181 p[2] = TempData[nReadCnt + 1];
00182 p[1] = TempData[nReadCnt + 2];
00183
00184 nReadCnt += 3;
00185
00186 if (m_ubBpp == 4)
00187 p[0] = TempData[nReadCnt + 3];
00188 else if (m_bAlpha)
00189 {
00190 p[0] = m_ubAlphaValue;
00191 }
00192
00193
00194 while(PixelCount--)
00195 {
00196
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];
00205
00206 }
00207 }
00208 else
00209 {
00210 while(PixelCount--)
00211 {
00212 p[3] = TempData[nReadCnt + 0];
00213 p[2] = TempData[nReadCnt + 1];
00214 p[1] = TempData[nReadCnt + 2];
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;
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
00254 pFile = fopen(strName.data(), "rb");
00255
00256
00257 if (pFile == NULL)
00258 {
00259
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
00268 int result = ferror(pFile);
00269
00270
00271 if (result)
00272 {
00273 MessageBox(NULL, "Failed To Get Data!", "Error", MB_OK);
00274 }
00275
00276
00277 fclose(pFile);
00278 }