TARGA.CPP 30 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398
  1. /*
  2. ** Command & Conquer Generals(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. /****************************************************************************
  19. *
  20. * 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
  21. *
  22. *----------------------------------------------------------------------------
  23. *
  24. * FILE
  25. * targa.cpp
  26. *
  27. * DESCRIPTION
  28. * Targa image file class.
  29. *
  30. * PROGRAMMER
  31. * Denzil E. Long, Jr.
  32. *
  33. * DATE
  34. * August 8, 1995
  35. *
  36. *----------------------------------------------------------------------------
  37. *
  38. * PUBLIC
  39. * Open - Open Targa image file.
  40. * Close - Close Targa image file.
  41. * Load - Load Targa image file.
  42. * Save - Save a Targa Image File.
  43. * XFlip - X flip the image.
  44. * YFlip - Y flip the image.
  45. * SetImage - Set the image buffer.
  46. * GetImage - Get the current image buffer address.
  47. * SetPalette - Set the palette buffer.
  48. * GetPalette - Retrieve the current palette buffer address.
  49. * GetExtension - Get Extension data. (Targa 2.0 files only)
  50. *
  51. * PRIVATE
  52. * DecodeImage - Decompress Targa image data.
  53. * EncodeImage - Compress the image using targa RLE.
  54. * InvertImage - Invert TrueColor image data.
  55. *
  56. * MODIFICATIONS:
  57. * Converted to work with FileClass, FileFactory (changes are inside
  58. * ifdefs, so can be easily reversed). Naty Hoffman, January 25, 2001
  59. *
  60. ****************************************************************************/
  61. #include "targa.h"
  62. #ifndef TGA_USES_WWLIB_FILE_CLASSES
  63. #include <stdio.h>
  64. #endif
  65. #include <malloc.h>
  66. #include <memory.h>
  67. #include <string.h>
  68. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  69. #include "wwfile.h"
  70. #include "ffactory.h"
  71. #else
  72. #include <io.h>
  73. #include <fcntl.h>
  74. #include <sys\stat.h>
  75. #endif
  76. /****************************************************************************
  77. *
  78. * NAME
  79. * Targa::Targa - Initialize a Targa instance.
  80. *
  81. * SYNOPSIS
  82. * Targa()
  83. *
  84. * void Targa(void);
  85. *
  86. * FUNCTION
  87. * Initialize the targa class instance.
  88. *
  89. * INPUTS
  90. * NONE
  91. *
  92. * RESULT
  93. * NONE
  94. *
  95. ****************************************************************************/
  96. Targa::Targa(void)
  97. {
  98. mImage = NULL;
  99. mPalette = NULL;
  100. Clear_File();
  101. mAccess = TGA_READMODE;
  102. mFlags = 0;
  103. memset(&Header, 0, sizeof(TGAHeader));
  104. memset(&mExtension, 0, sizeof(TGA2Extension));
  105. }
  106. /****************************************************************************
  107. *
  108. * NAME
  109. * Targa::~Targa - Targa class destructor.
  110. *
  111. * SYNOPSIS
  112. * ~Targa()
  113. *
  114. * void ~Targa(void);
  115. *
  116. * FUNCTION
  117. *
  118. * INPUTS
  119. * NONE
  120. *
  121. * RESULT
  122. * NONE
  123. *
  124. ****************************************************************************/
  125. Targa::~Targa(void)
  126. {
  127. /* Close the file if has been left open. */
  128. Close();
  129. /* Free the palette buffer if we allocated it. */
  130. if ((mPalette != NULL) && (mFlags & TGAF_PAL))
  131. free(mPalette);
  132. /* Free the image buffer if we allocated it. */
  133. if ((mImage != NULL) && (mFlags & TGAF_IMAGE))
  134. free(mImage);
  135. }
  136. /****************************************************************************
  137. *
  138. * NAME
  139. * Targa::Open - Open Targa image file.
  140. *
  141. * SYNOPSIS
  142. * Error = Open(Name, Mode)
  143. *
  144. * long Open(char *, long);
  145. *
  146. * FUNCTION
  147. * Open a Targa image file and read in its header. The file stream will
  148. * positioned after the ID field (if there is one).
  149. *
  150. * INPUTS
  151. * Name - Pointer to name of Targa file.
  152. * Mode - Access mode.
  153. *
  154. * RESULT
  155. * Error - Error code, 0 if okay.
  156. *
  157. ****************************************************************************/
  158. long Targa::Open(const char* name, long mode)
  159. {
  160. TGA2Footer footer;
  161. long size;
  162. long error = 0;
  163. /* File already open? */
  164. if (Is_File_Open() && (mAccess == mode)) {
  165. return (0);
  166. }
  167. Close();
  168. /* Initialize the access mode. */
  169. mAccess = mode;
  170. mFlags &= ~TGAF_TGA2;
  171. switch (mode) {
  172. /* Open targa file for read. */
  173. case TGA_READMODE:
  174. if (File_Open_Read(name)) {
  175. /* Check for 2.0 targa file by loading the footer */
  176. if (File_Seek(-26, SEEK_END) == -1) {
  177. // if (File_Seek(26, SEEK_END) == -1) {
  178. error = TGAERR_READ;
  179. }
  180. if (!error) {
  181. if (File_Read(&footer, sizeof(TGA2Footer)) != sizeof(TGA2Footer)) {
  182. error = TGAERR_READ;
  183. } else {
  184. /* If this a 2.0 file with an extension? */
  185. if (strncmp(footer.Signature, TGA2_SIGNATURE, 16) == 0) {
  186. if (footer.Extension != 0) {
  187. mFlags |= TGAF_TGA2;
  188. }
  189. }
  190. }
  191. }
  192. /* Read in Extension data */
  193. if (!error && (mFlags & TGAF_TGA2)) {
  194. if (File_Seek(footer.Extension, SEEK_SET) == -1) {
  195. error = TGAERR_READ;
  196. }
  197. if (!error) {
  198. if (File_Read(&mExtension, sizeof(TGA2Extension)) != sizeof(TGA2Extension)) {
  199. error = TGAERR_READ;
  200. }
  201. }
  202. }
  203. /* Read in header. */
  204. if (!error && (File_Seek(0, SEEK_SET) == -1)) {
  205. error = TGAERR_READ;
  206. } else {
  207. size = File_Read(&Header, sizeof(TGAHeader));
  208. if (size != sizeof(TGAHeader)) {
  209. error = TGAERR_READ;
  210. }
  211. }
  212. /* Skip the ID field */
  213. if (!error && (Header.IDLength != 0)) {
  214. if (File_Seek(Header.IDLength, SEEK_CUR) == -1) {
  215. error = TGAERR_READ;
  216. }
  217. }
  218. } else {
  219. error = TGAERR_OPEN;
  220. }
  221. break;
  222. /* Open targa file for write. */
  223. case TGA_WRITEMODE:
  224. if (!File_Open_Write(name)) {
  225. error = TGAERR_OPEN;
  226. } else {
  227. // printf("\r");
  228. }
  229. break;
  230. /* Open targa file for read/write.*/
  231. case TGA_RDWRMODE:
  232. if (File_Open_ReadWrite(name)) {
  233. /* Read in header. */
  234. size = File_Read(&Header, sizeof(TGAHeader));
  235. if (size != sizeof(TGAHeader)) {
  236. error = TGAERR_READ;
  237. }
  238. /* Skip the ID field */
  239. if (!error && (Header.IDLength != 0)) {
  240. if (File_Seek(Header.IDLength, SEEK_CUR) == -1) {
  241. error = TGAERR_READ;
  242. }
  243. }
  244. } else {
  245. error = TGAERR_OPEN;
  246. }
  247. break;
  248. }
  249. /* Close on any error! */
  250. if (error) {
  251. Close();
  252. }
  253. return (error);
  254. }
  255. /****************************************************************************
  256. *
  257. * NAME
  258. * Targa::Close - Close Targa image file.
  259. *
  260. * SYNOPSIS
  261. * Close()
  262. *
  263. * void Close(void);
  264. *
  265. * FUNCTION
  266. * Close the Targa image file and free its handle.
  267. *
  268. * INPUTS
  269. * NONE
  270. *
  271. * RESULT
  272. * NONE
  273. *
  274. ****************************************************************************/
  275. void Targa::Close(void)
  276. {
  277. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  278. if (TGAFile) {
  279. TGAFile->Close();
  280. _TheFileFactory->Return_File(TGAFile);
  281. TGAFile = NULL;
  282. }
  283. #else
  284. /* Close the file if it is open. */
  285. if (mFH != -1) {
  286. close(mFH);
  287. mFH = -1;
  288. }
  289. #endif
  290. }
  291. /****************************************************************************
  292. *
  293. * NAME
  294. * Targa::Load - Load Targa Image File into specified buffers.
  295. *
  296. * SYNOPSIS
  297. * Error = Load(Name, Palette, ImageBuffer)
  298. *
  299. * long Load(char *, char *, char *);
  300. *
  301. * FUNCTION
  302. * Open and load the Targa into the specified buffers. If either buffer
  303. * pointer is NULL then that field will not be processed.
  304. *
  305. * INPUTS
  306. * Name - Name of Targa image file to load.
  307. * Palette - Pointer to buffer to load the palette into.
  308. * ImageBuffer - Pointer to buffer to load the image data into.
  309. *
  310. * RESULT
  311. * Error - 0 if successful, or TGAERR_??? error code.
  312. *
  313. ****************************************************************************/
  314. long Targa::Load(const char* name, char* palette, char* image,bool invert_image)
  315. {
  316. long size;
  317. long depth;
  318. long error = 0;
  319. /* Open the Targa */
  320. if (Open(name, TGA_READMODE) == NULL) {
  321. /* Process ColorMap (palette) */
  322. if (Header.ColorMapType == 1) {
  323. depth = (Header.CMapDepth >> 3);
  324. size = (Header.CMapLength * depth);
  325. /* Load the palette from the TGA if a palette buffer is provided
  326. * otherwise we will skip it.
  327. */
  328. if ((palette != NULL) && (Header.CMapLength > 0)) {
  329. /* Adjust palette to the starting color entry. */
  330. palette += (Header.CMapStart * depth);
  331. /* Read in the palette. */
  332. if (File_Read(palette, size) != size) {
  333. error = TGAERR_READ;
  334. }
  335. } else {
  336. if (File_Seek(size, SEEK_CUR) == -1) {
  337. error = TGAERR_READ;
  338. }
  339. }
  340. }
  341. /* Load the image data from the TGA if an image buffer is provided
  342. * otherwise we are done.
  343. */
  344. if (!error && (image != NULL)) {
  345. depth = TGA_BytesPerPixel(Header.PixelDepth);
  346. size = ((Header.Width * Header.Height) * depth);
  347. switch (Header.ImageType) {
  348. case TGA_CMAPPED:
  349. if (File_Read(image, size) != size) {
  350. error = TGAERR_READ;
  351. }
  352. break;
  353. case TGA_TRUECOLOR:
  354. if (File_Read(image, size) == size) {
  355. if (invert_image) InvertImage();
  356. } else {
  357. error = TGAERR_READ;
  358. }
  359. break;
  360. case TGA_MONO:
  361. if (File_Read(image, size) != size) {
  362. error = TGAERR_READ;
  363. }
  364. break;
  365. case TGA_CMAPPED_ENCODED:
  366. error = DecodeImage();
  367. break;
  368. case TGA_TRUECOLOR_ENCODED:
  369. if ((error = DecodeImage()) == NULL) {
  370. if (invert_image) InvertImage();
  371. }
  372. break;
  373. default:
  374. error = TGAERR_NOTSUPPORTED;
  375. break;
  376. }
  377. /* Arrange the image so that the origin position (coordinate 0,0)
  378. * is the upperleft hand corner of the image.
  379. */
  380. if (!error) {
  381. if ( Header.ImageDescriptor & TGAIDF_XORIGIN ) {
  382. XFlip();
  383. Header.ImageDescriptor &= ~TGAIDF_XORIGIN;
  384. }
  385. // Mod (IML) : Locate the origin at the bottom-left corner instead. This
  386. // will make ot consistent with .TGA's that have been generated with our
  387. // existing software.
  388. // if (( Header.ImageDescriptor & TGAIDF_YORIGIN ) == 0){
  389. if ( Header.ImageDescriptor & TGAIDF_YORIGIN ) {
  390. YFlip();
  391. // Bug fix (IML) : Clear this flag to indicate to the targa reader
  392. // that the Y-origin is at the bottom of the image.
  393. Header.ImageDescriptor &= ~TGAIDF_YORIGIN;
  394. }
  395. }
  396. }
  397. /* Close the Targa */
  398. Close();
  399. } else {
  400. error = TGAERR_OPEN;
  401. }
  402. return (error);
  403. }
  404. /****************************************************************************
  405. *
  406. * NAME
  407. * Targa::Load - Load Targa Image File. (Auto buffer allocation).
  408. *
  409. * SYNOPSIS
  410. * Error = Load(Name, Flags)
  411. *
  412. * long Load(char, long);
  413. *
  414. * FUNCTION
  415. * Open and load the Targa into buffers allocated by this function.
  416. *
  417. * INPUTS
  418. * Name - Name of Targa image file to load.
  419. * Flags -
  420. *
  421. * RESULT
  422. * Error - 0 if successful, or TGAERR_??? error code.
  423. *
  424. ****************************************************************************/
  425. long Targa::Load(const char* name, long flags, bool invert_image)
  426. {
  427. long size;
  428. long error = 0;
  429. /* Open the file to get the header. */
  430. if (Open(name, TGA_READMODE) == 0) {
  431. /* Allocate palette memory if requested to and the targa has one. */
  432. if ((flags & TGAF_PAL) && (Header.ColorMapType == 1)) {
  433. /* Dispose of any previous palette. */
  434. if ((mPalette != NULL) && (mFlags & TGAF_PAL)) {
  435. free(mPalette);
  436. mPalette = NULL;
  437. mFlags &= ~TGAF_PAL;
  438. }
  439. /* Only allocate a palette if the client hasn't assigned one. */
  440. if ((mPalette == NULL) && !(mFlags & TGAF_PAL)) {
  441. /* Compute the size of the palette from the targa header. */
  442. size = (Header.CMapLength * (Header.CMapDepth >> 3));
  443. if (size != 0) {
  444. /* Allocate memory for the palette. */
  445. if ((mPalette = (char *)malloc(size)) != NULL) {
  446. mFlags |= TGAF_PAL; /* We allocated the palette. */
  447. } else {
  448. error = TGAERR_NOMEM;
  449. }
  450. }
  451. }
  452. }
  453. /* Allocate image memory if requested to. */
  454. if (!error && (flags & TGAF_IMAGE)) {
  455. /* Dispose of any previous image. */
  456. if ((mImage != NULL) && (mFlags & TGAF_IMAGE)) {
  457. free(mImage);
  458. mImage = NULL;
  459. mFlags &= ~TGAF_IMAGE;
  460. }
  461. /* Only allocate an image if the client hasn't assigned one. */
  462. if ((mImage == NULL) && !(mFlags & TGAF_IMAGE)) {
  463. /* Compute the size of the image data from the targa header. */
  464. size = ((Header.Width * Header.Height) * TGA_BytesPerPixel(Header.PixelDepth));
  465. if (size != 0) {
  466. /* Allocate memory for the image. */
  467. if ((mImage = (char *)malloc(size)) != NULL) {
  468. mFlags |= TGAF_IMAGE; /* We allocated the image. */
  469. } else {
  470. error = TGAERR_NOMEM;
  471. }
  472. }
  473. }
  474. }
  475. /* Read in the file contents. */
  476. if (!error) {
  477. error = Load(name, mPalette, mImage, invert_image);
  478. }
  479. /* Close the file. */
  480. Close();
  481. } else {
  482. error = TGAERR_OPEN;
  483. }
  484. return (error);
  485. }
  486. /****************************************************************************
  487. *
  488. * NAME
  489. * Targa::Save - Save a Targa Image File.
  490. *
  491. * SYNOPSIS
  492. * Error = Save(Name, Flags)
  493. *
  494. * long Save(char *, long);
  495. *
  496. * FUNCTION
  497. *
  498. * INPUTS
  499. * Name - Pointer to name of file to save.
  500. * Flags -
  501. *
  502. * RESULT
  503. * Error - 0 if successful, or TGAERR_??? error code.
  504. *
  505. ****************************************************************************/
  506. long Targa::Save(const char* name, long flags, bool addextension)
  507. {
  508. long size;
  509. long depth;
  510. char *palette;
  511. char *temppal;
  512. char *ptr;
  513. //long i,n;
  514. //char c;
  515. long error = 0;
  516. TGA2Footer footer;
  517. /* Open the Targa for write. */
  518. if (Open(name, TGA_WRITEMODE) == NULL)
  519. {
  520. Header.IDLength = 0;
  521. /* Set the ImageType for compression. */
  522. if (flags & TGAF_COMPRESS)
  523. {
  524. switch (Header.ImageType)
  525. {
  526. case TGA_CMAPPED:
  527. case TGA_TRUECOLOR:
  528. case TGA_MONO:
  529. Header.ImageType += 8;
  530. break;
  531. case TGA_CMAPPED_ENCODED:
  532. case TGA_TRUECOLOR_ENCODED:
  533. case TGA_MONO_ENCODED:
  534. break;
  535. /* Turn off compression for unknown types. */
  536. default:
  537. flags &= ~TGAF_COMPRESS;
  538. break;
  539. }
  540. }
  541. /*-----------------------------------------------------------------------
  542. * WRITE THE HEADER DATA SECTION
  543. *---------------------------------------------------------------------*/
  544. if (File_Write(&Header, sizeof(TGAHeader)) != sizeof(TGAHeader))
  545. error = TGAERR_WRITE;
  546. /*-----------------------------------------------------------------------
  547. * WRITE THE COLORMAP (PALETTE) DATA SECTION
  548. *---------------------------------------------------------------------*/
  549. if (!error && (flags & TGAF_PAL) && (mPalette != NULL)
  550. && (Header.CMapLength > 0))
  551. {
  552. /* Adjust palette to the starting color entry. */
  553. depth = (Header.CMapDepth >> 3);
  554. palette = mPalette + (Header.CMapStart * depth);
  555. size = (Header.CMapLength * depth);
  556. /* Allocate temporary buffer for palette manipulation. */
  557. if ((temppal = (char *)malloc(size)) != NULL)
  558. {
  559. memcpy(temppal, palette, size);
  560. ptr = temppal;
  561. #if(0)
  562. /* Swap the byte ordering of the palette entries. */
  563. for (i = 0; i < Header.CMapLength; i++)
  564. {
  565. c = *ptr;
  566. *ptr = *(ptr + (depth - 1));
  567. *(ptr + (depth - 1)) = c;
  568. /* Next entry */
  569. palette += depth;
  570. }
  571. #endif
  572. /* Write the palette. */
  573. if (File_Write(temppal, size) != size)
  574. error = TGAERR_WRITE;
  575. /* Free temporary palette buffer. */
  576. free(temppal);
  577. }
  578. else
  579. error = TGAERR_NOMEM;
  580. }
  581. /*-----------------------------------------------------------------------
  582. * WRITE THE IMAGE DATA SECTION
  583. *---------------------------------------------------------------------*/
  584. if (!error && (flags & TGAF_IMAGE) && (mImage != NULL))
  585. {
  586. bool imageinverted;
  587. /* Invert truecolor data. */
  588. if ((Header.ImageType == TGA_TRUECOLOR) || (Header.ImageType == TGA_TRUECOLOR_ENCODED)) {
  589. InvertImage();
  590. imageinverted = true;
  591. } else {
  592. imageinverted = false;
  593. }
  594. /* Write the image. */
  595. if (flags & TGAF_COMPRESS)
  596. EncodeImage();
  597. else
  598. {
  599. depth = TGA_BytesPerPixel(Header.PixelDepth);
  600. size = (((Header.Width * Header.Height)) * depth);
  601. if (File_Write(mImage, size) != size)
  602. error = TGAERR_WRITE;
  603. }
  604. // Bug fix (IML) : If the image was inverted, invert it again to restore it to its prior state.
  605. if (imageinverted) InvertImage();
  606. }
  607. /*-----------------------------------------------------------------------
  608. * WRITE THE EXTENSION DATA SECTION
  609. *---------------------------------------------------------------------*/
  610. // Mod (IML) Optionally add an extension to the file.
  611. if (addextension) {
  612. if (!error) {
  613. mExtension.ExtSize = 495;
  614. strncpy(mExtension.SoftID, "Denzil's Targa Code", 41);
  615. mExtension.SoftVer.Number = (1 * 100);
  616. mExtension.SoftVer.Letter = 0;
  617. /* Save position of extension area. */
  618. if ((footer.Extension = File_Seek(0, SEEK_CUR)) == -1)
  619. error = TGAERR_WRITE;
  620. if (!error && (File_Write(&mExtension, sizeof(TGA2Extension))
  621. != sizeof(TGA2Extension)))
  622. error = TGAERR_WRITE;
  623. }
  624. } else {
  625. footer.Extension = 0;
  626. }
  627. /*-----------------------------------------------------------------------
  628. * WRITE THE FOOTER DATA SECTION
  629. *---------------------------------------------------------------------*/
  630. if (!error)
  631. {
  632. footer.Developer = 0;
  633. strncpy(footer.Signature, TGA2_SIGNATURE, 16);
  634. footer.RsvdChar = '.';
  635. footer.BZST = 0;
  636. if (File_Write(&footer, sizeof(TGA2Footer)) != sizeof(TGA2Footer))
  637. error = TGAERR_WRITE;
  638. }
  639. /* Close targa file. */
  640. Close();
  641. }
  642. else
  643. error = TGAERR_OPEN;
  644. return (error);
  645. }
  646. /****************************************************************************
  647. *
  648. * NAME
  649. * Targa::XFlip - X flip the image.
  650. *
  651. * SYNOPSIS
  652. * XFlip()
  653. *
  654. * void XFlip();
  655. *
  656. * FUNCTION
  657. * Flip the image in memory on its X axis. (left to right)
  658. *
  659. * INPUTS
  660. * NONE
  661. *
  662. * RESULT
  663. * NONE
  664. *
  665. ****************************************************************************/
  666. void Targa::XFlip(void)
  667. {
  668. char *ptr,*ptr1;
  669. long x,y,d;
  670. char v,v1;
  671. char depth;
  672. /* Pixel depth in bytes. */
  673. depth = TGA_BytesPerPixel(Header.PixelDepth);
  674. for (y = 0; y < Header.Height; y++)
  675. {
  676. ptr = (mImage + ((Header.Width * depth) * y));
  677. ptr1 = (ptr + ((Header.Width * depth) - depth));
  678. for (x = 0; x < (Header.Width / 2); x++)
  679. {
  680. for (d = 0; d < depth; d++)
  681. {
  682. v = *(ptr + d);
  683. v1 = *(ptr1 + d);
  684. *(ptr + d) = v1;
  685. *(ptr1 + d) = v;
  686. }
  687. ptr += depth;
  688. ptr1 -= depth;
  689. }
  690. }
  691. }
  692. /****************************************************************************
  693. *
  694. * NAME
  695. * Targa::YFlip - Y flip the image.
  696. *
  697. * SYNOPSIS
  698. * YFlip()
  699. *
  700. * void YFlip();
  701. *
  702. * FUNCTION
  703. * Flip the image in memory on its Y axis. (top to bottom)
  704. *
  705. * INPUTS
  706. * NONE
  707. *
  708. * RESULT
  709. * NONE
  710. *
  711. ****************************************************************************/
  712. void Targa::YFlip(void)
  713. {
  714. char *ptr,*ptr1;
  715. long x,y;
  716. char v,v1;
  717. char depth;
  718. /* Pixel depth in bytes. */
  719. depth = TGA_BytesPerPixel(Header.PixelDepth);
  720. for (y = 0; y < (Header.Height >> 1); y++)
  721. {
  722. /* Compute address of lines to exchange. */
  723. ptr = (mImage + ((Header.Width * y) * depth));
  724. ptr1 = (mImage + ((Header.Width * (Header.Height - 1)) * depth));
  725. ptr1 -= ((Header.Width * y) * depth);
  726. /* Exchange all the pixels on this scan line. */
  727. for (x = 0; x < (Header.Width * depth); x++)
  728. {
  729. v = *ptr;
  730. v1 = *ptr1;
  731. *ptr = v1;
  732. *ptr1 = v;
  733. ptr++;
  734. ptr1++;
  735. }
  736. }
  737. }
  738. /****************************************************************************
  739. *
  740. * NAME
  741. * Targa::SetImage - Set the image buffer.
  742. *
  743. * SYNOPSIS
  744. * OldImage = SetImage(Image)
  745. *
  746. * char *SetImage(char *);
  747. *
  748. * FUNCTION
  749. * Set the image buffer to one provided by the caller.
  750. *
  751. * INPUTS
  752. * Image - Pointer to buffer to use for the image buffer.
  753. *
  754. * RESULT
  755. * OldImage - Previous caller assigned image buffer.
  756. *
  757. ****************************************************************************/
  758. char *Targa::SetImage(char *buffer)
  759. {
  760. char *oldbuffer = NULL;
  761. /* Free any image buffer before assigning another. */
  762. if ((mImage != NULL) && (mFlags & TGAF_IMAGE))
  763. {
  764. free(mImage);
  765. mImage = NULL;
  766. mFlags &= ~TGAF_IMAGE;
  767. }
  768. /* Get the old user buffer. */
  769. if (mImage != NULL)
  770. oldbuffer = mImage;
  771. /* Assign the new image buffer. */
  772. mImage = buffer;
  773. return (oldbuffer);
  774. }
  775. /****************************************************************************
  776. *
  777. * NAME
  778. * Targa::SetPalette - Set the palette buffer.
  779. *
  780. * SYNOPSIS
  781. * OldPal = SetPalette(Pal)
  782. *
  783. * char *SetPalette(char *);
  784. *
  785. * FUNCTION
  786. *
  787. * INPUTS
  788. * Pal - Pointer to buffer to use for palette.
  789. *
  790. * RESULT
  791. * OldPal - Pointer to previous user palette.
  792. *
  793. ****************************************************************************/
  794. char *Targa::SetPalette(char *buffer)
  795. {
  796. char *oldbuffer = NULL;
  797. /* Free any image buffer before assigning another. */
  798. if ((mPalette != NULL) && (mFlags & TGAF_PAL))
  799. {
  800. free(mPalette);
  801. mPalette = NULL;
  802. mFlags &= ~TGAF_PAL;
  803. }
  804. /* Get the old user buffer. */
  805. if (mPalette != NULL)
  806. oldbuffer = mPalette;
  807. /* Assign the new image buffer. */
  808. mPalette = buffer;
  809. return (oldbuffer);
  810. }
  811. bool Targa::IsCompressed(void)
  812. {
  813. if (Header.ImageType > 8)
  814. return true;
  815. return false;
  816. }
  817. /****************************************************************************
  818. *
  819. * NAME
  820. * Targa::GetExtension - Get Extension data. (Targa 2.0 files only)
  821. *
  822. * SYNOPSIS
  823. * Ext = GetExtension()
  824. *
  825. * TGA2Extension *GetExtension(void);
  826. *
  827. * FUNCTION
  828. * Retrieve a pointer to the Targa 2.0 extension data area. If the file
  829. * version is 1.0 OR there is no extensio area then a NULL will be returned.
  830. *
  831. * INPUTS
  832. * NONE
  833. *
  834. * RESULT
  835. * Ext - Pointer to Extension data, NULL if not available.
  836. *
  837. ****************************************************************************/
  838. TGA2Extension *Targa::GetExtension(void)
  839. {
  840. if (mFlags & TGAF_TGA2)
  841. return (&mExtension);
  842. return (NULL);
  843. }
  844. /****************************************************************************
  845. *
  846. * NAME
  847. * Targa::DecodeImage - Decompress Targa image data.
  848. *
  849. * SYNOPSIS
  850. * Error = DecodeImage()
  851. *
  852. * long DecodeImage();
  853. *
  854. * FUNCTION
  855. * Decode the RLE compressed image data into the specified buffer from
  856. * the file I/O stream.
  857. *
  858. * INPUTS
  859. * NONE
  860. *
  861. * RESULT
  862. * Error - 0 if successful, or TGAERR_??? error code.
  863. *
  864. ****************************************************************************/
  865. long Targa::DecodeImage()
  866. {
  867. char *image;
  868. char *color;
  869. unsigned char count;
  870. unsigned char depth;
  871. unsigned long pixel_count;
  872. unsigned long size;
  873. unsigned long c,i;
  874. long error = 0;
  875. /* Initialize */
  876. image = mImage;
  877. /* Compute pixel depth in bytes. */
  878. depth = TGA_BytesPerPixel(Header.PixelDepth);
  879. /* Total number of pixels compressed in this image. */
  880. pixel_count = (Header.Width * Header.Height);
  881. while ((pixel_count > 0) && !error)
  882. {
  883. /* Read count. */
  884. if (File_Read(&count, 1) == 1)
  885. {
  886. /* If bit 8 of the count is set then we have a run of pixels,
  887. * otherwise the data is raw pixels.
  888. */
  889. if (count & 0x80)
  890. {
  891. count &= 0x7F;
  892. count++;
  893. /* Read in run pixel. */
  894. if (File_Read(image, depth) == depth)
  895. {
  896. color = image;
  897. image += depth;
  898. /* Repeat the pixel for the run count in the image buffer. */
  899. for (c = 1; c < count; c++)
  900. for (i = 0; i < depth; i++)
  901. *image++ = *(color + i);
  902. }
  903. else
  904. error = TGAERR_READ;
  905. }
  906. else
  907. {
  908. count++;
  909. size = (count * depth);
  910. /* Read in raw pixels. */
  911. if ((unsigned)File_Read(image, size) == size)
  912. image += size;
  913. else
  914. error = TGAERR_READ;
  915. }
  916. /* Adjust the pixel count. */
  917. pixel_count -= count;
  918. }
  919. else
  920. error = TGAERR_READ;
  921. }
  922. return (error);
  923. }
  924. /****************************************************************************
  925. *
  926. * NAME
  927. * Targa::EncodeImage - Compress the image using targa RLE.
  928. *
  929. * SYNOPSIS
  930. * EncodeImage()
  931. *
  932. * void EncodeImage(void);
  933. *
  934. * FUNCTION
  935. * Encode the image data using the RLE algorithm outlined in the TARGA
  936. * file specification.
  937. *
  938. * INPUTS
  939. * NONE
  940. *
  941. * RESULT
  942. * NONE
  943. *
  944. ****************************************************************************/
  945. long Targa::EncodeImage()
  946. {
  947. char *packet;
  948. long packet_index;
  949. char *start;
  950. char *end;
  951. long depth;
  952. long pixels;
  953. long count;
  954. long match;
  955. long i;
  956. long error = 0;
  957. /* Initialize variables. */
  958. depth = TGA_BytesPerPixel(Header.PixelDepth);
  959. /* Allocate packet buffer to hold maximum encoded data run. */
  960. if ((packet = (char *)malloc(128 * depth)) != NULL)
  961. {
  962. pixels = Header.Width * Header.Height;
  963. start = mImage;
  964. end = start;
  965. count = 0;
  966. packet[0] = 0;
  967. packet_index = 1;
  968. while ((pixels != 0) && !error)
  969. {
  970. match = 1;
  971. /* Advance to the next pixel */
  972. end += depth;
  973. pixels--;
  974. /* Compare pixels. */
  975. for (i = 0; i < depth; i++)
  976. {
  977. if (start[i] != end[i])
  978. {
  979. match = 0;
  980. break;
  981. }
  982. }
  983. /* Run of pixels */
  984. if (match == 1)
  985. {
  986. count++;
  987. /* Continue counting until the maximum has been reached. */
  988. if (count < 128)
  989. {
  990. if (packet[0] == 0)
  991. continue;
  992. }
  993. else
  994. count--;
  995. }
  996. /* If there is a count then write out the run. Otherwise, write
  997. * the raw pixel to the packet.
  998. */
  999. if ((count != 0) && (packet[0] == 0))
  1000. {
  1001. /* Run count */
  1002. packet[0] = (count | 0x80);
  1003. /* Run pixel */
  1004. for (i = 0; i < depth; i++)
  1005. packet[i + 1] = start[i];
  1006. /* Write the run packet. */
  1007. if (File_Write(packet, (depth + 1)) != (depth + 1))
  1008. error = TGAERR_WRITE;
  1009. /* Reposition start and reset. */
  1010. start = end;
  1011. count = 0;
  1012. packet[0] = 0;
  1013. }
  1014. else
  1015. {
  1016. if (count == 0)
  1017. {
  1018. /* Copy the raw pixel to the packet. */
  1019. for (i = 0; i < depth; i++)
  1020. packet[packet_index + i] = start[i];
  1021. /* Increment the raw packet count. */
  1022. packet[0]++;
  1023. /* Reposition start */
  1024. start = end;
  1025. packet_index += depth;
  1026. }
  1027. /* Write the raw packet if the packet is full or a run has started
  1028. * or all the pixels have been processed.
  1029. */
  1030. if ((packet[0] == 127) || (count != 0) || (pixels == 0))
  1031. {
  1032. i = packet[0];
  1033. packet[0]--;
  1034. if (File_Write(packet, ((i * depth) + 1)) != ((i * depth) + 1))
  1035. error = TGAERR_WRITE;
  1036. packet_index = 1;
  1037. packet[0] = 0;
  1038. }
  1039. }
  1040. }
  1041. /* Free the packet buffer. */
  1042. free(packet);
  1043. }
  1044. else
  1045. error = TGAERR_NOMEM;
  1046. return (error);
  1047. }
  1048. /****************************************************************************
  1049. *
  1050. * NAME
  1051. * Targa::InvertImage - Invert TrueColor image data.
  1052. *
  1053. * SYNOPSIS
  1054. * InvertImage()
  1055. *
  1056. * void InvertImage(void);
  1057. *
  1058. * FUNCTION
  1059. *
  1060. * INPUTS
  1061. * NONE
  1062. *
  1063. * RESULT
  1064. * NONE
  1065. *
  1066. ****************************************************************************/
  1067. void Targa::InvertImage(void)
  1068. {
  1069. char *buffer;
  1070. long depth;
  1071. long pixel_count;
  1072. long i;
  1073. char c;
  1074. /* Initialize */
  1075. buffer = mImage;
  1076. /* Compute the pixel depth in bytes. */
  1077. depth = TGA_BytesPerPixel(Header.PixelDepth);
  1078. /* Total number of pixels in this image. */
  1079. pixel_count = (Header.Width * Header.Height);
  1080. /* 16-bit pixel layout is different that 24-bit and 32-bit. */
  1081. if (depth > 2)
  1082. {
  1083. while (pixel_count > 0)
  1084. {
  1085. for (i = 0; i < (depth / 2); i++)
  1086. {
  1087. c = *(buffer + i);
  1088. *(buffer + i) = *(buffer + ((depth - 1) - i));
  1089. *(buffer + ((depth - 1) - i)) = c;
  1090. }
  1091. /* Next pixel */
  1092. pixel_count--;
  1093. buffer += depth;
  1094. }
  1095. }
  1096. }
  1097. /*
  1098. ** These functions are just for ease of ifdef'ing between standard io calls and FileClass.
  1099. */
  1100. void Targa::Clear_File(void)
  1101. {
  1102. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1103. TGAFile = NULL;
  1104. #else
  1105. mFH = -1;
  1106. #endif
  1107. }
  1108. bool Targa::Is_File_Open(void)
  1109. {
  1110. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1111. return (TGAFile != NULL);
  1112. #else
  1113. return (mFH != -1);
  1114. #endif
  1115. }
  1116. bool Targa::File_Open_Read(const char* name)
  1117. {
  1118. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1119. TGAFile = _TheFileFactory->Get_File(name);
  1120. if (TGAFile && TGAFile->Is_Available()) {
  1121. return (TGAFile->Open(FileClass::READ) != 0);
  1122. } else {
  1123. return false;
  1124. }
  1125. #else
  1126. mFH = open(name, (O_RDONLY|O_BINARY));
  1127. return (mFH != -1);
  1128. #endif
  1129. }
  1130. bool Targa::File_Open_Write(const char* name)
  1131. {
  1132. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1133. TGAFile = _TheWritingFileFactory->Get_File(name);
  1134. if (TGAFile) {
  1135. return (TGAFile->Open(FileClass::WRITE) != 0);
  1136. } else {
  1137. return false;
  1138. }
  1139. #else
  1140. mFH = open(name, (O_CREAT|O_TRUNC|O_WRONLY|O_BINARY), (S_IREAD|S_IWRITE));
  1141. return (mFH != -1);
  1142. #endif
  1143. }
  1144. bool Targa::File_Open_ReadWrite(const char* name)
  1145. {
  1146. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1147. TGAFile = _TheWritingFileFactory->Get_File(name);
  1148. if (TGAFile && TGAFile->Is_Available()) {
  1149. return (TGAFile->Open(FileClass::READ|FileClass::WRITE) != 0);
  1150. } else {
  1151. return false;
  1152. }
  1153. #else
  1154. mFH = open(name, (O_RDWR|O_BINARY), (S_IREAD|S_IWRITE));
  1155. return (mFH != -1);
  1156. #endif
  1157. }
  1158. int Targa::File_Seek(int pos, int dir)
  1159. {
  1160. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1161. return TGAFile->Seek(pos, dir);
  1162. #else
  1163. return lseek(mFH, pos, dir);
  1164. #endif
  1165. }
  1166. int Targa::File_Read(void *buffer, int size)
  1167. {
  1168. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1169. return TGAFile->Read(buffer, size);
  1170. #else
  1171. return read(mFH, buffer, size);
  1172. #endif
  1173. }
  1174. int Targa::File_Write(void *buffer, int size)
  1175. {
  1176. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1177. return TGAFile->Write(buffer, size);
  1178. #else
  1179. return write(mFH, buffer, size);
  1180. #endif
  1181. }
  1182. // ----------------------------------------------------------------------------
  1183. //
  1184. // Output targa load error message.
  1185. //
  1186. // ----------------------------------------------------------------------------
  1187. long Targa_Error_Handler(long load_err,const char* filename)
  1188. {
  1189. switch (load_err) {
  1190. case 0:
  1191. return 0;
  1192. case TGAERR_OPEN:
  1193. WWDEBUG_SAY(("Targa: Failed to open file \"%s\"\n", filename));
  1194. break;
  1195. case TGAERR_READ:
  1196. WWDEBUG_SAY(("Targa: Failed to read file \"%s\"\n", filename));
  1197. break;
  1198. case TGAERR_NOTSUPPORTED:
  1199. WWDEBUG_SAY(("Targa: File \"%s\" is an unsupported Targa type\n", filename));
  1200. break;
  1201. case TGAERR_NOMEM:
  1202. WWDEBUG_SAY(("Targa: Failed to allocate memory for file \"%s\"\n", filename));
  1203. break;
  1204. default:
  1205. WWDEBUG_SAY(("Targa: Unknown error when loading file \"%s\"\n", filename));
  1206. break;
  1207. }
  1208. return load_err;
  1209. }