gBitmap.cpp 38 KB

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