llimagetga.cpp 29 KB

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