gBitmap.cpp 38 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374
  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 GFXTextureOp combineOp )
  736. {
  737. // Check valid texture ops
  738. switch( combineOp )
  739. {
  740. case GFXTOPAdd:
  741. case GFXTOPSubtract:
  742. break;
  743. default:
  744. Con::errorf( "GBitmap::combine - Invalid op type" );
  745. return false;
  746. }
  747. // Check bitmapA format
  748. switch( bitmapA->getFormat() )
  749. {
  750. case GFXFormatR8G8B8:
  751. case GFXFormatR8G8B8X8:
  752. case GFXFormatR8G8B8A8:
  753. break;
  754. default:
  755. Con::errorf( "GBitmap::combine - invalid format for bitmapA" );
  756. return false;
  757. }
  758. // Check bitmapB format
  759. switch( bitmapB->getFormat() )
  760. {
  761. case GFXFormatR8G8B8:
  762. case GFXFormatR8G8B8X8:
  763. case GFXFormatR8G8B8A8:
  764. break;
  765. default:
  766. Con::errorf( "GBitmap::combine - invalid format for bitmapB" );
  767. return false;
  768. }
  769. // Determine format of result texture
  770. // CodeReview: This is dependent on the order of the GFXFormat enum. [5/11/2007 Pat]
  771. GFXFormat resFmt = static_cast<GFXFormat>( getMax( bitmapA->getFormat(), bitmapB->getFormat() ) );
  772. U32 resWidth = getMax( bitmapA->getWidth(), bitmapB->getWidth() );
  773. U32 resHeight = getMax( bitmapA->getHeight(), bitmapB->getHeight() );
  774. // Adjust size OF bitmap based on the biggest one
  775. if( bitmapA->getWidth() != bitmapB->getWidth() ||
  776. bitmapA->getHeight() != bitmapB->getHeight() )
  777. {
  778. // Delete old bitmap
  779. deleteImage();
  780. // Allocate new one
  781. allocateBitmap( resWidth, resHeight, false, resFmt );
  782. }
  783. // Adjust format of result bitmap (if resFmt == getFormat() it will not perform the format convert)
  784. setFormat( resFmt );
  785. // Perform combine
  786. U8 *destBits = getWritableBits();
  787. const U8 *aBits = bitmapA->getBits();
  788. const U8 *bBits = bitmapB->getBits();
  789. for( S32 y = 0; y < getHeight(); y++ )
  790. {
  791. for( S32 x = 0; x < getWidth(); x++ )
  792. {
  793. for( S32 _byte = 0; _byte < mBytesPerPixel; _byte++ )
  794. {
  795. U8 pxA = 0;
  796. U8 pxB = 0;
  797. // Get contributions from A and B
  798. if( y < bitmapA->getHeight() &&
  799. x < bitmapA->getWidth() &&
  800. _byte < bitmapA->mBytesPerPixel )
  801. pxA = *aBits++;
  802. if( y < bitmapB->getHeight() &&
  803. x < bitmapB->getWidth() &&
  804. _byte < bitmapB->mBytesPerPixel )
  805. pxB = *bBits++;
  806. // Combine them (clamp values 0-U8_MAX)
  807. switch( combineOp )
  808. {
  809. case GFXTOPAdd:
  810. *destBits++ = getMin( U8( pxA + pxB ), U8_MAX );
  811. break;
  812. case GFXTOPSubtract:
  813. *destBits++ = getMax( U8( pxA - pxB ), U8( 0 ) );
  814. break;
  815. default:
  816. AssertFatal(false, "GBitmap::combine - Invalid combineOp");
  817. break;
  818. }
  819. }
  820. }
  821. }
  822. return true;
  823. }
  824. void GBitmap::fill( const ColorI &rColor )
  825. {
  826. // Set the first pixel using the slow
  827. // but proper method.
  828. setColor( 0, 0, rColor );
  829. mHasTransparency = rColor.alpha < 255;
  830. // Now fill the first row of the bitmap by
  831. // copying the first pixel across the row.
  832. const U32 stride = getWidth() * mBytesPerPixel;
  833. const U8 *src = getBits();
  834. U8 *dest = getWritableBits() + mBytesPerPixel;
  835. const U8 *end = src + stride;
  836. for ( ; dest != end; dest += mBytesPerPixel )
  837. dMemcpy( dest, src, mBytesPerPixel );
  838. // Now copy the first row to all the others.
  839. //
  840. // TODO: This could adaptively size the copy
  841. // amount to copy more rows from the source
  842. // and reduce the total number of memcpy calls.
  843. //
  844. dest = getWritableBits() + stride;
  845. end = src + ( stride * getHeight() );
  846. for ( ; dest != end; dest += stride )
  847. dMemcpy( dest, src, stride );
  848. }
  849. void GBitmap::fillWhite()
  850. {
  851. dMemset( getWritableBits(), 255, mByteSize );
  852. mHasTransparency = false;
  853. }
  854. GBitmap* GBitmap::createPaddedBitmap() const
  855. {
  856. if (isPow2(getWidth()) && isPow2(getHeight()))
  857. return NULL;
  858. AssertFatal(getNumMipLevels() == 1,
  859. "Cannot have non-pow2 bitmap with miplevels");
  860. U32 width = getWidth();
  861. U32 height = getHeight();
  862. U32 newWidth = getNextPow2(getWidth());
  863. U32 newHeight = getNextPow2(getHeight());
  864. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  865. for (U32 i = 0; i < height; i++)
  866. {
  867. U8* pDest = (U8*)pReturn->getAddress(0, i);
  868. const U8* pSrc = (const U8*)getAddress(0, i);
  869. dMemcpy(pDest, pSrc, width * mBytesPerPixel);
  870. pDest += width * mBytesPerPixel;
  871. // set the src pixel to the last pixel in the row
  872. const U8 *pSrcPixel = pDest - mBytesPerPixel;
  873. for(U32 j = width; j < newWidth; j++)
  874. for(U32 k = 0; k < mBytesPerPixel; k++)
  875. *pDest++ = pSrcPixel[k];
  876. }
  877. for(U32 i = height; i < newHeight; i++)
  878. {
  879. U8* pDest = (U8*)pReturn->getAddress(0, i);
  880. U8* pSrc = (U8*)pReturn->getAddress(0, height-1);
  881. dMemcpy(pDest, pSrc, newWidth * mBytesPerPixel);
  882. }
  883. return pReturn;
  884. }
  885. GBitmap* GBitmap::createPow2Bitmap() const
  886. {
  887. if (isPow2(getWidth()) && isPow2(getHeight()))
  888. return NULL;
  889. AssertFatal(getNumMipLevels() == 1,
  890. "Cannot have non-pow2 bitmap with miplevels");
  891. U32 width = getWidth();
  892. U32 height = getHeight();
  893. U32 newWidth = getNextPow2(getWidth());
  894. U32 newHeight = getNextPow2(getHeight());
  895. GBitmap* pReturn = new GBitmap(newWidth, newHeight, false, getFormat());
  896. U8* pDest = (U8*)pReturn->getAddress(0, 0);
  897. const U8* pSrc = (const U8*)getAddress(0, 0);
  898. F32 yCoeff = (F32) height / (F32) newHeight;
  899. F32 xCoeff = (F32) width / (F32) newWidth;
  900. F32 currY = 0.0f;
  901. for (U32 y = 0; y < newHeight; y++)
  902. {
  903. F32 currX = 0.0f;
  904. //U32 yDestOffset = (pReturn->mWidth * pReturn->mBytesPerPixel) * y;
  905. //U32 xDestOffset = 0;
  906. //U32 ySourceOffset = (U32)((mWidth * mBytesPerPixel) * currY);
  907. //F32 xSourceOffset = 0.0f;
  908. for (U32 x = 0; x < newWidth; x++)
  909. {
  910. pDest = (U8*) pReturn->getAddress(x, y);
  911. pSrc = (U8*) getAddress((S32)currX, (S32)currY);
  912. for (U32 p = 0; p < pReturn->mBytesPerPixel; p++)
  913. {
  914. pDest[p] = pSrc[p];
  915. }
  916. currX += xCoeff;
  917. }
  918. currY += yCoeff;
  919. }
  920. return pReturn;
  921. }
  922. void GBitmap::copyChannel( U32 index, GBitmap *outBitmap ) const
  923. {
  924. AssertFatal( index < mBytesPerPixel, "GBitmap::copyChannel() - Bad channel offset!" );
  925. AssertFatal( outBitmap, "GBitmap::copyChannel() - Null output bitmap!" );
  926. AssertFatal( outBitmap->getWidth() == getWidth(), "GBitmap::copyChannel() - Width mismatch!" );
  927. AssertFatal( outBitmap->getHeight() == getHeight(), "GBitmap::copyChannel() - Height mismatch!" );
  928. U8 *outBits = outBitmap->getWritableBits();
  929. const U32 outBytesPerPixel = outBitmap->getBytesPerPixel();
  930. const U8 *srcBits = getBits() + index;
  931. const U8 *endBits = getBits() + mByteSize;
  932. for ( ; srcBits < endBits; )
  933. {
  934. *outBits = *srcBits;
  935. outBits += outBytesPerPixel;
  936. srcBits += mBytesPerPixel;
  937. }
  938. }
  939. //------------------------------------------------------------------------------
  940. bool GBitmap::read(Stream& io_rStream)
  941. {
  942. // Handle versioning
  943. U32 version;
  944. io_rStream.read(&version);
  945. AssertFatal(version == csFileVersion, "Bitmap::read: incorrect file version");
  946. //-------------------------------------- Read the object
  947. U32 fmt;
  948. io_rStream.read(&fmt);
  949. mInternalFormat = GFXFormat(fmt);
  950. mBytesPerPixel = 1;
  951. switch (mInternalFormat) {
  952. case GFXFormatA8:
  953. case GFXFormatL8: mBytesPerPixel = 1;
  954. break;
  955. case GFXFormatR8G8B8: mBytesPerPixel = 3;
  956. break;
  957. case GFXFormatR8G8B8A8: mBytesPerPixel = 4;
  958. break;
  959. case GFXFormatL16:
  960. case GFXFormatR5G6B5:
  961. case GFXFormatR5G5B5A1: mBytesPerPixel = 2;
  962. break;
  963. default:
  964. AssertFatal(false, "GBitmap::read: misunderstood format specifier");
  965. break;
  966. }
  967. io_rStream.read(&mByteSize);
  968. mBits = new U8[mByteSize];
  969. io_rStream.read(mByteSize, mBits);
  970. io_rStream.read(&mWidth);
  971. io_rStream.read(&mHeight);
  972. io_rStream.read(&mNumMipLevels);
  973. for (U32 i = 0; i < c_maxMipLevels; i++)
  974. io_rStream.read(&mMipLevelOffsets[i]);
  975. checkForTransparency();
  976. return (io_rStream.getStatus() == Stream::Ok);
  977. }
  978. bool GBitmap::write(Stream& io_rStream) const
  979. {
  980. // Handle versioning
  981. io_rStream.write(csFileVersion);
  982. //-------------------------------------- Write the object
  983. io_rStream.write(U32(mInternalFormat));
  984. io_rStream.write(mByteSize);
  985. io_rStream.write(mByteSize, mBits);
  986. io_rStream.write(mWidth);
  987. io_rStream.write(mHeight);
  988. io_rStream.write(mNumMipLevels);
  989. for (U32 i = 0; i < c_maxMipLevels; i++)
  990. io_rStream.write(mMipLevelOffsets[i]);
  991. return (io_rStream.getStatus() == Stream::Ok);
  992. }
  993. //------------------------------------------------------------------------------
  994. //-------------------------------------- Persistent I/O
  995. //
  996. bool GBitmap::readBitmap( const String &bmType, Stream &ioStream )
  997. {
  998. PROFILE_SCOPE(ResourceGBitmap_readBitmap);
  999. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  1000. if ( regInfo == NULL )
  1001. {
  1002. Con::errorf( "[GBitmap::readBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  1003. return NULL;
  1004. }
  1005. return regInfo->readFunc( ioStream, this );
  1006. }
  1007. bool GBitmap::writeBitmap( const String &bmType, Stream &ioStream, U32 compressionLevel )
  1008. {
  1009. const GBitmap::Registration *regInfo = GBitmap::sFindRegInfo( bmType );
  1010. if ( regInfo == NULL )
  1011. {
  1012. Con::errorf( "[GBitmap::writeBitmap] unable to find registration for extension [%s]", bmType.c_str() );
  1013. return NULL;
  1014. }
  1015. return regInfo->writeFunc( this, ioStream, (compressionLevel == U32_MAX) ? regInfo->defaultCompression : compressionLevel );
  1016. }
  1017. template<> void *Resource<GBitmap>::create(const Torque::Path &path)
  1018. {
  1019. PROFILE_SCOPE( ResourceGBitmap_create );
  1020. #ifdef TORQUE_DEBUG_RES_MANAGER
  1021. Con::printf( "Resource<GBitmap>::create - [%s]", path.getFullPath().c_str() );
  1022. #endif
  1023. FileStream stream;
  1024. stream.open( path.getFullPath(), Torque::FS::File::Read );
  1025. if ( stream.getStatus() != Stream::Ok )
  1026. {
  1027. Con::errorf( "Resource<GBitmap>::create - failed to open '%s'", path.getFullPath().c_str() );
  1028. return NULL;
  1029. }
  1030. GBitmap *bmp = new GBitmap;
  1031. const String extension = path.getExtension();
  1032. if( !bmp->readBitmap( extension, stream ) )
  1033. {
  1034. Con::errorf( "Resource<GBitmap>::create - error reading '%s'", path.getFullPath().c_str() );
  1035. delete bmp;
  1036. bmp = NULL;
  1037. }
  1038. return bmp;
  1039. }
  1040. template<> ResourceBase::Signature Resource<GBitmap>::signature()
  1041. {
  1042. return MakeFourCC('b','i','t','m');
  1043. }
  1044. Resource<GBitmap> GBitmap::load(const Torque::Path &path)
  1045. {
  1046. Resource<GBitmap> ret = _load( path );
  1047. if ( ret != NULL )
  1048. return ret;
  1049. // Do a recursive search.
  1050. return _search( path );
  1051. }
  1052. Resource<GBitmap> GBitmap::_load(const Torque::Path &path)
  1053. {
  1054. PROFILE_SCOPE( GBitmap_load );
  1055. if ( Torque::FS::IsFile( path ) )
  1056. return ResourceManager::get().load( path );
  1057. Path foundPath;
  1058. if ( GBitmap::sFindFile( path, &foundPath ) )
  1059. {
  1060. Resource<GBitmap> ret = ResourceManager::get().load( foundPath );
  1061. if ( ret != NULL )
  1062. return ret;
  1063. }
  1064. return Resource< GBitmap >( NULL );
  1065. }
  1066. Resource<GBitmap> GBitmap::_search(const Torque::Path &path)
  1067. {
  1068. PROFILE_SCOPE( GBitmap_search );
  1069. // If unable to load texture in current directory
  1070. // look in the parent directory. But never look in the root.
  1071. Path newPath( path );
  1072. while ( true )
  1073. {
  1074. String filePath = newPath.getPath();
  1075. String::SizeType slash = filePath.find( '/', filePath.length(), String::Right );
  1076. if ( slash == String::NPos )
  1077. break;
  1078. slash = filePath.find( '/', filePath.length(), String::Right );
  1079. if ( slash == String::NPos )
  1080. break;
  1081. String truncPath = filePath.substr( 0, slash );
  1082. newPath.setPath( truncPath );
  1083. Resource<GBitmap> ret = _load( newPath );
  1084. if ( ret != NULL )
  1085. return ret;
  1086. }
  1087. return Resource< GBitmap >( NULL );
  1088. }
  1089. U32 GBitmap::getSurfaceSize(const U32 mipLevel) const
  1090. {
  1091. // Bump by the mip level.
  1092. U32 height = getMax(U32(1), mHeight >> mipLevel);
  1093. U32 width = getMax(U32(1), mWidth >> mipLevel);
  1094. if (mInternalFormat >= GFXFormatBC1 && mInternalFormat <= GFXFormatBC3)
  1095. {
  1096. // From the directX docs:
  1097. // max(1, width ÷ 4) x max(1, height ÷ 4) x 8(DXT1) or 16(DXT2-5)
  1098. U32 sizeMultiple = 0;
  1099. switch (mInternalFormat)
  1100. {
  1101. case GFXFormatBC1:
  1102. sizeMultiple = 8;
  1103. break;
  1104. case GFXFormatBC2:
  1105. case GFXFormatBC3:
  1106. sizeMultiple = 16;
  1107. break;
  1108. default:
  1109. AssertISV(false, "DDSFile::getSurfaceSize - invalid compressed texture format, we only support DXT1-5 right now.");
  1110. break;
  1111. }
  1112. return getMax(U32(1), width / 4) * getMax(U32(1), height / 4) * sizeMultiple;
  1113. }
  1114. else
  1115. {
  1116. return height * width* mBytesPerPixel;
  1117. }
  1118. }
  1119. DefineEngineFunction( getBitmapInfo, String, ( const char *filename ),,
  1120. "Returns image info in the following format: width TAB height TAB bytesPerPixel TAB format. "
  1121. "It will return an empty string if the file is not found.\n"
  1122. "@ingroup Rendering\n" )
  1123. {
  1124. Resource<GBitmap> image = GBitmap::load( filename );
  1125. if ( !image )
  1126. return String::EmptyString;
  1127. return String::ToString( "%d\t%d\t%d\t%d", image->getWidth(),
  1128. image->getHeight(),
  1129. image->getBytesPerPixel(),
  1130. image->getFormat());
  1131. }