TARGA.CPP 31 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448
  1. /*
  2. ** Command & Conquer Generals Zero Hour(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. memset(&footer, 0, sizeof(footer));
  164. /* File already open? */
  165. if (Is_File_Open() && (mAccess == mode)) {
  166. return (0);
  167. }
  168. Close();
  169. /* Initialize the access mode. */
  170. mAccess = mode;
  171. mFlags &= ~TGAF_TGA2;
  172. switch (mode) {
  173. /* Open targa file for read. */
  174. case TGA_READMODE:
  175. if (File_Open_Read(name)) {
  176. /* Check for 2.0 targa file by loading the footer */
  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. static __forceinline void _swapBytes(char *p1, char *p2, unsigned count)
  713. {
  714. _asm
  715. {
  716. mov esi,[p1]
  717. mov edi,[p2]
  718. mov ebx,[count]
  719. mov ecx,ebx
  720. shr ecx,4
  721. jz lessThan16
  722. lp:
  723. mov eax,dword ptr [esi]
  724. mov edx,dword ptr [esi+4]
  725. xchg eax,dword ptr [edi]
  726. xchg edx,dword ptr [edi+4]
  727. mov dword ptr [esi],eax
  728. mov dword ptr [esi+4],edx
  729. mov eax,dword ptr [esi+8]
  730. mov edx,dword ptr [esi+12]
  731. xchg eax,dword ptr [edi+8]
  732. xchg edx,dword ptr [edi+12]
  733. mov dword ptr [esi+8],eax
  734. mov dword ptr [esi+12],edx
  735. add esi,16
  736. add edi,16
  737. dec ecx
  738. jnz lp
  739. lessThan16:
  740. and ebx,15
  741. lpLessThan16:
  742. jz done
  743. mov al,byte ptr [esi]
  744. xchg al,byte ptr [edi]
  745. mov byte ptr [edi],al
  746. inc esi
  747. inc edi
  748. dec ebx
  749. jmp lpLessThan16
  750. done:
  751. }
  752. }
  753. void Targa::YFlip(void)
  754. {
  755. /* old code left in for reference...
  756. char *ptr,*ptr1;
  757. long x,y;
  758. char v,v1;
  759. char depth;
  760. / * Pixel depth in bytes. * /
  761. depth = TGA_BytesPerPixel(Header.PixelDepth);
  762. for (y = 0; y < (Header.Height >> 1); y++)
  763. {
  764. / * Compute address of lines to exchange. * /
  765. ptr = (mImage + ((Header.Width * y) * depth));
  766. ptr1 = (mImage + ((Header.Width * (Header.Height - 1)) * depth));
  767. ptr1 -= ((Header.Width * y) * depth);
  768. / * Exchange all the pixels on this scan line. * /
  769. for (x = 0; x < (Header.Width * depth); x++)
  770. {
  771. v = *ptr;
  772. v1 = *ptr1;
  773. *ptr = v1;
  774. *ptr1 = v;
  775. ptr++;
  776. ptr1++;
  777. }
  778. }
  779. */
  780. unsigned stride=Header.Width*TGA_BytesPerPixel(Header.PixelDepth);
  781. char *ptrTop=mImage,
  782. *ptrBottom=mImage+stride*(Header.Height-1);
  783. for (unsigned y=Header.Height/2;y;--y,ptrTop+=stride,ptrBottom-=stride)
  784. _swapBytes(ptrTop,ptrBottom,stride);
  785. }
  786. /****************************************************************************
  787. *
  788. * NAME
  789. * Targa::SetImage - Set the image buffer.
  790. *
  791. * SYNOPSIS
  792. * OldImage = SetImage(Image)
  793. *
  794. * char *SetImage(char *);
  795. *
  796. * FUNCTION
  797. * Set the image buffer to one provided by the caller.
  798. *
  799. * INPUTS
  800. * Image - Pointer to buffer to use for the image buffer.
  801. *
  802. * RESULT
  803. * OldImage - Previous caller assigned image buffer.
  804. *
  805. ****************************************************************************/
  806. char *Targa::SetImage(char *buffer)
  807. {
  808. char *oldbuffer = NULL;
  809. /* Free any image buffer before assigning another. */
  810. if ((mImage != NULL) && (mFlags & TGAF_IMAGE))
  811. {
  812. free(mImage);
  813. mImage = NULL;
  814. mFlags &= ~TGAF_IMAGE;
  815. }
  816. /* Get the old user buffer. */
  817. if (mImage != NULL)
  818. oldbuffer = mImage;
  819. /* Assign the new image buffer. */
  820. mImage = buffer;
  821. return (oldbuffer);
  822. }
  823. /****************************************************************************
  824. *
  825. * NAME
  826. * Targa::SetPalette - Set the palette buffer.
  827. *
  828. * SYNOPSIS
  829. * OldPal = SetPalette(Pal)
  830. *
  831. * char *SetPalette(char *);
  832. *
  833. * FUNCTION
  834. *
  835. * INPUTS
  836. * Pal - Pointer to buffer to use for palette.
  837. *
  838. * RESULT
  839. * OldPal - Pointer to previous user palette.
  840. *
  841. ****************************************************************************/
  842. char *Targa::SetPalette(char *buffer)
  843. {
  844. char *oldbuffer = NULL;
  845. /* Free any image buffer before assigning another. */
  846. if ((mPalette != NULL) && (mFlags & TGAF_PAL))
  847. {
  848. free(mPalette);
  849. mPalette = NULL;
  850. mFlags &= ~TGAF_PAL;
  851. }
  852. /* Get the old user buffer. */
  853. if (mPalette != NULL)
  854. oldbuffer = mPalette;
  855. /* Assign the new image buffer. */
  856. mPalette = buffer;
  857. return (oldbuffer);
  858. }
  859. bool Targa::IsCompressed(void)
  860. {
  861. if (Header.ImageType > 8)
  862. return true;
  863. return false;
  864. }
  865. /****************************************************************************
  866. *
  867. * NAME
  868. * Targa::GetExtension - Get Extension data. (Targa 2.0 files only)
  869. *
  870. * SYNOPSIS
  871. * Ext = GetExtension()
  872. *
  873. * TGA2Extension *GetExtension(void);
  874. *
  875. * FUNCTION
  876. * Retrieve a pointer to the Targa 2.0 extension data area. If the file
  877. * version is 1.0 OR there is no extensio area then a NULL will be returned.
  878. *
  879. * INPUTS
  880. * NONE
  881. *
  882. * RESULT
  883. * Ext - Pointer to Extension data, NULL if not available.
  884. *
  885. ****************************************************************************/
  886. TGA2Extension *Targa::GetExtension(void)
  887. {
  888. if (mFlags & TGAF_TGA2)
  889. return (&mExtension);
  890. return (NULL);
  891. }
  892. /****************************************************************************
  893. *
  894. * NAME
  895. * Targa::DecodeImage - Decompress Targa image data.
  896. *
  897. * SYNOPSIS
  898. * Error = DecodeImage()
  899. *
  900. * long DecodeImage();
  901. *
  902. * FUNCTION
  903. * Decode the RLE compressed image data into the specified buffer from
  904. * the file I/O stream.
  905. *
  906. * INPUTS
  907. * NONE
  908. *
  909. * RESULT
  910. * Error - 0 if successful, or TGAERR_??? error code.
  911. *
  912. ****************************************************************************/
  913. long Targa::DecodeImage()
  914. {
  915. char *image;
  916. char *color;
  917. unsigned char count;
  918. unsigned char depth;
  919. unsigned long pixel_count;
  920. unsigned long size;
  921. unsigned long c,i;
  922. long error = 0;
  923. /* Initialize */
  924. image = mImage;
  925. /* Compute pixel depth in bytes. */
  926. depth = TGA_BytesPerPixel(Header.PixelDepth);
  927. /* Total number of pixels compressed in this image. */
  928. pixel_count = (Header.Width * Header.Height);
  929. while ((pixel_count > 0) && !error)
  930. {
  931. /* Read count. */
  932. if (File_Read(&count, 1) == 1)
  933. {
  934. /* If bit 8 of the count is set then we have a run of pixels,
  935. * otherwise the data is raw pixels.
  936. */
  937. if (count & 0x80)
  938. {
  939. count &= 0x7F;
  940. count++;
  941. /* Read in run pixel. */
  942. if (File_Read(image, depth) == depth)
  943. {
  944. color = image;
  945. image += depth;
  946. /* Repeat the pixel for the run count in the image buffer. */
  947. for (c = 1; c < count; c++)
  948. for (i = 0; i < depth; i++)
  949. *image++ = *(color + i);
  950. }
  951. else
  952. error = TGAERR_READ;
  953. }
  954. else
  955. {
  956. count++;
  957. size = (count * depth);
  958. /* Read in raw pixels. */
  959. if ((unsigned)File_Read(image, size) == size)
  960. image += size;
  961. else
  962. error = TGAERR_READ;
  963. }
  964. /* Adjust the pixel count. */
  965. pixel_count -= count;
  966. }
  967. else
  968. error = TGAERR_READ;
  969. }
  970. return (error);
  971. }
  972. /****************************************************************************
  973. *
  974. * NAME
  975. * Targa::EncodeImage - Compress the image using targa RLE.
  976. *
  977. * SYNOPSIS
  978. * EncodeImage()
  979. *
  980. * void EncodeImage(void);
  981. *
  982. * FUNCTION
  983. * Encode the image data using the RLE algorithm outlined in the TARGA
  984. * file specification.
  985. *
  986. * INPUTS
  987. * NONE
  988. *
  989. * RESULT
  990. * NONE
  991. *
  992. ****************************************************************************/
  993. long Targa::EncodeImage()
  994. {
  995. char *packet;
  996. long packet_index;
  997. char *start;
  998. char *end;
  999. long depth;
  1000. long pixels;
  1001. long count;
  1002. long match;
  1003. long i;
  1004. long error = 0;
  1005. /* Initialize variables. */
  1006. depth = TGA_BytesPerPixel(Header.PixelDepth);
  1007. /* Allocate packet buffer to hold maximum encoded data run. */
  1008. if ((packet = (char *)malloc(128 * depth)) != NULL)
  1009. {
  1010. pixels = Header.Width * Header.Height;
  1011. start = mImage;
  1012. end = start;
  1013. count = 0;
  1014. packet[0] = 0;
  1015. packet_index = 1;
  1016. while ((pixels != 0) && !error)
  1017. {
  1018. match = 1;
  1019. /* Advance to the next pixel */
  1020. end += depth;
  1021. pixels--;
  1022. /* Compare pixels. */
  1023. for (i = 0; i < depth; i++)
  1024. {
  1025. if (start[i] != end[i])
  1026. {
  1027. match = 0;
  1028. break;
  1029. }
  1030. }
  1031. /* Run of pixels */
  1032. if (match == 1)
  1033. {
  1034. count++;
  1035. /* Continue counting until the maximum has been reached. */
  1036. if (count < 128)
  1037. {
  1038. if (packet[0] == 0)
  1039. continue;
  1040. }
  1041. else
  1042. count--;
  1043. }
  1044. /* If there is a count then write out the run. Otherwise, write
  1045. * the raw pixel to the packet.
  1046. */
  1047. if ((count != 0) && (packet[0] == 0))
  1048. {
  1049. /* Run count */
  1050. packet[0] = (count | 0x80);
  1051. /* Run pixel */
  1052. for (i = 0; i < depth; i++)
  1053. packet[i + 1] = start[i];
  1054. /* Write the run packet. */
  1055. if (File_Write(packet, (depth + 1)) != (depth + 1))
  1056. error = TGAERR_WRITE;
  1057. /* Reposition start and reset. */
  1058. start = end;
  1059. count = 0;
  1060. packet[0] = 0;
  1061. }
  1062. else
  1063. {
  1064. if (count == 0)
  1065. {
  1066. /* Copy the raw pixel to the packet. */
  1067. for (i = 0; i < depth; i++)
  1068. packet[packet_index + i] = start[i];
  1069. /* Increment the raw packet count. */
  1070. packet[0]++;
  1071. /* Reposition start */
  1072. start = end;
  1073. packet_index += depth;
  1074. }
  1075. /* Write the raw packet if the packet is full or a run has started
  1076. * or all the pixels have been processed.
  1077. */
  1078. if ((packet[0] == 127) || (count != 0) || (pixels == 0))
  1079. {
  1080. i = packet[0];
  1081. packet[0]--;
  1082. if (File_Write(packet, ((i * depth) + 1)) != ((i * depth) + 1))
  1083. error = TGAERR_WRITE;
  1084. packet_index = 1;
  1085. packet[0] = 0;
  1086. }
  1087. }
  1088. }
  1089. /* Free the packet buffer. */
  1090. free(packet);
  1091. }
  1092. else
  1093. error = TGAERR_NOMEM;
  1094. return (error);
  1095. }
  1096. /****************************************************************************
  1097. *
  1098. * NAME
  1099. * Targa::InvertImage - Invert TrueColor image data.
  1100. *
  1101. * SYNOPSIS
  1102. * InvertImage()
  1103. *
  1104. * void InvertImage(void);
  1105. *
  1106. * FUNCTION
  1107. *
  1108. * INPUTS
  1109. * NONE
  1110. *
  1111. * RESULT
  1112. * NONE
  1113. *
  1114. ****************************************************************************/
  1115. void Targa::InvertImage(void)
  1116. {
  1117. char *buffer;
  1118. long depth;
  1119. long pixel_count;
  1120. long i;
  1121. char c;
  1122. /* Initialize */
  1123. buffer = mImage;
  1124. /* Compute the pixel depth in bytes. */
  1125. depth = TGA_BytesPerPixel(Header.PixelDepth);
  1126. /* Total number of pixels in this image. */
  1127. pixel_count = (Header.Width * Header.Height);
  1128. /* 16-bit pixel layout is different that 24-bit and 32-bit. */
  1129. if (depth > 2)
  1130. {
  1131. while (pixel_count > 0)
  1132. {
  1133. for (i = 0; i < (depth / 2); i++)
  1134. {
  1135. c = *(buffer + i);
  1136. *(buffer + i) = *(buffer + ((depth - 1) - i));
  1137. *(buffer + ((depth - 1) - i)) = c;
  1138. }
  1139. /* Next pixel */
  1140. pixel_count--;
  1141. buffer += depth;
  1142. }
  1143. }
  1144. }
  1145. /*
  1146. ** These functions are just for ease of ifdef'ing between standard io calls and FileClass.
  1147. */
  1148. void Targa::Clear_File(void)
  1149. {
  1150. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1151. TGAFile = NULL;
  1152. #else
  1153. mFH = -1;
  1154. #endif
  1155. }
  1156. bool Targa::Is_File_Open(void)
  1157. {
  1158. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1159. return (TGAFile != NULL);
  1160. #else
  1161. return (mFH != -1);
  1162. #endif
  1163. }
  1164. bool Targa::File_Open_Read(const char* name)
  1165. {
  1166. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1167. TGAFile = _TheFileFactory->Get_File(name);
  1168. if (TGAFile && TGAFile->Is_Available()) {
  1169. return (TGAFile->Open(FileClass::READ) != 0);
  1170. } else {
  1171. return false;
  1172. }
  1173. #else
  1174. mFH = open(name, (O_RDONLY|O_BINARY));
  1175. return (mFH != -1);
  1176. #endif
  1177. }
  1178. bool Targa::File_Open_Write(const char* name)
  1179. {
  1180. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1181. TGAFile = _TheWritingFileFactory->Get_File(name);
  1182. if (TGAFile) {
  1183. return (TGAFile->Open(FileClass::WRITE) != 0);
  1184. } else {
  1185. return false;
  1186. }
  1187. #else
  1188. mFH = open(name, (O_CREAT|O_TRUNC|O_WRONLY|O_BINARY), (S_IREAD|S_IWRITE));
  1189. return (mFH != -1);
  1190. #endif
  1191. }
  1192. bool Targa::File_Open_ReadWrite(const char* name)
  1193. {
  1194. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1195. TGAFile = _TheWritingFileFactory->Get_File(name);
  1196. if (TGAFile && TGAFile->Is_Available()) {
  1197. return (TGAFile->Open(FileClass::READ|FileClass::WRITE) != 0);
  1198. } else {
  1199. return false;
  1200. }
  1201. #else
  1202. mFH = open(name, (O_RDWR|O_BINARY), (S_IREAD|S_IWRITE));
  1203. return (mFH != -1);
  1204. #endif
  1205. }
  1206. int Targa::File_Seek(int pos, int dir)
  1207. {
  1208. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1209. return TGAFile->Seek(pos, dir);
  1210. #else
  1211. return lseek(mFH, pos, dir);
  1212. #endif
  1213. }
  1214. int Targa::File_Read(void *buffer, int size)
  1215. {
  1216. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1217. return TGAFile->Read(buffer, size);
  1218. #else
  1219. return read(mFH, buffer, size);
  1220. #endif
  1221. }
  1222. int Targa::File_Write(void *buffer, int size)
  1223. {
  1224. #ifdef TGA_USES_WWLIB_FILE_CLASSES
  1225. return TGAFile->Write(buffer, size);
  1226. #else
  1227. return write(mFH, buffer, size);
  1228. #endif
  1229. }
  1230. // ----------------------------------------------------------------------------
  1231. //
  1232. // Output targa load error message.
  1233. //
  1234. // ----------------------------------------------------------------------------
  1235. long Targa_Error_Handler(long load_err,const char* filename)
  1236. {
  1237. switch (load_err) {
  1238. case 0:
  1239. return 0;
  1240. case TGAERR_OPEN:
  1241. WWDEBUG_SAY(("Targa: Failed to open file \"%s\"\n", filename));
  1242. break;
  1243. case TGAERR_READ:
  1244. WWDEBUG_SAY(("Targa: Failed to read file \"%s\"\n", filename));
  1245. break;
  1246. case TGAERR_NOTSUPPORTED:
  1247. WWDEBUG_SAY(("Targa: File \"%s\" is an unsupported Targa type\n", filename));
  1248. break;
  1249. case TGAERR_NOMEM:
  1250. WWDEBUG_SAY(("Targa: Failed to allocate memory for file \"%s\"\n", filename));
  1251. break;
  1252. default:
  1253. WWDEBUG_SAY(("Targa: Unknown error when loading file \"%s\"\n", filename));
  1254. break;
  1255. }
  1256. return load_err;
  1257. }