TARGA.H 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. /*
  2. ** Command & Conquer Renegade(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. #ifndef _TARGA_H_
  19. #define _TARGA_H_
  20. /****************************************************************************
  21. *
  22. * C O N F I D E N T I A L --- W E S T W O O D S T U D I O S
  23. *
  24. *----------------------------------------------------------------------------
  25. *
  26. * FILE
  27. * Targa.h
  28. *
  29. * DESCRIPTION
  30. * Targa image file class definitions.
  31. *
  32. * PROGRAMMER
  33. * Denzil E. Long, Jr.
  34. *
  35. * DATE
  36. * July 15, 1998
  37. *
  38. ****************************************************************************/
  39. #pragma pack(push, 1)
  40. // If you wish to display loading error messages call targa functions inside of
  41. // the following macro - for example TARGA_ERROR_HANDLER(targa.Open(filename, TGA_READMODE));
  42. // The error code is returned back from the handler so it can be used in an expression.
  43. long Targa_Error_Handler(long error_code,const char* filename);
  44. #define TARGA_ERROR_HANDLER(call,filename) Targa_Error_Handler(call,filename)
  45. /*---------------------------------------------------------------------------
  46. * STRUCTURES AND RELATED DEFINITIONS
  47. *-------------------------------------------------------------------------*/
  48. /* TGAHeader - Targa Image File header.
  49. *
  50. * IDLength - Size of Image ID field
  51. * ColorMapType - Color map type.
  52. * ImageType - Image type code.
  53. * CMapStart - Color map origin.
  54. * CMapLength - Color map length.
  55. * CMapDepth - Depth of color map entries.
  56. * XOffset - X origin of image.
  57. * YOffset - Y origin of image.
  58. * Width - Width of image.
  59. * Height - Height of image.
  60. * PixelDepth - Image pixel size
  61. * ImageDescriptor - Image descriptor byte.
  62. */
  63. typedef struct _TGAHeader
  64. {
  65. char IDLength;
  66. char ColorMapType;
  67. char ImageType;
  68. short CMapStart;
  69. short CMapLength;
  70. char CMapDepth;
  71. short XOffset;
  72. short YOffset;
  73. short Width;
  74. short Height;
  75. char PixelDepth;
  76. char ImageDescriptor;
  77. } TGAHeader;
  78. /* ImageType definiton */
  79. #define TGA_NOIMAGE 0 /* No image data included in file */
  80. #define TGA_CMAPPED 1 /* Color-mapped image data */
  81. #define TGA_TRUECOLOR 2 /* Truecolor image data */
  82. #define TGA_MONO 3 /* Monochrome image data */
  83. #define TGA_CMAPPED_ENCODED 9 /* Color-mapped image data (Encoded) */
  84. #define TGA_TRUECOLOR_ENCODED 10 /* Truecolor image data (Encoded) */
  85. #define TGA_MONO_ENCODED 11 /* Monochrome image data (Encoded) */
  86. /* ImageDescriptor definition */
  87. #define TGAIDF_ATTRIB_BITS (0x0F<<0) /* Number of attribute bits per pixel */
  88. #define TGAIDF_XORIGIN (1<<4)
  89. #define TGAIDF_YORIGIN (1<<5)
  90. /* Access modes. */
  91. #define TGA_READMODE 0
  92. #define TGA_WRITEMODE 1
  93. #define TGA_RDWRMODE 2
  94. /* Error codes */
  95. #define TGAERR_OPEN -1
  96. #define TGAERR_READ -2
  97. #define TGAERR_WRITE -3
  98. #define TGAERR_SYNTAX -4
  99. #define TGAERR_NOMEM -5
  100. #define TGAERR_NOTSUPPORTED -6
  101. /* Flags definitions */
  102. #define TGAF_IMAGE (1<<0)
  103. #define TGAF_PAL (1<<1)
  104. #define TGAF_COMPRESS (1<<2)
  105. #define TGAF_TGA2 (1<<3)
  106. /* Macro definitions */
  107. #define TGA_BytesPerPixel(a) ((a+7) >> 3)
  108. /*---------------------------------------------------------------------------
  109. * TARGA 2.0 DEFINITIONS
  110. *-------------------------------------------------------------------------*/
  111. #define TGA2_SIGNATURE "TRUEVISION-XFILE"
  112. /* TGA2Footer - Targa 2.0 footer
  113. *
  114. * Extension - Offset to the Extension area from start of file.
  115. * Developer - Offset to the Developer area from start of file.
  116. * Signature - 16 byte Targa 2.0 signature "TRUEVISION-XFILE"
  117. * RsvdChar - Reserved character, must be ASCII "." (period)
  118. * BZST - Binary Zero String Terminator.
  119. */
  120. typedef struct _TGA2Footer
  121. {
  122. long Extension;
  123. long Developer;
  124. char Signature[16];
  125. char RsvdChar;
  126. char BZST;
  127. _TGA2Footer() {}
  128. } TGA2Footer;
  129. /* TGA2DateStamp - A series of 3 WORD values which define the integer value
  130. * for the date the image was saved.
  131. *
  132. * Month - Month number (1 - 12)
  133. * Day - Day number (1 - 31)
  134. * Year - Year number (4 digit, ie. 1989)
  135. */
  136. typedef struct _TGA2DateStamp
  137. {
  138. short Month;
  139. short Day;
  140. short Year;
  141. } TGA2DateStamp;
  142. /* TGA2TimeStamp - A series of 3 WORD values which define the integer value
  143. * for the time the image was saved.
  144. *
  145. * Hour - Hour number, military time (0 - 23)
  146. * Minute - Minute number (0 - 59)
  147. * Second - Second number (0 - 59)
  148. */
  149. typedef struct _TGA2TimeStamp
  150. {
  151. short Hour;
  152. short Minute;
  153. short Second;
  154. } TGA2TimeStamp;
  155. /* TGA2SoftVer - Define the version of the software used to generate file.
  156. *
  157. * Number - Version number * 100
  158. * Letter - Version letter
  159. */
  160. typedef struct _TGA2SoftVer
  161. {
  162. short Number;
  163. char Letter;
  164. } TGA2SoftVer;
  165. /* TGA2Ratio - Numerator and denominator which when taken together specify
  166. * a ratio.
  167. *
  168. * Numer - Numerator
  169. * Denom - Denominator (a value of zero indicates no ratio specified)
  170. */
  171. typedef struct _TGA2Ratio
  172. {
  173. short Numer;
  174. short Denom;
  175. } TGA2Ratio;
  176. /* TGA2Extension - Extension area, provided for additional file information.
  177. * This data is pointed to by the Extension offset in the
  178. * TGA2Footer.
  179. *
  180. * ExtSize - Extension area size. (495 bytes for 2.0)
  181. * AuthName - Name of the person who created image (NULL terminated ASCII)
  182. * AuthComment - Comments of the author (NULL terminated ASCII)
  183. * DateStamp - Date the file was created. (See TGA2DateStamp)
  184. * TimeStamp - Time the file was created. (See TGA2TimeStamp)
  185. * JobName - Name of job image belongs to (NULL terminated ASCII)
  186. * JobTime - Elapsed time of the job.
  187. * SoftID - ID of software used to create image (NULL terminated ASCII)
  188. * SoftVer - Version number of software used.
  189. * KeyColor - Tranparent color value.
  190. * Aspect - Pixel aspect ratio.
  191. * Gamma - Fractional gamma value.
  192. * ColorCor - Color correction table offset.
  193. * PostStamp - Postage stamp image offset.
  194. * ScanLine - Scan line table offset.
  195. * Attributes - Alpha channel attributes. (Set defines below)
  196. */
  197. typedef struct _TGA2Extension
  198. {
  199. short ExtSize;
  200. char AuthName[41];
  201. char AuthComment[324];
  202. TGA2DateStamp Date;
  203. TGA2TimeStamp Time;
  204. char JobName[41];
  205. TGA2TimeStamp JobTime;
  206. char SoftID[41];
  207. TGA2SoftVer SoftVer;
  208. long KeyColor;
  209. TGA2Ratio Aspect;
  210. TGA2Ratio Gamma;
  211. long ColorCor;
  212. long PostStamp;
  213. long ScanLine;
  214. char Attributes;
  215. } TGA2Extension;
  216. /* Alpha channel attributes (Extension Area) */
  217. #define EXTA_NOALPHA 0 /* No alpha data included */
  218. #define EXTA_IGNORE 1 /* Undefined alpha data, can ignore */
  219. #define EXTA_RETAIN 2 /* Undefined alpha data, should retain */
  220. #define EXTA_USEFUL 3 /* Useful alpha channel */
  221. #define EXTA_PREMULT 4 /* Pre-Multiplied alpha data */
  222. #pragma pack(pop)
  223. /*
  224. ** This define changes this code from code that works with standard IO calls,
  225. ** to code that uses FileClass and FileFactoryClass.
  226. */
  227. #define TGA_USES_WWLIB_FILE_CLASSES
  228. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  229. class FileClass;
  230. #endif
  231. /*---------------------------------------------------------------------------
  232. * CLASS DEFINITION
  233. *-------------------------------------------------------------------------*/
  234. class Targa
  235. {
  236. public:
  237. /* Constructor/destructor */
  238. Targa(void);
  239. ~Targa();
  240. /* Function prototypes. */
  241. long Open(const char* name, long mode);
  242. void Close(void);
  243. long Load(const char* name, char* palette, char* image,bool invert_image=true);
  244. long Load(const char* name, long flags, bool invert_image=true);
  245. long Save(const char* name, long flags, bool addextension = false);
  246. void XFlip(void);
  247. void YFlip(void);
  248. char* SetImage(char* buffer);
  249. char* GetImage(void) const {return (mImage);}
  250. char* SetPalette(char* buffer);
  251. char* GetPalette(void) const {return (mPalette);}
  252. bool IsCompressed(void);
  253. TGA2Extension* GetExtension(void);
  254. TGAHeader Header;
  255. protected:
  256. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  257. FileClass *TGAFile;
  258. #else
  259. long mFH;
  260. #endif
  261. long mAccess;
  262. long mFlags;
  263. char* mImage;
  264. char* mPalette;
  265. TGA2Extension mExtension;
  266. private:
  267. // Utility functions
  268. long DecodeImage(void);
  269. long EncodeImage(void);
  270. void InvertImage(void);
  271. // These functions are for ease of ifdef'ing between standard io calls
  272. // and FileClass.
  273. void Clear_File(void);
  274. bool Is_File_Open(void);
  275. bool File_Open_Read(const char* name);
  276. bool File_Open_Write(const char* name);
  277. bool File_Open_ReadWrite(const char* name);
  278. int File_Seek(int pos, int dir);
  279. int File_Read(void *buffer, int size);
  280. int File_Write(void *buffer, int size);
  281. };
  282. #endif /* _TARGA_H_ */