gBitmap.cpp 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #include "platform/platform.h"
  23. #include "gfx/bitmap/gBitmap.h"
  24. #include "core/resourceManager.h"
  25. #include "core/stream/fileStream.h"
  26. #include "core/strings/stringFunctions.h"
  27. #include "core/color.h"
  28. #include "gfx/bitmap/bitmapUtils.h"
  29. #include "math/mRect.h"
  30. #include "console/console.h"
  31. #include "platform/profiler.h"
  32. #include "console/engineAPI.h"
  33. using namespace Torque;
  34. const U32 GBitmap::csFileVersion = 3;
  35. Vector<GBitmap::Registration> GBitmap::sRegistrations( __FILE__, __LINE__ );
  36. GBitmap::GBitmap()
  37. : mInternalFormat(GFXFormatR8G8B8),
  38. mBits(NULL),
  39. mByteSize(0),
  40. mWidth(0),
  41. mHeight(0),
  42. mBytesPerPixel(0),
  43. mNumMipLevels(0),
  44. mHasTransparency(false)
  45. {
  46. for (U32 i = 0; i < c_maxMipLevels; i++)
  47. mMipLevelOffsets[i] = 0xffffffff;
  48. }
  49. GBitmap::GBitmap(const GBitmap& rCopy)
  50. {
  51. mInternalFormat = rCopy.mInternalFormat;
  52. mByteSize = rCopy.mByteSize;
  53. mBits = new U8[mByteSize];
  54. dMemcpy(mBits, rCopy.mBits, mByteSize);
  55. mWidth = rCopy.mWidth;
  56. mHeight = rCopy.mHeight;
  57. mBytesPerPixel = rCopy.mBytesPerPixel;
  58. mNumMipLevels = rCopy.mNumMipLevels;
  59. dMemcpy(mMipLevelOffsets, rCopy.mMipLevelOffsets, sizeof(mMipLevelOffsets));
  60. mHasTransparency = rCopy.mHasTransparency;
  61. }
  62. GBitmap::GBitmap(const U32 in_width,
  63. const U32 in_height,
  64. const bool in_extrudeMipLevels,
  65. const GFXFormat in_format)
  66. : mBits(NULL),
  67. mByteSize(0)
  68. {
  69. for (U32 i = 0; i < c_maxMipLevels; i++)
  70. mMipLevelOffsets[i] = 0xffffffff;
  71. allocateBitmap(in_width, in_height, in_extrudeMipLevels, in_format);
  72. mHasTransparency = false;
  73. }
  74. GBitmap::GBitmap(const U32 in_width,
  75. const U32 in_height,
  76. const U8* data )
  77. : mBits(NULL),
  78. mByteSize(0)
  79. {
  80. allocateBitmap(in_width, in_height, false, GFXFormatR8G8B8A8);
  81. mHasTransparency = false;
  82. for (U32 x = 0; x < in_width; x++)
  83. {
  84. for (U32 y = 0; y < in_height; y++)
  85. {
  86. U32 offset = (x + y * in_width) * 4;
  87. ColorI color(data[offset],
  88. data[offset + 1],
  89. data[offset + 2],
  90. data[offset + 3]);
  91. if (color.alpha < 255)
  92. mHasTransparency = true;
  93. setColor(x, y, color);
  94. }
  95. }
  96. }
  97. //--------------------------------------------------------------------------
  98. GBitmap::~GBitmap()
  99. {
  100. deleteImage();
  101. }
  102. //--------------------------------------------------------------------------
  103. void GBitmap::sRegisterFormat( const GBitmap::Registration &reg )
  104. {
  105. U32 insert = sRegistrations.size();
  106. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  107. {
  108. if ( sRegistrations[i].priority <= reg.priority )
  109. {
  110. insert = i;
  111. break;
  112. }
  113. }
  114. sRegistrations.insert( insert, reg );
  115. }
  116. const GBitmap::Registration *GBitmap::sFindRegInfo( const String &extension )
  117. {
  118. for ( U32 i = 0; i < GBitmap::sRegistrations.size(); i++ )
  119. {
  120. const GBitmap::Registration &reg = GBitmap::sRegistrations[i];
  121. const Vector<String> &extensions = reg.extensions;
  122. for ( U32 j = 0; j < extensions.size(); ++j )
  123. {
  124. if ( extensions[j].equal( extension, String::NoCase ) )
  125. return &reg;
  126. }
  127. }
  128. return NULL;
  129. }
  130. bool GBitmap::sFindFile( const Path &path, Path *outPath )
  131. {
  132. PROFILE_SCOPE( GBitmap_sFindFile );
  133. const String origExt( String::ToLower( path.getExtension() ) );
  134. Path tryPath( path );
  135. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  136. {
  137. const Registration &reg = sRegistrations[i];
  138. const Vector<String> &extensions = reg.extensions;
  139. for ( U32 j = 0; j < extensions.size(); ++j )
  140. {
  141. // We've already tried this one.
  142. if ( extensions[j] == origExt )
  143. continue;
  144. tryPath.setExtension( extensions[j] );
  145. if ( !Torque::FS::IsFile( tryPath ) )
  146. continue;
  147. if ( outPath )
  148. *outPath = tryPath;
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154. bool GBitmap::sFindFiles( const Path &path, Vector<Path> *outFoundPaths )
  155. {
  156. PROFILE_SCOPE( GBitmap_sFindFiles );
  157. Path tryPath( path );
  158. for ( U32 i = 0; i < GBitmap::sRegistrations.size(); i++ )
  159. {
  160. const GBitmap::Registration &reg = GBitmap::sRegistrations[i];
  161. const Vector<String> &extensions = reg.extensions;
  162. for ( U32 j = 0; j < extensions.size(); ++j )
  163. {
  164. tryPath.setExtension( extensions[j] );
  165. if ( Torque::FS::IsFile( tryPath ) )
  166. {
  167. if ( outFoundPaths )
  168. outFoundPaths->push_back( tryPath );
  169. else
  170. return true;
  171. }
  172. }
  173. }
  174. return outFoundPaths ? outFoundPaths->size() > 0 : false;
  175. }
  176. String GBitmap::sGetExtensionList()
  177. {
  178. String list;
  179. for ( U32 i = 0; i < sRegistrations.size(); i++ )
  180. {
  181. const Registration &reg = sRegistrations[i];
  182. for ( U32 j = 0; j < reg.extensions.size(); j++ )
  183. {
  184. list += reg.extensions[j];
  185. list += " ";
  186. }
  187. }
  188. return list;
  189. }
  190. //--------------------------------------------------------------------------
  191. void GBitmap::deleteImage()
  192. {
  193. delete [] mBits;
  194. mBits = NULL;
  195. mByteSize = 0;
  196. mWidth = 0;
  197. mHeight = 0;
  198. mNumMipLevels = 0;
  199. }
  200. //--------------------------------------------------------------------------
  201. void GBitmap::copyRect(const GBitmap *src, const RectI &srcRect, const Point2I &dstPt, const U32 srcMipLevel, const U32 dstMipLevel)
  202. {
  203. if(src->getFormat() != getFormat())
  204. return;
  205. if(srcRect.extent.x + srcRect.point.x > src->getWidth(srcMipLevel) || srcRect.extent.y + srcRect.point.y > src->getHeight(srcMipLevel))
  206. return;
  207. if(srcRect.extent.x + dstPt.x > getWidth(dstMipLevel) || srcRect.extent.y + dstPt.y > getHeight(dstMipLevel))
  208. return;
  209. for(U32 i = 0; i < srcRect.extent.y; i++)
  210. {
  211. dMemcpy(getAddress(dstPt.x, dstPt.y + i, dstMipLevel),
  212. src->getAddress(srcRect.point.x, srcRect.point.y + i, srcMipLevel),
  213. mBytesPerPixel * srcRect.extent.x);
  214. }
  215. }
  216. //--------------------------------------------------------------------------
  217. void GBitmap::allocateBitmap(const U32 in_width, const U32 in_height, const bool in_extrudeMipLevels, const GFXFormat in_format )
  218. {
  219. //-------------------------------------- Some debug checks...
  220. U32 svByteSize = mByteSize;
  221. U8 *svBits = mBits;
  222. AssertFatal(in_width != 0 && in_height != 0, "GBitmap::allocateBitmap: width or height is 0");
  223. if (in_extrudeMipLevels == true)
  224. {
  225. AssertFatal(isPow2(in_width) == true && isPow2(in_height) == true, "GBitmap::GBitmap: in order to extrude mip levels, bitmap w/h must be pow2");
  226. }
  227. mInternalFormat = in_format;
  228. mWidth = in_width;
  229. mHeight = in_height;
  230. mBytesPerPixel = 1;
  231. switch (mInternalFormat)
  232. {
  233. case GFXFormatA8:
  234. case GFXFormatL8: mBytesPerPixel = 1;
  235. break;
  236. case GFXFormatR8G8B8: mBytesPerPixel = 3;
  237. break;
  238. case GFXFormatR8G8B8X8:
  239. case GFXFormatR8G8B8A8: mBytesPerPixel = 4;
  240. break;
  241. case GFXFormatR5G6B5:
  242. case GFXFormatR5G5B5A1: mBytesPerPixel = 2;
  243. break;
  244. default:
  245. AssertFatal(false, "GBitmap::GBitmap: misunderstood format specifier");
  246. break;
  247. }
  248. // Set up the mip levels, if necessary...
  249. mNumMipLevels = 1;
  250. U32 allocPixels = in_width * in_height * mBytesPerPixel;
  251. mMipLevelOffsets[0] = 0;
  252. if (in_extrudeMipLevels == true)
  253. {
  254. U32 currWidth = in_width;
  255. U32 currHeight = in_height;
  256. do
  257. {
  258. mMipLevelOffsets[mNumMipLevels] = mMipLevelOffsets[mNumMipLevels - 1] +
  259. (currWidth * currHeight * mBytesPerPixel);
  260. currWidth >>= 1;
  261. currHeight >>= 1;
  262. if (currWidth == 0) currWidth = 1;
  263. if (currHeight == 0) currHeight = 1;
  264. mNumMipLevels++;
  265. allocPixels += currWidth * currHeight * mBytesPerPixel;
  266. } while (currWidth != 1 && currHeight != 1);
  267. }
  268. AssertFatal(mNumMipLevels <= c_maxMipLevels, "GBitmap::allocateBitmap: too many miplevels");
  269. // Set up the memory...
  270. mByteSize = allocPixels;
  271. mBits = new U8[mByteSize];
  272. dMemset(mBits, 0xFF, mByteSize);
  273. if(svBits != NULL)
  274. {
  275. dMemcpy(mBits, svBits, getMin(mByteSize, svByteSize));
  276. delete[] svBits;
  277. }
  278. }
  279. //--------------------------------------------------------------------------
  280. void GBitmap::extrudeMipLevels(bool clearBorders)
  281. {
  282. if(mNumMipLevels == 1)
  283. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  284. switch (getFormat())
  285. {
  286. case GFXFormatR5G5B5A1:
  287. {
  288. for(U32 i = 1; i < mNumMipLevels; i++)
  289. bitmapExtrude5551(getBits(i - 1), getWritableBits(i), getHeight(i), getWidth(i));
  290. break;
  291. }
  292. case GFXFormatR8G8B8:
  293. {
  294. for(U32 i = 1; i < mNumMipLevels; i++)
  295. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  296. break;
  297. }
  298. case GFXFormatR8G8B8A8:
  299. case GFXFormatR8G8B8X8:
  300. {
  301. for(U32 i = 1; i < mNumMipLevels; i++)
  302. bitmapExtrudeRGBA(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  303. break;
  304. }
  305. default:
  306. break;
  307. }
  308. if (clearBorders)
  309. {
  310. for (U32 i = 1; i<mNumMipLevels; i++)
  311. {
  312. U32 width = getWidth(i);
  313. U32 height = getHeight(i);
  314. if (height<3 || width<3)
  315. // bmp is all borders at this mip level
  316. dMemset(getWritableBits(i),0,width*height*mBytesPerPixel);
  317. else
  318. {
  319. width *= mBytesPerPixel;
  320. U8 * bytes = getWritableBits(i);
  321. U8 * end = bytes + (height-1)*width - mBytesPerPixel; // end = last row, 2nd column
  322. // clear first row sans the last pixel
  323. dMemset(bytes,0,width-mBytesPerPixel);
  324. bytes -= mBytesPerPixel;
  325. while (bytes<end)
  326. {
  327. // clear last pixel of row N-1 and first pixel of row N
  328. bytes += width;
  329. dMemset(bytes,0,mBytesPerPixel*2);
  330. }
  331. // clear last row sans the first pixel
  332. dMemset(bytes+2*mBytesPerPixel,0,width-mBytesPerPixel);
  333. }
  334. }
  335. }
  336. }
  337. //--------------------------------------------------------------------------
  338. void GBitmap::extrudeMipLevelsDetail()
  339. {
  340. AssertFatal(getFormat() == GFXFormatR8G8B8, "Error, only handles RGB for now...");
  341. U32 i,j;
  342. if(mNumMipLevels == 1)
  343. allocateBitmap(getWidth(), getHeight(), true, getFormat());
  344. for (i = 1; i < mNumMipLevels; i++) {
  345. bitmapExtrudeRGB(getBits(i - 1), getWritableBits(i), getHeight(i-1), getWidth(i-1));
  346. }
  347. // Ok, now that we have the levels extruded, we need to move the lower miplevels
  348. // closer to 0.5.
  349. for (i = 1; i < mNumMipLevels - 1; i++) {
  350. U8* pMipBits = (U8*)getWritableBits(i);
  351. U32 numBytes = getWidth(i) * getHeight(i) * 3;
  352. U32 shift = i;
  353. U32 start = ((1 << i) - 1) * 0x80;
  354. for (j = 0; j < numBytes; j++) {
  355. U32 newVal = (start + pMipBits[j]) >> shift;
  356. AssertFatal(newVal <= 255, "Error, oob");
  357. pMipBits[j] = U8(newVal);
  358. }
  359. }
  360. AssertFatal(getWidth(mNumMipLevels - 1) == 1 && getHeight(mNumMipLevels - 1) == 1,
  361. "Error, last miplevel should be 1x1!");
  362. ((U8*)getWritableBits(mNumMipLevels - 1))[0] = 0x80;
  363. ((U8*)getWritableBits(mNumMipLevels - 1))[1] = 0x80;
  364. ((U8*)getWritableBits(mNumMipLevels - 1))[2] = 0x80;
  365. }
  366. //--------------------------------------------------------------------------
  367. bool GBitmap::setFormat(GFXFormat fmt)
  368. {
  369. if (getFormat() == fmt)
  370. return true;
  371. PROFILE_SCOPE(GBitmap_setFormat);
  372. // this is a nasty pointer math hack
  373. // is there a quick way to calc pixels of a fully mipped bitmap?
  374. U32 pixels = 0;
  375. for (U32 i=0; i < mNumMipLevels; i++)
  376. pixels += getHeight(i) * getWidth(i);
  377. switch( getFormat() )
  378. {
  379. case GFXFormatR8G8B8:
  380. switch ( fmt )
  381. {
  382. case GFXFormatR5G5B5A1:
  383. #ifdef _XBOX
  384. bitmapConvertRGB_to_1555(mBits, pixels);
  385. #else
  386. bitmapConvertRGB_to_5551(mBits, pixels);
  387. #endif
  388. mInternalFormat = GFXFormatR5G5B5A1;
  389. mBytesPerPixel = 2;
  390. break;
  391. case GFXFormatR8G8B8A8:
  392. case GFXFormatR8G8B8X8:
  393. // Took this out, it may crash -patw
  394. //AssertFatal( mNumMipLevels == 1, "Do the mip-mapping in hardware." );
  395. bitmapConvertRGB_to_RGBX( &mBits, pixels );
  396. mInternalFormat = fmt;
  397. mBytesPerPixel = 4;
  398. mByteSize = pixels * 4;
  399. break;
  400. default:
  401. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  402. return false;
  403. }
  404. break;
  405. case GFXFormatR8G8B8X8:
  406. switch( fmt )
  407. {
  408. // No change needed for this
  409. case GFXFormatR8G8B8A8:
  410. mInternalFormat = GFXFormatR8G8B8A8;
  411. break;
  412. case GFXFormatR8G8B8:
  413. bitmapConvertRGBX_to_RGB( &mBits, pixels );
  414. mInternalFormat = GFXFormatR8G8B8;
  415. mBytesPerPixel = 3;
  416. mByteSize = pixels * 3;
  417. break;
  418. default:
  419. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  420. return false;
  421. }
  422. break;
  423. case GFXFormatR8G8B8A8:
  424. switch( fmt )
  425. {
  426. // No change needed for this
  427. case GFXFormatR8G8B8X8:
  428. mInternalFormat = GFXFormatR8G8B8X8;
  429. break;
  430. case GFXFormatR8G8B8:
  431. bitmapConvertRGBX_to_RGB( &mBits, pixels );
  432. mInternalFormat = GFXFormatR8G8B8;
  433. mBytesPerPixel = 3;
  434. mByteSize = pixels * 3;
  435. break;
  436. default:
  437. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  438. return false;
  439. }
  440. break;
  441. case GFXFormatA8:
  442. switch( fmt )
  443. {
  444. case GFXFormatR8G8B8A8:
  445. mInternalFormat = GFXFormatR8G8B8A8;
  446. bitmapConvertA8_to_RGBA( &mBits, pixels );
  447. mBytesPerPixel = 4;
  448. mByteSize = pixels * 4;
  449. break;
  450. default:
  451. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  452. return false;
  453. }
  454. break;
  455. default:
  456. AssertWarn(0, "GBitmap::setFormat: unable to convert bitmap to requested format.");
  457. return false;
  458. }
  459. U32 offset = 0;
  460. for (U32 j=0; j < mNumMipLevels; j++)
  461. {
  462. mMipLevelOffsets[j] = offset;
  463. offset += getHeight(j) * getWidth(j) * mBytesPerPixel;
  464. }
  465. return true;
  466. }
  467. //------------------------------------------------------------------------------
  468. bool GBitmap::checkForTransparency()
  469. {
  470. mHasTransparency = false;
  471. ColorI pixel(255, 255, 255, 255);
  472. switch (mInternalFormat)
  473. {
  474. // Non-transparent formats
  475. case GFXFormatL8:
  476. case GFXFormatR8G8B8:
  477. case GFXFormatR5G6B5:
  478. break;
  479. // Transparent formats
  480. case GFXFormatA8:
  481. case GFXFormatR8G8B8A8:
  482. case GFXFormatR5G5B5A1:
  483. // Let getColor() do the heavy lifting
  484. for (U32 x = 0; x < mWidth; x++)
  485. {
  486. for (U32 y = 0; y < mHeight; y++)
  487. {
  488. if (getColor(x, y, pixel))
  489. {
  490. if (pixel.alpha < 255)
  491. {
  492. mHasTransparency = true;
  493. break;
  494. }
  495. }
  496. }
  497. }
  498. break;
  499. default:
  500. AssertFatal(false, "GBitmap::checkForTransparency: misunderstood format specifier");
  501. break;
  502. }
  503. return mHasTransparency;
  504. }
  505. //------------------------------------------------------------------------------
  506. ColorF GBitmap::sampleTexel(F32 u, F32 v) const
  507. {
  508. ColorF col(0.5f, 0.5f, 0.5f);
  509. // normally sampling wraps all the way around at 1.0,
  510. // but locking doesn't support this, and we seem to calc
  511. // the uv based on a clamped 0 - 1...
  512. Point2F max((F32)(getWidth()-1), (F32)(getHeight()-1));
  513. Point2F posf;
  514. posf.x = mClampF(((u) * max.x), 0.0f, max.x);
  515. posf.y = mClampF(((v) * max.y), 0.0f, max.y);
  516. Point2I posi((S32)posf.x, (S32)posf.y);
  517. const U8 *buffer = getBits();
  518. U32 lexelindex = ((posi.y * getWidth()) + posi.x) * mBytesPerPixel;
  519. if(mBytesPerPixel == 2)
  520. {
  521. //U16 *buffer = (U16 *)lockrect->pBits;
  522. }
  523. else if(mBytesPerPixel > 2)
  524. {
  525. col.red = F32(buffer[lexelindex + 0]) / 255.0f;
  526. col.green = F32(buffer[lexelindex + 1]) / 255.0f;
  527. col.blue = F32(buffer[lexelindex + 2]) / 255.0f;
  528. }
  529. return col;
  530. }
  531. //--------------------------------------------------------------------------
  532. bool GBitmap::getColor(const U32 x, const U32 y, ColorI& rColor) const
  533. {
  534. if (x >= mWidth || y >= mHeight)
  535. return false;
  536. const U8* pLoc = getAddress(x, y);
  537. switch (mInternalFormat) {
  538. case GFXFormatA8:
  539. case GFXFormatL8:
  540. rColor.set( *pLoc, *pLoc, *pLoc, *pLoc );
  541. break;
  542. case GFXFormatR8G8B8:
  543. case GFXFormatR8G8B8X8:
  544. rColor.set( pLoc[0], pLoc[1], pLoc[2], 255 );
  545. break;
  546. case GFXFormatR8G8B8A8:
  547. rColor.set( pLoc[0], pLoc[1], pLoc[2], pLoc[3] );
  548. break;
  549. case GFXFormatR5G5B5A1:
  550. #if defined(TORQUE_OS_MAC)
  551. rColor.set( (*((U16*)pLoc) >> 0) & 0x1F,
  552. (*((U16*)pLoc) >> 5) & 0x1F,
  553. (*((U16*)pLoc) >> 10) & 0x1F,
  554. ((*((U16*)pLoc) >> 15) & 0x01) ? 255 : 0 );
  555. #else
  556. rColor.set( *((U16*)pLoc) >> 11,
  557. (*((U16*)pLoc) >> 6) & 0x1f,
  558. (*((U16*)pLoc) >> 1) & 0x1f,
  559. (*((U16*)pLoc) & 1) ? 255 : 0 );
  560. #endif
  561. break;
  562. default:
  563. AssertFatal(false, "Bad internal format");
  564. return false;
  565. }
  566. return true;
  567. }
  568. //--------------------------------------------------------------------------
  569. bool GBitmap::setColor(const U32 x, const U32 y, const ColorI& rColor)
  570. {
  571. if (x >= mWidth || y >= mHeight)
  572. return false;
  573. U8* pLoc = getAddress(x, y);
  574. switch (mInternalFormat) {
  575. case GFXFormatA8:
  576. case GFXFormatL8:
  577. *pLoc = rColor.alpha;
  578. break;
  579. case GFXFormatR8G8B8:
  580. dMemcpy( pLoc, &rColor, 3 * sizeof( U8 ) );
  581. break;
  582. case GFXFormatR8G8B8A8:
  583. case GFXFormatR8G8B8X8:
  584. dMemcpy( pLoc, &rColor, 4 * sizeof( U8 ) );
  585. break;
  586. case GFXFormatR5G6B5:
  587. #ifdef TORQUE_OS_MAC
  588. *((U16*)pLoc) = (rColor.red << 11) | (rColor.green << 5) | (rColor.blue << 0) ;
  589. #else
  590. *((U16*)pLoc) = (rColor.blue << 0) | (rColor.green << 5) | (rColor.red << 11);
  591. #endif
  592. break;
  593. case GFXFormatR5G5B5A1:
  594. #ifdef TORQUE_OS_MAC
  595. *((U16*)pLoc) = (((rColor.alpha>0) ? 1 : 0)<<15) | (rColor.blue << 10) | (rColor.green << 5) | (rColor.red << 0);
  596. #else
  597. *((U16*)pLoc) = (rColor.blue << 1) | (rColor.green << 6) | (rColor.red << 11) | ((rColor.alpha>0) ? 1 : 0);
  598. #endif
  599. break;
  600. default:
  601. AssertFatal(false, "Bad internal format");
  602. return false;
  603. }
  604. return true;
  605. }
  606. //-----------------------------------------------------------------------------
  607. bool GBitmap::combine( const GBitmap *bitmapA, const GBitmap *bitmapB, const GFXTextureOp combineOp )
  608. {
  609. // Check valid texture ops
  610. switch( combineOp )
  611. {
  612. case GFXTOPAdd:
  613. case GFXTOPSubtract:
  614. break;
  615. default:
  616. Con::errorf( "GBitmap::combine - Invalid op type" );
  617. return false;
  618. }
  619. // Check bitmapA format
  620. switch( bitmapA->getFormat() )
  621. {
  622. case GFXFormatR8G8B8:
  623. case GFXFormatR8G8B8X8:
  624. case GFXFormatR8G8B8A8:
  625. break;
  626. default:
  627. Con::errorf( "GBitmap::combine - invalid format for bitmapA" );
  628. return false;
  629. }
  630. // Check bitmapB format
  631. switch( bitmapB->getFormat() )
  632. {
  633. case GFXFormatR8G8B8:
  634. case GFXFormatR8G8B8X8:
  635. case GFXFormatR8G8B8A8:
  636. break;
  637. default:
  638. Con::errorf( "GBitmap::combine - invalid format for bitmapB" );
  639. return false;
  640. }
  641. // Determine format of result texture
  642. // CodeReview: This is dependent on the order of the GFXFormat enum. [5/11/2007 Pat]
  643. GFXFormat resFmt = static_cast<GFXFormat>( getMax( bitmapA->getFormat(), bitmapB->getFormat() ) );
  644. U32 resWidth = getMax( bitmapA->getWidth(), bitmapB->getWidth() );
  645. U32 resHeight = getMax( bitmapA->getHeight(), bitmapB->getHeight() );
  646. // Adjust size OF bitmap based on the biggest one
  647. if( bitmapA->getWidth() != bitmapB->getWidth() ||
  648. bitmapA->getHeight() != bitmapB->getHeight() )
  649. {
  650. // Delete old bitmap
  651. deleteImage();
  652. // Allocate new one
  653. allocateBitmap( resWidth, resHeight, false, resFmt );
  654. }
  655. // Adjust format of result bitmap (if resFmt == getFormat() it will not perform the format convert)
  656. setFormat( resFmt );
  657. // Perform combine
  658. U8 *destBits = getWritableBits();
  659. const U8 *aBits = bitmapA->getBits();
  660. const U8 *bBits = bitmapB->getBits();
  661. for( S32 y = 0; y < getHeight(); y++ )
  662. {
  663. for( S32 x = 0; x < getWidth(); x++ )
  664. {
  665. for( S32 _byte = 0; _byte < mBytesPerPixel; _byte++ )
  666. {
  667. U8 pxA = 0;
  668. U8 pxB = 0;
  669. // Get contributions from A and B
  670. if( y < bitmapA->getHeight() &&
  671. x < bitmapA->getWidth() &&
  672. _byte < bitmapA->mBytesPerPixel )
  673. pxA = *aBits++;
  674. if( y < bitmapB->getHeight() &&
  675. x < bitmapB->getWidth() &&
  676. _byte < bitmapB->mBytesPerPixel )
  677. pxB = *bBits++;
  678. // Combine them (clamp values 0-U8_MAX)
  679. switch( combineOp )
  680. {
  681. case GFXTOPAdd:
  682. *destBits++ = getMin( U8( pxA + pxB ), U8_MAX );
  683. break;
  684. case GFXTOPSubtract:
  685. *destBits++ = getMax( U8( pxA - pxB ), U8( 0 ) );
  686. break;
  687. default:
  688. AssertFatal(false, "GBitmap::combine - Invalid combineOp");
  689. break;
  690. }
  691. }
  692. }
  693. }
  694. return true;
  695. }
  696. void GBitmap::fill( const ColorI &rColor )
  697. {
  698. // Set the first pixel using the slow
  699. // but proper method.
  700. setColor( 0, 0, rColor );
  701. mHasTransparency = rColor.alpha < 255;
  702. // Now fill the first row of the bitmap by
  703. // copying the first pixel across the row.
  704. const U32 stride = getWidth() * mBytesPerPixel;
  705. const U8 *src = getBits();
  706. U8 *dest = getWritableBits() + mBytesPerPixel;
  707. const U8 *end = src + stride;
  708. for ( ; dest != end; dest += mBytesPerPixel )
  709. dMemcpy( dest, src, mBytesPerPixel );
  710. // Now copy the first row to all the others.
  711. //
  712. // TODO: This could adaptively size the copy
  713. // amount to copy more rows from the source
  714. // and reduce the total number of memcpy calls.
  715. //
  716. dest = getWritableBits() + stride;
  717. end = src + ( stride * getHeight() );
  718. for ( ; dest != end; dest += stride )
  719. dMemcpy( dest, src, stride );
  720. }
  721. void GBitmap::fillWhite()
  722. {
  723. dMemset( getWritableBits(), 255, mByteSize );
  724. mHasTransparency = false;
  725. }
  726. GBitmap* GBitmap::createPaddedBitmap() const
  727. {
  728. if (isPow2(getWidth()) && isPow2(getHeight()))
  729. return NULL;
  730. AssertFatal(getNumMipLevels() == 1,
  731. "Cannot have non-pow2 bitmap with miplevels");
  732. U32 width = getWidth();
  733. U32 height = getHeight();
  734. U32 newWidth = getNextPow2(getWidth());
  735. U32 newHeight = getNextPow2(getHeight());
  736. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  737. for (U32 i = 0; i < height; i++)
  738. {
  739. U8* pDest = (U8*)pReturn->getAddress(0, i);
  740. const U8* pSrc = (const U8*)getAddress(0, i);
  741. dMemcpy(pDest, pSrc, width * mBytesPerPixel);
  742. pDest += width * mBytesPerPixel;
  743. // set the src pixel to the last pixel in the row
  744. const U8 *pSrcPixel = pDest - mBytesPerPixel;
  745. for(U32 j = width; j < newWidth; j++)
  746. for(U32 k = 0; k < mBytesPerPixel; k++)
  747. *pDest++ = pSrcPixel[k];
  748. }
  749. for(U32 i = height; i < newHeight; i++)
  750. {
  751. U8* pDest = (U8*)pReturn->getAddress(0, i);
  752. U8* pSrc = (U8*)pReturn->getAddress(0, height-1);
  753. dMemcpy(pDest, pSrc, newWidth * mBytesPerPixel);
  754. }
  755. return pReturn;
  756. }
  757. GBitmap* GBitmap::createPow2Bitmap() const
  758. {
  759. if (isPow2(getWidth()) && isPow2(getHeight()))
  760. return NULL;
  761. AssertFatal(getNumMipLevels() == 1,
  762. "Cannot have non-pow2 bitmap with miplevels");
  763. U32 width = getWidth();
  764. U32 height = getHeight();
  765. U32 newWidth = getNextPow2(getWidth());
  766. U32 newHeight = getNextPow2(getHeight());
  767. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  768. U8* pDest = (U8*)pReturn->getAddress(0, 0);
  769. const U8* pSrc = (const U8*)getAddress(0, 0);
  770. F32 yCoeff = (F32) height / (F32) newHeight;
  771. F32 xCoeff = (F32) width / (F32) newWidth;
  772. F32 currY = 0.0f;
  773. for (U32 y = 0; y < newHeight; y++)
  774. {
  775. F32 currX = 0.0f;
  776. //U32 yDestOffset = (pReturn->mWidth * pReturn->mBytesPerPixel) * y;
  777. //U32 xDestOffset = 0;
  778. //U32 ySourceOffset = (U32)((mWidth * mBytesPerPixel) * currY);
  779. //F32 xSourceOffset = 0.0f;
  780. for (U32 x = 0; x < newWidth; x++)
  781. {
  782. pDest = (U8*) pReturn->getAddress(x, y);
  783. pSrc = (U8*) getAddress((S32)currX, (S32)currY);
  784. for (U32 p = 0; p < pReturn->mBytesPerPixel; p++)
  785. {
  786. pDest[p] = pSrc[p];
  787. }
  788. currX += xCoeff;
  789. }
  790. currY += yCoeff;
  791. }
  792. return pReturn;
  793. }
  794. void GBitmap::copyChannel( U32 index, GBitmap *outBitmap ) const
  795. {
  796. AssertFatal( index < mBytesPerPixel, "GBitmap::copyChannel() - Bad channel offset!" );
  797. AssertFatal( outBitmap, "GBitmap::copyChannel() - Null output bitmap!" );
  798. AssertFatal( outBitmap->getWidth() == getWidth(), "GBitmap::copyChannel() - Width mismatch!" );
  799. AssertFatal( outBitmap->getHeight() == getHeight(), "GBitmap::copyChannel() - Height mismatch!" );
  800. U8 *outBits = outBitmap->getWritableBits();
  801. const U32 outBytesPerPixel = outBitmap->getBytesPerPixel();
  802. const U8 *srcBits = getBits() + index;
  803. const U8 *endBits = getBits() + mByteSize;
  804. for ( ; srcBits < endBits; )
  805. {
  806. *outBits = *srcBits;
  807. outBits += outBytesPerPixel;
  808. srcBits += mBytesPerPixel;
  809. }
  810. }
  811. //------------------------------------------------------------------------------
  812. bool GBitmap::read(Stream& io_rStream)
  813. {
  814. // Handle versioning
  815. U32 version;
  816. io_rStream.read(&version);
  817. AssertFatal(version == csFileVersion, "Bitmap::read: incorrect file version");
  818. //-------------------------------------- Read the object
  819. U32 fmt;
  820. io_rStream.read(&fmt);
  821. mInternalFormat = GFXFormat(fmt);
  822. mBytesPerPixel = 1;
  823. switch (mInternalFormat) {
  824. case GFXFormatA8:
  825. case GFXFormatL8: mBytesPerPixel = 1;
  826. break;
  827. case GFXFormatR8G8B8: mBytesPerPixel = 3;
  828. break;
  829. case GFXFormatR8G8B8A8: mBytesPerPixel = 4;
  830. break;
  831. case GFXFormatR5G6B5:
  832. case GFXFormatR5G5B5A1: mBytesPerPixel = 2;
  833. break;
  834. default:
  835. AssertFatal(false, "GBitmap::read: misunderstood format specifier");
  836. break;
  837. }
  838. io_rStream.read(&mByteSize);
  839. mBits = new U8[mByteSize];
  840. io_rStream.read(mByteSize, mBits);
  841. io_rStream.read(&mWidth);
  842. io_rStream.read(&mHeight);
  843. io_rStream.read(&mNumMipLevels);
  844. for (U32 i = 0; i < c_maxMipLevels; i++)
  845. io_rStream.read(&mMipLevelOffsets[i]);
  846. checkForTransparency();
  847. return (io_rStream.getStatus() == Stream::Ok);
  848. }
  849. bool GBitmap::write(Stream& io_rStream) const
  850. {
  851. // Handle versioning
  852. io_rStream.write(csFileVersion);
  853. //-------------------------------------- Write the object
  854. io_rStream.write(U32(mInternalFormat));
  855. io_rStream.write(mByteSize);
  856. io_rStream.write(mByteSize, mBits);
  857. io_rStream.write(mWidth);
  858. io_rStream.write(mHeight);
  859. io_rStream.write(mNumMipLevels);
  860. for (U32 i = 0; i < c_maxMipLevels; i++)
  861. io_rStream.write(mMipLevelOffsets[i]);
  862. return (io_rStream.getStatus() == Stream::Ok);
  863. }
  864. //------------------------------------------------------------------------------
  865. //-------------------------------------- Persistent I/O
  866. //
  867. bool GBitmap::readBitmap( const String &bmType, Stream &ioStream )
  868. {
  869. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  870. if ( regInfo == NULL )
  871. {
  872. Con::errorf( "[GBitmap::readBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  873. return NULL;
  874. }
  875. return regInfo->readFunc( ioStream, this );
  876. }
  877. bool GBitmap::writeBitmap( const String &bmType, Stream &ioStream, U32 compressionLevel )
  878. {
  879. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  880. if ( regInfo == NULL )
  881. {
  882. Con::errorf( "[GBitmap::writeBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  883. return NULL;
  884. }
  885. return regInfo->writeFunc( this, ioStream, (compressionLevel == U32_MAX) ? regInfo->defaultCompression : compressionLevel );
  886. }
  887. template<> void *Resource<GBitmap>::create(const Torque::Path &path)
  888. {
  889. PROFILE_SCOPE( ResourceGBitmap_create );
  890. #ifdef TORQUE_DEBUG_RES_MANAGER
  891. Con::printf( "Resource<GBitmap>::create - [%s]", path.getFullPath().c_str() );
  892. #endif
  893. FileStream stream;
  894. stream.open( path.getFullPath(), Torque::FS::File::Read );
  895. if ( stream.getStatus() != Stream::Ok )
  896. {
  897. Con::errorf( "Resource<GBitmap>::create - failed to open '%s'", path.getFullPath().c_str() );
  898. return NULL;
  899. }
  900. GBitmap *bmp = new GBitmap;
  901. const String extension = path.getExtension();
  902. if( !bmp->readBitmap( extension, stream ) )
  903. {
  904. Con::errorf( "Resource<GBitmap>::create - error reading '%s'", path.getFullPath().c_str() );
  905. delete bmp;
  906. bmp = NULL;
  907. }
  908. return bmp;
  909. }
  910. template<> ResourceBase::Signature Resource<GBitmap>::signature()
  911. {
  912. return MakeFourCC('b','i','t','m');
  913. }
  914. Resource<GBitmap> GBitmap::load(const Torque::Path &path)
  915. {
  916. Resource<GBitmap> ret = _load( path );
  917. if ( ret != NULL )
  918. return ret;
  919. // Do a recursive search.
  920. return _search( path );
  921. }
  922. Resource<GBitmap> GBitmap::_load(const Torque::Path &path)
  923. {
  924. PROFILE_SCOPE( GBitmap_load );
  925. if ( Torque::FS::IsFile( path ) )
  926. return ResourceManager::get().load( path );
  927. Path foundPath;
  928. if ( GBitmap::sFindFile( path, &foundPath ) )
  929. {
  930. Resource<GBitmap> ret = ResourceManager::get().load( foundPath );
  931. if ( ret != NULL )
  932. return ret;
  933. }
  934. return Resource< GBitmap >( NULL );
  935. }
  936. Resource<GBitmap> GBitmap::_search(const Torque::Path &path)
  937. {
  938. PROFILE_SCOPE( GBitmap_search );
  939. // If unable to load texture in current directory
  940. // look in the parent directory. But never look in the root.
  941. Path newPath( path );
  942. while ( true )
  943. {
  944. String filePath = newPath.getPath();
  945. String::SizeType slash = filePath.find( '/', filePath.length(), String::Right );
  946. if ( slash == String::NPos )
  947. break;
  948. slash = filePath.find( '/', filePath.length(), String::Right );
  949. if ( slash == String::NPos )
  950. break;
  951. String truncPath = filePath.substr( 0, slash );
  952. newPath.setPath( truncPath );
  953. Resource<GBitmap> ret = _load( newPath );
  954. if ( ret != NULL )
  955. return ret;
  956. }
  957. return Resource< GBitmap >( NULL );
  958. }
  959. DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
  960. "Returns image info in the following format: width TAB height TAB bytesPerPixel. "
  961. "It will return an empty string if the file is not found.\n"
  962. "@ingroup Rendering\n" )
  963. {
  964. Resource<GBitmap> image = GBitmap::load( filename );
  965. if ( !image )
  966. return String::EmptyString;
  967. return String::ToString( "%d\t%d\t%d", image->getWidth(),
  968. image->getHeight(),
  969. image->getBytesPerPixel() );
  970. }