gBitmap.cpp 38 KB

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