gBitmap.cpp 38 KB

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