PageRenderTime 58ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/indra/llimage/llimagetga.cpp

https://bitbucket.org/lindenlab/viewer-beta/
C++ | 1185 lines | 885 code | 150 blank | 150 comment | 113 complexity | 3a1719a238f675dca6e29c06fd5cf6fe MD5 | raw file
Possible License(s): LGPL-2.1
  1. /**
  2. * @file llimagetga.cpp
  3. *
  4. * $LicenseInfo:firstyear=2001&license=viewerlgpl$
  5. * Second Life Viewer Source Code
  6. * Copyright (C) 2010, Linden Research, Inc.
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU Lesser General Public
  10. * License as published by the Free Software Foundation;
  11. * version 2.1 of the License only.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16. * Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public
  19. * License along with this library; if not, write to the Free Software
  20. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  21. *
  22. * Linden Research, Inc., 945 Battery Street, San Francisco, CA 94111 USA
  23. * $/LicenseInfo$
  24. */
  25. #include "linden_common.h"
  26. #include "llimagetga.h"
  27. #include "lldir.h"
  28. #include "llerror.h"
  29. #include "llmath.h"
  30. #include "llpointer.h"
  31. // For expanding 5-bit pixel values to 8-bit with best rounding
  32. // static
  33. const U8 LLImageTGA::s5to8bits[32] =
  34. {
  35. 0, 8, 16, 25, 33, 41, 49, 58,
  36. 66, 74, 82, 90, 99, 107, 115, 123,
  37. 132, 140, 148, 156, 165, 173, 181, 189,
  38. 197, 206, 214, 222, 230, 239, 247, 255
  39. };
  40. inline void LLImageTGA::decodeTruecolorPixel15( U8* dst, const U8* src )
  41. {
  42. // We expand 5 bit data to 8 bit sample width.
  43. // The format of the 16-bit (LSB first) input word is
  44. // xRRRRRGGGGGBBBBB
  45. U32 t = U32(src[0]) + (U32(src[1]) << 8);
  46. dst[2] = s5to8bits[t & 0x1F]; // blue
  47. t >>= 5;
  48. dst[1] = s5to8bits[t & 0x1F]; // green
  49. t >>= 5;
  50. dst[0] = s5to8bits[t & 0x1F]; // red
  51. }
  52. LLImageTGA::LLImageTGA()
  53. : LLImageFormatted(IMG_CODEC_TGA),
  54. mColorMap( NULL ),
  55. mColorMapStart( 0 ),
  56. mColorMapLength( 0 ),
  57. mColorMapBytesPerEntry( 0 ),
  58. mIs15Bit( FALSE ),
  59. mAttributeBits(0),
  60. mColorMapDepth(0),
  61. mColorMapIndexHi(0),
  62. mColorMapIndexLo(0),
  63. mColorMapLengthHi(0),
  64. mColorMapLengthLo(0),
  65. mColorMapType(0),
  66. mDataOffset(0),
  67. mHeightHi(0),
  68. mHeightLo(0),
  69. mIDLength(0),
  70. mImageType(0),
  71. mInterleave(0),
  72. mOriginRightBit(0),
  73. mOriginTopBit(0),
  74. mPixelSize(0),
  75. mWidthHi(0),
  76. mWidthLo(0),
  77. mXOffsetHi(0),
  78. mXOffsetLo(0),
  79. mYOffsetHi(0),
  80. mYOffsetLo(0)
  81. {
  82. }
  83. LLImageTGA::LLImageTGA(const std::string& file_name)
  84. : LLImageFormatted(IMG_CODEC_TGA),
  85. mColorMap( NULL ),
  86. mColorMapStart( 0 ),
  87. mColorMapLength( 0 ),
  88. mColorMapBytesPerEntry( 0 ),
  89. mIs15Bit( FALSE )
  90. {
  91. loadFile(file_name);
  92. }
  93. LLImageTGA::~LLImageTGA()
  94. {
  95. delete [] mColorMap;
  96. }
  97. BOOL LLImageTGA::updateData()
  98. {
  99. resetLastError();
  100. // Check to make sure that this instance has been initialized with data
  101. if (!getData() || (0 == getDataSize()))
  102. {
  103. setLastError("LLImageTGA uninitialized");
  104. return FALSE;
  105. }
  106. // Pull image information from the header...
  107. U8 flags;
  108. U8 junk[256];
  109. /****************************************************************************
  110. **
  111. ** For more information about the original Truevision TGA(tm) file format,
  112. ** or for additional information about the new extensions to the
  113. ** Truevision TGA file, refer to the "Truevision TGA File Format
  114. ** Specification Version 2.0" available from Truevision or your
  115. ** Truevision dealer.
  116. **
  117. ** FILE STRUCTURE FOR THE ORIGINAL TRUEVISION TGA FILE
  118. ** FIELD 1 : NUMBER OF CHARACTERS IN ID FIELD (1 BYTES)
  119. ** FIELD 2 : COLOR MAP TYPE (1 BYTES)
  120. ** FIELD 3 : IMAGE TYPE CODE (1 BYTES)
  121. ** = 0 NO IMAGE DATA INCLUDED
  122. ** = 1 UNCOMPRESSED, COLOR-MAPPED IMAGE
  123. ** = 2 UNCOMPRESSED, TRUE-COLOR IMAGE
  124. ** = 3 UNCOMPRESSED, BLACK AND WHITE IMAGE
  125. ** = 9 RUN-LENGTH ENCODED COLOR-MAPPED IMAGE
  126. ** = 10 RUN-LENGTH ENCODED TRUE-COLOR IMAGE
  127. ** = 11 RUN-LENGTH ENCODED BLACK AND WHITE IMAGE
  128. ** FIELD 4 : COLOR MAP SPECIFICATION (5 BYTES)
  129. ** 4.1 : COLOR MAP ORIGIN (2 BYTES)
  130. ** 4.2 : COLOR MAP LENGTH (2 BYTES)
  131. ** 4.3 : COLOR MAP ENTRY SIZE (2 BYTES)
  132. ** FIELD 5 : IMAGE SPECIFICATION (10 BYTES)
  133. ** 5.1 : X-ORIGIN OF IMAGE (2 BYTES)
  134. ** 5.2 : Y-ORIGIN OF IMAGE (2 BYTES)
  135. ** 5.3 : WIDTH OF IMAGE (2 BYTES)
  136. ** 5.4 : HEIGHT OF IMAGE (2 BYTES)
  137. ** 5.5 : IMAGE PIXEL SIZE (1 BYTE)
  138. ** 5.6 : IMAGE DESCRIPTOR BYTE (1 BYTE)
  139. ** FIELD 6 : IMAGE ID FIELD (LENGTH SPECIFIED BY FIELD 1)
  140. ** FIELD 7 : COLOR MAP DATA (BIT WIDTH SPECIFIED BY FIELD 4.3 AND
  141. ** NUMBER OF COLOR MAP ENTRIES SPECIFIED IN FIELD 4.2)
  142. ** FIELD 8 : IMAGE DATA FIELD (WIDTH AND HEIGHT SPECIFIED IN
  143. ** FIELD 5.3 AND 5.4)
  144. ****************************************************************************/
  145. mDataOffset = 0;
  146. mIDLength = *(getData()+mDataOffset++);
  147. mColorMapType = *(getData()+mDataOffset++);
  148. mImageType = *(getData()+mDataOffset++);
  149. mColorMapIndexLo = *(getData()+mDataOffset++);
  150. mColorMapIndexHi = *(getData()+mDataOffset++);
  151. mColorMapLengthLo = *(getData()+mDataOffset++);
  152. mColorMapLengthHi = *(getData()+mDataOffset++);
  153. mColorMapDepth = *(getData()+mDataOffset++);
  154. mXOffsetLo = *(getData()+mDataOffset++);
  155. mXOffsetHi = *(getData()+mDataOffset++);
  156. mYOffsetLo = *(getData()+mDataOffset++);
  157. mYOffsetHi = *(getData()+mDataOffset++);
  158. mWidthLo = *(getData()+mDataOffset++);
  159. mWidthHi = *(getData()+mDataOffset++);
  160. mHeightLo = *(getData()+mDataOffset++);
  161. mHeightHi = *(getData()+mDataOffset++);
  162. mPixelSize = *(getData()+mDataOffset++);
  163. flags = *(getData()+mDataOffset++);
  164. mAttributeBits = flags & 0xf;
  165. mOriginRightBit = (flags & 0x10) >> 4;
  166. mOriginTopBit = (flags & 0x20) >> 5;
  167. mInterleave = (flags & 0xc0) >> 6;
  168. switch( mImageType )
  169. {
  170. case 0:
  171. // No image data included in file
  172. setLastError("Unable to load file. TGA file contains no image data.");
  173. return FALSE;
  174. case 1:
  175. // Colormapped uncompressed
  176. if( 8 != mPixelSize )
  177. {
  178. setLastError("Unable to load file. Colormapped images must have 8 bits per pixel.");
  179. return FALSE;
  180. }
  181. break;
  182. case 2:
  183. // Truecolor uncompressed
  184. break;
  185. case 3:
  186. // Monochrome uncompressed
  187. if( 8 != mPixelSize )
  188. {
  189. setLastError("Unable to load file. Monochrome images must have 8 bits per pixel.");
  190. return FALSE;
  191. }
  192. break;
  193. case 9:
  194. // Colormapped, RLE
  195. break;
  196. case 10:
  197. // Truecolor, RLE
  198. break;
  199. case 11:
  200. // Monochrome, RLE
  201. if( 8 != mPixelSize )
  202. {
  203. setLastError("Unable to load file. Monochrome images must have 8 bits per pixel.");
  204. return FALSE;
  205. }
  206. break;
  207. default:
  208. setLastError("Unable to load file. Unrecoginzed TGA image type.");
  209. return FALSE;
  210. }
  211. // discard the ID field, if any
  212. if (mIDLength)
  213. {
  214. memcpy(junk, getData()+mDataOffset, mIDLength); /* Flawfinder: ignore */
  215. mDataOffset += mIDLength;
  216. }
  217. // check to see if there's a colormap since even rgb files can have them
  218. S32 color_map_bytes = 0;
  219. if( (1 == mColorMapType) && (mColorMapDepth > 0) )
  220. {
  221. mColorMapStart = (S32(mColorMapIndexHi) << 8) + mColorMapIndexLo;
  222. mColorMapLength = (S32(mColorMapLengthHi) << 8) + mColorMapLengthLo;
  223. if( mColorMapDepth > 24 )
  224. {
  225. mColorMapBytesPerEntry = 4;
  226. }
  227. else
  228. if( mColorMapDepth > 16 )
  229. {
  230. mColorMapBytesPerEntry = 3;
  231. }
  232. else
  233. if( mColorMapDepth > 8 )
  234. {
  235. mColorMapBytesPerEntry = 2;
  236. }
  237. else
  238. {
  239. mColorMapBytesPerEntry = 1;
  240. }
  241. color_map_bytes = mColorMapLength * mColorMapBytesPerEntry;
  242. // Note: although it's legal for TGA files to have color maps and not use them
  243. // (some programs actually do this and use the color map for other ends), we'll
  244. // only allocate memory for one if _we_ intend to use it.
  245. if ( (1 == mImageType) || (9 == mImageType) )
  246. {
  247. mColorMap = new U8[ color_map_bytes ];
  248. if (!mColorMap)
  249. {
  250. llerrs << "Out of Memory in BOOL LLImageTGA::updateData()" << llendl;
  251. return FALSE;
  252. }
  253. memcpy( mColorMap, getData() + mDataOffset, color_map_bytes ); /* Flawfinder: ignore */
  254. }
  255. mDataOffset += color_map_bytes;
  256. }
  257. // heights are read as bytes to prevent endian problems
  258. S32 height = (S32(mHeightHi) << 8) + mHeightLo;
  259. S32 width = (S32(mWidthHi) << 8) + mWidthLo;
  260. // make sure that it's a pixel format that we understand
  261. S32 bits_per_pixel;
  262. if( mColorMap )
  263. {
  264. bits_per_pixel = mColorMapDepth;
  265. }
  266. else
  267. {
  268. bits_per_pixel = mPixelSize;
  269. }
  270. S32 components;
  271. switch(bits_per_pixel)
  272. {
  273. case 24:
  274. components = 3;
  275. break;
  276. case 32:
  277. components = 4;
  278. // Don't enforce this. ACDSee doesn't bother to set the attributes bits correctly. Arrgh!
  279. // if( mAttributeBits != 8 )
  280. // {
  281. // setLastError("Unable to load file. 32 bit TGA image does not have 8 bits of alpha.");
  282. // return FALSE;
  283. // }
  284. mAttributeBits = 8;
  285. break;
  286. case 15:
  287. case 16:
  288. components = 3;
  289. mIs15Bit = TRUE; // 16th bit is used for Targa hardware interupts and is ignored.
  290. break;
  291. case 8:
  292. components = 1;
  293. break;
  294. default:
  295. setLastError("Unable to load file. Unknown pixel size.");
  296. return FALSE;
  297. }
  298. setSize(width, height, components);
  299. return TRUE;
  300. }
  301. BOOL LLImageTGA::decode(LLImageRaw* raw_image, F32 decode_time)
  302. {
  303. llassert_always(raw_image);
  304. // Check to make sure that this instance has been initialized with data
  305. if (!getData() || (0 == getDataSize()))
  306. {
  307. setLastError("LLImageTGA trying to decode an image with no data!");
  308. return FALSE;
  309. }
  310. // Copy everything after the header.
  311. raw_image->resize(getWidth(), getHeight(), getComponents());
  312. if( (getComponents() != 1) &&
  313. (getComponents() != 3) &&
  314. (getComponents() != 4) )
  315. {
  316. setLastError("TGA images with a number of components other than 1, 3, and 4 are not supported.");
  317. return FALSE;
  318. }
  319. if( mOriginRightBit )
  320. {
  321. setLastError("TGA images with origin on right side are not supported.");
  322. return FALSE;
  323. }
  324. BOOL flipped = (mOriginTopBit != 0);
  325. BOOL rle_compressed = ((mImageType & 0x08) != 0);
  326. if( mColorMap )
  327. {
  328. return decodeColorMap( raw_image, rle_compressed, flipped );
  329. }
  330. else
  331. {
  332. return decodeTruecolor( raw_image, rle_compressed, flipped );
  333. }
  334. }
  335. BOOL LLImageTGA::decodeTruecolor( LLImageRaw* raw_image, BOOL rle, BOOL flipped )
  336. {
  337. BOOL success = FALSE;
  338. BOOL alpha_opaque = FALSE;
  339. if( rle )
  340. {
  341. switch( getComponents() )
  342. {
  343. case 1:
  344. success = decodeTruecolorRle8( raw_image );
  345. break;
  346. case 3:
  347. if( mIs15Bit )
  348. {
  349. success = decodeTruecolorRle15( raw_image );
  350. }
  351. else
  352. {
  353. success = decodeTruecolorRle24( raw_image );
  354. }
  355. break;
  356. case 4:
  357. success = decodeTruecolorRle32( raw_image, alpha_opaque );
  358. if (alpha_opaque)
  359. {
  360. // alpha was entirely opaque
  361. // convert to 24 bit image
  362. LLPointer<LLImageRaw> compacted_image = new LLImageRaw(raw_image->getWidth(), raw_image->getHeight(), 3);
  363. compacted_image->copy(raw_image);
  364. raw_image->resize(raw_image->getWidth(), raw_image->getHeight(), 3);
  365. raw_image->copy(compacted_image);
  366. }
  367. break;
  368. }
  369. }
  370. else
  371. {
  372. BOOL alpha_opaque;
  373. success = decodeTruecolorNonRle( raw_image, alpha_opaque );
  374. if (alpha_opaque && raw_image->getComponents() == 4)
  375. {
  376. // alpha was entirely opaque
  377. // convert to 24 bit image
  378. LLPointer<LLImageRaw> compacted_image = new LLImageRaw(raw_image->getWidth(), raw_image->getHeight(), 3);
  379. compacted_image->copy(raw_image);
  380. raw_image->resize(raw_image->getWidth(), raw_image->getHeight(), 3);
  381. raw_image->copy(compacted_image);
  382. }
  383. }
  384. if( success && flipped )
  385. {
  386. // This works because the Targa definition requires that RLE blocks never
  387. // encode pixels from more than one scanline.
  388. // (On the other hand, it's not as fast as writing separate flipped versions as
  389. // we did with TruecolorNonRle.)
  390. raw_image->verticalFlip();
  391. }
  392. return success;
  393. }
  394. BOOL LLImageTGA::decodeTruecolorNonRle( LLImageRaw* raw_image, BOOL &alpha_opaque )
  395. {
  396. alpha_opaque = TRUE;
  397. // Origin is the bottom left
  398. U8* dst = raw_image->getData();
  399. U8* src = getData() + mDataOffset;
  400. S32 pixels = getWidth() * getHeight();
  401. if (getComponents() == 4)
  402. {
  403. while( pixels-- )
  404. {
  405. // Our data is stored in RGBA. TGA stores them as BGRA (little-endian ARGB)
  406. dst[0] = src[2]; // Red
  407. dst[1] = src[1]; // Green
  408. dst[2] = src[0]; // Blue
  409. dst[3] = src[3]; // Alpha
  410. if (dst[3] != 255)
  411. {
  412. alpha_opaque = FALSE;
  413. }
  414. dst += 4;
  415. src += 4;
  416. }
  417. }
  418. else if (getComponents() == 3)
  419. {
  420. if( mIs15Bit )
  421. {
  422. while( pixels-- )
  423. {
  424. decodeTruecolorPixel15( dst, src );
  425. dst += 3;
  426. src += 2;
  427. }
  428. }
  429. else
  430. {
  431. while( pixels-- )
  432. {
  433. dst[0] = src[2]; // Red
  434. dst[1] = src[1]; // Green
  435. dst[2] = src[0]; // Blue
  436. dst += 3;
  437. src += 3;
  438. }
  439. }
  440. }
  441. else if (getComponents() == 1)
  442. {
  443. memcpy(dst, src, pixels); /* Flawfinder: ignore */
  444. }
  445. return TRUE;
  446. }
  447. void LLImageTGA::decodeColorMapPixel8( U8* dst, const U8* src )
  448. {
  449. S32 index = llclamp( *src - mColorMapStart, 0, mColorMapLength - 1 );
  450. dst[0] = mColorMap[ index ];
  451. }
  452. void LLImageTGA::decodeColorMapPixel15( U8* dst, const U8* src )
  453. {
  454. S32 index = llclamp( *src - mColorMapStart, 0, mColorMapLength - 1 );
  455. decodeTruecolorPixel15( dst, mColorMap + 2 * index );
  456. }
  457. void LLImageTGA::decodeColorMapPixel24( U8* dst, const U8* src )
  458. {
  459. S32 index = 3 * llclamp( *src - mColorMapStart, 0, mColorMapLength - 1 );
  460. dst[0] = mColorMap[ index + 2 ]; // Red
  461. dst[1] = mColorMap[ index + 1 ]; // Green
  462. dst[2] = mColorMap[ index + 0 ]; // Blue
  463. }
  464. void LLImageTGA::decodeColorMapPixel32( U8* dst, const U8* src )
  465. {
  466. S32 index = 4 * llclamp( *src - mColorMapStart, 0, mColorMapLength - 1 );
  467. dst[0] = mColorMap[ index + 2 ]; // Red
  468. dst[1] = mColorMap[ index + 1 ]; // Green
  469. dst[2] = mColorMap[ index + 0 ]; // Blue
  470. dst[3] = mColorMap[ index + 3 ]; // Alpha
  471. }
  472. BOOL LLImageTGA::decodeColorMap( LLImageRaw* raw_image, BOOL rle, BOOL flipped )
  473. {
  474. // If flipped, origin is the top left. Need to reverse the order of the rows.
  475. // Otherwise the origin is the bottom left.
  476. if( 8 != mPixelSize )
  477. {
  478. return FALSE;
  479. }
  480. U8* src = getData() + mDataOffset;
  481. U8* dst = raw_image->getData(); // start from the top
  482. void (LLImageTGA::*pixel_decoder)( U8*, const U8* );
  483. switch( mColorMapBytesPerEntry )
  484. {
  485. case 1: pixel_decoder = &LLImageTGA::decodeColorMapPixel8; break;
  486. case 2: pixel_decoder = &LLImageTGA::decodeColorMapPixel15; break;
  487. case 3: pixel_decoder = &LLImageTGA::decodeColorMapPixel24; break;
  488. case 4: pixel_decoder = &LLImageTGA::decodeColorMapPixel32; break;
  489. default: llassert(0); return FALSE;
  490. }
  491. if( rle )
  492. {
  493. U8* last_dst = dst + getComponents() * (getHeight() * getWidth() - 1);
  494. while( dst <= last_dst )
  495. {
  496. // Read RLE block header
  497. U8 block_header_byte = *src;
  498. src++;
  499. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  500. if( block_header_byte & 0x80 )
  501. {
  502. // Encoded (duplicate-pixel) block
  503. do
  504. {
  505. (this->*pixel_decoder)( dst, src );
  506. dst += getComponents();
  507. block_pixel_count--;
  508. }
  509. while( block_pixel_count > 0 );
  510. src++;
  511. }
  512. else
  513. {
  514. // Unencoded block
  515. do
  516. {
  517. (this->*pixel_decoder)( dst, src );
  518. dst += getComponents();
  519. src++;
  520. block_pixel_count--;
  521. }
  522. while( block_pixel_count > 0 );
  523. }
  524. }
  525. raw_image->verticalFlip();
  526. }
  527. else
  528. {
  529. S32 src_row_bytes = getWidth();
  530. S32 dst_row_bytes = getWidth() * getComponents();
  531. if( flipped )
  532. {
  533. U8* src_last_row_start = src + (getHeight() - 1) * src_row_bytes;
  534. src = src_last_row_start; // start from the bottom
  535. src_row_bytes *= -1;
  536. }
  537. S32 i;
  538. S32 j;
  539. for( S32 row = 0; row < getHeight(); row++ )
  540. {
  541. for( i = 0, j = 0; j < getWidth(); i += getComponents(), j++ )
  542. {
  543. (this->*pixel_decoder)( dst + i, src + j );
  544. }
  545. dst += dst_row_bytes;
  546. src += src_row_bytes;
  547. }
  548. }
  549. return TRUE;
  550. }
  551. BOOL LLImageTGA::encode(const LLImageRaw* raw_image, F32 encode_time)
  552. {
  553. llassert_always(raw_image);
  554. deleteData();
  555. setSize(raw_image->getWidth(), raw_image->getHeight(), raw_image->getComponents());
  556. // Data from header
  557. mIDLength = 0; // Length of identifier string
  558. mColorMapType = 0; // 0 = No Map
  559. // Supported: 2 = Uncompressed true color, 3 = uncompressed monochrome without colormap
  560. switch( getComponents() )
  561. {
  562. case 1:
  563. mImageType = 3;
  564. break;
  565. case 2: // Interpret as intensity plus alpha
  566. case 3:
  567. case 4:
  568. mImageType = 2;
  569. break;
  570. default:
  571. return FALSE;
  572. }
  573. // Color map stuff (unsupported)
  574. mColorMapIndexLo = 0; // First color map entry (low order byte)
  575. mColorMapIndexHi = 0; // First color map entry (high order byte)
  576. mColorMapLengthLo = 0; // Color map length (low order byte)
  577. mColorMapLengthHi = 0; // Color map length (high order byte)
  578. mColorMapDepth = 0; // Size of color map entry (15, 16, 24, or 32 bits)
  579. // Image offset relative to origin.
  580. mXOffsetLo = 0; // X offset from origin (low order byte)
  581. mXOffsetHi = 0; // X offset from origin (hi order byte)
  582. mYOffsetLo = 0; // Y offset from origin (low order byte)
  583. mYOffsetHi = 0; // Y offset from origin (hi order byte)
  584. // Height and width
  585. mWidthLo = U8(getWidth() & 0xFF); // Width (low order byte)
  586. mWidthHi = U8((getWidth() >> 8) & 0xFF); // Width (hi order byte)
  587. mHeightLo = U8(getHeight() & 0xFF); // Height (low order byte)
  588. mHeightHi = U8((getHeight() >> 8) & 0xFF); // Height (hi order byte)
  589. S32 bytes_per_pixel;
  590. switch( getComponents() )
  591. {
  592. case 1:
  593. bytes_per_pixel = 1;
  594. break;
  595. case 3:
  596. bytes_per_pixel = 3;
  597. break;
  598. case 2: // Interpret as intensity plus alpha. Store as RGBA.
  599. case 4:
  600. bytes_per_pixel = 4;
  601. break;
  602. default:
  603. return FALSE;
  604. }
  605. mPixelSize = U8(bytes_per_pixel * 8); // 8, 16, 24, 32 bits per pixel
  606. mAttributeBits = (4 == bytes_per_pixel) ? 8 : 0; // 4 bits: number of attribute bits (alpha) per pixel
  607. mOriginRightBit = 0; // 1 bit: origin, 0 = left, 1 = right
  608. mOriginTopBit = 0; // 1 bit: origin, 0 = bottom, 1 = top
  609. mInterleave = 0; // 2 bits: interleaved flag, 0 = none, 1 = interleaved 2, 2 = interleaved 4
  610. const S32 TGA_HEADER_SIZE = 18;
  611. const S32 COLOR_MAP_SIZE = 0;
  612. mDataOffset = TGA_HEADER_SIZE + mIDLength + COLOR_MAP_SIZE; // Offset from start of data to the actual header.
  613. S32 pixels = getWidth() * getHeight();
  614. S32 datasize = mDataOffset + bytes_per_pixel * pixels;
  615. U8* dst = allocateData(datasize);
  616. // Write header
  617. *(dst++) = mIDLength;
  618. *(dst++) = mColorMapType;
  619. *(dst++) = mImageType;
  620. *(dst++) = mColorMapIndexLo;
  621. *(dst++) = mColorMapIndexHi;
  622. *(dst++) = mColorMapLengthLo;
  623. *(dst++) = mColorMapLengthHi;
  624. *(dst++) = mColorMapDepth;
  625. *(dst++) = mXOffsetLo;
  626. *(dst++) = mXOffsetHi;
  627. *(dst++) = mYOffsetLo;
  628. *(dst++) = mYOffsetHi;
  629. *(dst++) = mWidthLo;
  630. *(dst++) = mWidthHi;
  631. *(dst++) = mHeightLo;
  632. *(dst++) = mHeightHi;
  633. *(dst++) = mPixelSize;
  634. *(dst++) =
  635. ((mInterleave & 3) << 5) |
  636. ((mOriginTopBit & 1) << 4) |
  637. ((mOriginRightBit & 1) << 3) |
  638. ((mAttributeBits & 0xF) << 0);
  639. // Write pixels
  640. const U8* src = raw_image->getData();
  641. llassert( dst == getData() + mDataOffset );
  642. S32 i = 0;
  643. S32 j = 0;
  644. switch( getComponents() )
  645. {
  646. case 1:
  647. memcpy( dst, src, bytes_per_pixel * pixels ); /* Flawfinder: ignore */
  648. break;
  649. case 2:
  650. while( pixels-- )
  651. {
  652. dst[i + 0] = src[j + 0]; // intensity
  653. dst[i + 1] = src[j + 0]; // intensity
  654. dst[i + 2] = src[j + 0]; // intensity
  655. dst[i + 3] = src[j + 1]; // alpha
  656. i += 4;
  657. j += 2;
  658. }
  659. break;
  660. case 3:
  661. while( pixels-- )
  662. {
  663. dst[i + 0] = src[i + 2]; // blue
  664. dst[i + 1] = src[i + 1]; // green
  665. dst[i + 2] = src[i + 0]; // red
  666. i += 3;
  667. }
  668. break;
  669. case 4:
  670. while( pixels-- )
  671. {
  672. dst[i + 0] = src[i + 2]; // blue
  673. dst[i + 1] = src[i + 1]; // green
  674. dst[i + 2] = src[i + 0]; // red
  675. dst[i + 3] = src[i + 3]; // alpha
  676. i += 4;
  677. }
  678. break;
  679. }
  680. return TRUE;
  681. }
  682. BOOL LLImageTGA::decodeTruecolorRle32( LLImageRaw* raw_image, BOOL &alpha_opaque )
  683. {
  684. llassert( getComponents() == 4 );
  685. alpha_opaque = TRUE;
  686. U8* dst = raw_image->getData();
  687. U32* dst_pixels = (U32*) dst;
  688. U8* src = getData() + mDataOffset;
  689. U8* last_src = src + getDataSize();
  690. U32 rgba;
  691. U8* rgba_byte_p = (U8*) &rgba;
  692. U32* last_dst_pixel = dst_pixels + getHeight() * getWidth() - 1;
  693. while( dst_pixels <= last_dst_pixel )
  694. {
  695. // Read RLE block header
  696. if (src >= last_src)
  697. return FALSE;
  698. U8 block_header_byte = *src;
  699. src++;
  700. U32 block_pixel_count = (block_header_byte & 0x7F) + 1;
  701. if( block_header_byte & 0x80 )
  702. {
  703. // Encoded (duplicate-pixel) block
  704. if (src + 3 >= last_src)
  705. return FALSE;
  706. rgba_byte_p[0] = src[2];
  707. rgba_byte_p[1] = src[1];
  708. rgba_byte_p[2] = src[0];
  709. rgba_byte_p[3] = src[3];
  710. if (rgba_byte_p[3] != 255)
  711. {
  712. alpha_opaque = FALSE;
  713. }
  714. src += 4;
  715. register U32 value = rgba;
  716. do
  717. {
  718. *dst_pixels = value;
  719. dst_pixels++;
  720. block_pixel_count--;
  721. }
  722. while( block_pixel_count > 0 );
  723. }
  724. else
  725. {
  726. // Unencoded block
  727. do
  728. {
  729. if (src + 3 >= last_src)
  730. return FALSE;
  731. ((U8*)dst_pixels)[0] = src[2];
  732. ((U8*)dst_pixels)[1] = src[1];
  733. ((U8*)dst_pixels)[2] = src[0];
  734. ((U8*)dst_pixels)[3] = src[3];
  735. if (src[3] != 255)
  736. {
  737. alpha_opaque = FALSE;
  738. }
  739. src += 4;
  740. dst_pixels++;
  741. block_pixel_count--;
  742. }
  743. while( block_pixel_count > 0 );
  744. }
  745. }
  746. return TRUE;
  747. }
  748. BOOL LLImageTGA::decodeTruecolorRle15( LLImageRaw* raw_image )
  749. {
  750. llassert( getComponents() == 3 );
  751. llassert( mIs15Bit );
  752. U8* dst = raw_image->getData();
  753. U8* src = getData() + mDataOffset;
  754. U8* last_src = src + getDataSize();
  755. U8* last_dst = dst + getComponents() * (getHeight() * getWidth() - 1);
  756. while( dst <= last_dst )
  757. {
  758. // Read RLE block header
  759. if (src >= last_src)
  760. return FALSE;
  761. U8 block_header_byte = *src;
  762. src++;
  763. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  764. if( block_header_byte & 0x80 )
  765. {
  766. // Encoded (duplicate-pixel) block
  767. do
  768. {
  769. if (src + 2 >= last_src)
  770. return FALSE;
  771. decodeTruecolorPixel15( dst, src ); // slow
  772. dst += 3;
  773. block_pixel_count--;
  774. }
  775. while( block_pixel_count > 0 );
  776. src += 2;
  777. }
  778. else
  779. {
  780. // Unencoded block
  781. do
  782. {
  783. if (src + 2 >= last_src)
  784. return FALSE;
  785. decodeTruecolorPixel15( dst, src );
  786. dst += 3;
  787. src += 2;
  788. block_pixel_count--;
  789. }
  790. while( block_pixel_count > 0 );
  791. }
  792. }
  793. return TRUE;
  794. }
  795. BOOL LLImageTGA::decodeTruecolorRle24( LLImageRaw* raw_image )
  796. {
  797. llassert( getComponents() == 3 );
  798. U8* dst = raw_image->getData();
  799. U8* src = getData() + mDataOffset;
  800. U8* last_src = src + getDataSize();
  801. U8* last_dst = dst + getComponents() * (getHeight() * getWidth() - 1);
  802. while( dst <= last_dst )
  803. {
  804. // Read RLE block header
  805. if (src >= last_src)
  806. return FALSE;
  807. U8 block_header_byte = *src;
  808. src++;
  809. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  810. if( block_header_byte & 0x80 )
  811. {
  812. // Encoded (duplicate-pixel) block
  813. do
  814. {
  815. if (src + 2 >= last_src)
  816. return FALSE;
  817. dst[0] = src[2];
  818. dst[1] = src[1];
  819. dst[2] = src[0];
  820. dst += 3;
  821. block_pixel_count--;
  822. }
  823. while( block_pixel_count > 0 );
  824. src += 3;
  825. }
  826. else
  827. {
  828. // Unencoded block
  829. do
  830. {
  831. if (src + 2 >= last_src)
  832. return FALSE;
  833. dst[0] = src[2];
  834. dst[1] = src[1];
  835. dst[2] = src[0];
  836. dst += 3;
  837. src += 3;
  838. block_pixel_count--;
  839. }
  840. while( block_pixel_count > 0 );
  841. }
  842. }
  843. return TRUE;
  844. }
  845. BOOL LLImageTGA::decodeTruecolorRle8( LLImageRaw* raw_image )
  846. {
  847. llassert( getComponents() == 1 );
  848. U8* dst = raw_image->getData();
  849. U8* src = getData() + mDataOffset;
  850. U8* last_src = src + getDataSize();
  851. U8* last_dst = dst + getHeight() * getWidth() - 1;
  852. while( dst <= last_dst )
  853. {
  854. // Read RLE block header
  855. if (src >= last_src)
  856. return FALSE;
  857. U8 block_header_byte = *src;
  858. src++;
  859. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  860. if( block_header_byte & 0x80 )
  861. {
  862. if (src >= last_src)
  863. return FALSE;
  864. // Encoded (duplicate-pixel) block
  865. memset( dst, *src, block_pixel_count );
  866. dst += block_pixel_count;
  867. src++;
  868. }
  869. else
  870. {
  871. // Unencoded block
  872. do
  873. {
  874. if (src >= last_src)
  875. return FALSE;
  876. *dst = *src;
  877. dst++;
  878. src++;
  879. block_pixel_count--;
  880. }
  881. while( block_pixel_count > 0 );
  882. }
  883. }
  884. return TRUE;
  885. }
  886. // Decoded and process the image for use in avatar gradient masks.
  887. // Processing happens during the decode for speed.
  888. BOOL LLImageTGA::decodeAndProcess( LLImageRaw* raw_image, F32 domain, F32 weight )
  889. {
  890. llassert_always(raw_image);
  891. // "Domain" isn't really the right word. It refers to the width of the
  892. // ramp portion of the function that relates input and output pixel values.
  893. // A domain of 0 gives a step function.
  894. //
  895. // | /----------------
  896. // O| / |
  897. // u| / |
  898. // t| / |
  899. // p|------------------/ |
  900. // u| | |
  901. // t|<---------------->|<-->|
  902. // | "offset" "domain"
  903. // |
  904. // --+---Input--------------------------------
  905. // |
  906. if (!getData() || (0 == getDataSize()))
  907. {
  908. setLastError("LLImageTGA trying to decode an image with no data!");
  909. return FALSE;
  910. }
  911. // Only works for unflipped monochrome RLE images
  912. if( (getComponents() != 1) || (mImageType != 11) || mOriginTopBit || mOriginRightBit )
  913. {
  914. llerrs << "LLImageTGA trying to alpha-gradient process an image that's not a standard RLE, one component image" << llendl;
  915. return FALSE;
  916. }
  917. raw_image->resize(getWidth(), getHeight(), getComponents());
  918. U8* dst = raw_image->getData();
  919. U8* src = getData() + mDataOffset;
  920. U8* last_dst = dst + getHeight() * getWidth() - 1;
  921. if( domain > 0 )
  922. {
  923. // Process using a look-up table (lut)
  924. const S32 LUT_LEN = 256;
  925. U8 lut[LUT_LEN];
  926. S32 i;
  927. F32 scale = 1.f / domain;
  928. F32 offset = (1.f - domain) * llclampf( 1.f - weight );
  929. F32 bias = -(scale * offset);
  930. for( i = 0; i < LUT_LEN; i++ )
  931. {
  932. lut[i] = (U8)llclampb( 255.f * ( i/255.f * scale + bias ) );
  933. }
  934. while( dst <= last_dst )
  935. {
  936. // Read RLE block header
  937. U8 block_header_byte = *src;
  938. src++;
  939. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  940. if( block_header_byte & 0x80 )
  941. {
  942. // Encoded (duplicate-pixel) block
  943. memset( dst, lut[ *src ], block_pixel_count );
  944. dst += block_pixel_count;
  945. src++;
  946. }
  947. else
  948. {
  949. // Unencoded block
  950. do
  951. {
  952. *dst = lut[ *src ];
  953. dst++;
  954. src++;
  955. block_pixel_count--;
  956. }
  957. while( block_pixel_count > 0 );
  958. }
  959. }
  960. }
  961. else
  962. {
  963. // Process using a simple comparison agains a threshold
  964. const U8 threshold = (U8)(0xFF * llclampf( 1.f - weight ));
  965. while( dst <= last_dst )
  966. {
  967. // Read RLE block header
  968. U8 block_header_byte = *src;
  969. src++;
  970. U8 block_pixel_count = (block_header_byte & 0x7F) + 1;
  971. if( block_header_byte & 0x80 )
  972. {
  973. // Encoded (duplicate-pixel) block
  974. memset( dst, ((*src >= threshold) ? 0xFF : 0), block_pixel_count );
  975. dst += block_pixel_count;
  976. src++;
  977. }
  978. else
  979. {
  980. // Unencoded block
  981. do
  982. {
  983. *dst = (*src >= threshold) ? 0xFF : 0;
  984. dst++;
  985. src++;
  986. block_pixel_count--;
  987. }
  988. while( block_pixel_count > 0 );
  989. }
  990. }
  991. }
  992. return TRUE;
  993. }
  994. // Reads a .tga file and creates an LLImageTGA with its data.
  995. bool LLImageTGA::loadFile( const std::string& path )
  996. {
  997. S32 len = path.size();
  998. if( len < 5 )
  999. {
  1000. return false;
  1001. }
  1002. std::string extension = gDirUtilp->getExtension(path);
  1003. if( "tga" != extension )
  1004. {
  1005. return false;
  1006. }
  1007. LLFILE* file = LLFile::fopen(path, "rb"); /* Flawfinder: ignore */
  1008. if( !file )
  1009. {
  1010. llwarns << "Couldn't open file " << path << llendl;
  1011. return false;
  1012. }
  1013. S32 file_size = 0;
  1014. if (!fseek(file, 0, SEEK_END))
  1015. {
  1016. file_size = ftell(file);
  1017. fseek(file, 0, SEEK_SET);
  1018. }
  1019. U8* buffer = allocateData(file_size);
  1020. S32 bytes_read = fread(buffer, 1, file_size, file);
  1021. if( bytes_read != file_size )
  1022. {
  1023. deleteData();
  1024. llwarns << "Couldn't read file " << path << llendl;
  1025. return false;
  1026. }
  1027. fclose( file );
  1028. if( !updateData() )
  1029. {
  1030. llwarns << "Couldn't decode file " << path << llendl;
  1031. deleteData();
  1032. return false;
  1033. }
  1034. return true;
  1035. }