TARGA.CPP 30 KB

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