gBitmap.cpp 38 KB

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