gBitmap.cpp 38 KB

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