gBitmap.cpp 37 KB

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