| 40 | } |
| 41 | |
| 42 | bool IMAGE::LoadTGA (const char* szFile) |
| 43 | { |
| 44 | FILE* pFile = fopen (szFile, "rb"); |
| 45 | |
| 46 | if (!pFile) |
| 47 | return (false); |
| 48 | |
| 49 | // Read the header of the TGA, compare it with the known headers for compressed and uncompressed TGAs |
| 50 | unsigned char ucHeader[18]; |
| 51 | fread (ucHeader, sizeof (unsigned char) * 18, 1, pFile); |
| 52 | |
| 53 | while (ucHeader[0] > 0) |
| 54 | { |
| 55 | --ucHeader[0]; |
| 56 | |
| 57 | unsigned char temp; |
| 58 | fread (&temp, sizeof (unsigned char), 1, pFile); |
| 59 | } |
| 60 | |
| 61 | m_iImageWidth = ucHeader[13] * 256 + ucHeader[12]; |
| 62 | m_iImageHeight = ucHeader[15] * 256 + ucHeader[14]; |
| 63 | m_iBytesPerPixel = ucHeader[16] / 8; |
| 64 | |
| 65 | |
| 66 | // check whether width, height an BitsPerPixel are valid |
| 67 | if ((m_iImageWidth <= 0) || (m_iImageHeight <= 0) || ((m_iBytesPerPixel != 1) && (m_iBytesPerPixel != 3) && (m_iBytesPerPixel != 4))) |
| 68 | { |
| 69 | fclose (pFile); |
| 70 | return (false); |
| 71 | } |
| 72 | |
| 73 | // allocate the image-buffer |
| 74 | m_Pixels.resize (m_iImageWidth * m_iImageHeight * 4); |
| 75 | |
| 76 | |
| 77 | // call the appropriate loader-routine |
| 78 | if (ucHeader[2] == 2) |
| 79 | { |
| 80 | LoadUncompressedTGA (pFile); |
| 81 | } |
| 82 | else |
| 83 | if (ucHeader[2] == 10) |
| 84 | { |
| 85 | LoadCompressedTGA (pFile); |
| 86 | } |
| 87 | else |
| 88 | { |
| 89 | fclose (pFile); |
| 90 | return (false); |
| 91 | } |
| 92 | |
| 93 | fclose (pFile); |
| 94 | |
| 95 | return (true); |
| 96 | } |
| 97 | |
| 98 | void IMAGE::LoadUncompressedTGA (FILE* pFile) |
| 99 | { |
no outgoing calls
no test coverage detected